
//special functions Start 
var whitespace = " \t\n\r";
var defaultEmptyOK = true;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;
var digitsInUSPhoneNumber = 10;
 
// 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;
}
// 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 isNumber(s)//checks for number in decimals 
{

	// our Regular Expression
	 var regex=/^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	// do the comparison, if we have a match
	//return true on error
	return !regex.test(s);
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

//checks for Maxlength being exceeded or not
//returns true on exceed
//false otherwise
function countit(str,intLength)
{
	return((str).toString().length>intLength)
}


// isSSN (STRING s [, BOOLEAN emptyOK])
// 
// isSSN returns true if string s is a valid U.S. Social
// Security Number.  Must be 9 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isSSN (s)
{   if (isEmpty(s)) 
       if (isSSN.arguments.length == 1) return defaultEmptyOK;
       else return (isSSN.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInSocialSecurityNumber)
}




// isUSPhoneNumber (STRING s [, BOOLEAN emptyOK])
// 
// isUSPhoneNumber returns true if string s is a valid U.S. Phone
// Number.  Must be 10 digits.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isUSPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}




// isInternationalPhoneNumber (STRING s [, BOOLEAN emptyOK])
// 
// isInternationalPhoneNumber returns true if string s is a valid 
// international phone number.  Must be digits only; any length OK.
// May be prefixed by + character.
//
// NOTE: A phone number of all zeros would not be accepted.
// I don't think that is a valid phone number anyway.
//
// NOTE: Strip out any delimiters (spaces, hyphens, parentheses, etc.)
// from string s before calling this function.  You may leave in 
// leading + character if you wish.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isInternationalPhoneNumber (s)
{   if (isEmpty(s)) 
       if (isInternationalPhoneNumber.arguments.length == 1) return defaultEmptyOK;
       else return (isInternationalPhoneNumber.arguments[1] == true);
    return (isPositiveInteger(s))
}




// 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.  
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

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)))
}
   // isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   
   
    // is s whitespace?
    if (isWhitespace(s)) return true;
    
    // 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 true;
    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 true;
    else return false;
}

//Start of Date functions 

function validateDate( strValue ) 
{
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return true; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[0],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return true;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return false; //found in lookup table, good date
    }
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return false; //Feb. had valid number of days
  }
  return true; //any other values, bad date
}

function buildDate(strDate)
{
	var objRegExp = /^\d{4}$/
	var mon
	var year
	var date1
	//check to see if in correct format
	if(!objRegExp.test(strDate))
	{
		return(true)
	}
	mon=strDate.substring(0,2)
	year=strDate.substring(2,4)
	date1=mon+"/"+"01"+"/"+year
	if(validateDate(date1))
	{
		return(true)
	}
	
	return(cmpDate(mon,year))
  	
}
function cmpDate(month,year)
{
	var today=new Date()
	if((month<today.getMonth()+1 && year==today.getYear()) || year>today.getYear())return(true)
		else return(false)
}

function cmpDateToday(formField)
{
	var result = true;
	var formValue = formField.value;
	var ans = false;
	if (result && (formField.value.length>0))
 	{
 		var elems = formValue.split("/");
 		
 		result = (elems.length == 3); // should be two components
 		
 		
 		if (result)
 		{
 			var month = parseInt(elems[0],10);
 			var year = parseInt(elems[2],10);
 			if (elems[2].length == 2)
 				year += 2000;
 			var now = new Date();
 			
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
 			
 			ans = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
 			
 			//result = allDigits(elems[0]) && (month > 0) && (month < 13) &&
			//		 allDigits(elems[1]) && ((elems[1].length == 2) || (elems[1].length == 4));
 		}
 		
  		if (ans)
		{	result = false;
 			alert('The credit card expiry date has expired.');
			formField.focus();
		}
	} 
	
	return (!result);
}

//End Date functions
// Get checked value from radio button.

function getRadioButtonValue(radio)
{  
	var result=""
	for (var i = 0; i < radio.length; i++)
    {   
		if (radio[i].checked) 
		{ 
			result=radio[i].value
			break 
		}
    }
	return result
}

//Credit card check
function isValidCreditCardNumber(formField,ccType)
{

	var result = true;
 	var ccNum = formField.value;
	//alert(ccNum)
	if (result && (formField.value.length>0))
 	{ 
 		if(!isInteger(ccNum))
 		{
 			/*alert('Please enter only numbers (no dashes or spaces) for Credit card number');
			formField.focus();*/
			result = false;
		}	

		if (result)
 		{ 
 			
 			if (!LuhnCheck(ccNum) || !validateCCNum(ccType,ccNum))
 			{
 				/*alert('Please enter a valid card number ');
				formField.focus();*/
				result = false;
			}	
		} 

	} 
	
	return result;
}

function LuhnCheck(str) 
{
  var result = true;

  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
    
  return result;
}
function validateCCNum(cardType,cardNum)
{
	var result = false;

	cardType = cardType.toUpperCase();
	
	var cardLen = cardNum.length;
	var firstdig = cardNum.substring(0,1);
	var seconddig = cardNum.substring(1,2);
	var first4digs = cardNum.substring(0,4);
	//alert("cardtype="+cardType)
	switch (cardType)
	{
		case "VISA"://Visa
			result = ((cardLen == 16) || (cardLen == 13)) && (firstdig == "4");
			break;
		case "AMEX"://Amex
			var validNums = "47";
			result = (cardLen == 15) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		case "MASTERCARD"://Master
			var validNums = "12345";
			result = (cardLen == 16) && (firstdig == "5") && (validNums.indexOf(seconddig)>=0);
			break;
		case "DISCOVER":
			result = (cardLen == 16) && (first4digs == "6011");
			break;
		case "DINERS":
			var validNums = "068";
			result = (cardLen == 14) && (firstdig == "3") && (validNums.indexOf(seconddig)>=0);
			break;
		
	}
	return result;
}
//Function to display error messages
//msgOption =0 for Blank error message
function displayErrorMessage(formName,ctrlName,msgOption,strFieldDescription)
{

	//Message Array 
	var arrMessages=new Array()
	arrMessages[0]=strFieldDescription+ " cannot be empty"
	arrMessages[1]=" Invalid "+strFieldDescription
	arrMessages[2]=" Select "+strFieldDescription
	arrMessages[3]="Credit card expired"
	arrMessages[4]=strFieldDescription+ " cannot be empty,if not applicable enter 'None'" 
	arrMessages[5]=" Invalid "+strFieldDescription +". Please enter a valid Credit Card number "
	arrMessages[6]=" Password does not match "+strFieldDescription
	arrMessages[7]=" Maximum length exceeded for "+strFieldDescription + "\n Please reduce the number of words."
	//End of Message array
	
	alert(arrMessages[msgOption])
	//for radio buttons   focus on first choice
	if(ctrlName.length>0)
	{
		if(ctrlName.type=="select" || ctrlName.type=="select-one")
		{
			ctrlName.focus();
			
		}
		else
		{
			ctrlName[0].focus();
		}
	}
	else
	{
		ctrlName.focus();
	}
	return(false);
}
//Special Functions end
function ComparePassword(strPass1, strPass2)
	{
	if (strPass1!=strPass2) return true;
	else
	return false;
	}
