
jQuery(function($) {

	if ($('.validate:first').length && $('.validate:first').is(':visible')) alert('Mylène: ajoute les css pour cacher les .validate, et donne des styles approprié.');
	
	
	$('form').submit(function() {
		// OPTIONAL TO PREVENT BOTS FROM POSTING YOUR FORM : 
		//
		//            Add an hidden input with class="post-to" with the real url where you want the form to be posted
		//            then change the action of your form for something different. 
		//            If the form valids, the action will be switched before posting.
		//            
		//            EXAMPLE : <form action="http://www.google.com/" (...)><input type="hidden" class="post-to" value="/real-form-post-url.php" />
		
		
		// FOR EVERY VALIDATIONS THE input, select or textarea must be in the parent element of the validate div or span
		// EXAMPLE : <div>
		//               <input (...) />(...)
		//               <div class="validate (other validator classes)">Error message.</div>
		//           </div>
		// 
		// LIST OF CURRENTLY SUPPORTED VALIDATOR CLASS
		// - required : ensure not blank / unfilled
		// - radio    : ensure one of the siblings radio was selected
		// - checkbox : ensure one of the siblings checkbox was selected
		// - date     : ensure the text entered is a valid date with format AAAA-MM-DD. If not used with required, blank is valid.
		// - can-pc   : ensure the text entered is a valid canadian postal code, valid format are either G1G1G1 or G1G 1G1. If not used with required, blank is valid.
		// - us-zip   : ensure the text entered is a valid US Zip code, valid format is any 5 digit long value. If not used with required, blank is valid.
		// - pc-zip   : ensure the text entered is either a valid canadian postal code or a valid US Zip. If not used with required, blank is valid.
		// - integer  : ensure the text entered is a valid integer (no decimal allowed). If not used with required, blank is valid.
		// - float    : ensure the text entered is a valid float (decimal allowed). If not used with required, blank is valid.
		// - email    : ensure the text entered is a valid email address. If not used with required, blank is valid.
		// - phone    : ensure the text entered is a valid phone number, format are 1111111111 and 111-111-1111. If not used with required, blank is valid.
		
		var valid = true;
		$('.validate',this).each(function() {
			
			// CHECK FOR REQUIRED 
			// USAGE   : add required to the class of the validate span / div
			// EXAMPLE : <input (...) /><div class="validate required">Error message.</div>
			// NOTE    : - Works with input / textarea / select, when using with select
			//           - to valid the user really selected a value, set the default option with value="" to something like this : 
			//             <option value="" selected="selected">Please select...</select>
			if ($(this).is('.required')) {
				var isvalid = true;
				$(':input',$(this).parent()).each(function() {
					if ($(this).val()=='') isvalid = false;
				});
				if (!isvalid) {
					if ($(this).is(':hidden')) $(this).show();
					valid = false;
				}
				else if ($(this).is(':visible')) $(this).hide();
			}
			
			// CHECK FOR RADIOS OR MULTIPLE CHECKBOX WHICH REQUIRE TO HAVE AT LEAST ONE SELECTED
			// USAGE   : add "radio" or "checkbox" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input type="radio" name="x" value="0" />(...)
			//               <input type="radio" name="x" value="0" />(...)
			//               <div class="validate radio">Error message.</div>
			//           </div>
			// NOTE    : - Works with input.
			if ($(this).is('.radio') || $(this).is('.checkbox')) {
				
				if (!$(':checked',$(this).parent()).length) {
					if ($(this).is(':hidden')) $(this).show();
					valid = false;
				}
				else if ($(this).is(':visible')) $(this).hide();
			}
			
			// CHECK FOR DATE VALIDATION FIELD 
			// USAGE : add "date" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate date">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.date')) {
				if (validDate($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR CANADIAN POSTAL CODE VALIDATION
			// USAGE : add "can-pc" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate can-pc">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.can-pc')) {
				if (validCanPC($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR US ZIP CODE VALIDATION
			// USAGE : add "us-zip" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate us-zip">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.us-zip')) {
				if (validUSZip($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR INTEGER VALIDATION
			// USAGE : add "integer" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate integer">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.integer')) {
				if (validInt($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR FLOAT VALIDATION
			// USAGE : add "float" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate float">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.float')) {
				if (validFloat($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR EMAIL VALIDATION
			// USAGE : add "email" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate email">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.email')) {
				if (validMail($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR PHONE VALIDATION
			// USAGE : add "phone" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate phone">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.phone')) {
				if (validPhone($('input:first',$(this).parent()).val(),$(this).is('.required'))) {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if ($(this).is(':hidden')) $(this).show();
					valid=false;
				}
			}
			
			// CHECK FOR US ZIP OR CANADIAN POSTAL CODE VALIDATION
			// USAGE : add "pc-zip" to the class of the validate span / div
			// EXAMPLE : <div>
			//               <input(...) />(...)
			//               <div class="validate pc-zip">Error message.</div>
			//           </div>
			// NOTE    : - Works with input
			//           - If not used with required, blank will be valid.
			if ($(this).is('.pc-zip')) {
				if ($(this).not('.required') && $('input:first',$(this).parent()).val() == '') {
					if ($(this).is(':visible')) $(this).hide();
				}
				else {
					if (validCanPC($('input:first',$(this).parent()).val(),false) || validUSZip($('input:first',$(this).parent()).val(),false)) {
						if ($(this).is(':visible')) $(this).hide();
					}
					else {
						if ($(this).is(':hidden')) $(this).show();
						valid=false;
					}
				}
			}
		});
		
		if (valid && $('input.post-to',this).length) $(this).attr('action',$('input.post-to',this).val());
		return valid;
		
		
	});
	var validDate = function(dateaaaammjj,isrequired) {
		if (!isrequired && dateaaaammjj=='') return true;
		var dt=dateaaaammjj.split("-"),date=new Date(dt[0],dt[1]-1,dt[2]);
		return date.getDate()==dt[2]&&date.getMonth()+1==dt[1]&&date.getFullYear()==dt[0]?date:false;
	}

	var validCanPC = function(fStr,isrequired){
		if (!isrequired && fStr=='') return true;
		var myReg = /^[A-Z][0-9][A-Z][\s]{0,1}[0-9][A-Z][0-9]$/;
		if(myReg.exec(fStr)!=fStr){return false};
		return true;
	}
	
	var validUSZip = function(fStr,isrequired){
		if (!isrequired && fStr=='') return true;
		var myReg = /^[0-9]{5}$/;
		if(myReg.exec(fStr)!=fStr){return false};
		return true;
	}	

	var validInt = function(fStr,isrequired){
		if (!isrequired && fStr=='') return true;
		var myReg = new RegExp("^[0-9]+$");
		if(myReg.exec(fStr)!=fStr){return false};
		return true;
	}

	// REALLY NOT THE BEST WAY OF TESTING A FLOAT VALUE BUT ANYWAY... 
	var validFloat = function(fStr,isrequired){
		if (!isrequired && fStr=='') return true;
		var myReg = new RegExp("^[0-9.]+$");
		if(myReg.exec(fStr)!=fStr){return false};
		return true;
	}
	
	var validMail = function(email,isrequired) {
		if (!isrequired && email=='') return true;
		var result = false
		var theStr = new String(email)
		var index = theStr.indexOf("@");
		if (index > 0) {
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1)) result = true;
		}
		return result;
	}

	var validPhone = function(val,isrequired) {
		if (!isrequired && val=='') return true;
		if (val.match(/^[ -(]{0,1}\d{3}[) -]{0,1}\d{3}[ -]{0,1}\d{4}$/) || val.match(/^\d{10}$/)) return true;
		//if (val.match(/^\d{3}-\d{3}-\d{4}$/) || val.match(/^\d{10}$/)) return true;
		return false;
	}
});
