var iDc = 0;

function s(num, val) {
	amount = num * 1; // amount is the num or NaN
	sum = (!num ? 0 : num) * val;  // the sum for that bill or coin
	
	if (isNaN(amount)) { // if the entire is not a number
		alert("' " + num + " ' is not a valid entry and that field will "
			  + "not be included in the total calculation.");
	    return 0;
	} else
		return sum; // if it is OK, send sum back
}

function check_promo(form){
//	url = 'gc_websvc.php?code=' + form.sPromo.value;
	eldiv = document.getElementById('dPromoText');
	el = document.getElementById('sPromo');
    	
//	Sarissa.updateContentFromURI(url, el, null, 
//		function(oNode, elem){
//			txt = elem.innerHTML;
//			if (txt.substring(133, 136) == '10%') {
//				iDc = 0.1;
//			}
//			else {
//				iDc = 0;
//			}
//		}	
//	);

	if (el.value.toUpperCase() == "AE OFFER") {
		iDc = 0.1;
		eldiv.style.display = "block";
	} else {
		iDc = 0;
		eldiv.style.display = "none";
	}  
}

function total1(form, d) {
	qty = s(form.iQuantity.value, 1); // Each amount is the returned value
	cst = s(form.cCost.value, 1);  // for the amount in the s() function
	shp = s(form.shipMethod.value, 1);
	if (d) {
		dsc = s(d, 1);
	} else {
		dsc = iDc;
	}
	
	typ = (form.sCardType.value);// Type of card
	//pro = (form.sPromo.value);// Promo code if applicable

	// add up all the amounts
	var ttl = (qty * cst);
	var ttl2 = (ttl*(1-dsc)) + shp;
	
	form.cTotal.value = ttl2.toFixed(2);// display total amount
	form.USER1.value = "QUANTITY: " + qty + "  GCTYPE: " + typ + "  PROMO: " + pro;  
	form.USER2.value = "CARD AMOUNT: " + cst + ".00"; 
	form.USER3.value = "SHIPPING: " + shp.toFixed(2); 
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else	// otherwise, update 'characters left' counter
		countfield.value = maxlimit - field.value.length;
}

function shipto(form) {
	nam = (form.shnam.value);
	add = (form.shadd.value);
	cit = (form.shcit.value);
	sta = (form.shsta.value);
	zip = (form.shzip.value);
	tto = (form.shto.value);
	fro = (form.shfrom.value);
	ins = (form.Instructions.value);

	form.USER4.value = "*****SHIP TO*****";
	form.USER5.value = "NAME: " + nam;
	form.USER6.value = "ADDRESS: " + add;
	form.USER7.value = "CITY: " + cit;
	form.USER8.value = "STATE: " + sta;
	form.USER9.value = "ZIP: " + zip;
	form.USER10.value = "TO: " + tto + ",   FROM: " + fro + ",   SPECIAL MESSAGE: " + ins;
}

function copyBillTo(form) {
	if (form.bShipToBill.checked) {
		form.cShipTo_Name.value = form.cBillTo_Name.value;
		form.cShipTo_Address.value = form.cBillTo_Address.value;
		form.cShipTo_City.value = form.cBillTo_City.value;
		form.cShipTo_State.value = form.cBillTo_State.value;
		form.cShipTo_Zip.value = form.cBillTo_Zip.value;
		form.cShipTo_Name.disabled = true;
		form.cShipTo_Address.disabled = true;
		form.cShipTo_City.disabled = true;
		form.cShipTo_State.disabled = true;
		form.cShipTo_Zip.disabled = true;
	} else {
		form.cShipTo_Name.disabled = false;
		form.cShipTo_Address.disabled = false;
		form.cShipTo_City.disabled = false;
		form.cShipTo_State.disabled = false;
		form.cShipTo_Zip.disabled = false;
	}
}

function quantityalert(){
	var qnty = document.getElementById('iQuantity').value;
	var shpmthd = document.getElementById('shipMethod');
	if ((qnty > 9) && (shpmthd.value == "3.00")) {
			alert("Please choose 'Priority' or 'Airborne' shipping when purchasing 10 or more cards.");
			shpmthd.focus();
			return false;	
	}
}

function validate(f){
	var aError = new Array();
	if (f.cShipTo_Name.value.length == 0) {
		aError[aError.length] = "Please Enter a Name to Ship to.";
	}
	if (f.cShipTo_Address.value.length == 0) {
		aError[aError.length] = "Please Enter an Address to Ship to.";
	}
	if (f.cShipTo_City.value.length == 0) {
		aError[aError.length] = "Please Enter a City to Ship to.";
	}
	if (f.cShipTo_State.value.length == 0) {
		aError[aError.length] = "Please Select a State to Ship to.";
	}
	if (f.cShipTo_Zip.value.length == 0) {
		aError[aError.length] = "Please Enter a Zip Code to Ship to.";
	}
	if (f.cBillTo_Name.value.length == 0) {
		aError[aError.length] = "Please Enter a Name to Bill to.";
	}
	if (f.cBillTo_Address.value.length == 0) {
		aError[aError.length] = "Please Enter an Address to Bill to.";
	}
	if (f.cBillTo_City.value.length == 0) {
		aError[aError.length] = "Please Enter a City to Bill to.";
	}
	if (f.cBillTo_State.value.length == 0) {
		aError[aError.length] = "Please Select a State to Bill to.";
	}
	if (f.cBillTo_Zip.value.length == 0) {
		aError[aError.length] = "Please Enter a Zip Code to Bill to.";
	}
	if (f.cBillTo_Phone.value.length == 0) {
		aError[aError.length] = "Please Enter a Phone number where you can be reached.";
	}
	if (f.cBillTo_Email.value.length == 0) {
		aError[aError.length] = "Please Enter an Email Address.";
	} else if (!checkEmail(f.cBillTo_Email)) {
		aError[aError.length] = "Please Enter a VALID Email Address.";
	}

	if (f.bShipToBill.checked) {
		f.cShipTo_Name.value = f.cBillTo_Name.value;
		f.cShipTo_Address.value = f.cBillTo_Address.value;
		f.cShipTo_City.value = f.cBillTo_City.value;
		f.cShipTo_State.value = f.cBillTo_State.value;
		f.cShipTo_Zip.value = f.cBillTo_Zip.value;
	}	
	
	if (aError.length > 0) {
		document.getElementById('dError').innerHTML = aError.join("<br />") + "<br />&nbsp;";
		window.scrollTo(0, 500);
		return false;
	} else {
		return true;
	}
}

function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		return false;
	}
	return true;	
}

function select_onchange3(i){
	var OC = document.getElementById("OC");
	var RB = document.getElementById("RB");
	var CH = document.getElementById("CH");
	var WB = document.getElementById("WB");
	var ML = document.getElementById("ML");
	
	switch(i){
	  case 0:
	 		OC.style.display="block"; 
			RB.style.display="none"; 
			CH.style.display="none"; 
			WB.style.display="none";
			ML.style.display="none";
	 break;
	  case 1:
	 		OC.style.display="none"; 
			RB.style.display="block"; 
			CH.style.display="none"; 
			WB.style.display="none";
			ML.style.display="none";
	 break;
	 case 2:
	 		OC.style.display="none"; 
			RB.style.display="none"; 
			CH.style.display="block"; 
			WB.style.display="none";
			ML.style.display="none";
	 break;
	 case 3:
	 		OC.style.display="none"; 
			RB.style.display="none"; 
			CH.style.display="none"; 
			WB.style.display="block";
			ML.style.display="none";
	 break;
	 case 4:
	 		OC.style.display="none"; 
			RB.style.display="none"; 
			CH.style.display="none"; 
			WB.style.display="none";
			ML.style.display="block";
	 break;
	}
}
