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). Please reenter it now."
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 iDay = "This field must be a day number between 1 and 31.  Please reenter it now."
var iMonth = "This field must be a month number between 1 and 12.  Please reenter it now."
var iYear = "This field must be a 2 or 4 digit year number.  Please reenter it now."
var iDatePrefix = "The Day, Month, and Year for "
var iDateSuffix = " do not form a valid date.  Please reenter them now."

var phoneNumberDelimiters = "()- ";

function work()
{
    alert("Work@!");
}

function openPicWindow(url,name)
{
    alert("what up");
    new_window = window.open('http://www.swimthings.com/picView.jsp?tool='+url,name,width=300,height=300);
}


function resubmit(x)
{
    x.submit();
}

function doubleCheck(f)
{
    for( i = 0; i < f.length ; i++)
    {	alert(f.elements[i].name);
        if(isWhitespace(f.elements[i].value))
        {

            return false;
        }

    }
    if(
    isEmail(f.email) &&
    isStateCode(f.state,f.state.value)  &&
    checkUSPhone(f.phone) &&
    isCreditCard(f.ccCard) &&
    checkExpire(f.ccExpire))
    {
     	return true;
    }
    else
    {
    	return false;
    }
    
   

}

// check CC


//check Email
//function checkEmail(theField)
//{
//    alert("Made it to checkEmail");
//
//     if (!isEmail(theField.value, false))
//       return warnInvalid (theField, iEmail);
//    else return true;
//
//     alert("Made it out checkEmail");
//}
//
// 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;
    whitespace = "  ";

    // Is s empty?
//    if (isEmpty(s)) alert("Sorry, all fields are required");

    // 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.
    alert("Sorry, all fields are required");
    return true;
}

function isEmail (theField,s)
{


    if (isEmpty(s))
        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) != "@"))
    {
        warnInvalid(theField,"Sorry the address you entered isn't a valid email, please try again");
        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) != "."))

    {
        warnInvalid(theField,"Sorry the address you entered isn't a valid email, please try again");
        return false;
    }
    else return true;
}


//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)))
//}
//function checkCreditCard (theField)
//{   var cardType = getRadioButtonValue (radio)
//    var normalizedCCN = stripCharsInBag(theField.value, creditCardDelimiters)
//    if (!isCardMatch(cardType, normalizedCCN))
//       return warnInvalid (theField, iCreditCardPrefix + cardType + iCreditCardSuffix);
//    else
//    {  theField.value = normalizedCCN
//       return true
//    }
//}

function checkBoxes(f)
{

    if(f.shipping.value == "Select Address")
    {
        alert("Please choose the address you would like this order shipped to.");
        return false;

    }
    else if(f.billing.value == "Select Credit Card")
    {
        alert("Please choose the credit card that you would like this order billed to.");
        return false;
    }
    else
    {
        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 isCreditCard(field) {
  // Encoding only works on cards with less than 19 digits
    st = field.value;
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    warnInvalid(field,"Sorry, that number is not a valid Credit Card number");

} // END FUNCTION isCreditCard()

function isInteger (s)

{   var i;

    // 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 checkPassword(f,p)
{
    if(f.value.length < 1 || p.value.length < 1)
    {
        alert("To continue, you must enter and re-enter a password");
    }
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isUSPhoneNumber (s)
{

          if(isInteger(s) && s.length == 10)
          {
              return true;
          }
        else
        {
            return false;
        }

}

function checkUSPhone (theField)
{

      var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters);
      if(normalizedPhone == 0)
      {
          return true;
      }


       if (!isUSPhoneNumber(normalizedPhone))
       {


           return warnInvalid (theField, iUSPhone);
       }

       else
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }

}

function reformatUSPhone (USPhone)
{
    return (reformat (USPhone, "(", 3, ") ", 3, "-", 4))
}


function reformat (s)
{

    var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}
function checkExpire(field)
{

    s = field.value;
    expire = stripCharsInBag(s, "-/.() ");

    if(expire.length == 4 && isDigit(s))
    {
        first2 = expire.substring(0,2);
        last2 = expire.substring(2);


        if((first2 > 0 && first2 < 13) && (last2 > 02 && last2 < 13))
        {

            return true;
        }
        else
        {
        warnInvalid(field,"Invalid expiration date. The month must be between 01 and 12, or maybe your card is expired");
        return false;
        }

    }
    else if(expire.length == 0)
    {
        return true;
    }
    else
    {
        warnInvalid(field,"Invalid expiration date. please try again\nRemember! (2 digit month(MM) / (YY)2 digit year)");
        return false;
    }



}

function isStateCode(theField, s)
{
    s = s.toUpperCase();
    if ( (USStateCodes.indexOf(s) == -1) &&
             (s.indexOf(USStateCodeDelimiter) == -1) )
    {
        warnInvalid(theField,iStateCode);
        return false;
    }
}

function warnInvalid (theField, s)
{
    if(confirm(s))
    {
        theField.focus();
        theField.select();
        return false;

    }
    else
    {
        return true;
    }
}
//
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"