﻿var isNN = (navigator.appName.indexOf("Netscape") != -1);

function autoTab(input, len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter, keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input) + 1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length) {
      if(arr[index] == ele)
        found = true;
      else
        index++;
    }
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1) {
      if (input.form[i] == input)
        index = i;
      else
        i++;
    }
    return index;
  }
  return true;
}

function ConfirmContinue() {
  var chargeAmountString = getCurrentChargeLabel().firstChild.nodeValue;
  // remove the dollar sign and parse current charge
  var chargeAmount = parseFloat(chargeAmountString.substring(1, chargeAmountString.length))
  if (chargeAmount <= 0.0) {
    return true;
  }  
  return confirm("Your credit card will be charged " + chargeAmountString);
}

function openNewWindow() {
  // Any page that uses this method, must impl the getTheme() and getCompanyName() methods
  // something like this - function getTheme() { return '<%= Page.Theme %>'; }
  // The theme variable is used to locate the print logo and the companyName shows the company name based on the theme.
  var theme = getTheme();
  var companyName = getCompanyName();
  infoWindow = window.open("", "purchaseConfirmationWindow", "toolbar=no,resizable=yes,menubar=yes,scrollbars=yes,top=250,left=250,location=no");
  infoWindow.document.write("<html>\n<head>\n<title>Purchase Confirmation</title>\n<link href='css/printerfriendly.css' type='text/css' rel='stylesheet' />\n</head>\n<body>");
  infoWindow.document.write("\n<div style='float: right; padding-right: 25px;'><a href='javascript:window.print();'>Print This Page</a></div>");
  infoWindow.document.write("\n<div style='float: left; padding: 0px 10px 20px 10px;'><img width='175' height='100' src='App_Themes/" + theme + "/images/Print_Logo.jpg' alt='' /></div>");
  infoWindow.document.write("\n<div style='padding-bottom:20px;'>\n<div style='font-weight: bold;'>" + companyName + "</div>\n<div>Phone: 800-322-6090</div>\n<div>E-mail: <a href='mailto:helpdesk@tvicorporate.com'>helpdesk@tvicorporate.com</a></div>\n<div>Office Hours: M – F, 8:30 am - 6:00 pm, Central Time</div>\n</div>");
  infoWindow.document.write("\n<div style='clear: left;'></div>");
  infoWindow.document.write("\n<hr/>");
  infoWindow.document.write("\n<div class='bold' style='text-align: center;'>Purchase Receipt</div>");
  infoWindow.document.write("\n<hr/>");
  infoWindow.document.write(document.getElementById('contact').innerHTML);
  infoWindow.document.write("</body>\n</html>");
  infoWindow.document.bgColor="white";
  infoWindow.document.close();
  infoWindow.focus();
}

function checkPromoCodeIsValid(sender, args) {
  if (getPromoCodeCustomValidator().style.display == 'inline') {
    args.IsValid = false;
  }
  else {
    args.IsValid = true;
  }
}

function checkDifferences(sender, args) {
  var Q1 = getQuestion1List().value;
  var Q2 = getQuestion2List().value;
  var Q3 = getQuestion3List().value;

  var Q1Validator = getQuestion1CustomValidator();
  var Q2Validator = getQuestion2CustomValidator();
  var Q3Validator = getQuestion3CustomValidator();

  args.IsValid = true;

  switch (sender.id.substring(sender.id.lastIndexOf('_') + 1, sender.id.length)) {
    case 'Q1':
      displayErrors(Q1, Q2, Q3, Q2Validator, Q3Validator, args);
      break;
    case 'Q2':  
      displayErrors(Q2, Q1, Q3, Q1Validator, Q3Validator, args);
      break;
    case 'Q3':
      displayErrors(Q3, Q1, Q2, Q1Validator, Q2Validator, args);
      break;
    default:
      args.IsValid = true;
      break;
  }
}

function displayErrors(original, otherOne, otherTwo, otherValidatorOne, otherValidatorTwo, args) {
  if (otherOne == otherTwo) {
    otherValidatorOne.style.display = 'inline';
    otherValidatorTwo.style.display = 'inline';         
  }
  else {
    otherValidatorOne.style.display = 'none';
    otherValidatorTwo.style.display = 'none';         
  }

  if (original == otherOne) {
    args.IsValid = false;
    otherValidatorOne.style.display = 'inline';
  }
  if (original == otherTwo) {
    args.IsValid = false;
    otherValidatorTwo.style.display = 'inline';
  }
}

function mutuallyExclusive(which, excludeAllContaining) {
  if (which.checked) {
    var inputElements = document.getElementsByTagName('input');
    for (var i = 0; i < inputElements.length; i++) {
      if (inputElements[i].getAttribute('type') == 'checkbox' && inputElements[i].id.indexOf(excludeAllContaining) != -1) {
        inputElements[i].checked = false;
      }
    }
    which.checked = true;
  }
}

function keepBoxChecked(checkbox) {
  checkbox.checked = true;
}

function validatePayment() {
  var contactInfoValidate = Page_ClientValidate('ContactInfo');
  var creditCardInfoValidate = true;
  if (!getCreditCardCheckbox().checked) {
    creditCardInfoValidate = Page_ClientValidate('CreditCardInfo');
  }

  // If at least one group didn't validate, validate the entire page.
  // If you don't do this, only the last group that failed validation
  // shows the validator's error message.
  
  if (!(contactInfoValidate && creditCardInfoValidate)) {
    return Page_ClientValidate();
  }

  // The fields validate
  return true;
}

function validateRegistration() {
  var contactInfoValidate = Page_ClientValidate('ContactInfo');
  var count = 0;
  // Check each group to see if it validates.
  var phoneInfoValidate = true;
  if (getPhoneCheckbox().checked) {
    phoneInfoValidate = Page_ClientValidate('PhoneInfo');
    count++;
  }
  var mailInfoValidate = true;
  if (getMailCheckbox().checked) {
    mailInfoValidate = Page_ClientValidate('MailInfo');
    count++;
  }
  
  // If at least one group didn't validate, validate the entire page.
  // If you don't do this, only the last group that failed validation
  // shows the validator's error message.
  if (!(contactInfoValidate && phoneInfoValidate && mailInfoValidate)) {
    return Page_ClientValidate();
  }

  // At least one checkbox is checked.
  if (count < 1) {
    alert('You must select at least one contact method.');
    return false;
  }
  
  // The fields validate and at least one checkbox is checked.
  return true;
}

function togglePhone() {
  toggleDiv(getPhoneDiv(), getPhoneCheckbox().checked);
}

function toggleMail() {
  toggleDiv(getMailDiv(), getMailCheckbox().checked);
}

function toggleCreditCard() {
  toggleDiv(getCreditCardDiv(), !getCreditCardCheckbox().checked);
}

function toggleDiv(div, isChecked) {
  if (isChecked) {
    div.className = 'show';
  }
  else {
    div.className = 'hide';
  }
}

function toggleHelp(divId) {
  var div = $(divId);
  if (div.style.display == 'none') {
    div.style.display = 'block';
  }
  else {
    div.style.display = 'none';
  }
}
