// ccart.js
// version 11.04

//function AddCss() {
  var l=document.createElement("link");
  l.setAttribute("type","text/css");
  l.setAttribute("rel","stylesheet");
  l.setAttribute("href","css/ccart.css");
  l.setAttribute("media","screen");
  document.getElementsByTagName("head")[0].appendChild(l);
//}

function ccartDisableEnterKey(e) {
  var key;     
  if(window.event)
    key = window.event.keyCode; //IE
  else
    key = e.which; //firefox     

  return (key != 13);
}

function ccartCreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function ccartReadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function ccartEraseCookie(name) {
	alert(name);
	ccartCreateCookie(name,"",-1);
}

function ccartShowNumItems(num) {
	if (num == 0) {
		document.getElementById('ccartnumitems').innerHTML='';
	}
	else {
		tnum = " (" + num + ")";
		document.getElementById('ccartnumitems').innerHTML=' (' + num + ')';
	}
}

function ccartCountryChange(frm) {
	countrycode = document.getElementById('country_code').value;
	if (countrycode == '00') {
		document.getElementById('province_row').style.visibility="collapse";
		document.getElementById('state_row').style.visibility="visible";
		document.getElementById('country_row').style.visibility="collapse";
		// Crap to get Safari to play nice.
		document.getElementById('province_row').style.display="none";
		document.getElementById('state_row').style.display="";
		document.getElementById('country_row').style.display="none";
		// End of crap
	}
	else {
		document.getElementById('province_row').style.visibility="visible";
		document.getElementById('state_row').style.visibility="collapse";
		document.getElementById('country_row').style.visibility="collapse";
		// Crap to get Safari to play nice.
		document.getElementById('province_row').style.display="";
		document.getElementById('state_row').style.display="none";
		document.getElementById('country_row').style.display="none";
		// End of crap
		if (countrycode == 'ZZ') {
			document.getElementById('country_row').style.visibility="visible";
			// Same crap, different place
			document.getElementById('country_row').style.display="";
			// End
		} else {
			document.getElementById('country_row').style.visibility="collapse";
			// Same crap, different place
			document.getElementById('country_row').style.display="none";
			// End
		}
	}
}

function ccartShipCountryChange(frm) {
	countrycode = document.getElementById('ship_country_code').value;
	if (countrycode == '00') {
		document.getElementById('ship_province_row').style.visibility="collapse";
		document.getElementById('ship_state_row').style.visibility="visible";
		document.getElementById('ship_country_row').style.visibility="collapse";
		// Crap to get Safari to play nice.
		document.getElementById('ship_province_row').style.display="none";
		document.getElementById('ship_state_row').style.display="";
		document.getElementById('ship_country_row').style.display="none";
		// End of crap
	}
	else {
		document.getElementById('ship_province_row').style.visibility="visible";
		document.getElementById('ship_state_row').style.visibility="collapse";
		document.getElementById('ship_country_row').style.visibility="collapse";
		// Crap to get Safari to play nice.
		document.getElementById('ship_province_row').style.display="";
		document.getElementById('ship_state_row').style.display="none";
		document.getElementById('ship_country_row').style.display="none";
		// End of crap
		if (countrycode == 'ZZ') {
			document.getElementById('ship_country_row').style.visibility="visible";
			// Safari
			document.getElementById('ship_country_row').style.display="";
			// End
		} else {
			document.getElementById('ship_country_row').style.visibility="collapse";
			// Safari
			document.getElementById('ship_country_row').style.display="none";
			// End
		}
	}
}

function ccartShip2billChange() {
	if (document.getElementById('ship2bill').checked == true) {
		document.getElementById('shipto_table').style.visibility="collapse";
		// Safari Stuff
		document.getElementById('shipto_table').style.display="none";
		// End Safari Stuff
	} else {
		document.getElementById('shipto_table').style.visibility="visible";
		// Safari Stuff
		document.getElementById('shipto_table').style.display="";
		// End Safari Stuff
	}
}

function ccartCheckRegisterForm() {
	var errors = 0;
	name = document.getElementById('name').value;
	address1 = document.getElementById('address1').value;
	city = document.getElementById('city').value;
	state_code = document.getElementById('state_code').value;
	province = document.getElementById('province').value;
	country_code = document.getElementById('country_code').value;
	country = document.getElementById('country').value;
	postal_code = document.getElementById('postal_code').value;
	phone_number = document.getElementById('phone_number').value;
	email = document.getElementById('email').value;
	confirmemail = document.getElementById('confirmemail').value;
	password = document.getElementById('password').value;
	confirmpassword = document.getElementById('confirmpassword').value;

	document.getElementById('formerror').innerHTML="";
	document.getElementById('name_row').style.backgroundColor="inherit";
	document.getElementById('address1_row').style.backgroundColor="inherit";
	document.getElementById('city_row').style.backgroundColor="inherit";
	document.getElementById('province_row').style.backgroundColor="inherit";
	document.getElementById('country_row').style.backgroundColor="inherit";
	document.getElementById('postal_code_row').style.backgroundColor="inherit";
	document.getElementById('phone_number_row').style.backgroundColor="inherit";
	document.getElementById('email_row').style.backgroundColor="inherit";
	document.getElementById('confirmemail_row').style.backgroundColor="inherit";
	document.getElementById('password_row').style.backgroundColor="inherit";
	document.getElementById('confirmpassword_row').style.backgroundColor="inherit";

	if (name.length < 2) {
		document.getElementById('name_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (address1.length < 5) {
		document.getElementById('address1_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (city.length < 2) {
		document.getElementById('city_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if ((country_code != '00') && (state.length < 2)) {
		document.getElementById('province_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if ((country_code == 'ZZ') && (country.length < 2)) {
		document.getElementById('country_row').style.backgroundColor="#FFFF00";
	}
	if (postal_code.length < 5) {
		document.getElementById('postal_code_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (phone_number.length < 10) {
		document.getElementById('phone_number_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (!ccartEmailCheck(email)) {
		document.getElementById('email_row').style.backgroundColor="#FFFF00";
		errors++
	}
	if (email != confirmemail) {
		document.getElementById('email_row').style.backgroundColor="#FFFF00";
		document.getElementById('confirmemail_row').style.backgroundColor="#FFFF00";
		errors++
	}
	if (password.length < 8) {
		document.getElementById('password_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (password != confirmpassword) {
		document.getElementById('password_row').style.backgroundColor="#FFFF00";
		document.getElementById('confirmpassword_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (errors > 0) {
		document.getElementById('formerror').innerHTML="<br>Please fix highlighted errors";
		document.getElementById('formerror').style.color="#FF0000";
		document.getElementById('formerror').style.fontWeight="bold";
		return false;
	}
	return true;
}

function ccartCheckCartForm() {
	var errors = 0;
	name = document.getElementById('name').value;
	address1 = document.getElementById('address1').value;
	city = document.getElementById('city').value;
	state_code = document.getElementById('state_code').value;
	province = document.getElementById('province').value;
	country_code = document.getElementById('country_code').value;
	country = document.getElementById('country').value;
	postal_code = document.getElementById('postal_code').value;
	phone_number = document.getElementById('phone_number').value;
	email = document.getElementById('email').value;

	ship_name = document.getElementById('ship_name').value;
	ship_address1 = document.getElementById('ship_address1').value;
	ship_city = document.getElementById('ship_city').value;
	ship_state_code = document.getElementById('ship_state_code').value;
	ship_province = document.getElementById('ship_province').value;
	ship_country_code = document.getElementById('ship_country_code').value;
	ship_country = document.getElementById('ship_country').value;
	ship_postal_code = document.getElementById('ship_postal_code').value;

	document.getElementById('formerror').innerHTML="";
	document.getElementById('name_row').style.backgroundColor="inherit";
	document.getElementById('address1_row').style.backgroundColor="inherit";
	document.getElementById('city_row').style.backgroundColor="inherit";
	document.getElementById('province_row').style.backgroundColor="inherit";
	document.getElementById('country_row').style.backgroundColor="inherit";
	document.getElementById('postal_code_row').style.backgroundColor="inherit";
	document.getElementById('phone_number_row').style.backgroundColor="inherit";
	document.getElementById('email_row').style.backgroundColor="inherit";

	document.getElementById('ship_name_row').style.backgroundColor="inherit";
	document.getElementById('ship_address1_row').style.backgroundColor="inherit";
	document.getElementById('ship_city_row').style.backgroundColor="inherit";
	document.getElementById('ship_province_row').style.backgroundColor="inherit";
	document.getElementById('ship_country_row').style.backgroundColor="inherit";
	document.getElementById('ship_postal_code_row').style.backgroundColor="inherit";

	if (name.length < 2) {
		document.getElementById('name_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (address1.length < 5) {
		document.getElementById('address1_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (city.length < 2) {
		document.getElementById('city_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if ((country_code != '00') && (province.length < 2)) {
		document.getElementById('province_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if ((country_code == 'ZZ') && (country.length < 2)) {
		document.getElementById('country_row').style.backgroundColor="#FFFF00";
	}
	if (postal_code.length < 5) {
		document.getElementById('postal_code_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (phone_number.length < 10) {
		document.getElementById('phone_number_row').style.backgroundColor="#FFFF00";
		errors++;
	}
	if (!ccartEmailCheck(email)) {
		document.getElementById('email_row').style.backgroundColor="#FFFF00";
		errors++
	}
	if (document.getElementById('ship2bill').checked == false) {
		if (ship_name.length < 2) {
			document.getElementById('ship_name_row').style.backgroundColor="#FFFF00";
			errors++;
		}
		if (ship_address1.length < 5) {
			document.getElementById('ship_address1_row').style.backgroundColor="#FFFF00";
			errors++;
		}
		if (ship_city.length < 2) {
			document.getElementById('ship_city_row').style.backgroundColor="#FFFF00";
			errors++;
		}
		if ((ship_country_code != '00') && (ship_province.length < 2)) {
			document.getElementById('ship_province_row').style.backgroundColor="#FFFF00";
			errors++;
		}
		if ((ship_country_code == 'ZZ') && (ship_country.length < 2)) {
			document.getElementById('ship_country_row').style.backgroundColor="#FFFF00";
		}
		if (ship_postal_code.length < 5) {
			document.getElementById('ship_postal_code_row').style.backgroundColor="#FFFF00";
			errors++;
		}
	}

	if (errors > 0) {
		document.getElementById('formerror').innerHTML="<br>Please fix highlighted errors";
		document.getElementById('formerror').style.color="#FF0000";
		document.getElementById('formerror').style.fontWeight="bold";
		return false;
	}
	return true;
}

function ccartEmailCheck(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		 return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		 return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
			return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
			return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
			return false;
	 }

	 return true					
}

function ccartUpdateShipping(str) {
	var values=str.split('~');
	shpamt=parseFloat(values[0]).toFixed(2);
	insamt=parseFloat(values[1]).toFixed(2);
  insav=values[2];
	inspaid=values[3];
	trkamt=parseFloat(values[4]).toFixed(2);
	
	trkav=values[5];
  tax=parseFloat(values[6]).toFixed(2);
  discamt=parseFloat(values[7]).toFixed(2);
  discount=values[8];
	
	addins=document.shippingform.addinsurance[0].checked;
	addtrk=document.shippingform.addtracking[0].checked;

//	document.shippingform.shippingamt.value=shpamt;
	document.getElementById('shipping').value=shpamt;
	
	if (addins) {
		document.getElementById('insurance').value=insamt;
	} else {
		document.getElementById('insurance').value='0.00';
	}
	
	if (insav == 'Y') {
		document.getElementById('insavail').style.visibility='visible';
		document.getElementById('insunavail').style.visibility='collapse';
		// Safari
		document.getElementById('insavail').style.display='';
		document.getElementById('insunavail').style.display='none';
		// End Safari
		if (inspaid == 'Y') {
			document.getElementById('insmsg').innerHTML='Insurance is paid by shipper.';
			document.getElementById('insunavail').style.visibility='visible'
			document.getElementById('insavail').style.visibility='collapse';
			// Safari
			document.getElementById('insunavail').style.display=''
			document.getElementById('insavail').style.display='none';
			// End Safari
		}
	} else {
			document.getElementById('insmsg').innerHTML='Insurance is unavailable.';
			document.getElementById('insunavail').style.visibility='visible'
			document.getElementById('insavail').style.visibility='collapse';
			// Safari
			document.getElementById('insunavail').style.display=''
			document.getElementById('insavail').style.display='none';
			// End Safari
	}
	
	if (addtrk) {
		document.getElementById('tracking').value=trkamt;
	} else {
		document.getElementById('tracking').value='0.00';
	}
	
	if (trkav == 'Y') {
		document.getElementById('trkavail').style.visibility='visible';
		// Safari
		document.getElementById('trkavail').style.display='';
		// End Safari
	} else {
		document.getElementById('trkavail').style.visibility='collapse';
		// Safari
		document.getElementById('trkavail').style.display='none';
		// End Safari
	}
  document.getElementById('tax').value=tax;

	if (discount.length > 0) {
		document.getElementById('promodescrow').style.visibility = 'visible';
		// Safari
		document.getElementById('promodescrow').style.display = '';
		// End Safari
	} else {
		document.getElementById('promodescrow').style.visibility = 'collapse';
		// Safari
		document.getElementById('promodescrow').style.display = 'none';
		// End Safari
	}
  document.getElementById('promo_id').value=document.getElementById('promo_id').value.toLowerCase();
	document.getElementById('discount').value=discamt;
	document.getElementById('promodesc').innerHTML = discount;
  
	ccartUpdateTotal();
}

function ccartUpdateCoupon(cpn) {
	var val=cpn.split(':');
	var desc = val[0];
	var amt = parseFloat(val[1]).toFixed(2);
	if (desc.length > 0) {
		document.getElementById('promodescrow').style.visibility = 'visible';
	} else {
		document.getElementById('promodescrow').style.visibility = 'collapse';
	}
	document.getElementById('promoamt').value=parseFloat(amt).toFixed(2);
	document.getElementById('promodesc').innerHTML = desc;
	ccartUpdateTotal();
}

function ccartUpdateTotal() {
	var subtotal = parseFloat(document.getElementById('subtotal').value);
	var shipping = parseFloat(document.getElementById('shipping').value);
	var insurance = parseFloat(document.getElementById('insurance').value);
	var tracking = parseFloat(document.getElementById('tracking').value);
  var tax = parseFloat(document.getElementById('tax').value);
	var discount = parseFloat(document.getElementById('discount').value);
	
	var total = subtotal + shipping + insurance + tracking + tax - discount;
	document.getElementById('total').value = total.toFixed(2);
}

function ccartShowPaymentInfo(payTyp) {
//  alert('ccartShowPaymentInfo(' + payTyp + ')');

//  document.getElementById('paysubmitrow').style.visibility="collapse";
  document.getElementById('paymentCA').style.visibility="collapse";
  document.getElementById('paymentCC').style.visibility="collapse";
  document.getElementById('paymentPP').style.visibility="collapse";
  document.getElementById('paymentGC').style.visibility="collapse";
  document.getElementById('paymentCH').style.visibility="collapse";
  document.getElementById('paymentWT').style.visibility="collapse";
  document.getElementById('paymentIN').style.visibility="collapse";
// Damn Safari  
  document.getElementById('paymentCA').style.display="none";
  document.getElementById('paymentCC').style.display="none";
  document.getElementById('paymentPP').style.display="none";
  document.getElementById('paymentGC').style.display="none";
  document.getElementById('paymentCH').style.display="none";
  document.getElementById('paymentWT').style.display="none";
  document.getElementById('paymentIN').style.display="none";
// End Safari

  if (payTyp == 'CA') {
    document.getElementById('paymentCA').style.visibility="visible";
    document.getElementById('paymentCA').style.display=""; // Safari
  }
  if (payTyp == 'CC') {
    document.getElementById('paymentCC').style.visibility="visible";
    document.getElementById('paymentCC').style.display=""; // Safari
  }
  if (payTyp == 'PP') {
    document.getElementById('paymentPP').style.visibility="visible";
    document.getElementById('paymentPP').style.display=""; // Safari
  }
  if (payTyp == 'GC') {
    document.getElementById('paymentGC').style.visibility="visible";
    document.getElementById('paymentGC').style.display=""; // Safari
  }
  if (payTyp == 'CH') {
    document.getElementById('paymentCH').style.visibility="visible";
    document.getElementById('paymentCH').style.display=""; // Safari
  }
  if (payTyp == 'IN') {
    document.getElementById('paymentIN').style.visibility="visible";
    document.getElementById('paymentIN').style.display=""; // Safari
  }
  if (payTyp == 'WT') {
    document.getElementById('paymentWT').style.visibility="visible";
    document.getElementById('paymentWT').style.display=""; // Safari
  }
//  if (payTyp != '') {
//    document.getElementById('paysubmitrow').style.visibility="visible";
//  }

}

function ccartShowProcessing() {
  //  document.getElementById('processing').style.top=window.pageYOffset+(window.innerHeight/2) + 'px';
  var crossobj=document.all? document.all.processing : document.getElementById('processing');
  var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
  //define universal dsoc left point
  var dsocleft=document.all? iebody.scrollLeft : pageXOffset
  //define universal dsoc top point
  var dsoctop=document.all? iebody.scrollTop : pageYOffset

  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  //if the user is using IE 4+ or Firefox/ NS6+
  if (document.all||document.getElementById){
    crossobj.style.left=parseInt(dsocleft)+(myWidth/2)+"px"
    crossobj.style.top=dsoctop+(myHeight/2)+"px"
  }

  document.getElementById('processing').style.display='block';
}

function ccartHideProcessing() {
  document.getElementById('processing').style.display='none';	
}

// Ajax functions

var xmlhttp;

function ccartUpdateCart(cmd,value) {
  ccartShowProcessing();
	xmlhttp=ccartGetXmlHttpObject();
	if (xmlhttp==null) {
	  alert ("Browser does not support HTTP Request");
	  return;
  }
	var url="ccart.php";
  var rstr = ccartMakeRqst(cmd,value);
	url=url+"?"+rstr;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=ccartStateChanged;
//	xmlhttp.open("GET",url,true);
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);  
}

function ccartStateChanged() {
	if (xmlhttp.readyState==4) {
//		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
//		alert(xmlhttp.responseText);
//    document.getElementById('processing').style.display='none';
		ccartHideProcessing();
		rtn=xmlhttp.responseText.split(';');
    if (rtn[0] == 'shp') {
			ccartUpdateShipping(rtn[1]);
		} else if (rtn[0] == 'pmt') {
      ccartShowPaymentInfo(rtn[1]);
		} else if (rtn[0] == 'add') {
      val=rtn[1].split('~');
			numItems = val[0];
			ccartUpdateNumItems(numItems);
//			document.getElementById('ccartnumitems').innerHTML=numItems;
			alert(val[1]);
    }
	}
}

function ccartUpdateNumItems(numItems) {
	document.getElementById('ccartnumitems').innerHTML=numItems;	
}

function ccartGetXmlHttpObject() {
	if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
	  return new XMLHttpRequest();
  }
	if (window.ActiveXObject) {
// code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
  }
	return null;
}

function ccartMakeRqst(cmd,value) {
  if (cmd == 'a') {
    var rstr='m=a&' + value;
  }
  else if (cmd == 'shp') {
  	var uid = document.getElementById('shipoption').value;
  	var cpn = document.getElementById('promo_id').value;
    var sub = document.getElementById('subtotal').value;
    var cnt = document.getElementById('itemcount').value;
   	var ccd = document.getElementById('ship_country_code').value;
  	var scd = document.getElementById('ship_state_code').value;
//    var rstr = 'm=shp&shp=' + sub + ':' + shp + ':' + ttl + ':' + ccd + ':' + scd;
    var rstr = 'm=shp&shp=' + uid + ':' + cpn + ':' + sub + ':' + cnt + ':' + ccd + ':' + scd;
  }
  else if (cmd == 'pmt') {
    var rstr = 'm=pmt&pmt=' + value;
  }
  return rstr;
}

function ccartMakeRqst1(cmd,val) {
  if (cmd == 'shp') {
    var shp = val;
    var sub = document.getElementById('subtotal').value;
    var cnt = document.getElementById('itemcount').value;
    var rstr = 'm=shp&shp=' + shp + ':' + sub + ':' + itm;
  }
  else if (cmd == 'cpn') {
    var cpn = val;
    var sub = document.getElementById('subtotal').value;
    var shp = document.getElementById('shipping').value;
    var ttl = document.getElementById('total').value;
    var rstr = 'm=cpn&sub=' + sub + '&shp=' + shp + '&ttl=' + ttl;
  }
  else {
    var rstr = cmd;
  }
  return rstr;
}
