//*********************************************************************
// File Name               : common_script.js
// Scope Of Program        : validation script for  blank, numeric,
//			     alpha-numeric,email id form field.
// Created On              : 10/03/2004
// Modified On             : 11/03/2004
// Reason of Modification  : Add date validation function
// Special Remark          :
//*********************************************************************
 

/* Function to display java Popup for calendar */

function javaCal(fld)
{
	/* Name		: javaCal						 */
	/* Purpose	: Creates a popup Window for selecting the date. The
			  selected date is set in the textfield.		 */
	/* Inputs	: fld = textfield for setting the date			 */
	/* Outputs	: Calendar Popup					 */
	/* Calls	: calnav.htm						 */
	/* Called By	: 							 */


	tmp_dt_today= new Date();
	tmp_str_dt  = tmp_dt_today.getDate();
        tmp_str_mon = tmp_dt_today.getMonth();
        tmp_str_yr  = tmp_dt_today.getFullYear();
	
	var tmp_dt_timeStamp  = new Date(tmp_str_yr,tmp_str_mon,tmp_str_dt,0,0,0);
	var tmp_int_todaySecs = Date.parse(tmp_dt_timeStamp);	
	
	/* allowPast = 1 allows selection of past dates
	   allowPast = 0 does not allow selection of past dates
	*/
	
	doc = "./include/inc_common/calnav.htm?frmField="+fld+"&tmp_int_todaySecs="+tmp_int_todaySecs+"&allowPast=1";
	window.open(doc,"Calendar","toolbar=0, location=0,directories=0,resizable=no,status=0,menubar=0,scrollbars=no,width=275,height=275,screenX=0,screenY=0");

}

//************** function restricts user from entering blank string and
// from entering only white speces.*****************
 
function isBlank(tmp_str)
{
// Name      : isBlank.

// Purpose   : keeping validation for blank field.

// Inputs    : tmp_str -> string for validation

// Outputs   : return the value of veriable newString.
//             if newString = "" returns null

var newString  = ''; //trim value of given string
var substring  = ''; // temporary string for checking white spaces in string.
beginningFound = false; // position of white space

// copy characters over to a new string
// retain whitespace characters if they are between other characters

for (var i = 0; i < tmp_str.length; i++)
 {  		
	// copy non-whitespace characters
	// hold whitespace characters in a temporary string if they follow a non-whitespace character
	
	if (tmp_str.charAt(i) != ' ' && tmp_str.charCodeAt(i) != 9)
	{
		// if the temporary string contains some whitespace characters, copy them first
		if (substring != '')
		{
			newString += substring;
			substring = '';
		}
		newString += tmp_str.charAt(i);
		 if (beginningFound == false)
		 {
		   beginningFound = true;
		 } 
	}

	else if (beginningFound == true)
	{
	   substring += tmp_str.charAt(i);
	}
  }

  return newString;

}

//************** function for allowing only Alpha-Numeric String *****************

function isAlphaNumeric(tmp_str)
{
  // Name      : isAlphanumeric.
  
  // Purpose   : allow user to enter only Alpha(A-Z)-Numeric(0-9) values.
  // Inputs    : tmp_str -> string for validations.          
  // Outputs   : return 1 -> if form field is alphanumeric
  //		 return -1 -> if form field is not alphanumeric

//ignore validation if tmp_str is blank.
if(tmp_str != "")
 {
  // searching whole string word by word
    if (tmp_str.search)
      {
        //checking the words in string.
        //  if string contains the non Alpha-Nemeric value, return -1.
        //  else return 1.
        
	 if ((tmp_str.search(/[^\w\s]/) != -1) || (tmp_str.search(/\W/) != -1))
	  {
	 	return -1;
	  }
      } 
 }     
 return 1;
}


//************** function for allowing only Numeric String *****************

function isNumeric(tmp_int)
{
  // Name      : isNumeric.
  
  // Purpose   :allow user to enter only Numeric(0-9) values.
  // Inputs    : tmp_int -> string for validations.
  // Outputs   : return 1 -> if form field is Numeric
  //		 return -1 -> if form field is not Numeric
  
//ignore validation if tmp_int is blank.

if(tmp_int != "")
 {
   // searching whole string word by word
    if (tmp_int.search)
     {
        //checking the words in string.
        //  if string contains the non Nemeric value, return -1.
        //  else return 1.
               
	if (tmp_int.search((/[^\d]/)) != -1)
	{		
		return -1;
	}
     }
 }
 return 1;
}

//************** function for allowing only Numeric String *****************

function isEmailId(tmp_str)
{
  // Name      : isEmailId.  
  // Purpose   : allow user to enter value in email id format(xxx@kk.com).
  // Inputs    : tmp_str -> string for validation. 
  // Outputs   : return 1 -> if form field is as email id format.
  //		 return -1 -> if form field is not as email id format.


//ignore validation if tmp_str is blank.

if(tmp_str != "")
 {
    // searching whole string word by word  

      if (tmp_str.search) 
       {
         //checking the words in string.
         //  if given string is not in email id format(xxx@zzz.com), return -1.
         //  else return 1.
               
           if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(tmp_str)))
	    { 
	      return -1;
            }
       }
 } 
 return 1;
}


//*********** function validate user entered date in required format
function isDate(DateFormat,formField)
{
  
  	var result = true;
    	    	  
   		if(formField != "")
   		{
			var elems = formField.split("-");

			result = (elems.length == 3); // should be three components   		

			if(result)
			{
				var day = parseInt(elems[0],10);
				var month = parseInt(elems[1],10);    			
				var year = parseInt(elems[2],10);
				result = isNumeric(elems[0]) && (day > 0) && (day < 32) &&
					 isNumeric(elems[1]) && (month > 0) && (month < 13) &&  			
					 isNumeric(elems[2]) && ((elems[2].length == 2) || (elems[2].length == 4));
			}   		   		
			if (!result)
			{
				result = false;	
			}   	
  		}
	return result;
}
//************** function for validates the form feilds *****************

function validate_form(doc)
{
  // Name      : validate_form.
  
  // Purpose   : validate the form fields.
     
  // Inputs    : all form feilds values
                               
  // Outputs   : return true -> if user enter enter validate data
  //             else return false  		 
  //             alert box displays with error message.
  // Calls     : isBlank(tmp_str) -> checks the field is blank or containing
  //		                       only white speces.
  //             isAlphaNemeric(tmp_str) -> checks the feild is Alpha-Nemeric
  //		 isNumeric(tmp_int) -> checks the field is nemeric
  //		 isEmailId(tmp_str) -> checks the field is in email id
  //		                        format(xxx@yyy.com)
     
 
 
 //declearing veriables
 
 var str;                     // stores error messages.
 var blank_field;             // stores a string of form fields and error lable for 
                              // blank validation.
 var numeric_field;           // stores a string of form fields and error lable for 
                              // numeric[0-9] validation.
 var AlphaNumeric_field;      // stores a string of form fields and error lable for 
                              // Alpha-Numeric[A-Z]and[0-9] validation.
 var email_field;             // stores a string of form fields and error lable for 
                              // email[xxx@zz.com] validation.
 var date_field               // stores a string of form date fields and error lable for 
                              // date validation
 var str_blank_field;         // array in which value of blank_field stored by 
                              // comma separating.
 var str_numeric_field;       // array in which value of numeric_field stored by 
                              // comma separating.
 var str_alphanumeric_field;  // array in which value of AlphaNumeric_field stored by 
                              // comma separating.
 var str_email_field;         // array in which value of email_field stored by 
                              // comma separating.
 var str_date_field;          // array in which value of date_field stored by 
                              // comma separating.                              
                          

 
 //assiging form fields values to veriables

 str = "";
 blank_field = doc.js_Blank.value;     
 numeric_field = doc.js_Numeric.value;     
 AlphaNumeric_field = doc.js_AlphaNumeric.value;    
 email_field = doc.js_Email.value;    
 date_field = doc.js_Date.value;    


//calls function is_blank for blank validation, if blank_field is not null
               
     if(blank_field != "")
     {        
       
       //creats array in which values stores without comma
        str_blank_field = blank_field.split(",");           
        
        //loop for getting value from array for validating fields.
        for(a=0; a<str_blank_field.length; a++)
         {                   
           //getting value of form fields
           tmp_str = eval('document.'+doc.name+'.'+str_blank_field[a]+'.value'); 

             a=a+1;
           
           //getting error lables for messages
           tmp_err_message = str_blank_field[a];
                                 
                    
           //calls function for blank validation
          tmp_Blank_validate = isBlank(tmp_str);
        
          //if value of tmp_validate = "", sets the error message.
          if(tmp_Blank_validate == "")
           { 
              str += "Please enter " + tmp_err_message + "\n";
              
           }  
         }
     }

//calls function isNumeric for numeric validation, if numeric_field is not null
              
     if(numeric_field != "")
     {
       //creats array in which values stores without comma
       str_numeric_field = numeric_field.split(",");   
       
       //loop for getting value from array for validating fields.
       for(a=0; a<str_numeric_field.length; a++)
       {    
         //getting value of form fields
	 tmp_str = eval('document.'+doc.name+'.'+str_numeric_field[a]+'.value');
	 
	  a=a+1;	 
	 
	 //getting error lables for messages
	 tmp_err_message = str_numeric_field[a];  
	 


	   //calls function for Numeric(0-9) validation
	   tmp_validate = isNumeric(tmp_str);

	//if value of tmp_validate = -1 setting error message.
	if(tmp_validate == -1)
	   { 
	      str += "Please enter validate " + tmp_err_message + "\n";
	   }   
       }      
      }
      
  //calls function isAlphaNumeric for AlphaNumeric validation, if AlphaNumeric_field is not null
    
      if(AlphaNumeric_field != "")
      {
        
        //creats array in which values stores without comma
        str_alphanumeric_field = AlphaNumeric_field.split(",");   
      
         //loop for getting value from array for validating fields.
         for(a=0; a<str_alphanumeric_field.length; a++)
          {    
            
            //getting value of form field
            tmp_str = eval('document.'+doc.name+'.'+str_alphanumeric_field[a]+'.value'); 
            
            a=a+1;
           
            //getting error lables for messages
            tmp_err_message = str_alphanumeric_field[a];
                                   
           //calls function for Alpha Numeric(A-Z and 0-9)  validation
          
           tmp_validate = isAlphaNumeric(tmp_str);
         
          //if value of tmp_validate = -1 setting error message
           if(tmp_validate == -1)
            { 
               str += "Please enter validate " + tmp_err_message + "\n";
            }                   
          }
       }
      
  
 //calls function isEmailId for Email Id validation, if email_field is not null
           
      
     if(email_field != "")
     {
       //creats array in which values stores without comma
       str_email_field = email_field.split(",");        
       
       //loop for getting value from array for validating fields.
       for(a=0; a<str_email_field.length; a++)
        {    
         
          //getting value of form field
	  tmp_str = eval('document.'+doc.name+'.'+str_email_field[a]+'.value');	 
	  
	  a=a+1;
	  
	  //getting error lables for messages
	  tmp_err_message = str_email_field[a];  	  
	  
	  

	 //calls function for Email ID(xxx@yyy.com) validation 

	 tmp_validate = isEmailId(tmp_str);

	 //if value of tmp_validate = -1 setting error message
	  if(tmp_validate == -1)
	    { 
	       str += "Please enter correct " + tmp_err_message + " \n";
            }      
        }      
     }
     
      //calls function isDate for Date validation, if date_field is not null
                           
       if(date_field != "")
          {
            //creats array in which values stores without comma
            str_date_field = date_field.split(",");        
            
            //loop for getting value from array for validating fields.
            for(a=0; a<str_date_field.length; a++)
             {    
            
             //getting date format
             tmp_date_format = str_date_field[a];
             
             a=a+1;
             
            //getting value of form field
     	    tmp_str = eval('document.'+doc.name+'.'+str_date_field[a]+'.value');	 
     	  
     	    a=a+1;
     	  
     	  //getting error lables for messages
     	  tmp_err_message = str_date_field[a];  	  
     	  
     	  
     
     	 //calls function for Email ID(xxx@yyy.com) validation 
     
     	 tmp_validate = isDate(tmp_date_format,tmp_str);
     
     	 //if value of tmp_validate = -1 setting error message
     	  if(tmp_validate == false)
     	    { 
     	       str += "Please enter a date in the format "+tmp_date_format+" for the " + tmp_err_message + " \n";
            }      
        }      
     }              
     
// if str contains the error messages return false
// else return true

   if(str)
    {  
       alert(str);
       return false;
    }
   else
    {
       return true;
    }     
}