function checkDistContact() {
  var status = document.getElementById('distributor_has_contact').checked;
  var tbody = document.getElementById('distContact');
  tbody.style.display = (status) ? "" : "none";
  
  return;
}

// ---- PNG Fix ---------------------------------------------------------------------- //
function correctPNG() {
  var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var isIE6 = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7.0);

	if (isIE6) {
   for(var i=0; i<document.images.length; i++) {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	     }
     }
  }
}


function alphaBackgrounds(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7.0);
	for (i=0; i<document.all.length; i++){
		var bg = document.all[i].currentStyle.backgroundImage;
		if (itsAllGood && bg){
			if (bg.match(/\.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
				document.all[i].style.backgroundImage = "url('images/blank.gif')";
			}
		}
	}
}
// ----------------------------------------------------------------------------------- //


// ---- admin product/add toggle ----------------------------------------------------- //
function toggleCategory() {
  var cat = document.getElementById('product_product_category_id').value;
  var tbody = document.getElementById('wine-details');
  var disp = (cat==1 || cat=='') ? "" : "none";
  for (i=0; i<tbody.rows.length; i++) {
    tbody.rows[i].style.display = disp;
  }
  return;
}

// ----------------------------------------------------------------------------------- //
function setCartHat(x){
  hatNum = document.getElementById('hat_color').value;
  act = x.action;
  act_sub = act.substring(0,act.length-2)
  x.action = act_sub+hatNum;
  return;
}

function setCartTshirt(x){
  teeNum = document.getElementById('tshirt_color').value;
  teeSize = document.getElementById('tshirt_size').value;
  teeVal = parseInt(teeNum) + parseInt(teeSize);
  act = x.action;
  act_sub = act.substring(0,act.length-2);
  x.action = act_sub+teeVal;
  return;
}

function checkBillingZip() {
	var bill_zipcode = document.getElementById('bill_to_address_zip');
    if (!isInteger(bill_zipcode.value) || bill_zipcode.value.length!=5) {
		alert("Please enter a valid zip code");
		bill_zipcode.value='';
		bill_zipcode.focus();
		return;
	} else {
		return;
	}
}

function checkShippingZip() {
	var ship_zipcode = document.getElementById('ship_to_address_zip');
    if (!isInteger(ship_zipcode.value) || ship_zipcode.value.length!=5) {
		alert("Please enter a valid zip code");
		ship_zipcode.value='';
		ship_zipcode.focus();
		return;
	} else {
		return;
	}
}

// ---- form validation -------------------------------------------------------------- //
function validateCheckout() {
  var shipSame = document.getElementById('order_ship_to_same');
  if (!validBilling()) {
		return false;
  } else if (!shipSame.checked && !validShipping()) {
		return false;
  } else if (!validCreditCard()) {
		return false;								
  } else {
		return true;
  }
}

function validBilling() {
  var bill_salutation = document.getElementById('bill_to_person_salutation').value;
  var bill_first_name = document.getElementById('bill_to_person_first_name').value;
  var bill_last_name = document.getElementById('bill_to_person_last_name').value;
  var bill_email1 = document.getElementById('bill_to_person_email').value;
  var bill_email2 = document.getElementById('bill_to_person_email_confirmation').value;
  var bill_phone1 = document.getElementById('bill_to_person_area_code').value;
  var bill_phone2 = document.getElementById('bill_to_person_phone_1').value;
  var bill_phone3 = document.getElementById('bill_to_person_phone_2').value;
  var bill_street1 = document.getElementById('bill_to_address_street_1').value;
  var bill_street2 = document.getElementById('bill_to_address_street_2').value;
  var bill_city = document.getElementById('bill_to_address_city').value;
  var bill_state = document.getElementById('bill_to_address_state_id').value;
  var bill_zip = document.getElementById('bill_to_address_zip').value;
  if(empty(bill_salutation)){
    alert("Please select a billing salutation");
    return false;
  } else if (!validLength(bill_first_name, "billing first name")){
    return false;
  } else if (!validLength(bill_last_name, "billing last name")){
    return false;
  } else if (!validEmail(bill_email1, bill_email2, "Billing")){
    return false;
  } else if (!validPhone(bill_phone1, bill_phone2, bill_phone3)){
    return false;
  } else if (!validLength(bill_street1, "billing street address")){
    return false;
  } else if (!validLength(bill_city, "billing city")){
    return false;
  } else if (empty(bill_state)){
    alert("Please select a billing state");
    return false;
  } else if (!validZip(bill_zip, "billing zip")){
    return false;
  } else {
    return true;
  }
  return false;
}

function validShipping () {
  var ship_salutation = document.getElementById('ship_to_person_salutation').value;
  var ship_first_name = document.getElementById('ship_to_person_first_name').value;
  var ship_last_name = document.getElementById('ship_to_person_last_name').value;
  var ship_email1 = document.getElementById('ship_to_person_email').value;
  var ship_email2 = document.getElementById('ship_to_person_email_confirmation').value;
  var ship_phone1 = document.getElementById('ship_to_person_area_code').value;
  var ship_phone2 = document.getElementById('ship_to_person_phone_1').value;
  var ship_phone3 = document.getElementById('ship_to_person_phone_2').value;
  var ship_street1 = document.getElementById('ship_to_address_street_1').value;
  var ship_street2 = document.getElementById('ship_to_address_street_2').value;
  var ship_city = document.getElementById('ship_to_address_city').value;
  var ship_state = document.getElementById('ship_to_address_state_id').value;
  var ship_zip = document.getElementById('ship_to_address_zip').value;
  if(empty(ship_salutation)){
    alert("Please select a shipping salutation");
    return false;
  } else if (!validLength(ship_first_name, "shipping first name")){
    return false;
    } else if (!validLength(ship_last_name, "shipping last name")){
    return false;
  } else if (!validEmail(ship_email1, ship_email2, "Shipping")){
    return false;
  } else if (!validPhone(ship_phone1, ship_phone2, ship_phone3)){
    return false;
  } else if (!validLength(ship_street1, "shipping street address")){
    return false;
  } else if (!validLength(ship_city, "shipping city")){
    return false;
  } else if (empty(ship_state)){
    alert("Please select a shipping state");
    return false;
  } else if (!validZip(ship_zip, "shipping zip")){
    return false;
  } else {
    return true;
  }
  return false;
}

function validCreditCard() {
	var cc_type = document.getElementById('order_credit_card_type').value;
	var cc_num = document.getElementById('order_credit_card_number').value;
	var cc_cid = document.getElementById('order_credit_card_code').value;
	var cc_exp_month = document.getElementById('date_month').value;
	var cc_exp_year = document.getElementById('date_year').value;

	if(empty(cc_type)) {
		alert('Please select a credit card type');
		return false;
	} else if(empty(cc_num)) {
		alert('Please enter a credit card number');
		return false;
	} else if(!validCCnum(cc_type,cc_num)) {
		alert('Please enter a valid credit card number');
		return false;
	} else if (!validCClength(cc_type, cc_num.length)) {
		alert('Please enter a credit card number of correct length');
		return false;
	} else if(empty(cc_cid)) {
		alert('Please enter a CID');
		return false;
	} else if(empty(cc_exp_month)) {
		alert('Please select a credit card expiration month');
		return false;
	} else if(empty(cc_exp_year)) {
		alert('Please select a credit card expiration year');
		return false;
	} else if (!validCCdate(cc_exp_month, cc_exp_year)) {
		alert('Please enter a valid credit card expiration date');
		return false;
	} else {
		return true;
	}
}

// form helpers //
function validLength(x, t){
  if (x.length < 2) {
    alert("Please enter a valid "+t)
    return false;
  } else {
    return true;
  }
}

function validZip(x, t) {
  if ((x.length != 5) || !isInteger(x)) {
    alert("Please enter a valid "+t)
    return false;
  } else {
    return true;
  }
}

function validPhone (p1,p2,p3) {
	if ((p1.length!=3 && p1.length>0) || (p2.length!=3 && p2.length>0) || (p3.length!=4 && p3.length>0)) {
	  alert("Please enter a valid phone number");
	  return false;
	} else if (!isInteger(p1) || !isInteger(p2) || !isInteger(p3) ){
	  alert("Please enter a valid phone number");
	  return false;
	} else {
	  return true;
	}
}

function validEmail(email1, email2, t) {
  if (!checkemail(email1)) {
    alert("Please enter a valid email");
    return false;
  } else if (email1 != email2) {
    alert (t+" email addresses do not match, please confirm")
    return false;
  } else {
    return true;
  }
}

function checkemail(str) {
	var testresults=true;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var emailPat=/^(.+)@(.+)$/; 
	var matchArray=str.match(emailPat); 
	if (matchArray==null) {	testresults=false; }
	if (!filter.test(str)) { testresults=false; }
	return testresults;
}

function empty(x){
  return (x == '');
}

function isInteger(s) {   
    for (i = 0; i < s.length; i++) {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function validCClength(cc_type, num_length) {
	if (cc_type == 'visa') { // Visa
		return (num_length==13 || num_length==16);
	} else if (cc_type == 'master') { // Mastercard
		return (num_length==16);
	} else if (cc_type == 'american_express') { // Amex
		return (num_length==15);
	} else {
		return false;
	}
	return;
}

function validCCnum(cc_type, cc_num) {
	if (cc_type == 'visa') { // Visa
		return (cc_num.substr(0,1) == 4);
	} else if (cc_type == 'master') { // Mastercard
		return (cc_num.substr(0,2)>=51 && cc_num.substr(0,2)<=55);
	} else if (cc_type == 'american_express') { // Amex
		return (cc_num.substr(0,2)==34 || cc_num.substr(0,2)==37);
	} else {
		return false;
	}
	return;
}

function validCCdate(cc_month, cc_year) {
	var today = new Date();
	var tmonth = today.getMonth() + 1;
	var tyear = today.getFullYear();
	if((cc_year < tyear) || ((tyear == cc_year) && (cc_month < tmonth))){
		return false;
	} else {
		return true;
	}
}

// ---- birthdate form validation -------------------------------------------------------------- //
function validateBirthdate() {
	var birth_year = document.getElementById('date_year').value;
	var birth_month = document.getElementById('date_month').value;
	var birth_day = document.getElementById('date_day').value;
    
	if(empty(birth_month)) {
		alert('Please select a month');
		return false;
	} else if(empty(birth_day)) {
		alert('Please select a day');
		return false;
	} else if(empty(birth_year)) {
		alert('Please select a year');
		return false;
	} else {
		return true;
	}
}
