var alerts	= "";
var code	= "";
var FedexUS = "23.55";
var FedexCanada	= "29.55";
var firstAlert	= true;
var GlobalPriority	= "18.55";

function roundOff(value, precision) {
    value = "" + value		// convert value to string
    precision = parseInt(precision);
    var whole = "" + Math.round(value * Math.pow(10, precision));
    var decPoint = whole.length - precision;
    if(decPoint != 0) {
        result = whole.substring(0, decPoint);
        result += ".";
        result += whole.substring(decPoint, whole.length);
        }
    else result = "0."+whole;
    return result;
    }

function stripchars(str, thechar) {
    while (str.indexOf(thechar) != -1) {
	index = str.indexOf(thechar);
	len = str.length;
	str = str.substring(0, index) + str.substring(index+1, len);
//	alert("str is now:"+str+":");
	}
    return str;
    }

function deblank(str) {
    str = stripchars(str, " ");
    str = stripchars(str, "-");
    return str
    }

function ClearShipping() {
    var of = document.Orderform;
    for (i=0; i<of.ShipHow.length; i++) {
		of.ShipHow[i].checked = false;
		}
    }

function Canadian(province) {
    if (province == "AB") return true;
    if (province == "BC") return true;
    if (province == "MB") return true;
    if (province == "NB") return true;
    if (province == "NF") return true;
    if (province == "NS") return true;
    if (province == "ON") return true;
    if (province == "PE") return true;
    if (province == "QC") return true;
    if (province == "SK") return true;
    return false;
    }

function American(state) {
    if (Canadian(state)) return false;
    if (state == "Not in US/Canada") return false;
    return true;
    }

function addAlert(str) {
    if (!firstAlert) alerts = alerts + "\n";
    firstAlert = false;
    alerts = alerts + str;
    }

function Calculate() {
    alerts = "";
    var of = document.Orderform;
    var pf = document.ppform;
	var ship_How = shipHow();
    NCas = 0;
    NCds = of.NCDs.value;
    NCDs = parseInt(document.Orderform.NCDs.value);
    NUnits = parseInt(NCas) + parseInt(NCds);
    
    ProdValue = (NCas * 39.95) + (NCds * 44.95);
    ProdValue = roundOff(ProdValue, 2);

	if (NCDs <= 0) addAlert("The number of sets ordered must be greater than zero!");

    if (of.State == "") {
        alert("Please indicate what state or province you are in or select 'Not in US/Canada.'");
	return false;
	}
    taxrate = 0;
    if (of.State.value == "AZ") taxrate = .05;
    ProdTax = roundOff(ProdValue * taxrate, 2);

    of.extrashipping.value = "0.00";
    if (NUnits > 1) of.extrashipping.value = roundOff(NUnits-1, 2);

    //++ Country / shipping interaction:
    // Override selected country, based on state/province selection:
    if (Canadian(of.State.value)) {
	Country = "Canada";
        // Fedex is only option to Canada.
        // If fedex account not given, only other option is to include fedex cost.
	if (!of.ShipHow[3].checked) {
	    ClearShipping();
	    of.ShipHow[2].checked = true;
	    of.shipping.value = FedexCanada;
	    }
	}
    else if (American(of.State.value)) {
	Country = "US";
	if (ship_How == -1) {
	    alert("Please select a shipping method.");
	    return false;
	    }
	of.shipping.value = of.ShipHow[shipHow()].value;
	if (of.shipping.value == "fedex") {	// Fedex to US or Canada
	    of.shipping.value = FedexUS;
	    }
	if (of.ShipHow[4].checked) {	// Global priority not allowed to US.
	    ClearShipping();
          of.ShipHow[2].checked = true;	// Send via express mail instead.
	    of.shipping.value = of.ShipHow[2].value;
	    }
	}
    else {
	Country = of.OtherCountry.value;
	if (of.ShipHow[3].checked) {
	    alert("Fedex is only available to the US and Canada.  Please select another shipping method.");
	    return false;
	    }
	ClearShipping();
	of.ShipHow[4].checked = true;
	of.shipping.value = GlobalPriority;
	}
    
    if (of.ShipHow[3].checked) {
	fac = deblank(of.fedexAccount.value);
	of.fedexAccount.value = fac;
	if (fac.length != 9) {
	    alert("Please enter your 9 digit Fedex account number or select another shipping method.");
	    return false;
	    }
	else of.shipping.value = "0.00";
	}

    //-- Country / shipping interaction.

    // Exchange fee:
    of.exchangefee.value ="0.00";
    if (of.ForeignCurrency[0].checked | of.ForeignCurrency[1].checked) {
        if (of.ForeignCurrency[1].checked) of.exchangefee.value ="15.00";
	  }
    else {
        addAlert("Please indicate whether or not you are paying in US dollars.");
//	return false;
	}
    
    of.costproduct.value = ProdValue;
    of.costtax.value = roundOff(ProdValue * taxrate, 2);
    of.costtotal.value =	parseFloat(of.costproduct.value) + 
				parseFloat(of.costtax.value) + 
				parseFloat(of.shipping.value) + 
				parseFloat(of.extrashipping.value) + 
				parseFloat(of.exchangefee.value);
    of.costtotal.value		=	roundOff(of.costtotal.value, 2);
    ppform.costtotal2.value =	of.costtotal.value;

    //+ Now sanity check the shipping information:
    if (of.FirstName.value.length == 0) {
		addAlert("Please enter your first name.");
	}
    if (of.LastName.value.length == 0) {
		addAlert("Please enter your last name.");
	}

//alert(of.Street1.value);  // [debug]
    if (of.Street1.value.length == 0) {
	addAlert("Please enter your street address.");
	}
    if (of.City.value.length == 0) {
	addAlert("Please enter your city.");
	}
    if (of.Phone.value.length == 0) {
	addAlert("Please enter your phone number.");
	}
    if (Country == "") {
	    if (of.OtherCountry.value.length==0) {
	        addAlert("Please enter your country.");
	        }
	    }
    if (!bEmailOK(of.Email.value)) {
	addAlert("Please enter a valid email address.");
	}

    if (alerts != "") {
	alert("There is one or more things missing from the form:\n\n"+alerts);
        return false;
        }

    //- Now sanity check the shipping information.

	code = ""+NCDs+"S"
    if (of.State.value == "AZ") code = code+"AZ"; else code = code + "";
    if		(ship_How==0) code = code+"PM"; 
    else if (ship_How==1) code = code+"EM"; 
    else if (ship_How==2) {
		if (Country == "Canada")	code = code+"FC"; 
		if (Country == "US")		code = code+"FU"; 
		}
    else if (ship_How==3) code = code+"FA"; 
    else if (ship_How==4) code = code+"GM"; 
    if (of.ForeignCurrency[0].checked) code = code+"USD"; else code = code+"EXC";
	pf.ordercode.value = code;
    return true;
    }
    
function shipHow() {
    var of = document.Orderform;
	for (i=0; i<of.ShipHow.length; i++) {
	    if (of.ShipHow[i].checked) return i;
	    }
	return -1;
	}

function ppOrder() {
    var of = document.Orderform;
    var pf = document.ppform;
if (!Calculate()) return false;
    ppform.item_number.value	= code+" "+of.Phone.value;
    if (shipHow()==3) ppform.item_number.value = ppform.item_number.value+" "+of.fedexAccount.value;
    ppform.amount.value			= roundOff(	parseFloat(of.costproduct.value) + 
								parseFloat(of.costtax.value), 2 );
    ppform.shipping.value		= roundOff(	parseFloat(of.shipping.value) + 
								parseFloat(of.extrashipping.value), 2);
    ppform.handling.value		= roundOff(	parseFloat(of.exchangefee.value)+1.5, 2);
    ppform.item_name.value		= NCDs+" AdoptingFromRussia set";
    ppform.first_name.value		= of.FirstName.value;
    ppform.last_name.value		= of.LastName.value;
    ppform.address1.value		= of.Street1.value;
    ppform.address2.value		= of.Street2.value;
    ppform.city.value			= of.City.value;
    ppform.state.value			= of.State.value;
    ppform.zip.value			= of.Postalcode.value;
    if (NCDs > 1) document.ppform.item_name.value	= document.ppform.item_name.value + "s"
//	dumpForm("Orderform",	of);
//	dumpForm("ppform",		pf);
    }    
    
function dumpForm(name, form) {
	var str = name + " Form elements:\n";
	for (var i=0; i<form.elements.length; i++) {
		//if (form.elements[i].type == "text") str += form.elements[i].name +": "+form.elements[i].value + "\n";
		str += form.elements[i].type+" "+form.elements[i].name +": "+form.elements[i].value + "\n";
		}
	alert(str);
	}