/************************************************************
* UserValidations.js - contains validations for standard user fields
* uses: c_EmailAddress.js
*************************************************************/
//used for localization ,we are declaring the  variable and set the alert message and that is localizing
//in  asp and aspx pages .by sunil -9Th Mar-2009
var js_Email='The field \"Email\" contains an invalid entry.';
var js_Email2='The field \"Email2\" contains an invalid entry.';
var js_Fax='Fax';
var js_DayPhone='Day Phone';
var js_InteractivePager='Interactive Pager';
var js_Pager='Pager';
var js_EveningPhone='Evening Phone';
var js_Field='The field \"';
var js_Field2='\" contains an invalid entry; first and last digit must be numeric.';
var js_Postalcode='The field \"Postal Code\" contains an invalid entry.';
var js_Invalidsymbol='"\" contains an invalid entry; symbols are not allowed except for:\ Hyphens.';
var js_ValidAlphaChar='"\" contains an invalid entry; Alpha characters are not allowed.';
var js_Isrequired='is required.';
var js_MobilePhone = 'Mobile Phone';

function ValidateUserDetails() {

    
    //email and SSN validation variables
    var charcodevalue;
    var strTemp;
    var lastChar;
    var isValidChar;
    var isValidFirstChar;
    var lastHyphen;                   
    
    //regular expressions
    var ssnRegex = new RegExp("[0-9]{3}\-[0-9]{2}\-[0-9]{4}");
    var noNumericsReverseRegex = new RegExp("((.)*([0-9]{1})(.)*)");
    var noAlphasReverseRegex = new RegExp("[a-zA-Z]");
    var disallowedSSNSymbolsRegex = new RegExp("((.)*([\~\!\$\%\^\@\_\&\#\{\}\|\\s\:\\\"\<\>\?\`\=\\(\\)\\[\\]\\+\;\'\/\\\\]{1})(.)*)");//Ravi added on november 8th
    var whitespace = new String(" \t\n\r");    
  
    //loop counter
    var loopCount;  
      
   //validate length does not exceed 50 characters   
   if (!isValidateLength("First Name", "standard0"))
        return false;    
    if (!isValidateLength("Last Name", "standard1"))
        return false;   
    if (!isValidateLength("Street 1", "standard2"))
        return false;   
    if (!isValidateLength("Street 2", "standard3"))
        return false;  
    if (!isValidateLength("City", "standard4"))
        return false;       
    if (!isValidateLength("State/Province", "standard5"))
        return false;               
    if (!isValidateLength("Postal Code", "standard6"))
        return false;    
    if (!isValidateLength("Country", "standard7"))
        return false;  
    if (!isValidateLength("Day Phone", "standard8"))
        return false;  
    if (!isValidateLength("Evening Phone", "standard9"))
        return false;  
    if (!isValidateLength("Pager", "standard10"))
        return false;                             
    if (!isValidateLength("Interactive Pager", "standard11"))
        return false;                                
    if (!isValidateLength("Mobile Phone", "standard12"))
        return false;  
    if (!isValidateLength("Fax", "standard13"))
        return false;     
    if (!isValidateLength("SSN", "standard16"))
        return false; 
    if (!isValidateLength("Nickname", "standard19"))
        return false; 
    if (!isValidateLength("Spouse Name", "standard20"))
        return false; 
    if (!isValidateLength("Company", "standard21"))
        return false; 
    if (!isValidateLength("Job Title", "standard22"))
        return false; 
    if (!isValidateLength("Department", "standard23"))
        return false; 
    if (!isValidateLength("Manager Name", "standard24"))
        return false; 
    if (!isValidateLength("Office", "standard25"))
        return false;     
    if (!isValidateLength("Profession", "standard26"))
        return false;     

    //validate phone numbers 
    // Localization by Sunil for the following strings : 9th March 09
    // The following commented lines can  be deleted once testing is over. We are keeping these lines
    // just for debug reference.
    //============================================================================================================== 
    //if (!validatePhone("Day Phone", "standard8"))//Commented: by sunil-18Th Feb 2009
    //if (!validatePhone("Evening Phone", "standard9"))//Commented: by sunil-18Th Feb 2009
    //if (!validatePhone("Pager", "standard10"))//Commented: by sunil-18Th Feb 2009
    //if (!validatePhone("Interactive Pager", "standard11"))//Commented: by sunil-18Th Feb 2009
    //if (!validatePhone("Mobile Phone", "standard12"))//Commented: by sunil-18Th Feb 2009
    //if (!validatePhone("Fax", "standard13")) //Commented: by sunil-18Th Feb 2009
    //============================================================================================================== 
    if (!validatePhone(js_DayPhone, "standard8"))//added localized code for Day Phone by sunil-18Th Feb 2009
      return false;    
    if (!validatePhone(js_EveningPhone, "standard9"))//added localized code for Evening Phone by sunil-18Th Feb 2009
      return false;   
     if (!validatePhone(js_Pager, "standard10"))//added localized code for Pager by sunil-18Th Feb 2009
      return false;   
    if (!validatePhone(js_InteractivePager, "standard11"))//added localized code for Interactive Pager by sunil-18Th Feb 2009
      return false;  
	if (!validatePhone(js_MobilePhone, "standard12"))
      return false;   
     if (!validatePhone(js_Fax, "standard13"))//added localized code for Fax  by sunil-18Th Feb 2009  
      return false;      
      
    //if the field exists
    if(document.getElementById("standard6") != null)
    { 
       isValidChar = false;
       strTemp=document.getElementById("standard6").value;             
       if (whitespace.indexOf(strTemp.charAt(strTemp.length-1)) != -1) 
       {
            loopCount = strTemp.length - 1;      
            while (loopCount >= 0 && whitespace.indexOf(strTemp.charAt(loopCount)) != -1) { loopCount--; }
            strTemp = strTemp.substring(0, loopCount+1);
        }
        
        for(loopCount=0;loopCount<strTemp.length;loopCount++) 
        {
            lastChar=strTemp.charCodeAt(loopCount);
            if ( ( lastChar>64 && lastChar<91 ) || ( lastChar>96 && lastChar<123 ) || ( lastChar == 32 ) || ( lastChar == 45 ) || (lastChar>47 && lastChar<58) ) 
                isValidChar=true;
            else
            {
                isValidChar=false;
                break;
            }            
        }//end for
        if (document.getElementById("standard6").value.replace(" ", "") != "" && isValidChar == false)
        {
            //alert("The field \"Postal Code\"  contains an invalid entry;");//Commentd by sunil-18Th Feb 2009
             alert(js_Postalcode);//Localized alert message: by sunil-18Th Feb 2009
            
            //exit now
            return false;
        }//end if
    }//end if
    
  
   //if the field exists
    if(document.getElementById("standard14") != null) 
    {
        if (document.getElementById("standard14").value.length > 0)
        {            
            try
            {
                var address = new EmailAddress(document.getElementById("standard14").value);
                address.validate();
            }
            catch (ex)
            {
                //alert("The field \"Email\" contains an invalid entry. " + ex);//Commentd by sunil-18Th Feb 2009
                 alert( js_Email + ex);//added localized variable js_Email from getpreinfo.asp page //Localized alert message: by sunil-18Th Feb 2009
               
                return false;
            }
        } 
    }//end if

    //if the field exists
    if(document.getElementById("standard15") != null)
    {
        if (document.getElementById("standard15").value.length > 0)
        {            
            try
            {
                var address = new EmailAddress(document.getElementById("standard15").value);
                address.validate();
            }
            catch (ex)
            {
                //alert("The field \"Email2\" contains an invalid entry. " + ex);//Commentd by sunil-18Th Feb 2009
                 alert(js_Email2 + ex);//Localized alert message: by sunil-18Th Feb 2009
                return false;
            }
        } 
    }

   
        //if the field exists
   /*Removing SSN validation for now as it is causing an issue with clients
    if(document.getElementById("standard16") != null)
    {
        if(document.getElementById("standard16").value != "" && !ssnRegex.test(document.getElementById("standard16").value))
        {
            //show an alert
            alert("The field \"SSN\" contains an invalid entry; only valid social security number formatting will be accepted (nnn-nn-nnnn).");
            
            //exit now
            return false;
        }//end if

    
        isValidChar = false       
        strTemp = new String(document.getElementById("standard16").value);      
        
        if (whitespace.indexOf(strTemp.charAt(strTemp.length-1)) != -1) 
        {
            loopCount = strTemp.length - 1;       
            while (loopCount >= 0 && whitespace.indexOf(strTemp.charAt(loopCount)) != -1) 
            { loopCount--; }
            strTemp = strTemp.substring(0, loopCount+1);
        }
        lastHyphen = strTemp.charCodeAt(strTemp.length-5);

        if(strTemp.length==11 && lastHyphen == 45 )
        {
            for (loopCount = strTemp.length - 4;loopCount< strTemp.length;loopCount++)
            {
                lastChar=strTemp.charCodeAt(loopCount)
                if(lastChar > 47 && lastChar < 58)
                    isValidChar=true;
                else
                {
                    isValidChar=false;
                    break;
                }//end if
            }//end for
        }//end if           
     
        if(document.getElementById("standard16").value != "" && !isValidChar)
        {
            alert("The field \"SSN\" contains an invalid entry; ");
                //exit now
            return false;
        }
 
        if(disallowedSSNSymbolsRegex.test(strTemp))
        {
            //show an alert
            alert("The field \"SSN\" contains an invalid entry; symbols are not allowed except for:\ Hyphens.");
            //exit now
            return false;
         }//end if    
    }
    */
    //if we reach this point
    return true;
}//end ()


/**********************************************************
* This function validates the entry is no longer then 50 characters.
**********************************************************/
function isValidateLength(fieldDisplayText, fieldID)
{
    //get a handle to the object
    field = document.getElementById(fieldID);
    
    var fieldLenth = 50;
    //if the field exists
    if (field != null)
    {
        if (fieldDisplayText == "First Name" || fieldDisplayText == "Last Name")
        {
            fieldLenth = 100;
        }
        if(field.value != "" && field.value.length > parseInt(fieldLenth))
        {
            //show an alert
            alert("The field \"" + fieldDisplayText + "\" should have an entry between 1 and " + fieldLenth + " characters long.");
            
            //exit now
            return false;
        }//end if
    }//end if
    
    //exit 
    return true;
}

/**********************************************************
* This function validates the phone is numeric and only contains valid sysmbols.
**********************************************************/
function validatePhone(fieldDisplayText, fieldID) 
{
    var charcodevalue;
    var strTemp;
    var lastChar;
    var isValidChar;
    var isValidFirstChar;
    var lastHyphen;   
    
    var disallowedPhoneSymbolsRegex = new RegExp("((.)*([\~\!\@\$\\s\%\+\\(\\)\,\#\^\&\_\{\}\|\:\\\"\<\>\?\`\=\\[\\]\;\'\/\\\\]{1})(.)*)");//Ravi added on november 8th
    var whitespace = new String(" \t\n\r");     
    var noAlphasReverseRegex = new RegExp("[a-zA-Z]");
      
    //get a handle to the object
    field = document.getElementById(fieldID);
    
    //if the field exists
    if(field != null)
    {  
        isValidChar = false     
        strTemp = new String(field.value);
        
        //if there are whitespace characters
        if (whitespace.indexOf(strTemp.charAt(strTemp.length-1)) != -1) 
        {
           //trim 
           loopCount = strTemp.length - 1;      
           while (loopCount >= 0 && whitespace.indexOf(strTemp.charAt(loopCount)) != -1) { loopCount--; }
           strTemp = strTemp.substring(0, loopCount+1);
        }

        //check last character is valid
        lastChar = strTemp.charCodeAt(strTemp.length-1);
        isValidFirstChar = false;
        if(lastChar > 47 && lastChar < 58) 
            isValidChar = true;
            
        //check first character is valid
        lastChar = strTemp.charCodeAt(0);
        if(lastChar > 47 && lastChar < 58) 
            isValidFirstChar = true;

        //if there is an invalid entry               
        if(field.value != "" && (isValidChar == false || isValidFirstChar == false))
        {
           //alert("The field \"" + fieldDisplayText + "\" contains an invalid entry; first and last digit must be numeric."); Commented : by sunil-18Th Feb 2009
           alert(js_Field + fieldDisplayText + js_Field2);//localized alert message  by sunil-18Th Feb 2009
            //exit now
           return false;
        }//end if
                     
        //if there is an invalid entry
        if(field.value != "" && disallowedPhoneSymbolsRegex.test(strTemp))
        {
            //show an alert
            //alert("The field \"" + fieldDisplayText + "\" contains an invalid entry; symbols are not allowed except for:\ Hyphens.");
            alert(js_Field + fieldDisplayText +js_Invalidsymbol);//localized alert message by sunil-18Th Feb 2009
            
            //exit now
            return false;
        }//end if
        
         //if there is an invalid entry
        if(noAlphasReverseRegex.test(field.value))
        {
            //show an alert
           //alert("The field \"" + fieldDisplayText + "\" contains an invalid entry; Alpha characters are not allowed.");
             alert(js_Field + fieldDisplayText + js_ValidAlphaChar);//localized alert message by sunil-18Th Feb 2009
           
            
            //exit now
            return false;
        }//end if          
    }//end if

    //exit
    return true;
}
			
function getCheckboxValues(arrayElements){
	for(var i=0; i<arrayElements.length; i++){
		if(arrayElements[i].checked){return true}
	}
	return false;
}
			
function ValidateRequiredFields(theform, requiredArray)
{
    var alertString='';
	for(var i=0; i<theform.length-1; i++){
		if(theform.elements[i].type=='checkbox'||theform.elements[i].type=='radio'){
			var arrayElements = document.getElementsByName(theform.elements[i].name)
			for(var j=0; j<arrayElements.length; j++){
				arrayElements[j].style.backgroundColor='';
			}
			for(var j=0; j<requiredArray.length; j++){
				if(theform.elements[i].name == requiredArray[j][0] && !getCheckboxValues(arrayElements)){
					//alertString+=requiredArray[j][1]+' is required.\n';//commented for localizing string"is required" by sunil 9Th Mar-2009
					alertString+=requiredArray[j][1]+' '+ js_Isrequired +'.\n';
					for(var k=0; k<arrayElements.length; k++){
						arrayElements[k].style.backgroundColor='#FF6666';
					}
				}
			}
			i+=arrayElements.length-1;
		}else{
            theform.elements[i].style.backgroundColor='#FFFFFF';
			for (var j = 0; j < requiredArray.length; j++) {
			    if (theform.elements[i].name == requiredArray[j][0]) {
			        if ((requiredArray[j][0] != 'photoID' && theform.elements[i].value == '') || (requiredArray[j][0] == 'photoID' && theform.elements[i].value == '0')) {
				        //alertString+=requiredArray[j][1]+' is required.\n';//commented for localizing string"is required"  by sunil 9Th Mar-2009
				        alertString += requiredArray[j][1] + ' ' + js_Isrequired + '.\n';
				        theform.elements[i].style.backgroundColor = '#FF6666';
				    }
				}
			}
		}
	}
    if(alertString==''){return true}else{alert(alertString);return false}		
}

function ValidateMinimumLength(theform, minLenArray)
{
   var alertString='';
    var strInvalidMinLenght = '';
	if(alertString==''){
	    for(var i=0; i<theform.length-1; i++){
            for(var j=0; j<minLenArray.length; j++){	    	        
                if(theform.elements[i].name == minLenArray[j][0] && theform.elements[i].value.length > 0 && theform.elements[i].value.length < parseInt(minLenArray[j][1])){                  
                    if (strInvalidMinLenght == ''){
                        strInvalidMinLenght = minLenArray[j][2] + ' (' + minLenArray[j][1] + ' Characters)\n';
                    }
                    else {
                        strInvalidMinLenght = strInvalidMinLenght + minLenArray[j][2] + ' (' + minLenArray[j][1] + ' Characters)\n';
                    }
                    theform.elements[i].style.backgroundColor='#FF6666';
                }
            }				    				    	                            
        } 
        if (strInvalidMinLenght != ''){
            alertString = 'Minimum field requirement not met:\n' + strInvalidMinLenght;
        }
	}
	
	if(alertString==''){return true}else{alert(alertString);return false}
}