/* 
    This is the source code for the validation function. 
    Add the following code just after the </HEAD> in the files where the 
    generalised validation functionality is required. 
    <SCRIPT language="JavaScript1.2" src="validation.js"></SCRIPT> 
*/ 

    /* 
    *   File : validation.js 

    */ 
//---------------------------------EMail Check ------------------------------------ 

/*  checks the validity of an email address entered 
*   returns true or false 
*   
*/ 

function validateEmail(email)
{
// a very simple email validation checking. 
// you can add more complex email checking if it helps 
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}


/*****isFloat************************/
function isFloat(inputValue)
{
	var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	
	if (reFloat.test(inputValue) == false)
	{
		return false;
	}
	else
	{
		return true;
	}	
	
}
/*********************************************************************/
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function isGreaterThanTodayDate(strDate)
{	
	var firstIndex = 0 + strDate.indexOf ('-');	
	var lastIndex = 0 + strDate.lastIndexOf ('-');	

	var date1 = strDate.substring (0, firstIndex);
	var month1 = strDate.substring (firstIndex+1, lastIndex);
	var year1 = strDate.substring (lastIndex+1, strDate.length);

	var currentDate = new Date();
	
	var enteredDate = new Date(year1,month1-1,date1); // sub one from month because in javascript months are indexed as 0-11	
	
	if (enteredDate.getTime() > currentDate.getTime())
	{	
		return true;
	}
	else
	{
		return false;
	}
}
function isLessThanEqualTodayDate(strDate)
{	
	var firstIndex = 0 + strDate.indexOf ('-');	
	var lastIndex = 0 + strDate.lastIndexOf ('-');	

	var date1 = strDate.substring (0, firstIndex);
	var month1 = strDate.substring (firstIndex+1, lastIndex);
	var year1 = strDate.substring (lastIndex+1, strDate.length);

	var currentDate = new Date();
	
	var enteredDate = new Date(year1,month1-1,date1); // sub one from month because in javascript months are indexed as 0-11	
	
	if (enteredDate.getTime() <= currentDate.getTime())
	{	
		return true;
	}
	else
	{
		return false;
	}
}


/*************************************************************************/

/* function validateData 
*  Checks each field in a form 
*  Called from validateForm function 
*/ 
function validateData(strValidateStr,objValue,strError) 
{ 
 var epos = strValidateStr.search("="); 
    var  command  = ""; 
    var  cmdvalue = ""; 
    if(epos >= 0) 
    { 
     command  = strValidateStr.substring(0,epos); 
     cmdvalue = strValidateStr.substr(epos+1); 
    } 
    else 
    { 
     command = strValidateStr; 
    } 

    switch(command) 
    { 
       case "req": 
        case "required": 
         { 
           if(eval(objValue.value.length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.id + " : Required Field"; 
              }//if 
              alert(strError); 
              return false; 
           }//if 
           break;             
         }//case required 
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.id + " : "+cmdvalue+" characters maximum "; 
               }//if 
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.id + " : " + cmdvalue + " characters minimum  "; 
               }//if               
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
               return false;                 
             }//if 
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alpha-numeric characters allowed "; 
                }//if 
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
		   case "applicantName": 
           { 
              var charpos = objValue.value.search("[^A-Za-z\/.,()&_ -]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only 'a-z,A-Z , . ( ) & _ - space' are allowed"; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//applicantName
		    case "industryName": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9\/.,()&_ -]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only 'a-z,A-Z,0-9 , . ( ) & _ - space' allowed"; 
                }//if 
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//case industryName  
		case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only digits allowed "; 
                }//if               
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break;               
           }//numeric 
        case "float": 
          { 
		   if(objValue.value) 
		   { 
               if(!isFloat(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.id+": Please enter a valid integer/decimal value"; 
                 }//if                                               
                 alert(strError); 
                 return false; 
               }//if 
		   }//if(objValue.value) 
           break; 
          }//case float 		   		   
        case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alpha 		   
        case "alphaspace": 
           { 
              var charpos = objValue.value.search("[^A-Za-z ]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alphaspace		   
        case "alphaspacecomma": 
           { 
              var charpos = objValue.value.search("[^A-Za-z ,]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alphaspacecomma		   		   
        case "alnumspacecomma": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9 ,]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alnumspacecomma			   		   
        case "phone": 
           { 
              var charpos = objValue.value.search("[^0-9 -]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//phone		    		   
        case "address": 
           { 
              var charpos = objValue.value.search("[^\n/A-Za-z0-9,-.()_?=\/% \\s]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Special Characters Are Not Allowed "; 
                }//if
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//address		
        case "alnumspace": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9 ]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.id+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                return false; 
              }//if 
              break; 
           }//alnumspace				
		 case "email": 
          { 
				if(objValue.value) 
				{ 
               if(!validateEmail(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.id+": Enter a valid Email address "; 
                 }//if                                               
                 alert(strError); 
                 return false; 
               }//if 
				}//if 
           break; 
          }//case email 
        case "lt": 
        case "lessthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.id+": Should be a number "); 
              return false; 
            }//if 
            if(eval(objValue.value) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.id + " : value should be less than "+ cmdvalue; 
              }//if               
              alert(strError); 
              return false;                 
             }//if             
            break; 
         }//case lessthan 
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.id+": Should be a number "); 
              return false; 
            }//if 
             if(eval(objValue.value) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.id + " : value should be greater than "+ cmdvalue; 
               }//if               
               alert(strError); 
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
        case "regexp":   // ["regexp=[A-Za-z0-9/,-. \n]$","Please Ensure No Special Characters Are Used In Address"]
         { 
            if(!objValue.value.match(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.id+": Invalid characters found "; 
              }//if                                                               
              alert(strError); 
              return false;                   
            }//if 
           break; 
         }//case regexp 
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item"); 
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.id+": Please Select one option "; 
              }//if                                                               
              alert(strError); 
              return false;                                   
             } 
             break; 
         }//case dontselect 
	   case "date": 
         { 
            if(!isDate(objValue.value)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.id+": Please enter date in format mm/dd/yyyy"; 
              }//if                                                               
              alert(strError); 
              return false;                   
            }//if 
           break; 
         }//case date 		 
        case "greaterthantodaydate": 
         { 
            if(!isGreaterThanTodayDate(objValue.value)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.id+": Date must be greater than todays date"; 
              }//if                                                               
              alert(strError); 
              return false;                   
            }//if 
           break; 
         }//case greaterthantodaydate 
		 case "lessthanequaltodaydate": 
         { 
            if(!isLessThanEqualTodayDate(objValue.value)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.id+": Date must be less than or equal to today's date"; 
              }//if                                                               
              alert(strError); 
              return false;                   
            }//if 
           break; 
         }
		 

    }//switch 
    return true; 
} 

/* 
* function validateForm 
* the function that can be used to validate any form 
* returns false if the validation fails; true if success 
* arguments : 
*   objFrm     : the form object 
*   arrObjDesc : an array of objects describing the validations to conduct on each 
*        input item. 
*          The array should consist of one object per input item in the order the input 
*          elements are present in the form. Each object consist of zero or more validation 
*          objects. Each of these validation object is a pair consisting of the validation 
*          descriptor string and an optional Error message. 
*/ 

function validateForm(objFrm,arrObjDesc) 
{ 
 for(var itrobj=0; itrobj < arrObjDesc.length; itrobj++) 
 { 
   if(objFrm.elements.length <= itrobj) 
   { 
        alert("BUG: Obj descriptor for a non existent form element"); 
        return false; 
   }//if 
   for(var itrdesc=0; itrdesc < arrObjDesc[itrobj].length ;itrdesc++) 
   { 
      if(validateData(arrObjDesc[itrobj][itrdesc][0], 
                 objFrm[itrobj],arrObjDesc[itrobj][itrdesc][1]) == false) 
       { 
	     objFrm[itrobj].focus();
         return false; 
       }//if 
   }//for 
 }//for 
} 

/***Call on submission of HTML form LabActivity Relation Association **/



function ValidateForm(objForm)
{
	var arrValidationDesc = new Array();

		arrValidationDesc1 = 
								[
									[  // Name
										["required"],									
										["address"]
									],	
									[  // Company
										["required"],
										["address"]						
									],	
									[  // Street Address
										["required"],
										["address"]
									],		
									[  // City
										["required"],
										["address"]
									],		
									[  // State
										["required"],
										["address"]
									],
									[  // Zip		
										["required"],
										["numeric"]
									],		
									[  //Phone
										["required"],
										["phone"]
									],		
									[  //Email Address	
										["required"],
										["email"]
									],	

								];
		
		
	var objInputDgSetNumber = document.getElementById("attachemntTempListRowNum");
	var storedDgSetNumber = objInputDgSetNumber.value - 0;

	// Now for every DgSet added, the below validations will be looped for each one
	var DgSetIndex = 1;
	var arrValidationDgSet = new Array();

	for (DgSetIndex = 1;DgSetIndex <= storedDgSetNumber;++DgSetIndex)
	{
			var arrValidationDesc2 = new Array();
				 arrValidationDesc2 =
										[

										];
										
		arrValidationDgSet = arrValidationDgSet.concat(arrValidationDesc2);
	}//for (DgSetIndex = 1;DgSetIndex <= storedDgSetNumber;++DgSetIndex)
		
		arrValidationDesc3 = 
								[
							
									[  //Comments:
									   ["required"],
									   [""]
									]
								];		
	arrValidationDesc = arrValidationDesc.concat(arrValidationDesc1,arrValidationDgSet,arrValidationDesc3);
	
	var y = validateForm(objForm,arrValidationDesc);
	
	if(y==false)
		return y;
				
	if(y!=false)
	{
			if(confirm("Are you sure, you want to submit this form?")) 
		  {
			return true;
		  }   
		  else
		  {    
			return false;
		  }
	} else {
		return false;
	}
	
}


// ************************************************************ end  :  userChangePassword **************************************** //

