/*
 * Copyright (c) 2009 Hatem Azra
 * 
 */
 
/*
 * Version: 1.0
 * Release: 26/02/2009
 */ 

//Replace all letters in a field into capital (now used for zipcode)
function ALTU(obj){
  obj.value=obj.value.toUpperCase().replace(/([^\s.0-9A-Z])/g,"");
}

//function ReplaceSpeChars(obj){
//  var strNew = obj.value;
//	strNew = strNew.replace("&nbsp;"," ").replace("&Hyphen","-");
//	obj.value  = strNew;
//}
//Replace the first letter of a word into capital
function FLTU(obj) {
var strNew = obj.value;
strNew=strNew.replace(/(\s+\w)|(^\s*\w)/g,function(a){return a.toUpperCase()})
strNew1 = strNew.replace(/\bDe\b/g,"de").replace(/\bVan\b/g,"van").replace(/\bDer\b/g,"der").replace(/\bDen\b/g,"den").replace(/\bVanden\b/g,"vanden").replace(/\bTen\b/g,"ten").replace(/\bTer\b/g,"ter").replace(/\bTe\b/g,"te");
	obj.value  = strNew1;
}

//Replace the first letter of the first word of a sentence to capital
function FWTU(obj) {
obj.value=obj.value.replace(/(\.\s+\w)|(^\s*\w)|(\.+\w)|(\n+\w)|(\n\s+\w)|(\?+\w)|(\!+\w)|(\?\s+\w)|(\!\s+\w)/g,function(a){return a.toUpperCase()})
}

//Add the dots in the voorletters field between letters
function ADTL(obj) {
	var strValue = obj.value;
	strValue = strValue.replace(/\b\./g,"").replace(/\s/g,"");

	var sp = strValue.split('');
	
	var strFirstLetter;
	var aryWord = new Array();
	for (i = 0 ; i < sp.length ; i ++ ) {
		strFirstLetter = sp[i].substring(0,1).toUpperCase() + ".";	
		aryWord[i] = strFirstLetter;
	}
	
	strNew  = aryWord.join('');
	obj.value  = strNew;
	
return true;   
}

// refresh the image for the captcha code
function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}

//Show the continuous feedback to the user while feeling the form.
function toggleInline(id) {
	var x = document.getElementById(id).style;
	if (x.display == 'inline') x.display = 'none';
	else x.display = 'inline';
	return false;
}

//Add custom methods for the custom validation rules.
(function(jQuery) {
		 
		//check if the email address already exist
		jQuery.validator.addMethod("EmailInUse", 
	  		function(value, element) {
			var blnCheckLogin = jQuery.ajax({
							 type: "GET",
							 url: "/functions/app/ajax/EmailInUseQuery.asp?Email=" +  jQuery("#Email").val() + "",
							 async: false
						   }).responseText;
//				setTimeout(jQuery('#loading').show(), 200);
				if (blnCheckLogin == "True")	    
				{
					IsLoginExist = false;
				}
				else
				{
					IsLoginExist = true;
				}			
			return this.optional(element) || IsLoginExist; 
		});		

//		jQuery.validator.addMethod("Captcha", 
//		  function(value, element) {
//			jQuery.getJSON("/functions/app/CaptchaAjax/captcha.asp?validateCaptchaCode="+jQuery("#captchacode").val()+"&format=json&jsoncallback=?", function(data){
//				if (data.status == "1")	    
//				{
//					m_result = true;
//				}
//				else
//				{
//					m_result = false;
//				}	
//			});	
//			return this.required(element) || m_result; 
//		});

		//validation for the Captcha 
		var m_result; 
		jQuery.validator.addMethod("Captcha", 
	  		function(value, element) {
			var data = jQuery.ajax({
							 type: "GET",
							 url: "/functions/app/CaptchaAjax/captcha.asp?validateCaptchaCode=" +  jQuery("#captchacode").val() + "",
							 async: false
						   }).responseText;
				if (data == "1")	    
				{
					m_result = true;
				}
				else
				{
					m_result = false;
				}	
			return this.optional(element) || m_result; 
		});

		//validation for the domaine and subdomaine for NMTEtalage 
		var m_result; 
		jQuery.validator.addMethod("subDomaine", 
	  		function(value, element) {
				ajaxObj = new AjaxHandler();
				ajaxObj.URL = "/content/forms/AjaxRequests/AjaxRequests.asp";
				ajaxObj.SetParam('Action', 'subDomaine');
				ajaxObj.SetParam('value', value);
//				ajaxObj.SetParam('token','<%=objSession.Token%>');
				
				var sucess = function(html)
				{
					if (html == "" )
					{
						m_result = true;
					}
					else 
					{	
						m_result = false;
					}
				}	   
				
				ajaxObj.Run(sucess);
				 	
			return this.optional(element) || m_result; 
		});
		
		//validation for the domaine and domaine for NMTEtalage 
		var m_result; 
		jQuery.validator.addMethod("domaine", 
	  		function(value, element) {
				ajaxObj = new AjaxHandler();
				ajaxObj.URL = "/content/forms/AjaxRequests/AjaxRequests.asp";
				ajaxObj.SetParam('Action', 'domaine');
				ajaxObj.SetParam('value', value);
//				ajaxObj.SetParam('token','<%=objSession.Token%>');
				
				var sucess = function(html)
				{
					if (html == "" )
					{
						m_result = true;
					}
					else 
					{	
						m_result = false;
					}
				}	   
				
				ajaxObj.Run(sucess); 	

			return this.optional(element) || m_result; 
		});


		
		//validation for the Recaptcha 
		var recaptchaResponse;
		jQuery.validator.addMethod("reCaptcha", 
	  		function() {					
//alert("fdsfsdf");			
	//			jQuery('.ac_loading').show();
//				input.addClass('.ac_loading');
			    jQuery('input[name="recaptcha_response_field"]').addClass("ac_loading");  
				
				var objLipu = new LIPUHandler();
				
				jQuery('input[name="recaptcha_response_field"]').addClass("ac_loading"); 
				
				objLipu.Plugin = 'Captcha';
				
				objLipu.SetParam('state', 'Verify');
				objLipu.SetParam('action', 'Verify');
				objLipu.Async = false;
				objLipu.SetParam('challenge', jQuery('#recaptcha_challenge_field').val());
				objLipu.SetParam('response',  jQuery('#recaptcha_response_field').val());
				objLipu.SetParam('JavascriptEnabled',  'Yes');
				
				var oncompleteFunc = function(lipuObj) 
				{ 
//					console.log("Code:"+lipuObj.ErrorCode);
//					console.log("HTML:"+lipuObj.HtmlParam);
		//			jQuery('.ac_loading').hide();	
					if (lipuObj.ErrorCode == "100")
					{
						recaptchaResponse = true;
					}
					else 
					{
						recaptchaResponse = false;
						Recaptcha.reload();
					}
//					console.log(recaptchaResponse);
					jQuery('input[name="recaptcha_response_field"]').removeClass("ac_loading");
					jQuery('#recaptcha_response_field').show();
					
				};
			
		
		//		setTimeout(function(){objLipu.ProcessInput(oncompleteFunc);}, 1000);
				objLipu.ProcessInput(oncompleteFunc);
			return recaptchaResponse; 
		});

		// validation for the BSN!
		// valid bsn 287654321 for testing
		jQuery.validator.addMethod("bsn", 
		  function(value, element) { 
		  var array = new Array();
		  array[array.length]=value;
          var string = "";
          for(i = 0; i < array.length; i++) {
          string = array[i];
			}  
			var blnBSN=false;
			var str=string;
			var ary= new Array();
			ary=str.match(/\w/g);
			var a;
			a = ary[0]*9+ary[1]*8+ary[2]*7+ary[3]*6+ary[4]*5+ary[5]*4+ary[6]*3+ary[7]*2+ary[8]*(-1);
			var b;	
			b = a%11;
			if(b==0) blnBSN=true;
  			return this.optional(element) || blnBSN; 
			}, "<font color= red>* Een geldig BSN nummer is verplicht.</font>");

		// validation for the verzekeraar drop down
		jQuery.validator.addMethod("Verzekeraar", 
          function(value) {
           return /^[0-9]*$/.test(value);
           } 
     	); 
		
		// validation for the dutch number
		//jQuery.validator.addMethod("DutchNumber", 
//          function(value) {
//           return /^(\d|,)*\.?\d*$/.test(value);
//           } 
//     	); 		
		
		// validation when a field should be bigger than another field
		jQuery.validator.addMethod("BigThan", function(value, element, param) { 
			var value1 =  parseInt(value);
			var value2 =  parseInt(jQuery(param).val());
//			alert("value :"+value1);
//			alert("param :"+value2);			
			return value1 > value2; 
			}
		);		
		// validation for a valid Bank account (at least 5 numbers)
		jQuery.validator.addMethod("Bank", 
          function(value) {
           return /\d{5}$/.test(value);
           } 
     	); 
		// validation for the phone number (NL format)
		jQuery.validator.addMethod("Telefoon", 
          function(value) {
           return /^[0]{1}[1-9]{1}[0-9]{8}$/.test(value);
           } 
     	);
		
		// validation for the phone number (0900)
		jQuery.validator.addMethod("TelefoonServiceNummers", 
          function(value) {
           return /^[0]{1}[1-9]{1}[0-9]{8}$/.test(value) || /^[0]{1}[9][0][0]\d{4,7}$/.test(value);
           } 
     	);
		
		// validation for the mobile phone number (NL format)
		jQuery.validator.addMethod("Mobiel", 
          function(value) {
           return /^[0]{1}[6]{1}[0-9]{8}$/.test(value);
           } 
     	);
		// validation for the mobile phone number (NL format)
		// jQuery.validator.addMethod("AGB", 
          // function(value) {
           // return /^[0]{1}[1]{1}[0-9]{6}$/.test(value);
           // } 
     	// );		
		// validation for the mobile phone number (NL format)
		jQuery.validator.addMethod("Mobiel2", 
          function(value) {
		   return /^[0]{1}[6]{1}[-]{1}[0-9]{8}$/.test(value);
           } 
     	);
		
		// validation to allow only alpha characters (A-z, a-z, space, ', -)
		jQuery.validator.addMethod("Alpha", 
          function(value) {
           return /^([a-zA-Za-zA-Z\&\'][a-zA-Z\&\.\'\s\-]*|)$/.test(value);
           } 
     	);
		// validation to allow only alphanumeric characters (A-z, a-z, space, ', -)
		jQuery.validator.addMethod("AlphaNum", 
          function(value) {
           return /^([a-zA-Za-zA-Z\&\'\/][a-zA-Z0-9\&\.\'\s\-\/\+]*|)$/.test(value);
           } 
     	);
		
		// validation for Doamin and subdomain
		jQuery.validator.addMethod("domainSub", 
          function(value) {
           return /^([a-zA-Z0-9][a-zA-Z0-9\-\_]*|)$/.test(value);
           } 
     	);

        // validation for the time (24h format)
        //jQuery.validator.addMethod("Time",
//		   function(value) {
//		       return /^[0-2]\d:[0-6]\d$/.test(value);
//		   }
//		);
		// validation for the zip code (NL format)
		jQuery.validator.addMethod("Postcode", 
		   function(value) {
		   return /^[1-9]\d{3}\s?[A-Za-z]{2}$/.test(value);
		   }
		);
		// validation for the huisnummer (accepts only numeric characters and first character can NOT be a zero)		
		jQuery.validator.addMethod("Num", 
          function(value) {
           return /^[1-9]+((\s[A-Za-z0-9]+)|([A-Za-z0-9]+))*$/.test(value);
           } 
     	);
		// second validation for the BSN nummer so that the first digit can't be other than 0, 1 or 2
		jQuery.validator.addMethod("bsn1", 
          function(value) {
           return /^[0-2]+([0-9]+)*$/.test(value);
           } 
     	);
		// check for just 0 and 1
	//	jQuery.validator.addMethod("ZeroOne", 
    //     function(value) {
    //      return /^[0-2]+([0-9]+)*$/.test(value);
    //       } 
    // 	);
		// Valdiation for date dd/mm/jjjj
		jQuery.validator.addMethod("date1", 
          function(value) {
           return /^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((18|[2468][048]|[3579][26])00))))$/.test(value);
           } 
     	);
		// Valdiation for date dd-mm-jjjj
		jQuery.validator.addMethod("date2", 
          function(value) {
           return /^(((0[1-9]|[12]\d|3[01])\-(0[13578]|1[02])\-((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\-(0[13456789]|1[012])\-((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\-02\-((1[6-9]|[2-9]\d)\d{2}))|(29\-02\-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((18|[2468][048]|[3579][26])00))))$/.test(value);
           } 
     	);
		
		// A validation for prices
		jQuery.validator.addMethod("price", 
          function(value) {
           return /^\€?\d+(\,(\d{2}))?$/.test(value);
           } 
     	);
		// Valdiation for email
		jQuery.validator.addMethod("emailstrict", 
          function(value) {
		   return /^([\w-\.]+@([\w-]+\.)+[a-zA-Z-]{2,4})?$/.test(value);
           } 
     	);		
		
		
})(jQuery);
