/*
1. Removing spaces form both sides.
	checkTrim(txtString)
2. Validation for field which should not be empty
	isEmpty(fieldname,fieldvalue)
3. Validation for field which can contain only alphanumeric value
	hasOnlyAlphaNumeric(fieldname , fieldvalue)
4. Validation for field which cannot contain Space inbetween.
	isSpace(fieldname , fieldvalue)
5. Validation for field which cannot start with number
	isStartsWithNumber(fieldname , fieldvalue)
6. Validation for field which can allow only alphabets
	hasOnlyAlphabets(fieldname ,fieldvalue)
7. Validation for field which allows only numbers
	 hasOnlyNumeric(fieldname , fieldvalue)
8.  Validation for length of the field
	isTooLong(fieldName,checkStr,length)
9.  Check for Valid Email.
	emailCheck (emailStr) 
10.Validation for two field to be same 			
	isDuplicate(firstValue,SecondValue)
11.	returns true if it is a valid phone Number
	 isValidPhoneNO(fieldname , fieldvalue)
12.	returns true if it is a valid Float Value 
	 isFloat(fieldname , fieldvalue)
13.returns true if it is a valid Date
	validateDate(startDate,EndDate) 
14.Validation for special characters like < and >.
	isValid(fieldname , fieldvalue)

*/ 
function checkTrim(txtString)
{
	txtString = LTrim(txtString);
	txtString = RTrim(txtString);
	return txtString;
}

//returns the string after deleting  the trailing spaces
function LTrim(txtString) 
{
	ctr = 0;
	while( ctr < txtString.length && (txtString.substring(ctr,ctr+1) == " "))
	{
		ctr=ctr+1;
	}
	return txtString.substring(ctr);
}
// returns the string after deleting the leading spaces
function RTrim(txtString) 
{
	ctr = txtString.length;
	while( ctr > 0  && (txtString.substring(ctr,ctr-1) == " "))
	{
		ctr = ctr - 1;
	}
	return txtString.substring(0,ctr);
}
//Validation for field which should not be empty
function isEmpty(fieldname,fieldvalue)
{

	var str=checkTrim(fieldvalue)
	if(str.length==0)
	{
		alert(fieldname + ' cannot be blank ');
		return true;
	}
	return false;
}

//Validation for field which can contain only alphanumeric value
function hasOnlyAlphaNumeric(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) <= ' ')))
		{
			alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z) and (1 to 9) ');
			return false;
		}
		i++;
	}
	return true;
}
function hasValidCharacter(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) == ' ')||(str.charAt(i) == '-')))
		{
			alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z),(" "),(-) and (1 to 9) ');
			return false;
		}
		i++;
	}
	return true;
}
function hasValidRemarks(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )||(str.charAt(i) == ' ')||(str.charAt(i) == '-')||(str.charAt(i) == '(')||(str.charAt(i) == ')')))
		{
			alert(fieldname+' contains only alphanumeric values \n\nValid Characters :(A to Z),(a to z),(" "),(-),("("),(")") and (1 to 9) ');
			return false;
		}
		i++;
	}
	return true;
}
//Validation for field which cannot contain Space inbetween.
function isSpace(fieldname , fieldvalue)
{
	var str=fieldvalue
	if((str).indexOf(" ")!=-1)
	{
		alert(' Space cannot allow in  '+fieldname);
		return false;
	}
	return true;
}
//Validation for field which cannot start with number
function isStartsWithNumber(fieldname , fieldvalue)
{
	var numbers = "0123456789";
	startsWithNumber=false;
	var str = checkTrim(fieldvalue)
	for(i=0;i<numbers.length;i++)
	{	
	   if(str.charAt(0)==numbers.charAt(i))
	   {
	   alert(fieldname+' cannot start with number');
       return false;
	   }
	}
	return true;
}
//Validation for field which can allow only alphabets
function hasOnlyAlphabets(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))))
		{
			alert(fieldname+' can contain only alphabets\n\nValid Characters :(A to Z),(a to z) ');
			return false;
		}
		i++;
	}
	return true;
}
//Validation for field which allows only numbers
function hasOnlyNumeric(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9")))
		{
			alert(fieldname+' can contain only numeric value');
			return false;
		}
		i++;
	}
	return true;
}


	function hasOnlyNumericAndSpecificChar(fieldname , fieldvalue) {
		var str = fieldvalue;
		i = 0;
		while(i < str.length) {
			if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9") || (str.charAt(i) == "-") || (str.charAt(i) == " ") || (str.charAt(i) == ",") ) ) {
				alert(fieldname+' can contain only numeric value,whitespace and hyphen');
				return false;
			}
			i++;
		}
		return true;
	}

	function hasOnlyNumericAndComma(fieldname , fieldvalue) {
		var str = fieldvalue;
		i = 0;
		while(i < str.length) {
			if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9") || (str.charAt(i) == ",") ) ) {
				alert(fieldname+' can contain only numeric value and comma');
				return false;
			}
			i++;
		}
		return true;
	}

	
	function hasOnlyAlphabetsAndSpecificChar(fieldname , fieldvalue) {
		var str = fieldvalue;
		i = 0;
		while(i < str.length) {
			if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') || (str.charAt(i) == " ") || (str.charAt(i) == "-") || (str.charAt(i) == "_") || (str.charAt(i) == ",") || (str.charAt(i) == ".") || (str.charAt(i) == "'") || (str.charAt(i) >= "0") && (str.charAt(i) <= "9")))) {
				alert(fieldname+' can contain only alphabets\n\nValid Characters :(A to Z),(a to z),whitespace and hyphen ');
				return false;
			}
			i++;
		}
		return true;
	}

	function hasOnlySpecificChar(fieldObj, validChars, alertMsg) {
		var str = fieldObj.value;
		i = 0;
		
		while(i < str.length) {
			n = 0;
			while (n < validChars.length) {
				if(str.charAt(i) == validChars.charAt(n)) {
					break;
				}
				n++;				
			}
			if (n == validChars.length) {
				alert(alertMsg);
				return false;
			}
			i++;
		}
		return true;
	}


//Validation for field which allows only numbers
// Updated by Neha
function isFloat(fieldname , fieldvalue)
{
	var str = fieldvalue;
	var str1;
	i = 0;
	j=0;
	if(str.charAt(0)==".")
	{
		alert(fieldname+' is not valid\n\n e.g 57.55');
		return false;
	}
	while(i < str.length)
	{
		if((!((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))) && (str.charAt(i) != "."))
		{
			alert(fieldname+' is not valid\n\n e.g 57.55');
			return false;
		}
		if(str.charAt(i) == ".")
			j++;
		i++;
	}
	if(j>1)
	{
		alert(fieldname+' is not valid\n\n e.g 57.55');
		return false;
	}
	if(str.indexOf('.')>=0)
	{
		str1=str.substring(str.indexOf('.'),str.length-1);
		if(str1.length>2)
		{
			alert(fieldname+' is not valid\n\nOnly 2 digits allowed after the decimal');
			return false;
		}
	}
	return true;
}


//Validation for length of the field
function isTooLong(fieldName,checkStr,length)
{
	checkStr = checkTrim(checkStr);
	if((checkStr.length)>length)
	{
		alert (fieldName+' cannot exceed ' + length + ' character');
		return false; // false if the length exceeds
	}
	else 
		return true;  // else true
}


//Check for Valid Email.
function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
   
var matchArray=emailStr.match(emailPat) 
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("Email address seems incorrect\n(The username doesn't seem to be valid)")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat) 
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Email address seems incorrect\n(Destination IP address is invalid)")
		return false
	    }
    }
    return true
}

// Domain is symbolic name 
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("Email address seems incorrect\n(The domain name doesn't seem to be valid)")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("Email address seems incorrect\n(The address must end in a three-letter domain, or two letter country)")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="Email address seems incorrect\n(This address is missing a hostname)"
   alert(errStr)
   return false 
}

// If we've gotten this far, everything's valid!
return true;
}

//Validation for two field to be same 			
function isDuplicate(firstValue,SecondValue)
{
	if(firstValue==SecondValue)
		return true;
	else
		return false;
}
//returns true if it is a valid phone Number
function isValidPhoneNO(fieldname , fieldvalue)
{
	var str = fieldvalue;
  var checkOK = "0123456789-";
  var checkStr = checkTrim(str);
  var allValid = true;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch != ",")
      allNum += ch;
  }
  if (allValid)
      return (true);
  else
  alert('Please enter valid '+fieldname +'\n\n e.g. NNN-NNN-NNNN');
  	  return (false);
}

function check_usphone(phonenumber,useareacode) 
{ 
if(!useareacode)useareacode=1; 
if((phonenumber.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && ((useareacode!=1) && (phonenumber.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))) return false; 
return true; 
} 


//Validation for field which can contain only alphanumeric value and not allow space
function isValidID(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))||((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z') )))
		{
			alert(fieldname+' contains only alphanumeric values(without whitespace) \n\nValid Characters :(A to Z),(a to z) and (1 to 9) ');
			return false;
		}
		i++;
	}
	return true;
}
function validateSingleDate(dtDate)
{
	if(dtDate=="")
	{
		alert("Date cannot be empty");
		return false;
	}
	var month;
	var dat;
	var year;
	var firstIndex;
	var secIndex;
	var str = dtDate;
	var i = 0;
	var count=0;
	
	if(str.charAt(2)!="/")
	{
		alert("Please enter valid Date (e.g. 02/07/2002)");
		return false;
	}
	
	if(str.charAt(5)!="/")
	{
		alert("Please enter valid Date (e.g. 02/07/2002)");
		return false;
	}
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))|| (str.charAt(i) == '/')))
		{
			alert("Please enter valid Date (e.g. 12/27/2002)");
			return false;
		}
		else
		{
			if(str.charAt(i) == '/')
			count=count+1;
		}
		i++;
	}
	if(count>2)
	{
		alert("Please enter valid Date (e.g. 12/27/2002)");
		return false;
	}
	month=dtDate.substring(0,2);
	dat=dtDate.substring(3,5);
	year=dtDate.substring(6,10);
	if(month>12)
	{
		alert("Please enter valid Date (e.g. 12/27/2002)");
		return false;
	}
	if(month==1 || month==3 ||month==5||month==7||month==8||month==10||month==12)
	{
		if(dat>31)
		{
			alert("Please enter valid Date (e.g. 12/27/2002)");
			return false;
		}
	}
	if(month==2 || month==4 ||month==6||month==9||month==11)
	{
		if(dat>30)
		{
			alert("Please enter valid Date (e.g. 12/27/2002)");
			return false;
		}
	}
	if((year%4==0 && year%100!=0) || year%400==0)
	{
		if(month==2)
		{
			if(dat>29)
			{
				alert("Please enter valid Date (e.g. 12/27/2002)");
				return false;
			}
		}
	}
	else
	{
		if(month==2)
		{
			if(dat>28)
			{
				alert("Please enter valid Date (e.g. 12/27/2002)");
				return false;
			}	
		}
	}
	return true;
}
function validateDate(startDate,endDate)
{
	
	var TodayDate
	var stDate
	TodayDate = new Date()
	stDate = new Date(startDate)
	enDate = new Date(endDate)
			
	if (TodayDate < stDate)
	{
		alert("From Date cannot be the future date")
		return false;
	}
	if (TodayDate < enDate)
	{
		alert("To Date cannot be the future date")
		return false;
	}
	if (enDate < stDate)
	{
		alert("Invalid Date range selection");
		return false;
	}
	return true;
}

	function isDateBefore(date1Name,date1Value,date2Name,date2Value)
	{
		//check that the renew date is not lesser than the date rented
		var vDate1 = convertStringToDate(date1Value,5);
		var vDate2= convertStringToDate(date2Value,5);
		
		if(vDate1<vDate2)
		{
			alert(date1Name+" cannot be later than the "+date2Name+".");
			return true;
		}
		return false;
	}
	function isDateAfter(date1Name,date1Value,date2Name,date2Value)
	{
		//check that the renew date is not lesser than the date rented
		var vDate1 = convertStringToDate(date1Value,5);
		var vDate2= convertStringToDate(date2Value,5);
		
		if(vDate1>=vDate2)
		{
			alert(date1Name+" cannot be equal or later than the "+date2Name+".");
			return true;
		}
		return false;
	}
//Validation for field which can not allow < and > and ".
function isValid(fieldname , fieldvalue)
{
	var str = fieldvalue; 
	i = 0;
	while(i < str.length)
	{   
		if((str.charAt(i) == '<') || (str.charAt(i) == '>') || (str.charAt(i) == '\"')|| (str.charAt(i) == '"'))
		{ 
			alert('The value for '+fieldname+' is not valid value');
			return false;
		}
		i++;
	}
	return true;   
}
/*
//Validation for field which can not allow < and >.
function isValidSSN(ssn)
{
	var matchArr = ssn.match(/^(\d{3})?\d{2}?\d{4}$/); 
	var numDashes = ssn.split('-').length - 1;
	if (matchArr == null || numDashes == 1) 
	{   
		alert('Invalid SSN. Must be 9 digits'); 
		msg = "does not appear to be valid";
		return false; 
	}
	else if (parseInt(matchArr[1],10)==0) 
	{ 
		alert("Invalid SSN: SSN's can't start with 000.");
		msg = "does not appear to be valid";
		return false;
	}
	
	return true;
}//end of function isValidSSN
*/
function isValidZipCode(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;
	if (field.length!=5 && field.length!=10) 
	{
		alert("Please enter your 5 digit or 5 digit+4 zip code."); 
		return false;
	}
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{ 
			alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}//end of for
	return true;
}

//Validation for field which allows only numbers
function isValidNumeric(fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if(!((str.charAt(i) >= "0") && (str.charAt(i) <= "9")))
		{
			return false;
		}
		i++;
	}
	return true;
}

	function isValidCreditCardNo(fieldName, fieldObj) {
		if (! checkTextData(fieldName, fieldObj, true, false, true, false, false, false, false)) return false;
		
		var cardNoLen = fieldObj.value.length;
		
		if (! (cardNoLen == 15 || cardNoLen == 16)) {
			alert("Invalid "+fieldName);
			fieldObj.focus();
			return false;
		}
		return true;
	}

	function checkRadioCheckBox(fieldName, fieldObj) {
		var checked = false;
		
		for(i = 0; i < fieldObj.length; i++) {
			if (fieldObj[i].checked) {
				checked = true;
				break;
			}
		}
		
		if (! checked) {
			alert(fieldName+" must be specified");
			fieldObj[0].focus();
			return false;
		}
		
		return checked;
	}

	/*
	IMPORTANT: validChars and specAlertMsg will only used when chkSpecific is true.
	*/
	function checkTextData(fieldName, fieldObj, chkEmpty, chkAlpha, chkNum, chkAlphaNum, chkAlphaSpec, chkNumSpec, chkNumComma, chkSpecific, validChars, specAlertMsg) {
		if (chkEmpty) {
			if(isEmpty(fieldName, fieldObj.value)){
				fieldObj.focus();
				return false;
			}
		}

		if (chkAlpha) {
			if(! hasOnlyAlphabets(fieldName , fieldObj.value)){
				fieldObj.focus();
				return false;
			}
		}

		if (chkNum) {
			if(!hasOnlyNumeric(fieldName, fieldObj.value)) {
				fieldObj.focus();
				return false;
			}
		}

/*
		// not required yet. Uncomment and populate when required. 
		if (chkAlphaNum) {
		
		}
*/

		if (chkAlphaSpec) {
			if(!hasOnlyAlphabetsAndSpecificChar(fieldName, fieldObj.value)) {
				fieldObj.focus();
				return false;
			}
		}
		
		
		if (chkNumSpec) {
			if(!hasOnlyNumericAndSpecificChar(fieldName, fieldObj.value)) {
				fieldObj.focus();
				return false;
			}
		}

		if (chkNumComma) {
			if(!hasOnlyNumericAndComma(fieldName, fieldObj.value)) {
				fieldObj.focus();
				return false;
			}
		}

		if (chkSpecific) {
			if(!hasOnlySpecificChar(fieldObj, validChars, specAlertMsg)) {
				fieldObj.focus();
				return false;
			}
		}
		
		return true;
	}



	//Functions added by Hetal for rental transactions pages. For date caclulations
	function addDays(myDate,days) 
	{
		return new Date(myDate.getTime() + days*24*60*60*1000);
	}
	
	function addMonths(myDate,months) 
	{
		myDate.setMonth(myDate.getMonth() + months)
		return myDate ;
	}
	function convertStringToDate(vDate,vType)
	{
		/*
		   vDate is a date passed as a string in the following
		   formats:

		   type 1 : 19970529
		   type 2 : 970529
		   type 3 : 29/05/1997
		   type 4 : 05/29/97
		   type 5 : 05/29/1997

		   vType is a numeric integer from 1 to 4, representing
		   the type of dateString passed, as defined above.

		*/
		if (vType == 1)
		{
			var dNewDate = new Date(vDate.substring(0,4),
			vDate.substring(4,6)-1,
			vDate.substring(6,8));
			return dNewDate;
		}
		else if (vType == 2)
		{
			var dNewDate = new Date(vDate.substring(0,2),
			vDate.substring(2,4)-1,
			vDate.substring(4,6));
			return dNewDate;
		}
		else if (vType == 3)
		{
			var dNewDate = new Date(vDate.substring(6,10),
			vDate.substring(3,5)-1,
			vDate.substring(0,2));
			return dNewDate;
		}//end of else if
		else if (vType == 4)
		{
			var dNewDate = new Date(vDate.substring(6,8),
			vDate.substring(0,2)-1,
			vDate.substring(3,5));
			return dNewDate;
		}
		else if (vType == 5)
		{
			var dNewDate = new Date(vDate.substring(6,10),
			vDate.substring(0,2)-1,
			vDate.substring(3,5));
			return dNewDate;
		}
		else
			return '';
		
	}//end of function convertStringToDate;
	
	function getDateDiff(dFrom,dTo) 
	{
		/*
		   function getDateDiff
		   parameters: dateString dateType
		   returns: boolean
		   Returns string containing the age in years, months and days
		   in the format yyy years mm months dd days.
		   Returns empty string if dateType is not one of the expected
		   values.
		*/
		
		var yearDob = dFrom.getYear();
		var monthDob = dFrom.getMonth();
		var dateDob = dFrom.getDate();

		var yearNow = dTo.getYear();
		var monthNow = dTo.getMonth();
		var dateNow = dTo.getDate();
		
		yearAge = yearNow - yearDob;

		if (monthNow >= monthDob)
			var monthAge = monthNow - monthDob;
		else {
			yearAge--;
			var monthAge = 12 + monthNow -monthDob;
		}

		if (dateNow >= dateDob)
			var dateAge = dateNow - dateDob;
		else {
			monthAge--;
			var dateAge = 31 + dateNow - dateDob;

			if (monthAge < 0) {
				monthAge = 11;
				yearAge--; 
			}
		}
		monthAge = monthAge + (yearAge*12);
		return monthAge + '/' + dateAge;
	}

	function y2k(number) 
	{ 
		return (number < 1000) ? number + 1900 : number; 
	}
	
	function daysElapsed(date1,date2) {
		var difference =
			Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
		  - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
		return difference/1000/60/60/24;
	}
	//rounding the numbers
function round (n, d) {
  n = n - 0; // force number
  if (d == null) d = 2;
  var f = Math.pow(10, d);
  n += Math.pow(10, - (d + 1)); // round first
  n = Math.round(n * f) / f;
  n += Math.pow(10, - (d + 1)); // and again
  n += ''; // force string
  return d == 0 ? n.substring(0, n.indexOf('.')) :
	  n.substring(0, n.indexOf('.') + d + 1);
}
	
	//Added by hetal : for US PHone validation
	// Declaring required variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "-";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters ;
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;
	
	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++)
	    {   
	        // Check that current character isn't whitespace.
	        var c = s.charAt(i);
	        if (bag.indexOf(c) == -1) returnString += c;
	    }
	    return returnString;
	}
	
	function checkUSPhone(strPhone)
	{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}

	//Date Validation
	//Added By :Hetal
	/**********************************************************************/ 
  /*Function name :isDigit(theDigit) */ 
  /*Usage of this function :test for an digit */ 
  /*Input parameter required:thedata=string for test whether is digit */ 
  /*Return value :if is digit,return true */ 
  /* else return false */ 
  /**********************************************************************/ 
	
  function isDigit(theDigit) 
  { 
	  var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 
		
	  for (j = 0; j < digitArray.length; j++) 
	  {
		  if (theDigit == digitArray[j]) 
		  return true 
	  } 
	  return false 
		
  } 
	
  /*************************************************************************/ 
  /*Function name :isPositiveInteger(theString) */ 
  /*Usage of this function :test for an +ve integer */ 
  /*Input parameter required:thedata=string for test whether is +ve integer*/ 
  /*Return value :if is +ve integer,return true */ 
  /* else return false */ 
  /*function require :isDigit */ 
  /*************************************************************************/ 
  function isPositiveInteger(theString) 
  { 
	  var theData = new String(theString) 
		
	  if (!isDigit(theData.charAt(0))) 
	  if (!(theData.charAt(0)== '+')) 
	  return false 
		
	  for (var i = 1; i < theData.length; i++) 
	  if (!isDigit(theData.charAt(i))) 
	  return false 
	  return true 
  } 
	
  /**********************************************************************/ 
  /*Function name :isDate(s,f) */ 
  /*Usage of this function :To check s is a valid format */ 
  /*Input parameter required:s=input string */ 
  /* f=input string format */ 
  /* =1,in mm/dd/yyyy format */ 
  /* else in dd/mm/yyyy */ 
  /*Return value :if is a valid date return 1 */ 
  /* else return 0 */ 
  /*Function required :isPositiveInteger() */ 
  /**********************************************************************/ 
	
  function isValidDate(s,f) 
  {
  	if(s!=null && checkTrim(s)!="")
  	{
	  var a1=s.split("/"); 
	  var a2=s.split("-"); 
	  var e=true; 
	  if ((a1.length!=3) && (a2.length!=3)) 
	  { 
		  e=false; 
	  } 
	  else 
	  {
		  if (a1.length==3) 
		  var na=a1; 
		  if (a2.length==3) 
		  var na=a2; 
		  if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])) 
		  { 
			  if (f==1) 
			  {
				  var d=na[1],m=na[0]; 
			  } 
			  else 
			  {
				  var d=na[0],m=na[1]; 
			  } 
			  var y=na[2]; 
			  if (((e) && (y<1000)||y.length>4)) 
			  e=false 
			  if (e) 
			  { 
				  v=new Date(m+"/"+d+"/"+y); 
				  if (v.getMonth()!=m-1) 
				  e=false; 
			  } 
		  } 
		  else 
		  { 
			  e=false; 
		  } 
	  } 
	  return e 
	 }//end of if
	 else
	 {
		 return true;
	 }
  } 
  
	
	function maxlength(obj,objname,maxlen)
    {
      var q = eval(obj.value.length);
      var r = q - maxlen;

	var msg = "Sorry , You have typed "+q+" characters in "+objname+" textbox. The text box has limit of "+maxlen+" characters. Please shorten your text by "+r+" characters.";
      if (q > maxlen)
	  {
	   alert(msg);
	   return false;
      }
	  else
	  {
	   return true;
	  } 
	  
	} 
	
//Validation for field which allows only float numbers - allow also negative values
function isValidFloat(fieldname , fieldvalue)
{
	var str = fieldvalue;
	var str1;
	i = 0;
	j=0;
	while(i < str.length)
	{
		if((!((str.charAt(i) >= "0") && (str.charAt(i) <= "9"))) && (str.charAt(i) != ".") && (str.charAt(i) != "-"))
		{
			alert(fieldname+' is not valid\n\n e.g -57.55');
			return false;
		}

		if(str.charAt(i) == ".")
			j++;
			
		if(str.charAt(i) == "-"){
			if( i != 0 ){
				alert(fieldname+' is not valid\n\n e.g 57.55');
				return false;
			}
		}
		i++;
	}
	if(j>1)
	{
		alert(fieldname+' is not valid\n\n e.g 57.55');
		return false;
	}
	if(str.indexOf('.')>=0)
	{
		str1=str.substring(str.indexOf('.'),str.length-1);
		if(str1.length>2)
		{
			alert(fieldname+' is not valid\n\n e.g 57.55');
			return false;
		}
	}
	return true;
}

//Validation for field which can not allow < and > and ".
function isValidPassword(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	j = 0;
	if(str.length < 8)
	{
		alert('Password should be atleast 8 characters long');
		return false;
	}
	if(str.length > 15)
	{
		alert('Password should be maximum 15 characters long');
		return false;
	}
	while(i < str.length)
	{
	
		if((str.charAt(i) == '<') || (str.charAt(i) == '>') || (str.charAt(i) == '\"') || (str.charAt(i) == '\'') || (str.charAt(i) == ' '))
		{
			alert('The value for '+fieldname+' is not valid value');
			return false;
		}
		if(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || (str.charAt(i) == '!') || (str.charAt(i) == '@') || (str.charAt(i) == '#') || (str.charAt(i) == '$') || (str.charAt(i) == '^') || (str.charAt(i) == '&') || (str.charAt(i) == '*') && (str.charAt(i) == '(') && (str.charAt(i) == ')'))
		{
			j++;
		}
		i++;
		
	}
	if(j==0)
	{
			alert('The password should have atleast one special character or numeric character');
			return false;
	}
	return true;
	
}

//Validation for field which can not allow < and > and ".
function isValidUserName(fieldname , fieldvalue)
{
	var str = fieldvalue;
	i = 0;
	while(i < str.length)
	{
		if((str.charAt(i) == '<') || (str.charAt(i) == '>') || (str.charAt(i) == '\"') || (str.charAt(i) == '\'') || (str.charAt(i) == ' '))
		{
			alert('The value for '+fieldname+' is not valid value');
			return false;
		}
		i++;
	}
	return true;
}
  
//phone No validation -code updated By Astha 	  
function maskIt(fld,frmName)
{
	
	fldVal = fld.value;
	var formName = frmName;	
	var tmpStr = "";	
	var isNamedFone;	
	keyCount = fldVal.length;	
	keyEntered =fldVal.substring(keyCount-1,keyCount);
	
	if (keyCount < 2) 
	
	{isNamedFone = false;}
	 
	if (!isNamedFone) {isNamedFone = chkNAN(keyEntered);}
	 
	keyCount++;
	
	with (formName)      
	{ 		
		switch (keyCount)
		{		
			case 2:			 
			tmpStr += fldVal;
			fld.value = tmpStr;
			break;
			
			case 4: 
			fld.value += "-"; 
			break;
			
			case 5:
			fld.value += "";
			break;
			
			case 8:
			fld.value += "-"; 
			break;
		
		}  
	} 
}

function checkPhoneNO(phoneName,phone)
{

	if(isValidPhoneNO(phoneName,phone))
	{
		if(phone.indexOf("-")!=3)
		{
			alert(phoneName+" should be of format xxx-xxx-xxxx");
			return false;
		}
	
		if(phone.lastIndexOf("-")!=7) 
		{
			alert(phoneName+" should be of format xxx-xxx-xxxx");
			return false;
		}
	
		if(phone.length!=12)  
		{
			alert(phoneName+" should be of format xxx-xxx-xxxx");
			return false;
		} 		
		return true; //Added by Astha  
					 	
	}
	else
		return false;
}
  
function chkNAN(char2chk)
{
	var validNum = "0123456789"; 
	if (validNum.indexOf(char2chk) == "-1")
	{
		alert("You have entered a non-numeric character.");
		return false;
	}
} 
//Validation for field which allows float number
function allowFloat(fieldname , fieldvalue, precision, scale)
{
	var str = checkTrim(fieldvalue);
	j=0;
	i = 0;
	while(i < str.length)
	{
		if(str.charAt(i) == "."){
			if( j != 0 ){
				alert(fieldname+' is not valid value');
				return false;
			}
			else{
				j++;
			}
		}
		i++;
	}	
	i=0;
	if( j != 0 ) {
		var firstStr = str.substring(0,str.indexOf("."));
		var secondStr = str.substring(str.indexOf(".")+1,str.length);

		if(firstStr.length == 0 || secondStr.length == 0){
			alert(fieldname+' is not valid value');
			return false;						
		}
		if( !checkLength(firstStr,precision) ){
			alert(fieldname+' is not valid value \n\nOnly '+precision+' digits allowed before the decimal');
			return false;			
		}
		if( !checkLength(secondStr,scale) ){
			alert(fieldname+' is not valid value\n\nOnly '+scale+' digits allowed after the decimal');
			return false;
		}
	}
	else{
		if( !checkLength(str,precision) ){
			alert(fieldname+' is not valid value');
			return false;			
		}
	}
	while(i < str.length)
	{
		if(!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || str.charAt(i) == "."))
		{
			alert(fieldname+' contains only numeric value');
			return false;
		}
		i++;
	}
	return true;
}


//Validation for checking length
function checkLength(checkStr,length){
	checkStr = checkTrim(checkStr);
	if((checkStr.length)>length)
	{
		return false; // false if the length exceeds
	}
	else{ 
		return true;  // else true
	}
}

//Email Validation 

function validateEmail(fld1) {
 var inputField=eval(fld1);
// var mailVal = /^(([a-z])*([1-9]+[a-z]))+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})*([\.-]?\w+)*(\.\w{2,3})+$/i ;  
 var mailVal = /^(\w+)([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/i ;     
                           
 if(! mailVal.test(inputField.value) )  
 {
  return true;
 }
 
}//end of validateEmail   

function strMonth(intMonth){
 
	 if(intMonth==1){
	  return "January"; 
	 }
	 
	 if(intMonth==2){
	  return "February"; 
	 }
	 
	 if(intMonth==3){
	  return "March"; 
	 }
	 
	 if(intMonth==4){
	  return "April"; 
	 }
	 
	 if(intMonth==5){
	  return "May"; 
	 }
	 
	 if(intMonth==6){
	  return "June"; 
	 }
	 
	 if(intMonth==7){
	  return "July"; 
	 }
	 
	 if(intMonth==8){
	  return "August"; 
	 }
	 
	 if(intMonth==9){
	  return "September"; 
	 }
	 
	 if(intMonth==10){
	  return "October"; 
	 }
	 
	 if(intMonth==11){
	  return "November"; 
	 }

	if(intMonth==12){
	  return "December"; 
	}
}//end of function strMonth
