/** Version 1.08 Modified on 23-02-2007
*
*
* Form is validated to the CheckRequired function by typing -- 'return(CheckRequired(this)'
*The form fields are supposed to be marked as by their ID fields.
*
*        Conventions for ID trms used
* 'txt' for Text fields
* 'num' for numeric fields. This will accept only numeric values ans ('.')
* 'phn' for telephone or other code numbers which will be having numbers with some special characters like ('-',' ',',',']','[','/').
* 'eml' for email ID
* 'psw' for password
*
* Any part which is comming after this conventional terms will be concidered as the label of that particular field.
*
*It is to be taken car that each field should be marked with '*' prior to its id and idtype
**/
function Validate(form)
{
	var pass=false;
	var dispAlert="Please make sure that you filled following fields,\n";
	if(document.forms)
	{
		for (i=0;i<form.length;i++)
		{
			var tempObj=form.elements[i]
			if(tempObj.id.substring(0,1)=="*")
			{
				var label=tempObj.id.substring(4)
				var fieldType=tempObj.id.substring(1,4)				
				if(tempObj.value=='')
				{
					pass=" field is mandatory \n";
					tempObj.focus();
				}
				else
				{
					if(fieldType=="txt")
					{						
					}
					else if(fieldType=="num")
					{
						pass=chkNumField(tempObj.value)
					}
					else if(fieldType=="eml")
					{
						pass=chkEmailField(tempObj.value)
					}
					else if(fieldType=="phn")
					{
						pass=chkPhoneField(tempObj.value)
					}					
				}	
			if (pass)
			{
				alert(label+pass)
				tempObj.focus();
				return false
			}
				
			}
			if(tempObj.id.substring(1,5)=="date")
			{
			    tempObj.value=ConvDateUs(tempObj.value);
			}
		}
	}
if(!pass)
	return true;
}
/**************************************************************************/
//			Number Validation //will accept '-',' ',',',']','[','/')
function  chkNumField(c1) 
{		
	if (trim(c1) != "")
	{	
		for (var i = 1; i < c1.length; i++)     
	    {    
	    	var ch = c1.substring(i, i + 1);   
	     	if ((ch < "0" || "9" < ch) & (ch !="."))
		     {   
		     	return(" field accepts only numbers and \".\" ");
		     }
	    } 
	}
}
//			Phone Number validation

function chkPhoneField(c)
{
   if(c==0 || c.length<=5)
    {   
    	return(" enter valid data"); 
    }			
	if (trim(c) != "")
	{
	    for (var i = 1; i < c.length; i++)     
	    {    
	    	var ch = c.substring(i, i + 1);   
		    if ((ch < "0" || "9" < ch) & (ch != '-') & (ch != ' ') & (ch !=",") & (ch != "]") & (ch != "[") & (ch != "/") & (ch != ",") )
	        {  
	            return(" field accepts only numbers , blank space & some special characters - , [ ] /");
	    	        	        
	        }
		} 
	}
}

//  Email Validation
function chkEmailField(str)
{	
	if (trim(str) == "")
	{    
		return(" enter valid email Id");
	}
	else
	{
	    for (var i = 1; i < str.length; i++)     
	    {    
	    	var ch = str.substring(i, i + 1);   
            if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_')&& (ch !='@')&& (ch != '.') && (ch != '-') )
	        {   
	            	return(" some special character and blank space not allowed");
	        }
	     }	     
	     if (!isEmailAddr(str)) 
		 {
		    return(" please enter a complete email address in the form: yourname@yourdomain.com");
         }
		 if (str.length < 3)
         {
		    return(" please enter at least 3 characters in the \"email\" field.");
         }
    }
}

/*	   if(document.f1.checkbox.checked)	   return true;	   
	   else{
	    	   alert("Please accept the Terms & Condition");
			   return false;
			}
}*/
 
 //-------- Methods for Checking E-Mail Validation   ---->
    function trim(str) 
    {       while (str.charAt(str.length - 1)==" ")   str = str.substring(0, str.length - 1);
            while (str.charAt(0)==" ")   str = str.substring(1, str.length);
            return str;
    }
   function isEmailAddr(email)
   {     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;
    }
/**************************************************************************/
/*         DOB date validation
 	var dateLength=DOBdate.length;
	var DOBerror=null;
	if(dateLength >=8)
	{
		if(DOBdate.search("/"))
		{
		   var DOBarray=(DOBdate.split("/"))
		}
		
		if(DOBdate.search("-"))
		{
		   var DOBarray=(DOBdate.split("-"))
		}
		
		var DOBday=DOBarray[0]
		var DOBmonth=DOBarray[1]
		var DOByear=DOBarray[2]
		
		var d = new Date();
		var currDay=d.getDate();
		var currMonth=d.getMonth();
			currMonth+=1;
		var currYear = d.getFullYear();
		if(DOByear>currYear) 
		{
		  DOBerror="Enter proper year of birth";
		}	
		else if(DOByear==currYear)
		{
			if(DOBmonth>currMonth)
			{
				DOBerror="Enter proper month and year of birth";
			}
			else if(DOBmonth==currMonth)
			{
				if(DOBday>currDay)
				{
					DOBerror="Enter proper date month and year of birth";
				}
			}
		}
	}
	else
	{
		DOBerror="Enter proper date of birth";
	}
	******************************
	if(DOBerror)
	{
		alert(DOBerror);
		return false;
	}*/
	
	
function ConvDateUs(date)
{
	if(date && date.length==10 && date.substring(2,3)=="-")
	{
		var day=date.substring(0,2)
		var month=date.substring(3,5)
		var year=date.substring(6,10)
		var returnDate=(year+"-"+month+"-"+day)
		return returnDate
	}
	else
		return null;
}