/***************************************************
Javascript for HRR site
Jan 2009, Fraser Howard
****************************************************/

$(document).ready(function(){

		stripetables();

		// autocomplete for input forms
		$("#gtitle").autocomplete("hint.php", { minChars:2 });
		$("#yname").autocomplete("../inc/namehint.php", { minChars:2 });
		$("#yemail").autocomplete("../inc/emailhint.php", { minChars:2 });

		// finally do the lightbox stuff //{fitToScreen: true}
		$(".gallery a.tb").lightbox({fitToScreen: true});

		// mouseovers for thumbnails
		$(".gallery img").mouseover(function() {
			$(this).css("background-color","#ff9900");
		}).mouseout(function(){
      	$(this).css("background-color","#989898");
   	});

// end doc ready loop
});

function validateFormOnSubmit(theForm) {

  var reason = "";

  // check text fields are completed
  reason += validateEmpty(theForm.yourname);
  //reason += validateEmpty(theForm.youremail);
  reason += validateEmpty(theForm.gallery);
  // tags - not enforced
  //reason += validateEmpty(theForm.tags);

  // at least 1 photo needs to be uploaded
  reason += validateEmpty(theForm.photo1);
  reason += validateEmpty(theForm.postingpassword);

  // valid text?
  reason += validateText(theForm.photo1);
  reason += validateText(theForm.tags);
  reason += validateText(theForm.gallery);
  reason += validateText(theForm.youremail);
  reason += validateText(theForm.yourname);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  if (theForm.deleteme.checked == true) {
  	  var answer = confirm ("Are you sure you want to remove the image?")
  	  if (answer) {
       alert ("Image will be removed from gallery and deleted.");
     }
     else {
       alert ("Image deletion aborted.");
       return false;
     }
  }

  return true;
}
function validateEmpty(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow';
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateNumb(fld,min,max) {
    var error = "";
    if (isNaN(fld.value)) {
	     fld.style.background = 'Yellow';
    	  error = "The required field is incomplete.\n";
    }
    else {
    		if ( (fld.value >= min ) && (fld.value <= max) ){
        		fld.style.background = 'White';
        	}
        	else {
        		fld.style.background = 'Yellow';
    	  		error = "The required field is out of range (" + min + " - " + max + ").\n";
        	}
    }

    return error;
}

function validateText(fld) {
	var error = "";
	if (fld.value.length > 0 ) {
		if (fld.value.indexOf("<") != -1) {
			fld.style.background = 'Yellow';
    		error = "The required field is invalid (contains invalid character).\n";
		}
		else if (fld.value.indexOf("/") != -1) {
			fld.style.background = 'Yellow';
    		error = "The required field is invalid (contains invalid character).\n";
		}
		else {
			fld.style.background = 'White';
		}
	}

	return error;
}
