// methods for client-side data validation
function isNumeric(checkStr)
{
    return checkStr.match(/^\d*$/);
}

function stripNonDigit(value)
{
    strippedValue = "";
    for (var i = 0; i < value.length; i++)
    {
        if (isNumeric(new String(value.charAt(i))))
            strippedValue += value.charAt(i);
    }

    return strippedValue;
}

function stripPeriodComma(value)
{
    var strippedValue = "";
    var ch;

    for (var i = 0; i < value.length, ch = value.charAt(i); i++)
    {
        if (ch != ',' && ch != '.')
            strippedValue += ch;
    }

    return strippedValue;
}

function isValidEmailChar(ch)
{
	  return ch != ' ' && ch != '!' && ch != '#' && ch != '$' && 
		    ch != '%' && ch != '^' && ch != '&' && ch != '*' &&
		    ch != '(' && ch != ')' && ch != '{' && ch != '}' && ch != '|' && 
		    ch != '\\' && ch != '=' && ch != '~' && ch != '`' && ch != '/';
}

function isValidEmail(email)
{
    var foundAt = -1;
    var foundDot = -1;

	  // look for invalid characters
    for (var i = 0; i < email.length; i++)
    {
		    if (! isValidEmailChar(email.charAt(i)))
			      return false;
	  }
    
    // verify xx@xx.xx pattern
    for (var i = 0; i < email.length; i++)
    {
        if (foundAt == -1)
        {
            if (email.charAt(i) == '@')
            {
                if (i > 0)
                    foundAt = i;
                else
                    return false;
            }
        }
        else if (foundDot == -1)
        {
            if (email.charAt(i) == '.')
            {
                if (i > foundAt + 1)
                    foundDot = i;
                else
                    return false;
            }
            else if (email.charAt(i) == '@')
                return false;
        }
        else
        {
            if (i > foundDot + 1)
                return true;
            else if (email.charAt(i) == '@')
                return false;
        }
    }
    
    return false;
}

function isValidUserID(userID)
{
    return userID.length >= 5 && userID.match(/^[A-Za-z0-9]{5,20}$/);
}

function isValidPassword(password)
{
    return password.length >= 6 && password.match(/^[A-Za-z0-9]{6,20}$/);
}

function isValidCtmNumber(ctmNumber)
{
    return ctmNumber.length <= 9 && ctmNumber.length >= 2 && isNumeric(ctmNumber);
}

function isCountryUsa(country)
{
    return country.length == 0;
}

function isCountryCanada(country)
{
    return country == "CAN";
}
    
function isValidState(country, state)
{
    return (!isCountryUsa(country) && !isCountryCanada(country)) || state.length == 2;
}

function isValidZip(country, zipCode)
{
    return !isCountryUsa(country) || isValidUsZip(zipCode);
}

function isStateRequired(country)
{
    return isCountryUsa(country) || isCountryCanada(country);
}

function isZipRequired(country)
{
    return isCountryUsa(country);
}

function isValidUsZip(zipCode)
{
    zipCode = stripNonDigit(zipCode);
    return (zipCode.length == 5 || zipCode.length == 9) && zipCode != "00000"
        && zipCode != "99999";
}

function isValidPhone(country, zip, phone)
{
    return !isPhoneUsa(country, zip) || isValidUsPhone(phone);
}

function isPhoneUsa(country, zip)
{   // determine if address should have a domestic phone number
	  if (! isCountryUsa(country))
		    return false;
		
	  // look for APO/FPO addresses
    zip = stripNonDigit(zip);
	  if (zip.length == 5)
		    zip *= 10000;
		    
	  // return true if zip code not in APO/FPO ranges
	  return !((zip >= 90000000 && zip <= 98999999) ||
		    (zip >= 340000000 && zip <= 340999999) || 
		    (zip >= 962000000 && zip <= 966999999));
	}

function isValidUsPhone(phone)
{
    return stripNonDigit(phone).length == 10;
}

function radioSelected(radio)
{   // return the value of the radio button that is selected, null if none
    for (var i = 0; i < radio.length; i++)
    {
        if (radio[i].checked)
            return radio[i].value;
    }
    return null;
}

function badEmailNotice()
{
    if (parseInt(navigator.appVersion, 10) >= 4)
		{
        window.open("../badEmailNotice.aspx", "InvalidEmail", 
            "height=200,width=400,scrollbars=1,resizable=1");
        return true;
    }
    return false;
}

function coppaNotice()
{
    if (parseInt(navigator.appVersion, 10) >= 4)
        window.open("../coppaNotice.aspx", "COPPANotice", 
            "height=450,width=400,scrollbars=1,resizable=1");
}

function emailNotice()
{
    if (parseInt(navigator.appVersion, 10) >= 4)
        window.open("emailNotice.aspx", "UndeliverableEmailNotice", 
            "height=200,width=400,scrollbars=1,resizable=1");
}
    
function isValidCardNumber(cardNumber)
{
    return cardNumber.length == 13 || cardNumber.length == 16
         || cardNumber.length == 15;
}
	
function isValidCardMonth(cardMonth)
{
    return isNumeric(cardMonth) && parseInt(cardMonth, 10) >= 1 && 
        parseInt(cardMonth, 10) <= 12;
}
    
function isValidCardYear(cardYear)
{
    return isNumeric(cardYear) && parseInt(cardYear, 10) >= 1 && 
        parseInt(cardYear, 10) <= 30;
}
    
function dateDiff(date1, date2)
{   // determine the number of days between two dates
    formattedDate1 = new Date(date1);
    formattedDate2 = new Date(date2);
    var totalMSeconds = Math.abs(formattedDate1.getTime() - formattedDate2.getTime());
    return totalMSeconds / 86400000;
}
