var reqd = new Array('fname','lname','address1','city','zip','phone_home','email','username','password','password2');
var reqd_name = new Array('First Name','Last Name','Address','City','ZIP code','Home Phone','E-mail Address','Username','Password','Password Verification');
var email_friend_reqd = new Array();
email_friend_reqd = [
                      ["your_name",    "Your Name"],
                      ["your_email",   "Your Email"],
                      ["friend_email", "Friend's Email"]
                    ];

function validateApp(form)
{
  for (var i=0; i<reqd.length; i++) {
    var elt = form.elements[reqd[i]];
    if (elt.value == null || elt.value == "") {
      alert(reqd_name[i] + " is a required field.");
      elt.focus();
      return false;
    }
  }

  if (form.elements['state'].selectedIndex == 0) {
    alert("State is a required field.");
    form.elements['state'].focus();
    return false;
  }

  if (form.elements['password'].value != form.elements['password2'].value) {
    alert("Password fields do not match.  Please re-enter.");
    form.elements['password'].value = '';
    form.elements['password2'].value = '';
    form.elements['password'].focus();
    return false;
  }

  // trim leading and trailing whitespace
  form.elements['username'].value = form.elements['username'].value.replace(/^\s+|\s+\$/,'');
  // inspect username for validity
  var username = form.elements['username'].value;
  var usernameOK = true;
  var errmsg = "";
  // invalid length?
  if (username.length < 5 || username.length > 20) {
      errmsg = "Valid usernames are between 5 and 20 characters long.";
      usernameOK = false;
  }
  // contains illegal chars?
  if (username.search(/[^A-Za-z0-9\.\-_\@]/) > -1) {
      errmsg = "Valid usernames contain no spaces and consist only of letters, numbers and the following four symbols: ._-@";
      usernameOK = false;
  }
  // must start with a letter
  if (username.search(/^[^A-Za-z]/) == 0) {
      errmsg = "Valid usernames must begin with a letter";
      usernameOK = false;
  }
  if (!usernameOK) {
      alert(errmsg);
      form.elements['username'].focus();
      return false;
  }

  ///checkbox removed (added back)
  if (form.elements['terms_agree'].checked == false) {
    alert("You must agree to program conditions before proceeding.");
    form.elements['terms_agree'].focus();
    return false;
  }

  return true;
}

function activateProFields(checkbox) {
  if (checkbox.checked) {
    checkbox.form.elements.uspta_id.readOnly = false;
    checkbox.form.elements.ptr_id.readOnly = false;
    checkbox.form.elements.uspta_id.setAttribute('class','enabled');
    checkbox.form.elements.ptr_id.setAttribute('class','enabled');
  } else {
    checkbox.form.elements.uspta_id.readOnly = true;
    checkbox.form.elements.ptr_id.readOnly = true;
    checkbox.form.elements.uspta_id.value = '';
    checkbox.form.elements.ptr_id.value = '';
    checkbox.form.elements.uspta_id.setAttribute('class','disabled');
    checkbox.form.elements.ptr_id.setAttribute('class','disabled');
  }
}

function showMen() {
  var men = document.getElementById('men');
  var women = document.getElementById('women');
  men.style.display = 'block';
  women.style.display = 'none';
}

function showWomen() {
  var men = document.getElementById('men');
  var women = document.getElementById('women');
  men.style.display = 'none';
  women.style.display = 'block';
}

function showEmailFriend() {
  document.getElementById('email_friend').style.display = 'block';
}

function validateEmailFriend(form) {

    for(var i=0;i<email_friend_reqd.length;i++){

        var elt = form.elements[email_friend_reqd[i][0]];
        if (elt.value == null || elt.value == "") {
            alert(email_friend_reqd[i][1] + " is a required field.");
            elt.focus();
           return false;
        }
    }

    return true;
}

function validateAddToCart(form) {
    // force user to select size
    var f = document.forms.product_form;

    for(i=0; i<f.elements.length; i++){
        if(f.elements[i].type == "select-one"){
            var container = grokSizesDiv(f.elements[i]);
            if (typeof(container) != 'undefined' && container.style.visibility == 'hidden') {
                continue; // skip this one
            }
            if(f.elements[i].options[f.elements[i].selectedIndex].text == "Select a Size"){
                alert("Please select a size for each product!");
                f.elements[i].focus();
                return false;
            }
       }
    }
    return true;
}

function grokSizesDiv(elt) {
    var x = elt;
    while (x.parentNode) {
        x = x.parentNode;
        if (x.id && x.id.indexOf("sizes") == 0) {
            return x;
        }
    }
}

function validateState(form) {
  // force user to select size
  var f = document.forms.checkout;
  if (f == null) { f = document.forms.edit_account; }
  var state = f.state;

  if (typeof(state.options) != 'undefined') {
    if (state.options.length > 2 && state.selectedIndex < 1) {
      alert('Please select a state before proceeding.');
      state.focus();
      return false;
    }
  } else if (state.value == "" || state.value == "--") {
    alert('Please provide a value for state/province');
    state.focus();
    return false;
  }
  var b_state = f.b_state;
  var b_street = f.b_address1;
  var bfname = f.b_fname;
  var blname = f.b_lname;
  if (b_street.value.length == 0 || bfname.value.length == 0 || blname.value.length == 0) {
    if (b_street.value.length > 0 || bfname.value.length > 0 || blname.value.length > 0) {
      alert('Please leave billing fields blank if billing and shipping address are the same.\nIf you are providing a separate billing address, please fill out all fields.');
      bfname.focus();
      return false;
    }
  }
  if (b_street.value.length > 3 && typeof(b_state.options) != 'undefined') {
    if (b_state.options.length > 2 && b_state.selectedIndex < 1) {
      alert('Please select a state for your billing address.');
      b_state.focus();
      return false;
    } else if (f.b_zip.value.length < 5) {
      alert('Please correct your billing zip code.');
      f.b_zip.focus();
      return false;
    }
  }
  // enforce area code
  var phone_day = f.phone_day;
  if (phone_day.value.length > 1 && phone_day.value.length < 10) {
    alert('Phone number must include area code.');
    phone_day.focus();
    return false;
  }
  var phone_night = f.phone_night;
  if (phone_night.value.length > 1 && phone_night.value.length < 10) {
    alert('Phone number must include area code.');
    phone_night.focus();
    return false;
  }
  if (phone_day.value.length < 1 && phone_night.value.length < 1) {
    alert('At least one phone number is required.');
    phone_day.focus();
    return false;
  }
  var street = f.address1;
  if (street.value.length > 22) {
    alert('Shipping label limits street address to 22 chars wide.  Please make use of the second line and/or abbreviate.');
    street.focus();
    return false;
  }
  var street2 = f.address2;
  if (street2.value.length > 22) {
    alert('Street address too long.  Max 22 chars per line.');
    street2.focus();
    return false;
  }
  if (b_street.value.length > 22) {
    alert('Shipping label limits street address to 22 chars wide.  Please make use of the second line and/or abbreviate.');
    b_street.focus();
    return false;
  }
  var bstreet2 = f.b_address2;
  if (bstreet2.value.length > 22) {
    alert('Street address too long.  Max 22 chars per line.');
    bstreet2.focus();
    return false;
  }
  if (bfname.value.length > 0) {
    if (f.b_city.value.length == 0) {
      alert('Please provide a complete billing address with city.');
      f.b_city.focus();
      return false;
    }
  }
  return true;
}

function showWarning() {
  var warningImg;
  if (document.all) {
    warningImg = document.all['blinkwarn82'];
  } else {
    warningImg = document.images['blinkwarn82'];
  }
  warningImg.src = '/kswiss/images/kswiss_buttons/please_wait.gif';
  alert('Click OK to process your order.\nPlease be patient, credit card processing\nmay take up to three minutes.');
}
