var ret;
var ret2;
ret="true";
var bgColor = "#ef523b";

function validation(){
ret2="true";
   for(i=0;i<document.thisForm.elements.length;i++)
    {      
   		var elmType=document.thisForm.elements[i];
		var elmFirst=(elmType.id).substring((elmType.id).lastIndexOf("_")+1,(elmType.id).length-1);
		var elmSecond=(elmType.id).substring((elmType.id).lastIndexOf("_")+2,(elmType.id).length);
	if(elmFirst!= "O")
		{
            if(elmSecond=="N" || elmFirst=="N")
			{
				if(checkNum(elmType)=="false")
				   ret2 = "false";
				else{ret2="true";}
			}
	    if(elmSecond=="C" || elmFirst=="C")
			{
				 if(checkChar(elmType)=="false")
					ret2 = "false";
				 else{ret2="true";}
			}
 	    if(elmSecond=="W")
			{
				 if(checkpassword(elmType)=="false")
					ret2 = "false";
				 else{ret2="true";}					
 			}
 	    if(elmFirst=="D" ||elmSecond=="D")
 			{
 				 if(checkDate(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 							     
 			}
 	    if(elmFirst=="E" || elmSecond=="E")
 			{
 				  if(checkEmail(elmType)=="false")
 					ret2 = "false";
				  else{ret2="true"} 			
 			}
 	    if(elmFirst=="Z" || elmSecond=="Z")
 			{
 				 if(checkcurrency(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
 	    if(elmFirst=="P" || elmSecond=="P")
 			{
 				 if(checktelephonenumber(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
 	    if(elmFirst=="F" || elmSecond=="F")
 			{
 				if(checkfaxnumber(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
 	    if(elmFirst=="G" || elmSecond=="G")
 			{
 				if(checkpercentage(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
 	    if(elmFirst=="U" || elmSecond=="U")
 			{
 	            if(checkUrl(elmType)=="false")			
 				    ret2 = "false";
				else{ret2="true";} 			
 			}           
 		}
	    if(elmSecond=="X")
 			{
 				if(checkDouble(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
	    if(elmSecond=="Y")
 			{
 				if(checkDecimal(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
	    if(elmSecond=="O")
 			{
 				if(checkNull(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
	    if(elmSecond=="R")
 			{
 				if(checkRatio(elmType)=="false")
 					ret2 = "false";
				else{ret2="true";} 			
 			}
         if (ret2=="false"){ return ret2; }
 	 }
return ret2
}

function checkNum(elmType)
{
	var numVal=elmType.value;
	var len=numVal.length;
	if (len == 0){
		alert("Enter a number in the field that changes colour.");
		elmType.style.backgroundColor=""+ bgColor +"";
		elmType.focus();
		ret2 = "false";
		return ret2
	}
	for ( var i=0 ; i<len ; i++)
	{
		var strVal=numVal.charAt(i);
		if(isNaN(strVal))
		{
			alert("Enter a number in the field that changes colour.");
			ret2 = "false";
                        elmType.style.backgroundColor=""+ bgColor +"";
   			elmType.focus();
			return ret2
		}
		else{ret2="true";}
	}
return ret2
}
function checkRatio(elmType)
{
  var numVal=elmType.value;
  var len=numVal.length;
  var charpos = numVal.search("[A-Za-z]"); 
  var strVal=parseFloat(numVal);
  if (len == 0){
     alert("Enter a ratio in the field that changes colour.");
     elmType.style.backgroundColor=""+ bgColor +"";
     elmType.focus();
     ret2 = "false";
     return ret2
   }
   else if(numVal.length > 0 &&  charpos >= 0){
     	alert("This field cannot contain characters");
     	elmType.style.backgroundColor=""+ bgColor +"";
     	elmType.focus();
     	ret2 = "false";
     	return ret2
   }
   else	if(strVal > 1 || strVal < 0 ){ //there needs to be a check here for any characters
     	alert("The ratio must be between 0 and 1");
     	ret2 = "false";
     	elmType.style.backgroundColor=""+ bgColor +"";
     	elmType.focus();
     	return ret2
   }
//return ret2
}
function checkDouble(elmType)
{
	var numVal=elmType.value;
	var len=numVal.length;
	if (len == 0){
		alert("Enter Number [Double]");
		elmType.style.backgroundColor=""+ bgColor +"";
		elmType.focus();
		ret2 = "false";
		return ret2
	}
	for ( var i=0 ; i<len ; i++)
	{
		var strVal=numVal.charAt(i);
		if(isNaN(strVal))
		{
			alert("Enter a number in the field that changes colour.");
			elmType.style.backgroundColor=""+ bgColor +"";
			elmType.focus();
			ret2 = "false";
			return ret2
		}
		else{ret2="true";}
	}
return ret2
}
function checkDecimal(elmType)
{
	var numVal=elmType.value;
	var len=numVal.length;
	var charpos = numVal.search("[A-Za-z]"); 
	if (len == 0){
		alert("Enter a Decimal as 0.00 in the field that changes colour.");
		elmType.style.backgroundColor=""+ bgColor +"";
		elmType.focus();
		ret2 = "false";
		return ret2
	}
	else if(numVal.length > 0 &&  charpos >= 0){
     		alert("This field cannot contain characters");
     		elmType.style.backgroundColor=""+ bgColor +"";
     		elmType.focus();
     		ret2 = "false";
     		return ret2
   	}

	//var pos1=elmType.value.indexOf(".",0);
	//if(pos1 != 1)
	//{
	//	alert("Enter a Decimal as 0.00 in the field that changes colour.");
	//	elmType.value="";
        //        elmType.style.backgroundColor=""+ bgColor +"";
	//	elmType.focus();
	//	ret2 = "false";
	//	return ret2
	//}
	else{ret2="true";}

//return ret2
}
function checkNull(elmType)
{
	var numVal=elmType.value;
	var len=numVal.length;
	if (len == 0){
		alert("Field cannot be empty");
		elmType.style.backgroundColor=""+ bgColor +"";
		elmType.focus();
		ret2 = "false";
		return ret2
	}
return ret2
}
function checkChar(elmType)
{
	var numVal=elmType.value;
	var len=numVal.length;
	if (len == 0){
		alert("Enter Characters only in the field that changes colour.");
		elmType.style.backgroundColor=""+ bgColor +"";
		elmType.focus();
		ret2 = "false";
		return ret2
	}
	for ( var i=0 ; i<len ; i++)
	{
		var strVal=numVal.charAt(i);
		if(strVal !=" ")
		{
			if (strVal == 1 || strVal == 2  ||  strVal == 3 || strVal == 4 || strVal == 5 || strVal == 6 || strVal == 7 || strVal == 8 || strVal == 9 || strVal == 0 )
			{
				alert("Enter Characters only in the field that changes colour.");
                                elmType.style.backgroundColor=""+ bgColor +"";
				elmType.focus();
				ret2 = "false";
				return ret2
			}
			else{ret2="true";}
		}
	}
return ret2
}
function checkEmail(elmType)
{
		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
		if(regex.test(elmType.value)==false)
		{
			alert("Enter Valid Email address");
                        elmType.style.backgroundColor=""+ bgColor +"";
			elmType.focus();
			ret2 = "false";
		}
		else{ret2="true";}
return ret2
}
function checkUrl(elmType)
{
    if (elmType.value!="")
	{
		var pos1=elmType.value.length;
		var value=elmType.value;
		pos1=pos1-4
		value=value.substring(pos1,pos1+1)
		var pos2=elmType.value.indexOf(".",0);
		if (pos2 == -1) 
		{
			alert("Enter a Valid URL");
                        elmType.style.backgroundColor=""+ bgColor +"";
			elmType.focus();			
			ret2 = "false";
		}
		else{ret2="true";}
	} 
return ret2
}  
/*date function to be tested*/
function checkDate(elmType)
{

  if (elmType.value.length == 0){
	validID="true";
  }
  var date1,year1,month1,test,separator1,separator2;
  if (elmType.value.length == 10) 
  {
    date1  = parseInt(elmType.value.substring(0,2));
    month1 = parseInt(elmType.value.substring(3,5));
    year1  = parseInt(elmType.value.substring(6,10));
    separator1 = elmType.value.substring(2,3);
    separator2 = elmType.value.substring(5,6);
    if (isNaN(parseInt(date1))==false && isNaN(parseInt(month1))==false &&  isNaN(parseInt(year1))==false) 
     {
      if (separator1 == "/" && separator2 == "/")
       {
        if ((date1 < 0) || (date1 > 31) || (month1 < 0) || (month1 > 12)) 
	 {
	  alert("Please Enter a Valid Date1");
	  elmType.style.backgroundColor=""+ bgColor +"";
	  elmType.focus();
	  ret2 = "false";
          return ret2
	 }
	if(month1 == "02") 
	 {
	  if((year1%4 == 0) & (year1%100 == 0) || (year1%400 == 0)) 
	   {
	    if(date1 > 29) 
	     {
	      alert("Please Enter a Valid Date");
              elmType.style.backgroundColor=""+ bgColor +"";
	      elmType.focus();
	      ret2 = "false";
              return ret2
	     }
	   }
	   else 
	      {	
	       if(date1 > 28) 
		{
		 alert("Please Enter a Valid Date3");
                 elmType.style.backgroundColor=""+ bgColor +"";
                 elmType.focus();
		 ret2 = "false";
                 return ret2
		}
	      }
	   }
	   else 
	      {
	       if (month1 == "04" || month1 == "06" || month1 == "09" || month1 == "11") 
		{
		 if(date1 > 30) 
		  {
		   alert("Please Enter a Valid Date4");
                   elmType.style.backgroundColor=""+ bgColor +"";
		   elmType.focus();
		   ret2 = "false";
                   return ret2
		  }
		 }
	       }
	       ret2 = "true";
	    }
	    else
	       {
	        alert("Please Enter Date in dd/mm/yyyy format");
                elmType.style.backgroundColor=""+ bgColor +"";
	        elmType.focus();
	        ret2 = "false";
	       }
	   }
	   else
	      {
	       alert("Character Not Allowed");
               elmType.style.backgroundColor=""+ bgColor +"";
	       elmType.focus();
	       ret2 = "false";
               return ret2
	      }
	 }
  else
  {
		alert("Please Enter Date in dd/mm/yyyy format");
                elmType.style.backgroundColor=""+ bgColor +"";
		elmType.focus();
    ret2 = "false";
  }
return ret2
}