/***************Regular Expression****************/
stringtest=/^[a-zA-Z. ]{1,}$/;
emptytest = /^[ ]{1,}$/;
alphatest = /^[a-zA-Z0-9_]{1,}$/;
floattest = /^[0-9.+-]{1,}$/;
inttest = /^[0-9+-]{1,}$/;
currencytest = /^[0-9.,+-]{1,}$/;

/***************Regular Expression****************/

/******************************************************************************************************/
/*PURPOSE: 
/*		This function splits date into date, month and year part and returns array
/*PARAMETERS:
/*		objTextBox: textbox cotaining date whose value needs to be split
/*		dtSep: seperator of date value
/******************************************************************************************************/
function SplitDate(objTextBox, dtSep)
{
	//Get the current date
	var dtCurrentDate = new Date();
	var year,month,date;
	
	//Get the input date valued
	var myString = new String(objTextBox.value);
	
	//Get the year, month and date
	var splitString = myString.split(dtSep);
	
	//returns the parts of date
	return splitString;
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function returns the date part of the date entered
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/******************************************************************************************************/
function GetDate(objTextBox, dtFormat, dtSep)
{
	//split the date into date, month and year part
	var splitString = SplitDate(objTextBox, dtSep);
	
	//return date part according to format
	switch(dtFormat)
	{
		case "DMY": 
			return parseInt(splitString[0]);
			break;
		case "MDY":
			return parseInt(splitString[1]);
			break;
		case "YMD":
			return parseInt(splitString[2]);
			break;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function returns the month part of the date entered
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/******************************************************************************************************/
function GetMonth(objTextBox, dtFormat, dtSep)
{
	//split the date into date, month and year part
	var splitString = SplitDate(objTextBox, dtSep);
	
	//return month part according to format
	switch(dtFormat)
	{
		case "DMY": 
			return parseInt(splitString[1]-1);
			break;
		case "MDY":
			return parseInt(splitString[0]-1);
			break;
		case "YMD":
			return parseInt(splitString[1]-1) ;
			break;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function returns the month part of the date entered
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/******************************************************************************************************/
function GetYear(objTextBox, dtFormat, dtSep)
{
	//split the date into date, month and year part
	var splitString = SplitDate(objTextBox, dtSep);
	
	//return year part according to format
	switch(dtFormat)
	{
		case "DMY": 
			return parseInt(splitString[2]);
			break;
		case "MDY":
			return date=parseInt(splitString[2]);
			break;
		case "YMD":
			return parseInt(splitString[0]);
			break;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered date is future date or not
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/******************************************************************************************************/
function IsFutureDate(objTextBox, dtFormat, dtSep)
{
	//Get the current date
	var dtCurrentDate = new Date();
	var year,month,date;
	
	date = GetDate(objTextBox, dtFormat, dtSep);
	month = GetMonth(objTextBox, dtFormat, dtSep);
	year = GetYear(objTextBox, dtFormat, dtSep);	

	//Create the date input by user
	var dtInputDate = new Date(year, month, date);
	
	//create the current date
	var dtCurrentDate = new Date(dtCurrentDate.getFullYear(),dtCurrentDate.getMonth(),dtCurrentDate.getDate());
	
	//Check whether date is future or not
	var intDifference = dtCurrentDate-dtInputDate;	
    
    
	//If Difference is greater than zero then its future date else it valid past date		
	if(intDifference<0)
	{
		return true;
	}
	else
	{					
		return false;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether birthdate is valid or not
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/******************************************************************************************************/
function IsDOB(objTextBox, dtFormat, dtSep)
{
    var strMsg;
    strMsg = null;
	//Check whether valid date has been entered or not
	if(chkDateValid(objTextBox, dtFormat, dtSep))
	{
		//Check entered date should not be future date
		if (IsFutureDate(objTextBox, dtFormat, dtSep))
		{
			strMsg = 'Date of birth can not be future date.';
			objTextBox.focus();
			objTextBox.select();	
			return strMsg;
		}
	}
	else
	{
	    strMsg = 'Please enter valid date of birth.';
		return strMsg;
	}
	return true;
}


/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether Passport will expired in the given period or not
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/*		intPeriod: integer value specifying period for which expiry date needs to be checked
/******************************************************************************************************/
function IsPassportExpired(objTextBox, dtFormat, dtSep, intPeriod)
{
    var strMsg;
	//Check whether valid date has been entered or not
	if(chkDateValid(objTextBox, dtFormat, dtSep))
	{
		//Check entered date should be future date
		if (!IsFutureDate(objTextBox, dtFormat, dtSep))
		{
		    strMsg = "Your passport has already expired.";
			objTextBox.focus();
			objTextBox.select();			
			return strMsg;  
		}
		else	//If entered date is future then check for period
		{
			//Today's date
			var dtTodayDate = new Date();
			//Passport Expiry Date
			var dtExpiryDate =new Date(GetYear(objTextBox, dtFormat, dtSep), GetMonth(objTextBox, dtFormat, dtSep), GetDate(objTextBox, dtFormat, dtSep), 00, 00, 00)    
			//Find the days remaining in expiry of passport from today
			var intRemainingDays = parseInt((dtExpiryDate-dtTodayDate)/86400000);			
			//Check whether remaining days is less than allowed period or not
			if(intRemainingDays < intPeriod)
			{
			    strMsg = "Passport expiry date must be more than " + intPeriod + " days."
				objTextBox.focus();
				objTextBox.select();
				return strMsg;     				
			}
		}
	}
	else
	{
	    strMsg = "Please enter valid passport expiry date";
	    return strMsg;	    
	}
	return true;	
}


/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is numeric or not
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function on onkeypress event of textbox.
/*		For example, onkeypress="javascript:return AcceptDecimal();"
/******************************************************************************************************/
function AcceptDecimal(e)
{
	e = e || window.event;	
	if ((e.keyCode >= 48 && e.keyCode <= 57) || (e.keyCode == 46))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is numeric or not
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function on onkeypress event of textbox.
/*		For just allowing numbers, write as follows: onkeypress="javascript:return AcceptNumeric();"
/*		For just allowing numbers and some special chars pass ASCII value comma seperated, 
/*      For e.g., to allow '-' pass 45 which ASCII value of '-', onkeypress="javascript:return AcceptNumeric('45');"
/******************************************************************************************************/
function AcceptNumeric(pstrSpChars)
{
	var e = this.window.event;
	
	if ((e.keyCode >= 48 && e.keyCode <= 57) || (pstrSpChars != null && pstrSpChars.indexOf(e.keyCode) >= 0))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is alphabets or not
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function on onkeypress event of textbox.
/*		For example, onkeypress="javascript:return AcceptAlphabets();"
/*		For just allowing numbers and some special chars pass ASCII value comma seperated, 
/*      For e.g., to allow '-' pass 45 which ASCII value of '-', onkeypress="javascript:return AcceptAlphabets('45');"
/******************************************************************************************************/
function AcceptAlphabets(pstrSpChars)
{	    
	var e = this.window.event;

	//For allowing a-z (97-122), A-Z (65-90)
	if ((e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 97 && e.keyCode <= 122) || (pstrSpChars != null && pstrSpChars.indexOf(e.keyCode) >= 0))
	{
		return true;
	}	
	else
	{
		return false;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is alphanumerics or not
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function on onkeypress event of textbox.
/*		For example, onkeypress="javascript:return AcceptAlphaNumerics();"
/*		For just allowing numbers and some special chars pass ASCII value comma seperated, 
/*      For e.g., to allow '-' pass 45 which ASCII value of '-', onkeypress="javascript:return AcceptAlphaNumerics('45');"
/******************************************************************************************************/
function AcceptAlphaNumeric(pstrSpChars)
{	
	if(AcceptAlphabets(pstrSpChars) || AcceptNumeric(pstrSpChars))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is alphanumerics or not (with spaces allowed)
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function on onkeypress event of textbox.
/*		For example, onkeypress="javascript:return AcceptAlphaNumericWithSpace();"
/*		For just allowing numbers and some special chars pass ASCII value comma seperated, 
/*      For e.g., to allow '-' pass 45 which ASCII value of '-', onkeypress="javascript:return AcceptAlphaNumericWithSpace('45');"
/******************************************************************************************************/
function AcceptAlphaNumericWithSpace(pstrSpChars)
{
	if(AcceptAlphabetsWithSpace(pstrSpChars) || AcceptNumeric(pstrSpChars))
	{
		return true;
	}
	else
	{
		return false;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is alphabets only (with spaces allowed) or not 
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function on onkeypress event of textbox.
/*		For example, onkeypress="javascript:return AcceptAlphaNumericWithSpace();"
/*		For just allowing numbers and some special chars pass ASCII value comma seperated, 
/*      For e.g., to allow '-' pass 45 which ASCII value of '-', onkeypress="javascript:return AcceptAlphaNumericWithSpace('45');"
/*      For '.' pass value 46, For '/' pass value 47
/******************************************************************************************************/
function AcceptAlphabetsWithSpace(pstrSpChars)
{	
	var e = this.window.event;
	
	//For allowing a-z (97-122), A-Z (65-90) and Space (32)
	if ((e.keyCode >= 65 && e.keyCode <= 90)|| (e.keyCode == 32 ) || (e.keyCode >= 97 && e.keyCode <= 122) || (pstrSpChars != null && pstrSpChars.indexOf(e.keyCode) >= 0))
	{
		return true;
	}	
	else
	{
		return false;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether textbox is empty
/*PARAMETERS:
/*		None
/*HOW TO CALL;
/*		call this function in validate function of form.
/******************************************************************************************************/
function IsEmpty(objTextBox)
{
	var strText;
	strText = objTextBox.value.toString();

	if (strText.length == 0)
	{
		return true;
	}
	else
	{
		for (var i = 0; i < strText.length; i++)
		{
			var ch = strText.substring(i, i + 1);
			if(ch != " ")
			{
				return false;
			}
		}
	}
	return true;
}
/******************************************************************************************************/
/*PURPOSE: 
/*		This function Removes zero padding from the string
/*PARAMETERS:
/*		objTextBox: textbox cotaining time string 
/*		time Format: format of date ('HHMM')
/*HOW TO CALL;
/*		call this function in validate function of form.
/******************************************************************************************************/

function RemovePadding(strText)
{
	var strRet;
    strRet = "";
	for (var i = 0; i < strText.length; i++)
	{
		var ch = strText.substring(i, i + 1);
		if(ch != "0")
		{ 
		for (var j=i;j<strText.length;j++)
		{
		var ch= strText.substring(j,j+1);
		strRet += ch;
		}	
		return strRet;
		}
	}
	return strRet;
}
/******************************************************************************************************/
/*PURPOSE: 
/*		This function fills particular textbox with today's date in given format and seperator
/*PARAMETERS:
/*		objTextBox: textbox cotaining date string 
/*		dtFormat: format of date ('DMY', 'MDY', 'YMD')
/*		dtSep: seperator of date value
/*HOW TO CALL;
/*		call this function in validate function of form.
/******************************************************************************************************/
function FillDefaultDate(objTextBox, dtFormat, dtSep)
{
	var year, month, day;	
	var dtTodayDate = new Date();
	year = dtTodayDate.getFullYear();
	month = dtTodayDate.GetMonth()+1;
	day = dtTodayDate.getDay();	
	switch(dtFormat)
	{
		case 'DMY':
			objTextBox.value = day + dtSep +  month + dtSep + year;
			break;
		case 'MDY':
			objTextBox.value = month + dtSep +  day + dtSep + year;
			break;
		case 'YMD':
			objTextBox.value = year + dtSep +  month + dtSep + day;
			break;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks length of value entered in textbox
/*PARAMETERS:
/*		objTextBox: textbox to validate for max length 
/*		intMaxChar: maximum length allowed
/*HOW TO CALL;
/*		call this function on keypress event of textbox.
/******************************************************************************************************/
function CheckLength(objTextBox, intMaxChar)
{
	if(event.type == 'keypress')
	{
		if(objTextBox.value.length > intMaxChar)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function checks whether entered value is email adress or not
/*PARAMETERS:
/*		objTextBox: textbox to validate 
/*HOW TO CALL;
/*		call this function on onblur event of textbox.
/******************************************************************************************************/
function IsEmailAddress(objTextBox, AllowEmpty)
{
	var result;
	var theStr;	
	var filter  = /^([a-zA-Z0-9]+)([\._-]?[a-zA-Z0-9]+)*(@)([a-z0-9])((([-]+)?([a-z0-9]+))?)*(([.]{1}[a-z]{1,})?([.]{1}[a-z]{2,}))$/; 
 
	if(!IsEmpty(objTextBox))
	{
		result  = false;
		theStr  = new String(objTextBox.value);
	
		if (filter.test(theStr))
		{
			result = true;
		}
		return result;  
	}
	else
	{
	    if(AllowEmpty == 'Y')
	    {
	        return true;
        }
	    else
	    {
	        return false;
        }
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function converts text box value to upper case
/*PARAMETERS:
/*		objTextBox: textbox to validate 
/*HOW TO CALL;
/*		call this function on onblur event of textbox.
/******************************************************************************************************/
function ConvertToUpperCase(obj)
{
	if (!IsEmpty(obj))
	{
	var strText = new String(obj.value);
	
	strText = strText.replace("'", "");
	strText = strText.replace("\"", "");
		
	var newstrText = new String();
	newstrText = strText.toUpperCase();
	obj.value = newstrText;
	return;
	}
}

/******************************************************************************************************/
/*PURPOSE: 
/*		This function Checks Strong Password and returns True or False with messages
/*PARAMETERS:
/*		String to be checked 
/*HOW TO CALL;
/*		call this function on submit.
/******************************************************************************************************/
function validatePwd(fieldname) 
{
 
//Initialise variables
var errorMsg = "";
var space  = " ";
//fieldname   = document.myform.password; 
fieldvalue  = fieldname.value; 
fieldlength = fieldvalue.length; 
 
 //It must be at least 8 characters long.
if (!(fieldlength >= 8)) {
     errorMsg += "\nMinimum 8 characters long.\n";
}

//It must not contain a space
if (fieldvalue.indexOf(space) > -1) {
     errorMsg += "\nPasswords cannot include a space.\n";
}
else
{
 
    //It must contain at least one number character
    if (!(fieldvalue.match(/\d/))) {
         errorMsg += "\nAt least one number.\n";
    }
    //It must start with at least one letter     
    if (!(fieldvalue.match(/^[a-zA-Z]+/))) {
         errorMsg += "\nAt least one letter.\n";
    }
    //It must contain at least one upper case character     
    if (!(fieldvalue.match(/[A-Z]/))) {
         errorMsg += "\nAt least one uppercase letter.\n";
    }
    //It must contain at least one lower case character
    if (!(fieldvalue.match(/[a-z]/))) {
         errorMsg += "\nMust include one or more lowercase letters.\n";
    }
    //It must contain at least one special character
    if (!(fieldvalue.match(/\W+/))) {
         errorMsg += "\nAt least one special character - #,@,%,!\n";
    }
    
   }

//If there is aproblem with the form then display an error
     if (errorMsg != ""){
            //alert (errorMsg);
          msg = "Not a strong password.Password should be alphanumeric, should have first letter as alphabet, should contain atleast one special character,atleast one uppercase letter,atleast one lowercase letter, should not contain space and should be minimum 8 characters long.";
          //msg += "Please enter following characters to make a strong password:\n";
          errorMsg = alert(msg);
          
          fieldname.focus();
          return false;
     }
     
     return true;
}

