﻿

 // FormChek.js
//Function setInitFocus has been added for IdealPath.
//This function sets the focus on a particular dataEntry field as soon the page downloads.
//Function chkDrpDwn has been added for IdealPath.
//This function checks whether something is selected in a dropdown(select box) in a form or not.
//Function setFldValue has been added for IdealPath
//This functions sets the value of a particular field in a form


// VARIABLE DECLARATIONS
var digits = "0123456789";

var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"

var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


// whitespace characters
var whitespace = " \t\n\r";


// decimal point character differs by language and culture
var decimalPointDelimiter = "."


// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";


// characters which are allowed in US phone numbers
var validUSPhoneChars = digits + phoneNumberDelimiters;


// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = digits + phoneNumberDelimiters + "+";


// U.S. phone numbers have 10 digits.
// They are formatted as 123 456 7890 or (123) 456-7890.
var digitsInUSPhoneNumber = 10;


// non-digit characters which are allowed in ZIP Codes
var ZIPCodeDelimiters = "-";


// our preferred delimiter for reformatting ZIP Codes
var ZIPCodeDelimeter = "-";

// characters which are allowed in Social Security Numbers
var validZIPCodeChars = digits + ZIPCodeDelimiters;

// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;

// non-digit characters which are allowed in credit card numbers
var creditCardDelimiters = " ";

var defaultEmptyOK = false;

var CheckAvailable="0";

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

//var daysInMonth = makeArray(12);
var daysInMonth = new Array(13);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

// Valid U.S. Postal Codes for states, territories, armed forces, etc.
// See http://www.usps.gov/ncsc/lookups/abbr_state.txt.

var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP"



//***********************
var iDate = "Invalid date value."
//var iInteger = "Invalid integer value.  The field can contain only digits."
var iInteger = "Value must contain numbers only"
var iPosInteger = "Invalid integer value.  The field can contain only digits and must be positive."
var iNegInteger = "Invalid integer value.  The field can contain only digits and must be negative."
var iNumeric = "Invalid numeric value.  The field can contain only digits and decimal point."
var iNum = "please enter a numeric value."
var iWeekday = "Invalid week day."
var iStateCode = "This field must be a valid two character U.S. state abbreviation (like CA for California). Please reenter it now."
var iZIPCode = "This field must be a 5 or 9 digit U.S. ZIP Code (like 94043). Please reenter it now."
var iUSPhone = "This field must be a 10 digit U.S. phone number (like 415 555 1212). Please reenter it now."
var iWorldPhone = "This field must be a valid international phone number. Please reenter it now."
var iSSN = "This field must be a 9 digit U.S. social security number (like 123 45 6789). Please reenter it now."
var iEmail = "This field must be a valid email address (like foo@bar.com)."
var iCreditCardPrefix = "This is not a valid "
var iCreditCardSuffix = " credit card number. (Click the link on this form to see a list of sample numbers.) Please reenter it now."
//var iNotEmpty = "  Also it cannot be empty."
var iNotEmpty = " and cannot be null."

//***********************

//====================================================================================================
/* FUNCTIONS TO INTERACTIVELY CHECK VARIOUS FIELDS. */
//====================================================================================================



// checkString (TEXTFIELD theField, STRING s, [, BOOLEAN emptyOK==false])
function checkString (theField, label, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    
    
    
    if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
    	{
	  alert("Please fill in the field: " + label);
          theField.value = ""
	  theField.focus();
	  return false;
	}
    else 
	{return true;}
}


function checkBlankString (theField, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    
    //alert(label)
    
    if (checkBlankString.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
    	{return false;}
    else 
	{return true;}
}


function checkStringTextArea (theField, label, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    
       
    if (checkStringTextArea.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       {alert("Please fill in the field: " + label);
	return false;}
    else
	{ return true;}
}

function checkHiddenValue (theField, label, emptyOK)
{   // Next line is needed on NN3 to avoid "undefined is not a number" error
    // in equality comparison below.
    
	
       
    if (checkHiddenValue.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (isWhitespace(theField.value)) 
       {alert("Please fill in the field: " + label);
	return false;}
    else 
	{return true;}
}

function checkDateBox (theField, label, emptyOK)
{  
	var sMsg;

	
    if (arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((emptyOK == true) && (isWhitespace(theField.value))) return true; 

	sMsg = (emptyOK) ? iDate : iDate + iNotEmpty
	if (Date.parse(theField.value).toString() == 'NaN')
		{alert("Field:" + label + ". " + sMsg);
	return false;}

    return true;
}

function checkDateDropdown (month, day, year, emptyOK)
{  
	var sMsg;
		
    if (arguments.length == 3) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(month)||isEmpty(day)||isEmpty(year))) return true;
    if ((emptyOK == true) && (isWhitespace(month)||isWhitespace(day)||isWhitespace(year))) return true; 
    if ((!emptyOK) && (isEmpty(month)||isEmpty(day)||isEmpty(year))) return false;
    if ((!emptyOK) && (isWhitespace(month)||isWhitespace(day)||isWhitespace(year))) return false; 

	return isValidExpiration(month, day, year);
}

function checkNumeric (theField, label, emptyOK)
{
	var sMsg;

	//theField = eval(theField);	//switch this line on if theField parameter to this function is passed as a String

    if (arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((emptyOK == true) && (isWhitespace(theField.value))) return true; 

	sMsg = (emptyOK) ? iNumeric : iNumeric + iNotEmpty
	//if (!isFloat(String(theField.value).replace(/,/g,"")))
	if (!isFloat(theField.value))
		{alert("Field:" + label + ". " + sMsg)
		return false;}
		
    return true;
}

/*
function checkInteger (theField, label, emptyOK)
{
	var sMsg;

	//theField = eval(theField);

	if (arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((emptyOK == true) && (isWhitespace(theField.value))) return true; 

	sMsg = (emptyOK) ? iInteger : iInteger + iNotEmpty
	if (!isInteger(theField.value))
		{alert("Field:" + label + ". " + sMsg)
		theField.focus();
		return false;}
		
    return true;
}
*/

function checkInteger (theField, label, emptyOK)
{
	var sMsg;

	//theField = eval(theField);

	if (arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if ((emptyOK == true) && (isWhitespace(theField.value))) return true; 

	sMsg = (emptyOK) ? iInteger : iInteger + iNotEmpty
	if (!isInteger(theField.value))
		{alert("Invalid " + label + " Entered: " + sMsg)
		theField.focus();
		return false;}
		
    return true;
}

function checkSignedInteger (theField, label, lt_gt)
{
	//theField = eval(theField);
	var sMsg;
	if (lt_gt == 'gt')
	{
		if(!isPositiveInteger(theField.value))
			{sMsg = iPosInteger + iNotEmpty;
			alert("Field:" + label + ". " + sMsg)
			return false;}
	}
	else
	{
		if(!isNonpositiveInteger(theField.value))
			{sMsg = iNegInteger + iNotEmpty;
			 alert("Field:" + label + ". " + sMsg)
			return false;}
	} 
    return true;
}

function checkWorkingDays (theField, label)
{
	//theField = eval(theField);


	if (!isPositiveInteger(theField.value))
		{alert("Field :" + label + ". " + iPosInteger + iNotEmpty)
		return false;}
	if (parseInt(theField.value) > 7)
		{alert("Field:" + label + ". " + iWeekday)
		return false;}

    return true;
}

function checkDropDown(theField, label)	
{
	//theField = eval(theField);
	
	if(theField.selectedIndex == 0)
		{
		alert("Please select a value: " + label)
		theField.focus();
		return false;
		}

	return true;
}

function checkList(theField, label)	
{
	//theField = eval(theField);
	
	if(theField.options.length==1)
		{alert("Please select a value: " + label)
		return false;}

	return true;
}

function checkMultiSelect(theField, label)	
{
	//theField = eval(theField);
	if(theField.selectedIndex == -1)
		{alert("Please select a value: " + label)
		return false;}

	return true;
}


// checkStateCode (TEXTFIELD theField ,label,[, BOOLEAN emptyOK==false])
function checkStateCode (theField, label,emptyOK)
{   
	//theField = eval(theField)

	if (checkStateCode.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  theField.value = theField.value.toUpperCase();
       if (!isStateCode(theField.value, false)) 
          	{alert("Please fill in valid State Code: " + label)
		return false;}
       else return true;
    }
}


// checkEmail (TEXTFIELD theField [, BOOLEAN emptyOK==false])
function checkEmail (theField, label, emptyOK)
{   
	//theField = eval(theField)

	if (checkEmail.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false))
        {alert("Please fill in a valid Email address: " + label)
	theField.focus();
	return false;}
    else return true;
}

	
// Get checked value from radio button.
function getRadioButtonValue (radio)
{   for (var i = 0; i < radio.length; i++)
    {   if (radio[i].checked) { break }
    }
    return radio[i].value
}

// Function checkChkBox checks for two things
//	-- Check whether checkbox is clicked or not
//	-- Check whether more than ONE checkbox is clicked
//	If more than one is clicked it will return FALSE
function checkChkBox(theChkBox, theLabel)
{
	var chkTrueFlag;
	chkTrueFlag = 0;
	
	if (!theChkBox.length) {
		return true;
	}
	
	//theChkBox = eval(theChkBox)
	for(i=0;i<theChkBox.length;i++)
		{if(theChkBox[i].checked == true)
		chkTrueFlag = chkTrueFlag + 1;}
	if (chkTrueFlag == 0)
	{alert("Please check at least one check box " + theLabel)
	  return false;}
	
	if(chkTrueFlag > 1)
	{alert("Please check only one check box" + theLabel)
	  return false;}
	  
	 return true;
}
	
	

//Sets field value of a particular field in a form
function setFldValue(theForm,theElement,theValue)
{
	argArray = setFldValue.arguments
	
	for(i=0;i<argArray.length;i+=3)
	{
	
	strReference = String("document.")
	strReference = strReference + argArray[i] + "."
	strReference = strReference + argArray[i+1]
	strReference = eval(strReference)
	strReference.value = argArray[i+2]
	
	}
		
}

//===================================================================================================


// Check whether string s is empty.

function isEmpty(s)
{
	   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)

{   var i = 0;

    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    
    return s.substring (i, s.length);
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

// isInteger (STRING s [, BOOLEAN emptyOK])
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isSignedInteger (s)

{   
	if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

// isPositiveInteger (STRING s [, BOOLEAN emptyOK])
function isPositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a positive, not negative, number

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

// isNegativeInteger (STRING s [, BOOLEAN emptyOK])
function isNegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNegativeInteger.arguments.length > 1)
        secondArg = isNegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a negative, not positive, number

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) < 0) ) );
}

// isNonpositiveInteger (STRING s [, BOOLEAN emptyOK])
function isNonpositiveInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonpositiveInteger.arguments.length > 1)
        secondArg = isNonpositiveInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number <= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) <= 0) ) );
}

// isFloat (STRING s [, BOOLEAN emptyOK])
// True if string s is an unsigned floating point (real) number. 
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
// Does not accept exponential notation.
function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isSignedFloat (STRING s [, BOOLEAN emptyOK])
// True if string s is a signed or unsigned floating point 
// (real) number. First character is allowed to be + or -.
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isSignedInteger, then call isSignedFloat.
// Does not accept exponential notation.
function isSignedFloat (s)

{   if (isEmpty(s)) 
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedFloat.arguments.length > 1)
            secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}

// isAlphabetic (STRING s [, BOOLEAN emptyOK])
function isAlphabetic (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        return false;
    }

    // All characters are letters.
    return true;
}

// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
function isAlphanumeric(s)
{   
    var i;
    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.
    
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}


// isZIPCode (STRING s [, BOOLEAN emptyOK])
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code.  Must be 5 or 9 digits only.
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.  
function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}

// isStateCode (STRING s [, BOOLEAN emptyOK])
// Return true if s is a valid U.S. Postal Code 
// (abbreviation for state).
function isStateCode(s)
{   if (isEmpty(s)) 
       if (isStateCode.arguments.length == 1) return defaultEmptyOK;
       else return (isStateCode.arguments[1] == true);
    return ( (USStateCodes.indexOf(s) != -1) &&
             (s.indexOf(USStateCodeDelimiter) == -1) )
}

// isEmail (STRING s [, BOOLEAN emptyOK])
function isEmail (s)
{
   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) 
    {return false;}
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != "."))
    {return false;}
    else return true;
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


// isDate (STRING year, STRING month, STRING day)
function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    //if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year);
    var intMonth = parseInt(month);
    var intDay = parseInt(day);
    var intLeap = 0;

    // catch invalid days, except for February
    
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intYear % 4 == 0) || (intYear % 100 == 0) || (intYear % 400 == 0)) {
      intLeap = 1;
	}
	if ((intMonth == 2) && (intLeap == 1) && (intDay > 29)) {
      return false;
	}

	if ((intMonth == 2) && (intLeap != 1) && (intDay > 28)) {
      return false;
    }

    //if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

//----------------------------------------------------------------------------------------
// Function checkRadButton checks whether RadioButton is clicked or not
//	If one is not clicked it will return FALSE

function checkRadButton(theRadButton, theLabel)
{
	var chkTrueFlag;
	var qtyChecked = 0;
	//theRadButton = eval(theRadButton)
	theRadButton = theRadButton
	for(i=0;i<theRadButton.length;i++)
	  {
		  if(theRadButton[i].checked == true)
			qtyChecked = qtyChecked + 1;
	  }
	  if (!(qtyChecked == 1))
	  {
		  alert("Please select at least one radio button: " + theLabel)
		  return false;
	  }
	return true;
}
//----------------------------------------------------------------------------------------
//This function added to validate the date

function isValidExpiration(month, day, year,strictCheck)
{
	
	if(strictCheck){
	if((month == "") ||(day == "")||(year == ""))
	{return false;}
	}
	
	if((month != "") ||(day != "")||(year != "")){
	if(parseInt(month) >0 && parseInt(month) <= 12)
	{
			month = parseInt(month);
			day = parseInt(day);
			year = parseInt(year);
			mmddyy = false;	
			
			if(((month == 2) && ((year%4)!=0 )&& (day >28)))
			{
				count=1;
				mmddyy = true;
			}
				
			if(((month == 2) && ((year%4) == 0) && (day > 29)))
			{
				count = 2;
				mmddyy = true;
			}	

			if( ((month == 4 || month==6 || month == 9 || month==11) && (day > 30)))
			{
				count =3;
				mmddyy = true;
			}	

			if(((month == 1 || month==3 || month == 5 || month== 7 || month == 8 ||month ==10 || month == 12) &&
			    (day > 31)))
			{
				count = 4;
				mmddyy= true;
			}	
			return(!mmddyy) 
	}
	
	return(false)
	}
	return true
}

function CheckFax(theField,label)
{
	var regExp1 = /^\d{3}\-\d{3}\-\d{4}$/
	if (!regExp1.test(theField.value))
	{alert("Please enter the " + label + " Number in the correct format")
	theField.focus();
	return false;}
	else
	return true
}



function keyPress(chCode, srcName)
{
	// chCode = 13
	if (window.event.keyCode == chCode)
	{
		if (window.event.srcElement.name == srcName)
		{
			window.event.keyCode == 0;
			return true
		}
		else
		{
			return false;
		}
	}
}

function submitenter(myfield,e)
	{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	   {
	   myfield.form.submit();
	   return false;
	   }
	else
	   return true;
	}
	
	
	
	

//Validatation (Created By Sanjeev Bansal Date on: 16/02/2007)

var msginvalid="Invalid entry";
var msgAlphaNumeric="Please enter alphanumeric characters.";
var msgWhiteSpace="Space is not allowed";
var msgAlphabet="Please enter alpha characters.";
var msgDigit="Please enter only Number";
var msgBlank="Cannot be blank.";
var msgZip="Please enter a valid Zip.";
var IsValid;
IsValid=1;
var object;
//Check for Blank entry
function ChkBlank(obj)
{
    obj.value=TrimUsingWhileLoop(obj.value);
    if(obj.value!="")
    {
        return false;
    }
    return true;
}
//Check for Blank entry (DropDown List)
function ChkBlankDDL(obj,fieldName,Label,Display)
{
    Label.innerText="";
    obj.value=TrimUsingWhileLoop(obj.value);
    if(obj.value==0)
    {
       return DisplayMessage(obj,fieldName,msgBlank,Display,Label);
    }
    return true;
}
//Check Alphabet (Allow only Alphabet only)z 
function checkAlphabets(obj)
{
    var iChars = "~`!@#$%^&*()+=[]\\\';/{}|\":<>?,.0123456789";
                     
    for (var i = 0; i < obj.value.length; i++) 
    {
    
        if (iChars.indexOf(obj.value.charAt(i)) != -1) 
        {	
           return true;
           
        }  
    }
    return false;  
}

//Allow only AlphaNumeric Entry
 function checkAlphaNumeric(obj)
 {
    var iChars = "~`!@#$%^&*()+=[]\\\';/{}|\":<>?,.";
    obj.value=TrimUsingWhileLoop(obj.value);
    for (var i = 0; i < obj.value.length; i++) 
    {
        if (iChars.indexOf(obj.value.charAt(i)) != -1) 
        {	
           return true;
        }
    }   
    return false;
}

function chkWhiteSpace(obj)
{
    for (var i = 0; i < obj.value.length; i++) 
    {
         //Get ASCII Value of Space
           if(obj.value.charCodeAt(i)==32)
           {
               return true;          
           }
    }
    return false;
}
//Check Data for Specified pattern(s)
   function CheckData(theField,fieldName,Label,Display)
    {
    Label.innerText="";     
    if(theField.value=="")
    {
                if(Display==1)
                {
                        Label.innerText=msgBlank;
                      
                        theField.focus();
                        return false;
                        
                }
                else
                {
                    alert(fieldName + msgBlank)
                    
                    theField.focus();
                    return false;     
                }
    }
    
    if(theField.value.length>0)
    {
        var regExp1 = /^\d{3}\-\d{3}\-\d{4}$/       //w-Alphanumeric 
        //var regExp1 = /^\d{3}\-\d{3}\-\d{4}$/
        if (!regExp1.test(theField.value))
            {
                if(Display==1)
                {
                        Label.innerText="Please enter the " + fieldName + " Number in the correct format(XXX-XXX-XXXX)";
                        theField.focus();
                        return false;
                        
                }
                else
                {
                    alert("Please enter the " + fieldName + " Number in the correct format(XXX-XXX-XXXX)")     
                    theField.focus();
                    return false;     
                }
            }
     }
     return true;
}

//Check Zip Code field
function chkZipCode(obj,fieldName,Label,Display)
 {
    Label.innerText="";  
    obj.value=TrimUsingWhileLoop(obj.value);
    if(obj.value=="")
    {
       
        return DisplayMessage(obj,fieldName,msgBlank,Display,Label);
    }
    Label.innerText="";
    var delimiter='-';
    for (var i = 0; i < obj.value.length; i++) 
    {
        if (delimiter.indexOf(obj.value.charAt(i)) == -1) 
	     {	
	        if(checkAlphaNumeric(obj))
	        {
    	        return DisplayMessage(obj,fieldName,msgZip,Display,Label);
	        }
	     }
    }
    return true;
}
	            
// Trim() -Remove the Space around the str
function TrimUsingWhileLoop(str)
{  while(str.charAt(0) == (" ") )
  {  str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}

//Check Digit(s)
function chkdigit(obj)
{
var digits = "0123456789";                        
    for (var i = 0; i < obj.value.length; i++) 
    {   
        if (digits.indexOf(obj.value.charAt(i)) != -1) 
        {	
           return true;
           
        }  
    }
    return false;  
}

//Created By Sanjeev Bansal on 18 Feb 2009
/*===========================================================*/
//Check Digit(s)
function chkNumeric(obj)
{
var digits = "0123456789";                        
    for (var i = 0; i < obj.value.length; i++) 
    {          
        if (digits.indexOf(obj.value.charAt(i)) == -1) 
        {	
           return false;
        }  
    }
    return true;  
}
//Check Digit(s) and Decimal
function chkNumericDecimal(obj)
{
var digits = "0123456789.";                        
    for (var i = 0; i < obj.value.length; i++) 
    {          
        if (digits.indexOf(obj.value.charAt(i)) == -1) 
        {	
           return false;
        }  
    }
    return true;  
}
/*===========================================================*/

//Check for Maximum Length
function ChkLength(obj,Width)
{
    if(obj.value.length<Width)
    {
        return true;
    }
        return false;
}


//Check validation for Company Identifier

function chkValidation(obj,fieldName,Blank,AlphaNumeric,WhiteSpace,Alphabet,Digit,Length,Width,Display,Label)
{
        Label.innerText="";
        //Check for Blank entry
       
        if(parseInt(Blank)==1)
        {
            if(ChkBlank(obj))
            {
                return DisplayMessage(obj,fieldName,msgBlank,Display,Label);
            }
        }
        //Check for AlphaNumeric entry
        if(parseInt(AlphaNumeric)==1)
        {
          
            if(checkAlphaNumeric(obj))
            {
          
               return DisplayMessage(obj,fieldName,msgAlphaNumeric,Display,Label);
            }
        }
        //Check for WhiteSpace entry
        if(parseInt(WhiteSpace)==1)
        {
            if (chkWhiteSpace(obj))
            {
                return DisplayMessage(obj,fieldName,msgWhiteSpace,Display,Label);
            }
         }
        //Check for Alphabet entty
        if(parseInt(Alphabet)==1)
        {
            if(checkAlphabets(obj))
            {
                return DisplayMessage(obj,fieldName,msgAlphabet,Display,Label);
            }
        } 
        
        //Check for Digit entry
        if(parseInt(Digit)==1)
        {
            if(chkdigit(obj))
            {
                return DisplayMessage(obj,fieldName,msgDigit,Display,Label);
            }
        }
        //Check for Maximum Width
        if(parseInt(Length)==1)
        {
            if(!(ChkBlank(obj)))
            {
                if(ChkLength(obj,Width))
                {
                    if(parseInt(Display)==1)    //1 for Label Display, 0 for Alert Display
                    {
                        Label.innerText="Please enter "+Width+" alphanumeric characters.";             
                        obj.focus();
                        return false;
                     
                    }
                    else
                    {
                       
                       alert(Message);
                       obj.focus();
                       return false;
                    }
                }
            }
        }
        return true;
 }
//Display Message
 function DisplayMessage(obj,fieldName,Message,Display,Label)
 {
        if(parseInt(Display)==1)    //1 for Label Display, 0 for Alert Display
        {
          Label.innerText=Message;
          
          obj.focus();
          return false;
        }
        else
        {       
           alert(Message);
           obj.focus();
           return false;
        }
  
 }

//Check for Validation for Date Picker Control (Telerik)
function ChkDate(obj,fieldName,Label,Display)
{
    Label.innerText="";
    obj.value=TrimUsingWhileLoop(obj.value);
    if(obj.value=="")
    {
      if(parseInt(Display)==1)    //1 for Label Display, 0 for Alert Display
        {
          Label.innerText=msgBlank;
          return false;
        }
        else
        {       
           alert(msgBlank);
           return false;
        }
    }
    return true;
}
//Validate Date (obj should be equal or greater than Cdate)
function validatedate(obj,Cdate) 
{
    var x;
    x=obj;    
    var curdate=Cdate;//'<%=DateTime.Now.ToShortDateString()%>';                
    var dt1 = new Date(curdate);   

    var str=new String();
    str=x.value;
    var dt3=new Date(str);
 
    var yy1=str.substr(0,4);    
    //var mm1=str.substr(5,2); // commented by rohit on 19 Dec
    var dd1;
    
    if(str.length==9) // change by rohit on 19 Dec for date validation
    {

        dd1 = str.substr(str.lastIndexOf('-')+1);           
        var remainstr = str.substr(0,str.lastIndexOf('-'));
        mm1 = remainstr.substr(remainstr.indexOf('-')+1);
        //alert('day' + dd1);   
        //alert('month' + mm1);        

//        if(mm1.substr(1,1)=='-')
//        {
//            dd1=str.substr(7,2);
//            mm1=str.substr(5,1); 
//        }
//        else
//        {
//             mm1=str.substr(5,2);
//             var dd1=str.substr(7,1);
//        }
    }
    if(str.length==10)
    {
        dd1=str.substr(8,2);
        mm1=str.substr(5,2);
        
        //dd1=str.substr(5,2);
        //mm1=str.substr(7,2);
    }
    if(str.length==8) 
    {
        dd1=str.substr(7,1);
        mm1=str.substr(5,1);
        
        //dd1=str.substr(5,1);
        //mm1=str.substr(7,1);
    }
 

    str=dd1+"/"+mm1+"/"+yy1;
     
    dt2=new Date(str);      

      
    if(yy1<dt1.getFullYear())   //Year 
    {
        //alert("DisConnect Date must be Today or greater than Today");
        return false;
        
    }
    else if(yy1==dt1.getFullYear())
    {
        if(mm1<dt1.getMonth()+1)    //month (0 to 11)
       {
            //alert("DisConnect Date must be Today or greater than Today");
            return false;
       }
       else if(mm1==dt1.getMonth()+1)    //month (0 to 11)
        {
            if(dd1<dt1.getDate())   //Day (1 to 31)
            {
                //alert("DisConnect Date must be Today or greater than Today");
                return false;
            }
        }
    }
    return true;
}

//Validate Date (obj should be equal or less than Cdate)
function validatedate_equallessthan(obj,Cdate)
{
    var x;
    x=obj;    
    var curdate=Cdate;//'<%=DateTime.Now.ToShortDateString()%>';                
    var dt1 = new Date(curdate);   

    var str=new String();
    str=x.value;
    var dt3=new Date(str);
 
    var yy1=str.substr(0,4);    
    var mm1=str.substr(5,2);
    var dd1;
    
    if(str.length==9)
    {
    //alert('Inside1')
        if(mm1.substr(1,1)=='-')
        {
           dd1=str.substr(7,2);
           mm1=str.substr(5,1);
           
            //alert(mm1.substr(1,1));
        }
        else
        {
             mm1=str.substr(5,2);
             var dd1=str.substr(7,1);
        }
    }
    if(str.length==10)
    {
        dd1=str.substr(7,2);
        mm1=str.substr(5,2);
        
        //dd1=str.substr(5,2);
        //mm1=str.substr(7,2);
    }
    if(str.length==8)
    {
        dd1=str.substr(7,1);
        mm1=str.substr(5,1);
        
        //dd1=str.substr(5,1);
        //mm1=str.substr(7,1);
    }
 

    str=dd1+"/"+mm1+"/"+yy1;
     
      dt2=new Date(str);
      
      
      
    if(yy1<dt1.getFullYear())   //Year 
    {
        //alert("DisConnect Date must be Today or greater than Today");
        return false;
        
    }
    else if(yy1==dt1.getFullYear())
    {
        if(mm1<dt1.getMonth()+1)    //month (0 to 11)
       {
            //alert("DisConnect Date must be Today or greater than Today");
            return false;
       }
       else if(mm1==dt1.getMonth()+1)    //month (0 to 11)
        {
            if(dd1<dt1.getDate())   //Day (1 to 31)
            {
                //alert("DisConnect Date must be Today or greater than Today");
                return false;
            }
        }
    }
    return true;
}


//Check Zip code for format 99999-9999/99999/X9X-9X9/X9X 9X9/X9X9X9
function ValidateZipCode(number)
{
    if(number.length ==10)//99999-9999 
    {
        //check for format 99999-9999 
        if (number.indexOf("-")!=5) return false;
        else//check for numeric values
        {
            //we extract all the numbers from the entry, excluding the hyphens
            var num = number.substr(0,5);
            num = num.concat(number.substr(6,4));
            //now we check that only digits are entered
            var c;
            for( i=0; i<9; i++ )
            {
                //convert the i-th character to ascii code value
                c = num.charCodeAt(i); 
                if( (c<48) || (c>57) ) return false;
            }
            return true;   
        }
    }
    else if(number.length ==5)//99999
    {
        //check for no "-"
        if (number.indexOf("-")>0) return false;
        else//check for numeric values
        {
            //now we check that only digits are entered
            var c;
            for( i=0; i<5; i++ )
            {
                //convert the i-th character to ascii code value
                c = number.charCodeAt(i); 
                if( (c<48) || (c>57) ) return false;
            }
            return true;   
        }
        
    }
    else if(number.length ==7)//X9X-9X9
    {
        //check hyphen and Space 
        //check for format X9X-9X9 / X9X 9X9 
        if (number.indexOf("-")!=3 && number.indexOf(" ")!=3) return false;
        else//check for numeric and alpha character values
        {
            //we extract all the numbers from the entry, excluding the hyphens
            var num = number.substr(1,1);
            num = num.concat(number.substr(4,1));
            num = num.concat(number.substr(6,1));
            //now we check that only digits are entered
            var c;
            for( i=0; i<3; i++ )
            {
                //convert the i-th character to ascii code value
                c = num.charCodeAt(i); 
                if( (c<48) || (c>57) ) return false;
            }
            
            
            //we extract all the characters from the entry, excluding the hyphens
            var character = number.substr(0,1);
            character = character.concat(number.substr(2,1));
            character = character.concat(number.substr(5,1));
            //now we check that only alpha characters are entered
            var ch;
            for( i=0; i<3; i++ )
            {
                //convert the i-th character to ascii code value
                ch = character.charCodeAt(i); 
                if( (ch<65) || ((ch>90)&&(ch<97)) ||(ch>122)) return false;
            }
            return true; 
        }
    }
    else if(number.length ==6)//X9X9X9
    {
            //we extract all the numbers from the entry, excluding the hyphens
            var num = number.substr(1,1);
            num = num.concat(number.substr(3,1));
            num = num.concat(number.substr(5,1));
             //now we check that only digits are entered
             //alert(num);
            var c;
            for( i=0; i<3; i++ )
            {
                //alert(i);
                //convert the i-th character to ascii code value
                c = num.charCodeAt(i); 
                //alert(c);
                if( (c<48) || (c>57) ) return false;
            
            //we extract all the characters from the entry, excluding the hyphens
                var character = number.substr(0,1);
                character = character.concat(number.substr(2,1));
                character = character.concat(number.substr(4,1));
                //alert('char '+character);
                //now we check that only alpha characters are entered
                var ch;
                for( j=0; j<3; j++ )
                {
                    
                    //convert the i-th character to ascii code value
                    ch = character.charCodeAt(j); 
                    //alert(ch);
                    if( (ch<65) || ((ch>90)&&(ch<97)) ||(ch>122)) return false;
                }
             
            }
            return true; 
    }
    else
    {
        return false;
    }
    
    

        
}



//////END OF FILE/////////////
