/** GENERAL VALIDATION **/


jQuery.validator.addMethod("letterswithbasicpunc", function(value, element) {
	return this.optional(element) || /^[a-z-.,()'\"\s]+$/i.test(value);
}, "Letters or punctuation only please");  

jQuery.validator.addMethod("alphanumeric", function(value, element) {
	return this.optional(element) || /^\w+$/i.test(value);
}, "Letters, numbers, spaces or underscores only please");  

jQuery.validator.addMethod("lettersonly", function(value, element) {
	return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please"); 

jQuery.validator.addMethod("nowhitespace", function(value, element) {
	return this.optional(element) || /^\S+$/i.test(value);
}, "No white space please"); 

jQuery.validator.addMethod("anything", function(value, element) {
	return this.optional(element) || /^.+$/i.test(value);
}
, "May contain any characters.");

jQuery.validator.addMethod("integer", function(value, element) {
	return this.optional(element) || /^-?\d+$/.test(value);
}, "A positive or negative non-decimal number please");



/** PHONE VALIDATION **/


jQuery.validator.addMethod("phone", function(phone_number) {
	
	//phone_number = phone_number.replace(/s+/g, "");
	
	//return phone_number.match(/^0[123456789]\d{8,9}$/);
	
	

  	var telnum = phone_number;
  	
  	if (telnum.length< 10) {
		return false;
	}
  	
  //check to see if the phone is mobile.  If it is force to be 11 digits
	exp = (/^(070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) == true) {
	  	if (telnum.length< 11) {
			return false;
		}
	}
 
  	//Add Prefix of 0 if phone number is only 10 digits and not 11
 	if (telnum.length == 10 && telnum.substring(0,1) != "0") {
 		telnum = "0" + phone_number;
	}
  
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
			telNumberErrorNo = 2;
			//alert("hello 2");
			return false;
		}
  	
	if (telnum.indexOf(" ")> -1) {
		return false;
	}
  
	if (telnum.indexOf("-")> -1) {
		return false;
	}
  
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}
  
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 4;
		return false;
	}
	
	// Disallow numbers allocated for dramas.
	 
	// Array holds the regular expressions for the drama telephone numbers
	var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
		if ( tnexp[i].test(telnum) ) {
			telNumberErrorNo = 5;
			return false;
		}
	}
  
	// Finally check that the telephone number is appropriate.
	exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 5;
		return false;
	}
  
	return true;
	
}, "Phone number must be 11 characters, beginning with a 0, and no spaces.");

jQuery.validator.addMethod("french_phone", function(french_phone) {
	//alert("Inside the aditional methods french_phone");
  	var telnum = french_phone;
//  	if (telnum.length > 10 || telnum.length <10) {
//		return false;
//	}
  	
   //Add Prefix of 0 if phone number is only 10 digits and not 11
// 	if (telnum.length == 10 && telnum.substring(0,1) != "0") {
// 		telnum = "0" + telnum;
//	}
  	//Check the first two digits starts with 01,02,03... and so on till 09
  	//if (telnum.substring(0,2)!="01" || telnum.substring(0,2)!="02" || telnum.substring(0,2)!="03" || telnum.substring(0,2)!="04" || telnum.substring(0,2)!="05"
     //   || telnum.substring(0,2)!="06" || telnum.substring(0,2)!="07" || telnum.substring(0,2)!="08" || telnum.substring(0,2)!="09"){
  	//	return false;
  	//}
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
			telNumberErrorNo = 2;
			return false;
		}
  	
	if (telnum.indexOf(" ")> -1) {
		return false;
	}
  
	if (telnum.indexOf("-")> -1) {
		return false;
	}
  
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}
  
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 4;
		return false;
	}		
	return true;
	
}, "Le num\u00E9ro indiqu\u00E9 n'est pas valide. Veuillez saisir un autre num\u00E9ro.");//$.i18n.prop("field.age")


jQuery.validator.addMethod("aus_phone", function(aus_phone) {

  	var telnum = aus_phone;
  	if (telnum.length< 10) {
		return false;
	}
  	
   //Add Prefix of 0 if phone number is only 10 digits and not 11
 	if (telnum.length == 10 && telnum.substring(0,1) != "0") {
 		telnum = "0" + telnum;
	}
  
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
			telNumberErrorNo = 2;
			return false;
		}
  	
	if (telnum.indexOf(" ")> -1) {
		return false;
	}
  
	if (telnum.indexOf("-")> -1) {
		return false;
	}
  
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}
  
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 4;
		return false;
	}		
	return true;
	
}, "Please enter a valid phone number <br/>(0 followed by 9 digits)");


jQuery.validator.addMethod("aus_phone_simple", function(aus_phone) {

  	var telnum = aus_phone;
  	if (telnum.length< 10) {
		return false;
	}
  
  	exp = /^(02|03|04|05|07|08)[0-9]{8}$/;
	if (exp.test(telnum) != true) {
		return false;
	}		
	return true;
	
}, "Please enter a valid phone number <br/>(0 followed by 9 digits)");


jQuery.validator.addMethod("nl_phone_simple", function(nl_phone) {

  	var telnum = nl_phone;
  	
  	if (telnum.length != 10) {
		return false;
	}
  
  	exp = /^0[0-9]{9}$/;
	if (exp.test(telnum) != true) {
		return false;
	}
	
	return true;
	
}, "Gelieve een geldig telefoonnummer op te geven");


jQuery.validator.addMethod("us_phone", function(us_phone) {

  	var telnum = us_phone;
  	if (telnum.length != 10) {
		return false;
	}
  	
   //Add Prefix of 0 if phone number is only 10 digits and not 11
 	if ( telnum.substring(0,1) == "0" || telnum.substring(0,3)=="555") {
 		return false;
	}
  
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
			telNumberErrorNo = 2;
			return false;
		}
  	
	if (telnum.indexOf(" ")> -1) {
		return false;
	}
  
	if (telnum.indexOf("-")> -1) {
		return false;
	}
  
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}  	
	return true;
	
}, "Please enter a valid phone number.");


jQuery.validator.addMethod("uk_mobile_phone", function(uk_mobilephone) {
	
  	var telnum = uk_mobilephone;
  	
  	if(telnum == "")
  		return true;
  	
  	if (telnum.length< 11) {
		return false;
	}
  	//Add Prefix of 0 if phone number is only 9 digits and not 10 or 11
 	if (telnum.substring(0,1) != "0") {
 		return false;
	} 

  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
			telNumberErrorNo = 2;
			//alert("hello 2");
			return false;
		}
  	
	if (telnum.indexOf(" ")> -1) {
		return false;
	}
  
	if (telnum.indexOf("-")> -1) {
		return false;
	}
  
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}
  
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 4;
		return false;
	}
	
	exp = (/^(070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 5;
		return false;
	}	
  
	return true;
	
}, "Please enter a valid UK mobile phone number starting with 0.");


jQuery.validator.addMethod("uk_home_phone", function(uk_homephone) {
	
  	var telnum = uk_homephone;
  	
  	if (telnum.length< 10) {
		return false;
	}

  	//Add Prefix of 0 if phone number is only 10 digits and not 11
 	if (telnum.length == 10 && telnum.substring(0,1) != "0") {
 		telnum = "0" + uk_homephone;
	}
  
  	// Don't allow country codes to be included (assumes a leading "+")
  	var exp = /^(\+)[\s]*(.*)$/;
  	if (exp.test(telnum) == true) {
		telNumberErrorNo = 2;
		//alert("hello 2");
		return false;
	}
  	
	if (telnum.indexOf(" ")> -1) {
		return false;
	}
  
	if (telnum.indexOf("-")> -1) {
		return false;
	}
  
	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 3;
		return false;
	}
  
	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 4;
		return false;
	}
	
	// Disallow numbers allocated for dramas.
	 
	// Array holds the regular expressions for the drama telephone numbers
	var tnexp = new Array ();
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) {
		if ( tnexp[i].test(telnum) ) {
			telNumberErrorNo = 5;
			return false;
		}
	}
  
	// Finally check that the telephone number is appropriate.
	exp = (/^(01|02|03|05)[0-9]+$/);
	if (exp.test(telnum) != true) {
		telNumberErrorNo = 5;
		return false;
	}
  
	return true;
	
}, "Please enter a valid UK phone number.");


//This Front End validation is to be added later(Back end validation is available). Need a place holder since it gives a Javascript error otherwise
jQuery.validator.addMethod("uk_mobile_phone_with_countrycode", function(uk_mobilePhoneWithCC) {
	
	return true;
	
}, "Please enter a valid UK phone number with country code.");


//This Front End validation is to be added later(Back end validation is available). Need a place holder since it gives a Javascript error otherwise
jQuery.validator.addMethod("aus_phone_with_countrycode", function(aus_PhoneWithCC) {
	
	return true;
	
}, "Please enter a valid UK phone number with country code.");


//This Front End validation is to be added later(Back end validation is available). Need a place holder since it gives a Javascript error otherwise 
jQuery.validator.addMethod("aus_home_phone", function(aus_homephone) { 
	
	return true;
	
}, "Please enter a valid Australian home phone number.");


//This Front End validation is to be added later(Back end validation is available). Need a place holder since it gives a Javascript error otherwise 
jQuery.validator.addMethod("aus_mobile_phone", function(aus_mobilephone) { 
	
	return true;
	
}, "Please enter a valid Australian home phone number.");




/** POSTAL CODE VALIDATION **/


// UK Postal Code
jQuery.validator.addMethod("zip", function(zipcode, element) {
    
	//return zipcode.match(/[A-Z]{1,2}[0-9R][0-9A-Z]?.[0-9][A-Z]{2}/i);
	return zipcode.match(/[A-Z]{1,2}[0-9R][0-9A-Z]?\s?[0-9][A-Z]{2}/i); // Added optional space in postal code.
	
    //return zipcode.length > 4;
}, "Valid UK postal code required");

//CA Postal Code
jQuery.validator.addMethod("ca_zip", function(cazipcode) {
    
	return cazipcode.match(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/); // Added optional space in postal code.
	
}, "Valid CA postal code required");

// US Postal Code
jQuery.validator.addMethod("us_postcode", function(us_postcode) {  
	if (us_postcode.length !=5  ){
		return false;
	}
	exp = /[0-9][0-9][0-9][0-9][0-9]/;  //5 digit numeric 
	if (exp.test(us_postcode) != true) {
		return false;
	}
	return true;	
}, "Please enter a valid US post code.");


// ES Postal Code - Spain
jQuery.validator.addMethod("es_postcode", function(es_postcode) {
    
	if (es_postcode.length !=5  ){
		return false;
	}
	exp = /^[0-9][0-9][0-9][0-9][0-9]$/;  //5 digit numeric 
	if (exp.test(es_postcode) != true) {
		return false;
	}
	return true;
	
});


// FR Postal Code - France
jQuery.validator.addMethod("fr_postcode", function(fr_postcode) {
    
	if (fr_postcode.length !=5  ){
		return false;
	}
	exp = /^[0-9][0-9][0-9][0-9][0-9]$/;  //5 digit numeric 
	if (exp.test(fr_postcode) != true) {
		return false;
	}
	return true;
	
});


//Place holder. Need to use the logic from the CustomRuleValidator here

// CA Postal Code - Canada
jQuery.validator.addMethod("canadian_postcode", function(canadian_postcode) {  	
	return true;	
}, "Please enter a valid Canadian post code.");


//Place holder. Need to use the logic from the CustomRuleValidator here

// AU Postal Code - Australia
jQuery.validator.addMethod("aus_postcode", function(canadian_postcode) {  	
	return true;	
}, "Please enter a valid Australian post code.");




/** AGE VALIDATION **/


jQuery.validator.addMethod("validateBirth", function(value, element) {
	var result = true;
	
	var strDay = $("#dobDay").val();
	var strMonth = $("#dobMonth").val();
	var strYear = $("#dobYear").val();

	if(strDay == "" || strMonth == "" || strYear == "")
		return true;

	// force parseInt to use base 10 to avoid octal interpretation of 08 and 09 as 0
	var day = parseInt($("#dobDay").val(),10);
	var month = parseInt($("#dobMonth").val(),10);
	var year = parseInt($("#dobYear").val());

	var legalDate = new Date();
	legalDate.setFullYear(year+18, month-1, day);
	
	var today = new Date();
	
	if (today >= legalDate) {
		$("#dob").val(day + "/" + month + "/" + year);
		//alert($("#dob").val()); // just checking to make sure 08 and 09 haven't become 0
		result = true;
	}
	else {
		result = false;
	}
	return result;
}, $.i18n.prop("field.age"));


/**
 * does an age check. Must be 18 years of age
 */
jQuery.validator.addMethod("age", function(dob, element) {
	var result = true;
    // WARNING WE ASSUME THAT THE DATEFORMAT IS EUROPEAN!!!!!!!! DD/MM/YYYY
    if (dob.length == 10) {
    	//Couldn't we use a date object to parse this?
    	
        var dateAry = dob.split("/");
    	// force parseInt to use base 10 to avoid octal interpretation of 08 and 09 as 0
        var day = parseInt(dateAry[0],10);
        var month = parseInt(dateAry[1],10) - 1;
        var year = parseInt(dateAry[2]);

        var birthMark = new Date();
        birthMark.setFullYear(birthMark.getFullYear() - 18, birthMark.getMonth(), birthMark.getDay() + 1);
        var then = new Date();
        then.setFullYear(year, month, day);

		if(then <= birthMark)
			result = true;
		else
			result = false;
    }

    return result;
}, $.i18n.prop("field.age"));




/** ADDITIONAL VALIDATION **/


// SSN - Social Security Number
jQuery.validator.addMethod("ssn", function(ssn, element) {
	return (ssn.length > 8);
}, "Please enter a valid social security number");



// First Name
jQuery.validator.addMethod("firstname", function(firstname, element) {
	//if ( firstname.match(/\d+$/) == null){
	//alert("FirstName test " + firstname.match(/^[\x00-\x7F]+[A-Za-z]$/));
	//if (firstname.match(/\uhhhh/) == null){
	//	return false;
	//}
	//return true;
	//var fname = document.getElementById("firstname").value;
    var length = firstname.length;
    var fCharacterSet = 0;
   
    for(i = 0; i < length; i++){
        if(firstname.charAt(i).match(/^[\x00-\x7F\d]+$/) == null) {
            fCharacterSet++;   
        }
        else if(firstname.charAt(i).match(/^[a-zA-Z\'\,\.\- ]$/) != null) {
            fCharacterSet++;
        }
    }

    if(fCharacterSet == length) {
        return true;
    }
    return false;	
//}, "Please enter a valid firstname. ");
}, $.i18n.prop("field.first_name"));

// Last Name
jQuery.validator.addMethod("lastname", function(lastname, element) {
//	if (lastname.length > 1 && lastname.match(/\d+$/) == null){
	//alert("test " + lastname.match(/^[A-Za-z]+$/));
    var lnLength = lastname.length;
    var lCharacterSet = 0;
   
    for(i = 0; i < lnLength; i++){
        if(lastname.charAt(i).match(/^[\x00-\x7F\d]+$/) == null) {
            lCharacterSet++;   
        }
        else if(lastname.charAt(i).match(/^[a-zA-Z\'\,\.\- ]$/) != null) {
            lCharacterSet++;
        }
    }

    if(lCharacterSet == lnLength) {
        return true;
    }
    return false;
//}, "Please enter a valid lastName. ");
}, $.i18n.prop("field.last_name"));

// Password
jQuery.validator.addMethod("password", function(password, element) {
	if(password == "")
		return true;
	if (password.length < 6 ){
		return false;
	}
	return true;
		
}, "Password entered should be at least 6 characters. ");


// CC - Credit Card Number
jQuery.validator.addMethod("cc", function(cc, element) {
	return cc.match(/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/);
}, "Please enter a valid credit card number");


// Intela Email Address
jQuery.validator.addMethod("intelaemail", function(email, element) {
	if (email.length > 50 ){
		return false;
	}
	return email.match(/^.*?@\w*?\..{1,}$/);

}, "Please enter a valid email address");


// Email Address
jQuery.validator.addMethod(  
   "email",  
   function echeck(value, element) { 
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
		
		if (!filter.test(element.value)) {
			 return false;
		}
		return true;
   },
   "Please enter a valid email." 
);

jQuery.validator.messages["required"] = $.i18n.prop("field.required");

