// JavaScript Document

function togglevis(val){
var el = document.getElementById(val);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function setAttr(el_name,at_value){
	var el = document.getElementsByName(el_name);
	//alert("document.getElementsByName('" + el_name + "')." + at_name +" = '"+ at_value+"'");
	el[0].id=at_value;
	//alert(at_value)
}

function show(obj) {
	var el = document.getElementById(obj);
if(el != null){
		el.style.display = '';
}
}

function hide(obj) {
	var el = document.getElementById(obj);
if(el != null){
		el.style.display = 'none';
}
}

// Email Validation 


function checkPostCode (toCheck) {

  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  var pcexp = new Array ();

  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  var postCode = toCheck;

  var valid = false;
  
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      pcexp[i].exec(postCode);
      
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      valid = true;
      
      break;
    }
  }
  
 if (valid) {return true;} else return false;
}


function checknumber(val){
var x=val;
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
testresult=false
}
return (testresult)
}


function checkemail(email_address){
 var str=email_address;
 var filter=/^.+@.+\..{2,3}$/

if (filter.test(str)){
    testresults=true
}else{
    testresults=false
}
 return (testresults)
}

function check_date(dateStr)
{
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
return false;
}

day = matchArray[1];
month = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
return false;
}

if (day < 1 || day > 31) {
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
return false;
}
}
return true; // date is valid
}

function checkForValidDate(){
var ddlVMonth = document.getElementById("_ctl0_DropDownListMaanden");
var ddlVDay = document.getElementById("_ctl0_DropDownListDagen");
var ddlVYear = document.getElementById("_ctl0_DropDownListJaren");

if (!isDate(ddlVDay.value + '/' + ddlVMonth.value + '/' + ddlVYear.value))
{ return(false); }

return(true);


}


// Form Validation 
function checkForm(theform){
var cht = 0;
var standard_error = "Please check for errors";
var msg = "";

// form object id layout 
// id="need|validation type|min length|max_length|field_name|error_msg"
//       0       1              2          3          4          5



for (x = 0; x < theform.elements.length; x++){
	var valer = 0;
	// collect form objects
    var el = theform.elements[x];
		
if(el.type != null){
		
		var el_type = el.type.toLowerCase();
		var field_options = el.id.split("|"); 
		
// check if form object need validating 

   if (field_options[0] == 'need') {
   
// check the type of object
	if(el_type == "text" || el_type == "textarea" || el_type == "select-one" || el_type == "password" ){
		 
// standard validation		 
		if(field_options[1] == "check"){	
			
			if(!el.value){
				 
				 msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				 valer = 0;			
			
			}else{
				 
				 valer = 1;
			
			}
		
//string length = X
		}else if(field_options[1] == "length"){

			 if(el.value.length < field_options[2] || el.value.length > field_options[2]){
				 
				 msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				 valer = 0;
			 
			 }else{
				 
				 valer = 1;
			 
			 }
//string length less than X		
		}else if(field_options[1] == "length_min"){

			 if(el.value.length < field_options[2]){
				 
				 msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				 valer = 0;
			 
			 }else{
				 
				 valer = 1;
			 
			 }

// string length greater that X
		}else if(field_options[1] == "length_max"){
			
			 if(el.value.length > field_options[3]){
		    
			     msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				 valer = 0;
			 
			 }else{
			
			   	valer = 1;
			
			}
			
		}else if(field_options[1] == "range"){
			
			 if(el.value.length > field_options[2] && el.value.length < field_options[3]){
			     
				 valer = 1;
			 
			 }else{
				 
				 msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				 valer = 0;

			 }		 
		 
		 }else if(field_options[1] == "email"){
		 
		 	if(!checkemail(el.value)){
				
				msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				valer = 0;				
			
			}else{
				valer = 1;
			}
			
		}else if(field_options[1] == "date"){
		 	
			if(!check_date(el.value)){
				
				msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				valer = 0;				
			
			}else{
				valer = 1;
			}		
		
		}else if(field_options[1] == "postcode"){
		 	
			if(!checkPostCode(el.value)){
				
				msg = msg + field_options[4] + " - " + field_options[5] + "\n";
				valer = 0;				
			
			}else{
				valer = 1;
			}
			//
		
		}else if(field_options[1] == "number"){
		 	
			if(!checknumber(el.value)){
				
				msg = msg + field_options[4] + " - Please input a valid number " + field_options[5] + "\n";
				valer = 0;				
			
			}else{
				valer = 1;
			}
		
		}// validation types 
	   
	   }// need if 
	   
	   if(el_type == "checkbox" || el_type =="radio"){
	   	
			if(el.checked == false){
				
				msg = msg + field_options[4] + " - " + field_options[5] + "\n";
	     		valer = 0;
	   	  
		  }else{
		 
				 valer = 1;
		
		}// validation check
	   
	   }// form types
	   
	    if (valer == 0){
            
			el.className='formError';
            cht = 1;
            
			}else{
			
			el.className='formOK'; 
		
		}
	   
	   valer = 0;
	 
	 }

} 

}

if (cht == 1){
	
	if(!msg){
		
		alert(standard_error);
	
	}else{
		
		alert(msg);
	
	}

return false;

}else{

return true;	   

} 

}
