//### validation rules ###//
$(document).ready(validDocEL); // fire off Validation check
$(document).ready(validDocEL2); // fire off Validation check for 2nd form
/*
  Easy Function Reference - BASED ON rc_formValidtor to mimic via JS...

  checkNotEmpty($field, $msg='', $allowNull) - // essentialy does any data exist
  checkIsNumeric($field, $msg='', $allowNull) - // make sure that the data is a number
**  checkLength($field, $min, $max, $msg='', $allowNull) - //
**  checkIdentical($field, $field2, $msg='', $allowNull) - // used for testing against dual passwords or email entry
  checkIsValidEmail($field, $msg='', $allowNull) - // email validation only regex not MX records
  checkIsText($field, $msg='', $allowNull) - // not needed as alphanumrec is the same sort of thing
  checkIsAlphabetic($field, $msg='', $allowNull) - // same as checkIsNumeric but text only no special characters
  checkIsDigit($field, $msg='', $allowNull) - // same as checkIsNumeric but 0-9 only
  checkIsAlphanum($field, $msg='', $allowNull) - // essentially no special characters
**  checkPassword($field, $field2, $min, $max, $msg='', $allowNull)
**  checkIsOfValues($field, $validValues, $msg='', $allowNull)
**  checkIsWithinRange($field, $min, $max, $msg='', $allowNull)
**  checkIsSmallerThan($field, $value, $msg='', $allowNull)
**  checkIsGreaterThan($field, $value, $msg='', $allowNull)
  checkDateFormat($field, $format, $seperator='-', $msg='', $allowNull) - // checks date via regex dd/mm/yyyy only

** ones not done as yet,

*/

// event like listener for validation
function validDocEL(){
 $("li.validate input").blur( function() {  // if label has class validate and an input field
	 var v = $(this).attr("id"); // get ID to validate
	 var vr = $(this).attr("class"); // get all rules for input field
	inputCheck(v,vr) } );

	$("li.validate input").change( function() {  // capture input on change
	 var v = $(this).attr("id");
	 var vr = $(this).attr("class");
	inputCheck(v,vr) } );

	$("li.validate select").blur( function() {  // if label has class validate and an input field
	 var v = $(this).attr("id"); // get ID to validate
	 var vr = $(this).attr("class"); // get all rules for input field
	inputCheck(v,vr) } );

	$("li.validate select").change( function() {  // capture select on change
	 var v = $(this).attr("id");
	 var vr = $(this).attr("class");
	inputCheck(v,vr) } );

	$("li.validate textarea").blur( function() {  // if label has class validate and an input field
	 var v = $(this).attr("id"); // get ID to validate
	 var vr = $(this).attr("class"); // get all rules for input field
	inputCheck(v,vr) } );

	$("li.validate textarea").change( function() {  // capture select on change
	 var v = $(this).attr("id");
	 var vr = $(this).attr("class");
	inputCheck(v,vr) } );
}
//Validation for 2nd form
function validDocEL2(){
 $("li.validate2 input").blur( function() {  // if label has class validate2 and an input field
	 var v = $(this).attr("id"); // get ID to validate2
	 var vr = $(this).attr("class"); // get all rules for input field
	inputCheck(v,vr) } );

	$("li.validate2 input").change( function() {  // capture input on change
	 var v = $(this).attr("id");
	 var vr = $(this).attr("class");
	inputCheck(v,vr) } );

	$("li.validate2 select").blur( function() {  // if label has class validate2 and an input field
	 var v = $(this).attr("id"); // get ID to validate2
	 var vr = $(this).attr("class"); // get all rules for input field
	inputCheck(v,vr) } );

	$("li.validate2 select").change( function() {  // capture select on change
	 var v = $(this).attr("id");
	 var vr = $(this).attr("class");
	inputCheck(v,vr) } );

	$("li.validate2 textarea").blur( function() {  // if label has class validate2 and an input field
	 var v = $(this).attr("id"); // get ID to validate2
	 var vr = $(this).attr("class"); // get all rules for input field
	inputCheck(v,vr) } );

	$("li.validate2 textarea").change( function() {  // capture select on change
	 var v = $(this).attr("id");
	 var vr = $(this).attr("class");
	inputCheck(v,vr) } );
}


function validDoc(typ){
 var formValidated = true;
 $("li.validate input").each(function(i){  // if label has class validate and an input field
  var v = $(this).attr("id"); // get ID to validate
	 var vr = $(this).attr("class"); // get all rules for input field
  if(inputCheck(v,vr) == false){
    //alert(inputCheck(v,vr));
    formValidated = false;
  }} );

	$("li.validate select").each(function(i) {  // if label has class validate and an input field
	 var v = $(this).attr("id"); // get ID to validate
	 var vr = $(this).attr("class"); // get all rules for input field
	 if(inputCheck(v,vr) == false){
    //alert(inputCheck(v,vr));
    formValidated = false;
  }} );

	$("li.validate textarea").each(function(i){  // if label has class validate and an input field
	 var v = $(this).attr("id"); // get ID to validate
	 var vr = $(this).attr("class"); // get all rules for input field
	 if(inputCheck(v,vr) == false){
    //alert(inputCheck(v,vr));
    formValidated = false;
  }} );
if(formValidated == false){
 // ThickBox pop up custom modal
  tb_show('SORRY WE FOUND SOME ERRORS', '#TB_inline?height=200&width=335&inlineId=customDialog&modal=true', null);
  $("#TB_window").css({ border: "0", background:"transparent" });
  return false;
}

}

//Repeated for 2 forms on 1 page
function validDoc2(typ){
 var formvalidate2d = true;
 $("li.validate2 input").each(function(i){  // if label has class validate2 and an input field
  var v = $(this).attr("id"); // get ID to validate2
	 var vr = $(this).attr("class"); // get all rules for input field
  if(inputCheck(v,vr) == false){
    //alert(inputCheck(v,vr));
    formvalidate2d = false;
  }} );

	$("li.validate2 select").each(function(i) {  // if label has class validate2 and an input field
	 var v = $(this).attr("id"); // get ID to validate2
	 var vr = $(this).attr("class"); // get all rules for input field
	 if(inputCheck(v,vr) == false){
    //alert(inputCheck(v,vr));
    formvalidate2d = false;
  }} );

	$("li.validate2 textarea").each(function(i){  // if label has class validate2 and an input field
	 var v = $(this).attr("id"); // get ID to validate2
	 var vr = $(this).attr("class"); // get all rules for input field
	 if(inputCheck(v,vr) == false){
    //alert(inputCheck(v,vr));
    formvalidate2d = false;
  }} );
if(formvalidate2d == false){
 // ThickBox pop up custom modal
  tb_show('SORRY WE FOUND SOME ERRORS', '#TB_inline?height=200&width=335&inlineId=customDialog&modal=true', null);
  $("#TB_window").css({ border: "0", background:"transparent" });
  return false;
}

}

// used to pretest if the field is required and to split class data into usable items
function inputCheck(v,vr){
 var returnVar = true;
 rules = vr.split(" "); // array all classes at space
 $.each(rules, function(i, n){ // check all class values being 'rules'
  var doVal = n.search(/vr#/); // for each rule check for validation trigger vr#
   if(doVal != -1){ // if vr# exists run next pass to find rules
	   r = n.split("#"); // split at # to find all rules
	   rules = r[1];
	   // check to see if required exists...
	   if(r[1].search(/req/) != -1){
	     req = true;
	   }else{
	     req = false;
	   }
    if(validCheck(v,req,rules) == false){
      returnVar = false;
    }
   }
  }
 );
 return returnVar;
}

// the validator checks validation rules for the field sent
function validCheck(v,req,rules){
	ru = rules.split(":"); // split at : to find an exact rule
	if(ru.length == 1 && req==true){ // check for just required value - find out how many items are in the array ru.lenght
	  return checkNotEmpty(v);
	}else if(ru[0] == 'email' || ru[1] == 'email'){ // email check
	  return checkIsValidEmail(v,req);
	}else if(ru[0] == 'phone' || ru[1] == 'phone'){ // email check
	  return phoneNumber(v,req);
	}else if(ru[0] == 'number' || ru[1] == 'number'){ // email check
	  return checkIsNumeric(v,req);
	}else if(ru[0] == 'date' || ru[1] == 'date'){ // email check
	  return checkDateFormat(v,req);
	}else if(ru[0] == 'alpha' || ru[1] == 'alpha'){ // email check
	  return checkIsAlphanum(v,req);
	}else if(ru[0] == 'digit' || ru[1] == 'digit'){ // email check
	  return checkIsDigit(v,req);
	}else if(ru[0] == 'alphab' || ru[1] == 'alphab'){ // email check
   return checkIsAlphabetic(v,req);
	}else{
	 return false;
	}
} // end validCheck

// basic does this exist data
function checkNotEmpty(v){
 var fielddata = document.getElementById(v).value;
 var data = $.trim(fielddata);
 var m = 'This field is required'; // message for this error
  if(data==null || data==""){
		  return failure(v,m);
	 }else{
		  return success(v);
	 }
}


// numbers only including .
function checkIsNumeric(v,req) {
  var data=document.getElementById(v).value;
  var data=$.trim(data).replace(/[a-zA-Z ]+/g,'');

  if(req==true && data == '' || data == null){ // check if this is a required field
    var m = 'A number is required'; // message for this error
	   return failure(v,m);
  }else if(data != ''){
    document.getElementById(v).value = data;
	   return success(v);
 	}else if(data == '' && req == false){
    document.getElementById(v).value = data;
	   return successFalse(v);
  }else{
	   var m = 'Expecting numbers only please correct'; // message for this error
	   return failure(v,m);
	 }
}

// phone number data
function phoneNumber(v,req){
  var tnumber=$.trim(document.getElementById(v).value);
  var telnr = /^\+?[0-9 ()-.]+[0-9]$/;
	 if(req==true && tnumber == ''){ // check if this is a required field
	   var m = 'A phone number is required'; // message for this error
		  return failure(v,m);
	 }else if(tnumber != '' && telnr.test(tnumber)){
		  if(tnumber.length > 8 && tnumber.length < 16){
		    return success(v);
		  }else{
	     var m = 'Your phone number appears incorrect. (Permitted are digits, space ()-. and leading +)'; // message for this error
		    return failure(v,m);
		  }
	 }else if(tnumber == '' && req == false){
		  return successFalse(v);
  }else if(req==false && telnr.test(tnumber)  && length.tnumber > 8 && length.tnumber < 14){
		  if(tnumber.length > 8 && tnumber.length < 16){
		    return success(v);
		  }else{
	     var m = 'Your phone number appears incorrect. (Permitted are digits, space ()-. and leading +)'; // message for this error
		    return failure(v,m);
		  }
	 }else{
	    var m = 'Your phone number appears incorrect. (Permitted are digits, space ()-. and leading +)'; // message for this error
		   return failure(v,m);
	 }
}

// date entry
function checkDateFormat(v,req){
	  var dateE=$.trim(document.getElementById(v).value);
	  var dateChar=/(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
	  if(req==true && dateE == ''){ // check if this is a required field
	    var m = 'A date is required'; // message for this error
		   return failure(v,m);
	  }else if(dateE != '' && dateChar.test(dateE) || req==false && dateChar.test(dateE)){
		   return success(v);
	  }else if(dateE == '' && req == false){
		   return successFalse(v);
	  }else{
	    var m = 'Not a valid date format, expecting dd/mm/yyyy. Please use pop up selector or re-type'; // message for this error
		   return failure(v,m);
	  }
}

// check is alphanumeric
function checkIsAlphanum(v,req){
	  var dataA=$.trim(document.getElementById(v).value);
	  var dateChar=/^[a-z0-9]+$/i;
	  if(req==true && dataA == ''){ // check if this is a required field
	    var m = 'Some input is required'; // message for this error
		   return failure(v,m);
	  }else if(dataA != '' && dateChar.test(dataA) || req==false && dateChar.test(dataA)){
		   return success(v);
	  }else if(dataA == '' && req == false){
		   return successFalse(v);
	  }else{
	    var m = 'Please use numbers or text only, no special characters like (.,)*&^ etc...'; // message for this error
		   return failure(v,m);
	  }
}

// check is digit
function checkIsDigit(v,req){
	  var dataA=$.trim(document.getElementById(v).value);
	  var dateChar=/^[0-9]+$/;
	  if(req==true && dataA == ''){ // check if this is a required field
	    var m = 'Some input is required'; // message for this error
		   return failure(v,m);
	  }else if(dataA != '' && dateChar.test(dataA) || req==false && dateChar.test(dataA)){
		   return success(v);
	  }else if(dataA == '' && req == false){
		    return successFalse(v);
	  }else{
	    var m = 'Please use numbers only, no special characters like (.,)*&^ etc... or text a-z'; // message for this error
		   return failure(v,m);
	  }
}

// check is alphabetic
function checkIsAlphabetic(v,req){
	  var dataA=$.trim(document.getElementById(v).value);
	  var dateChar=/^[a-z]+$/i;
	  if(req==true && dataA == ''){ // check if this is a required field
     var m = 'Some input is required'; // message for this error
		   return failure(v,m);
	   }else if(dataA != '' && dateChar.test(dataA) || req==false && dateChar.test(dataA)){
      return success(v);
	   }else if(dataA == '' && req == false){
		    return successFalse(v);
	   }else{
	     var m = 'Please use a-z or A-Z only, and no special characters like (.,)*&^ etc...'; // message for this error
		    return failure(v,m);
	   }
}

// email check
function checkIsValidEmail(v,req){
	  var emaile=$.trim(document.getElementById(v).value);
	  var emailchar=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	  if(req==true && emaile == ''){ // check if this is a required field
	    var m = 'An email address is required'; // message for this error
		   return failure(v,m);
	  }else if(emaile != '' && emailchar.test(emaile) || req==false && emailchar.test(emaile)){
		   return success(v);
	  }else if(emaile == '' && req == false){
 		 return successFalse(v);
	  }else{
	    var m = 'Not a valid email address'; // message for this error
		   return failure(v,m);
	  }

}


// failure details, change class and add comments
function failure(v,m){ var fail = true;$('#' + v).addClass("fail"); $('#sta_' + v).addClass("fail");$('#sta_' + v).removeClass("success");$('#' + v).removeClass("success");$('#' + v + '_msg').css({display:"block"}); $('#' + v + '_msg').html(m);return false;}
// success details, display green for go class
function success(v){$('#' + v).removeClass("fail");$('#sta_' + v).removeClass("fail");$('#' + v).removeClass("error-php");$('#sta_' + v).addClass("success");$('#' + v).addClass("success");$('#' + v + '_msg').css({display:"none"});return true;}
// success false is a success without requiring a tick
function successFalse(v){$('#' + v).removeClass("fail");$('#sta_' + v).removeClass("fail");$('#sta_' + v).removeClass("success");$('#' + v).removeClass("success");$('#' + v + '_msg').css({display:"none"});return true;}

//### end validation rules ###//
