
// Author : Kiran
// This File contains all the validations for the asp files
// naming conventions has to be used to use this file


// This function check the type of the form field and uses respective logic to check whther it is empty

/*

reqErrorMessage 

This variable is used to store Messages to Be displayed if the data is missing in the required fields
and this variable is initialized in the respective page according to field index in the form
If you do not want any message please leave it with empty text


invalidDateErrorMessage

This variable is used to store Messages to Be displayed when Invalid Dates Are Choosen 

compareDateErrorMessage

This variable is used to store Messages to Be displayed when Two Dates Are Compared 


*/

var reqErrorMessage,invalidDateErrorMessage,compareDateErrorMessage;



// This is the actual function called when a form is submitted 
// this function loops through the form controls and searches for strings parametes in control name and does the respective validate 
// For example "req" is searched and validated for empty fields

function Validate()
{
   var f;	

   if(arguments[0])
      f=arguments[0];
   else
      f=document.forms[0];
   
   
   for(i=0; i<f.elements.length; ++i)
   {

       // This if handles Required Fields	       
       if(f.elements[i].name.indexOf("req")!=-1)
	 if(ValidateEmptyField(f.elements[i])==false)
	   {   
		if(reqErrorMessage) 
		   alert(reqErrorMessage[i]);				  	
		else
                   alert("Please Enter Data In The Required Fields"); 			
	        f.elements[i].focus();
                return false;
	    }		
		 
	// This if handles whether valid EMail is entered or not
        if(f.elements[i].name.indexOf("EMail")!=-1)
          if(ValidateEmail(f.elements[i])==false)
	   {
               alert("Please Enter Valid Email Address");  
	       f.elements[i].focus();
	       return false;
	   }		
  
	// Checks To See If The Date Entered Is After Current Date
        if(f.elements[i].name.indexOf("CurDate")!=-1)
          if(ValidateCurDate(f.elements[i],f.elements[i+1],f.elements[i+2])==false)
          {
              alert("Cannot Enter Details After Current Date");
	      f.elements[i].focus();
              return false;         
          } 
	
	// Checks To See If The Time Entered Is After Current Time
	if(f.elements[i].name.indexOf("CurTime")!=-1)
	 if(ValidateCurTime(f.elements[i-3],f.elements[i-2],f.elements[i-1],f.elements[i],f.elements[i+1],f.elements[i+2])==false)
          {
              alert("Cannot Enter Details After Current Time");
	      f.elements[i].focus();
              return false;         
          } 

  	// Validates For Numeric Values	 
        if(f.elements[i].name.indexOf("numeric")!=-1)
          if(ValidateNumeric(f.elements[i])==false)
          {
              alert("The value '" + f.elements[i].value + "' you have entered is not a valid Numeric Value\n Please Enter a numeric value.");
	      f.elements[i].focus();
              return false;         
          }  

        if(f.elements[i].name.indexOf("ValidDate")!=-1)
         if(ValidateDate(f.elements[i],f.elements[i+1],f.elements[i+2])==false)
          {
              if(invalidDateErrorMessage) 
		   alert(invalidDateErrorMessage[i]);				  	
	      else
                   alert("Please Select Valid Date"); 

	      f.elements[i].focus();
              return false;         
          } 
        
        if(f.elements[i].name.indexOf("ValidDate1")!=-1)
         if(CompareDate(f.elements[i],f.elements[i+1],f.elements[i+2],f.elements[i+3],f.elements[i+4],f.elements[i+5])==false)
          {
              if(compareDateErrorMessage) 
		   alert(compareDateErrorMessage[i]);				  	
	      else
                   alert("First Date Cannot Be Greater Than Second Date");
                
	      f.elements[i].focus();
              return false;         
          }     
        
	// This If Validates Retype Password Fields
	if(f.elements[i].name.indexOf("Password1")!=-1)
         if(ValidateReTypePassWord(f.elements[i],f.elements[i+1])==false)
          {
              alert("ReType PassWord Does Not Match");
	      f.elements[i+1].focus();
              return false;         
          }
          
   		if(f.elements[i].name.indexOf("ValidDateAfter")!=-1)
	     
         if(ValidateAfterDate(f.elements[i],f.elements[i+1],f.elements[i+2])==false)
          {
              alert("Cannot Enter Details Before Current Date");
			  f.elements[i].focus();
                return false;         
          } 
        
        if(f.elements[i].name.indexOf("Diff20")!=-1)
        
         if( DateDiff20(f.elements[i+5], f.elements[i+2]) == false)
          {
              alert("Difference between the Two Dates should be atleast 20");
			  f.elements[i].focus();
                return false;         
          }     
          
    
	}
	return true;
}

function ValidateEmptyField(FieldName)
{ 
    

     switch(FieldName.type)
     {
         case "text":
            if(IsEmpty(FieldName))
               return false;
		    break;
         case "file":
            if(IsEmpty(FieldName))
               return false;
		    break;    
 	 case "textarea":
            if(IsEmpty(FieldName))
		       return false;
	        break;   
	 case "password":
	        if(IsEmpty(FieldName))
		       return false;
			break;   
	 case "select-one":
		if(IsSelect(FieldName))
		      return false;  
	        break;
	 case "select-multiple":
		if(IsMultipleSelect(FieldName))
		{ 
		    alert("Please Select From The List");
		    return false;  
		} 

	        break;	
       }
       
       
      return true;
}


// This function is used to validate email address

function ValidateEmail(FieldName)
{ 

if(FieldName.value.length!=0)
   if((FieldName.value.indexOf("@")!=-1) && (FieldName.value.indexOf(".")!=-1))
       return true; 
   
   else
       
   return false;
   
}


// This is function validate a numerica value in a form field which is passed as parameter

function ValidateNumeric(FieldName)
{ 

if(FieldName.value.length!=0)
{
   if(isNaN(FieldName.value)==false)
   {
     if(parseInt(FieldName.value)>=0)
	  return true; 
	 else return false; 
	}	  
   else    
   return false;
}  

}

// This is function validate a Retype password option like same password is entered in both the text boxes
// This is used in reenter password option

function ValidateReTypePassWord(FieldName1,FieldName2)
{ 

if(FieldName1.value==FieldName2.value)
       return true; 
else    
   return false;


}

// This Function validates date whether entered data is valid or not

function ValidateDate(MonthName,DayName,yearname)
{   
	var Datestr = new Array()
	if((yearname.options[yearname.selectedIndex].value%4)==0 || (yearname.options[yearname.selectedIndex].value)==0)
		Datestr = [31,29,31,30,31,30,31,31,30,31,30,31,30,31];
	else
		Datestr = [31,28,31,30,31,30,31,31,30,31,30,31,30,31];
		if(DayName.options[DayName.selectedIndex].value > Datestr[MonthName.options[MonthName.selectedIndex].value-1])	
		 return false;
}

// This Function validates date whether entered data is less than or equal to current date

function ValidateCurDate(MonthName,DayName,YearName)
{    
	var date1=new Date(YearName.options[YearName.selectedIndex].value + "/" + MonthName.options[MonthName.selectedIndex].value + "/" + DayName.options[DayName.selectedIndex].value);
	var CurrentDate=new Date();
	if(date1<=CurrentDate)
	   return true;
	else
	   return false; 
}

// This Function validates date whether entered data is greater than or equal to current date

function ValidateAfterDate(MonthName,DayName,YearName)
{    
	var date1=new Date(YearName.options[YearName.selectedIndex].value + "/" + MonthName.options[MonthName.selectedIndex].value + "/" + DayName.options[DayName.selectedIndex].value);
	
	var CurrentDate=new Date();
	date1.setHours(CurrentDate.getHours()+3);
	date1.setMinutes(CurrentDate.getMinutes());
	
	if(date1 >= CurrentDate)
	   return true;
	else
	   return false; 
}

// This Function validates whether entered time is less than or equal to current time

function ValidateCurTime(MonthName,DayName,YearName,Hour,Minute)
{    
	
	var CurrentDate=new Date();
	var hours1,minutes1,hours2,minutes2;
	hours1=CurrentDate.getHours();
	minutes1=CurrentDate.getMinutes()+1;
	hours2=parseInt(Hour.options[Hour.selectedIndex].value);
	minutes2=parseInt(Minute.options[Minute.selectedIndex].value);
	
	if((hours1>=hours2) && (minutes1>minutes2))
		return true;
 	else
	{
		
		var date1=new Date(YearName.options[YearName.selectedIndex].value + "/" + MonthName.options[MonthName.selectedIndex].value + "/" + DayName.options[DayName.selectedIndex].value);
		//var date1=new Date(YearName.options[YearName.selectedIndex].value,MonthName.options[MonthName.selectedIndex].value,DayName.options[DayName.selectedIndex].value,hours2,minutes2);
		date1.setHours(hours2);
		date1.setMinutes(minutes2);
		var CurrentDate=new Date();
		if(date1<CurrentDate)
	   return true;
		else
	   return false;
	   
	}   
}

// This Function compares two dates and return whther first date is greater than second or not

function CompareDate(MonthName1,DayName1,YearName1,MonthName2,DayName2,YearName2)
{   
	var date1=new Date(YearName1.options[YearName1.selectedIndex].value + "/" + MonthName1.options[MonthName1.selectedIndex].value + "/" + DayName1.options[DayName1.selectedIndex].value);
	var date2=new Date(YearName2.options[YearName2.selectedIndex].value + "/" + MonthName2.options[MonthName2.selectedIndex].value + "/" + DayName2.options[DayName2.selectedIndex].value);
	if(date2>=date1)
	   return true;
	else
	   return false;   
	
}







function DateDiff20(D1,D2)
{
	if ( (D1.options[D1.selectedIndex].value - D2.options[D2.selectedIndex].value) < 20) 

		return false;

	else

		return true;
		
}


// This function is used to validate whther field is empty or not

function IsEmpty(FieldName)
{

	if (FieldName.value.length == 0)
	{
		
		FieldName.focus();
		return true;
	}
	else
		return false;
} 

// This function is used to validate whther Select box is selected or not

function IsSelect(ListName)
{	
  
	if(ListName.options(ListName.selectedIndex).value <= 0)
		{
		alert("Please Select In this List");
		return true;
		}
	else 
		return false;
}	

// This function is used to validate whther Multiple Select box is selected or not

function IsMultipleSelect(ListName)
{ 
  value=true;

  for(i=0; i<ListName.options.length; i++)
 { 
   
  
   if(ListName.options[i].selected==true)
		    value=false;
	}		
			
  return value;			

}


// This function Fill List Box With Days Of Respective Month

function fillDays()
{
   var day,year,f,i;
   var noofdays=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   f=arguments[0].form;

   for(i=0; i<f.elements.length; ++i)
   {
       if(f.elements[i].name.indexOf(arguments[0].name)!=-1)
         break;

   }
   day=f.elements[i+1];
   year=f.elements[i+2];
   alert(day.options[day.options.selectedIndex].value);

}



	


