/* =1 */
var browser = navigator.appName + " " + navigator.appVersion;
var totalamount = 0;
var dtotalamount = 0;
var totaltaxes = 0;
var dtotaltaxes = 0;
var totalshipping = 0;
var dtotalshipping = 0;
var dtotaltotal = 0;
var theamount = 0;
/* 6 */

/* CHALLENGER Soccer Racket */
var chamount = 0;
var chprice = 499;
var ch_name = "CHALLENGER Soccer Racket";
var ch_quantity = 0;
var chamount_cc = 0;
var chtaxes = 0;
var chshipping = 0;
var chtotal = 0;

/* Racket frame */
var chframount = 0;
var chfrprice = 299;
var chfr_name = "Racket frame (racket frame only) for CHALLENGER";
var chfr_quantity = 0;
var chfrtaxes = 0;
var chfrshipping = 0;
var chfrtotal = 0;

/* Caster Wheel Set */
var chwamount = 0;
var chwprice = 69;
var chw_name = "Caster Wheel Set for  CHALLENGER";
var chw_quantity = 0;
var chwtaxes = 0;
var chwshipping = 0;
var chwtotal = 0;

/* Lower Adjustment Tube */
var chltamount = 0;
var chltprice = 119;
var chlt_name = "Lower Adjustment Tube for CHALLENGER";
var chlt_quantity = 0;
var chlttaxes = 0;
var chltshipping = 0;
var chlttotal = 0;

/* Upper Adjustment Tube */
var chutamount = 0;
var chutprice = 119;
var chut_name = "Upper Adjustment Tube for  CHALLENGER";
var chut_quantity = 0;
var chuttaxes = 0;
var chutshipping = 0;
var chuttotal = 0;

/* Closure Clasp */
var chccamount = 0;
var chccprice = 19;
var chcc_name = "Closure Clasp for  CHALLENGER";
var chcc_quantity = 0;
var chcctaxes = 0;
var chccshipping = 0;
var chcctotal = 0;

var gquantity = 0;
var gtotal = 0;
var thenumber = 0;

var i = 0;
var newContent = null;
var attribute_names = ["item_name_", "amount_", "quantity_"]

/* bruce */
var cc_set = ["KoachKarl", "Koach Karl", "koachkarl", "koach karl", "footy4kids", "Footy4kids"];
var cc_value = 20;
var cc_amount = 0;
var dcc_amount = 0;
var cc_selected = null;

function notnumber(num){
    var thenumber = num.value
    if (isNaN(thenumber) || thenumber == "" || thenumber == " " || thenumber == "  ") {
        alert("Invalid Number Entry")
        num.value = 0
        num.focus()
        num.select()
    }
}

function cc_verify(cc){
	for (var i=0; i<cc_set.length; i++){
		if (cc_set[i] == cc)
			return true;
	}
	return false;
}

function viewit(){

    if (checkit()) {
    
        var newWind = window.open("", "", 'width=820,height=570,scrollbars=no,resizable=yes,menubar=yes');
        if (newWind.opener == null) {
            newWind.opener = window;
        }
        newContent = '<html><head><link rel="stylesheet" type="text/css" href="scripts/all.css" media="all"/></head><body id="order_details"><center><h3>Order Details</h3><table border cellpadding=15><tr align=center><td><b>Item</b></td><td><b>Qty</b></td><td><b>Cost</b></td><td><b>Discount</b></td><td><b>Tax</b></td><td><b>Ship</b></td><td><b>Total</b></td></tr>';
        
        /* =2 */
        
        individual_total_price();        

        /* =3 */
        gquantity = parseFloat(document.theform.RFquantity.value) + 
		parseFloat(document.theform.jrquantity.value) + 
		parseFloat(document.theform.Groundsupport.value) + 
		parseFloat(document.theform.Indoorsupport.value) + 
		parseFloat(document.theform.Singlestring.value) + 
		parseFloat(document.theform.Completerestringing.value) + 
		parseFloat(document.theform.FactoryRF.value) + 
		parseFloat(document.theform.chquantity.value) + 
		parseFloat(document.theform.chwquantity.value) + 
		parseFloat(document.theform.chfrquantity.value) +
        parseFloat(document.theform.chltquantity.value) +
        parseFloat(document.theform.chutquantity.value) +
        parseFloat(document.theform.chccquantity.value);
        gtotal = parseFloat(dtotaltotal);
        
        newContent += "<tr><td><b>Total</b></td><td align=right><b>" + dollarize(gquantity) + "</b></td><td align=right><b>" + dollarize(totalamount) + "</b></td><td align=right><b>" + dollarize(cc_amount) + "</b></td><td align=right><b>" + dollarize(totaltaxes) + "</b></td><td align=right><b>" + dollarize(totalshipping) + "</b></td><td align=right><b>" + dollarize(gtotal) + "</b></td></tr>";        
        newContent += "</table><p>";
        
        if ((document.theform.Address[0].checked && document.theform.RFquantity.value != 0 || document.theform.jrquantity.value != 0 || document.theform.Groundsupport.value != 0 || document.theform.Indoorsupport.value != 0)) {
            newContent += "<p><font color=red>Shipping fee includes a $25.00 transfer fee by the ground carrier for delivery in a residential area</font>"
        }
        newContent += "<p><form><input type=button value='PRINT' onclick='window.print()'></form><p>"
        
        newContent += "</center></body></html>"
        
        newWind.document.write(newContent)
    }
}

function individual_total_price(){
	
		chtotal = chamount_cc + chtaxes + chshipping; 
        newContent += "<tr><td>Challenger</td><td align=right>" + ch_quantity + "</td><td align=right>" + dollarize(chamount) + "</td><td align=right>" + dollarize(cc_amount) + "</td><td align=right>" + dollarize(chtaxes) + "</td><td align=right>" + dollarize(chshipping) + "</td><td align=right>" + dollarize(chtotal) + "</td></tr>";
       
		chwtotal = chwamount + chwtaxes + chwshipping;
        newContent += "<tr><td>Challenger Wheelset</td><td align=right>" + document.theform.chwquantity.value + "</td><td align=right>" + dollarize(chwamount) + "</td><td align=right>" + "0.00" + "</td><td align=right>" + dollarize(chwtaxes) + "</td><td align=right>" + dollarize(chwshipping) + "</td><td align=right>" + dollarize(chwtotal) + "</td></tr>";
        
        chfrtotal = chframount + chfrtaxes + chfrshipping;
        newContent += "<tr><td>Challenger Racket Frame Replacement</td><td align=right>" + document.theform.chfrquantity.value + "</td><td align=right>" + dollarize(chframount) + "</td><td align=right>" + "0.00" + "</td><td align=right>" + dollarize(chfrtaxes) + "</td><td align=right>" + dollarize(chfrshipping) + "</td><td align=right>" + dollarize(chfrtotal) + "</td></tr>";
        
        chuttotal = chutamount + chuttaxes + chutshipping;
        newContent += "<tr><td>Challenger Upper Tube Replacement</td><td align=right>" + document.theform.chutquantity.value + "</td><td align=right>" + dollarize(chutamount) + "</td><td align=right>" + "0.00" + "</td><td align=right>" + dollarize(chuttaxes) + "</td><td align=right>" + dollarize(chutshipping) + "</td><td align=right>" + dollarize(chuttotal) + "</td></tr>";
        
        chlttotal = chltamount + chlttaxes + chltshipping;
        newContent += "<tr><td>Challenger Lower Tube Replacement</td><td align=right>" + document.theform.chltquantity.value + "</td><td align=right>" + dollarize(chltamount) + "</td><td align=right>" + "0.00" + "</td><td align=right>" + dollarize(chlttaxes) + "</td><td align=right>" + dollarize(chltshipping) + "</td><td align=right>" + dollarize(chlttotal) + "</td></tr>";
        
        chcctotal = chccamount + chcctaxes + chccshipping;
        newContent += "<tr><td>Challenger Closure Clasp Replacement</td><td align=right>" + document.theform.chccquantity.value + "</td><td align=right>" + dollarize(chccamount) + "</td><td align=right>" + "0.00" + "</td><td align=right>" + dollarize(chcctaxes) + "</td><td align=right>" + dollarize(chccshipping) + "</td><td align=right>" + dollarize(chcctotal) + "</td></tr>";

}

function format(expr, decplaces){
    // raise incoming value by power of 10 times the
    // number of decimal places; round to an integer; convert to string
    var str = "" + Math.round(eval(expr) * Math.pow(10, decplaces))
    // pad small value strings with zeros to the left of rounded number
    while (str.length <= decplaces) {
        str = "0" + str
    }
    // establish location of decimal point
    var decpoint = str.length - decplaces
    // assemble final result from: (a) the string up to the position of
    // the decimal point; (b) the decimal point; and (c) the balance
    // of the string. Return finished product.
    return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}

// turn incoming expression into a dollar value
function dollarize(expr){
    return format(expr, 2)
}

function checkit(){
    if (document.theform.x_First_Name.value == "") {
        alert("First Name is required")
        document.theform.x_First_Name.focus()
        document.theform.x_First_Name.select()
        return;
    }
    else {
        if (document.theform.x_Last_Name.value == "") {
            alert("Last Name is required")
            document.theform.x_Last_Name.focus()
            document.theform.x_Last_Name.select()
            return;
        }
        else {
            if (document.theform.x_Email.value == "") {
                alert("Email Address is required")
                document.theform.x_Email.focus()
                document.theform.x_Email.select()
                return;
            }
            else {
                if (document.theform.x_Address.value == "") {
                    alert("Address is required")
                    document.theform.x_Address.focus()
                    document.theform.x_Address.select()
                    return;
                }
                else {
                    if (document.theform.x_City.value == "") {
                        alert("City is required")
                        document.theform.x_City.focus()
                        document.theform.x_City.select()
                        return;
                    }
                    else {
                        if (document.theform.x_State.options[document.theform.x_State.selectedIndex].value == "") {
                            alert("State is required")
                            document.theform.x_State.focus()
                            document.theform.x_State.select()
                            return;
                        }
                        else {
                            if (document.theform.x_Zip.value == "") {
                                alert("Zip Code is required")
                                document.theform.x_Zip.focus()
                                document.theform.x_Zip.select()
                                return;
                            }
                            else {
                                if (document.theform.x_Country.value == "") {
                                    alert("Country Name is required")
                                    document.theform.x_Country.focus()
                                    document.theform.x_Country_Name.select()
                                    return;
                                }
                                else {
                                    if (document.theform.x_Phone.value == "") {
                                        alert("Phone Number is required")
                                        document.theform.x_Phone.focus()
                                        document.theform.x_Phone.select()
                                        return;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (calcit()) {
        return true;
    }
}

function make_null(){
	totalamount = 0;
	dtotalamount = 0;
	totaltaxes = 0;
	dtotaltaxes = 0;
	totalshipping = 0;
	dtotalshipping = 0;
	dtotaltotal = 0;
	theamount = 0;
	
	rfamount = 0;
	rftaxes = 0;
	rfshipping = 0;
	rftotal = 0;
	
	jramount = 0;
	jrtaxes = 0;
	jrshipping = 0;
	jrtotal = 0;
	
	outamount = 0;
	outtaxes = 0;
	outshipping = 0;
	outtotal = 0;
	
	inamount = 0;
	intaxes = 0;
	inshipping = 0;
	intotal = 0;
	
	ssamount = 0;
	sstaxes = 0;
	ssshipping = 0;
	sstotal = 0;
	
	csamount = 0;
	cstaxes = 0;
	csshipping = 0;
	cstotal = 0;
	
	rfframount = 0;
	rffrtaxes = 0;
	rffrshipping = 0;
	rffrtotal = 0;
	
	jrframount = 0;
	jrfrtaxes = 0;
	jrfrshipping = 0;
	jrfrtotal = 0;
	
	/* CHALLENGER Soccer Racket */
	chamount = 0;
	ch_quantity = 0;
	chamount_cc = 0;
	chtaxes = 0;
	chshipping = 0;
	chtotal = 0;
	
	/* Caster Wheel Set */
	chwamount = 0;
	chw_quantity = 0;
	chwtaxes = 0;
	chwshipping = 0;
	chwtotal = 0;
	
	/* Racket frame */
	chframount = 0;
	chfr_quantity = 0;
	chfrtaxes = 0;
	chfrshipping = 0;
	chfrtotal = 0;
	
	/* Lower Adjustment Tube */
	chltamount = 0;
	chlt_quantity = 0;
	chlttaxes = 0;
	chltshipping = 0;
	chlttotal = 0;
	
	/* Upper Adjustment Tube */
	chutamount = 0;
	chut_quantity = 0;
	chuttaxes = 0;
	chutshipping = 0;
	chuttotal = 0;
	
	/* Closure Clasp */
	chccamount = 0;
	chcc_quantity = 0;
	chcctaxes = 0;
	chccshipping = 0;
	chcctotal = 0;
	
	gquantity = 0;
	gtotal = 0;
	thenumber = 0;
	
	remove_child_nodes("cart_elements")
	
	i = 0;
	
	cc_amount = 0;
	dcc_amount = 0;
	cc_selected = null;
}

function remove_child_nodes(element) {
		var the_element = document.getElementById(element);
		if ( the_element.hasChildNodes() ){
			while ( the_element.childNodes.length >= 1 ) {
				the_element.removeChild(the_element.firstChild);
			}
		}
}

function add_cart_element(attribute_values) {
	
		var add_cart = document.getElementById("cart_elements");
		var form_input;
		var attribute_node;
		var attribute_assembled;
		var j = 0;
		
		for (; j < 3; j++) {
			form_input = document.createElement("input");
			
			//type
			attribute_node = document.createAttribute("type");
			attribute_node.value = "hidden";
			form_input.setAttributeNode(attribute_node);
			
			//name
			attribute_node = document.createAttribute("name");
			attribute_assembled = attribute_names[j] + i;
			//alert(attribute_assembled);
			attribute_node.value = attribute_assembled;
			form_input.setAttributeNode(attribute_node);
			
			//value
			attribute_node = document.createAttribute("value");
			//alert(attribute_values[j]);
			attribute_node.value = attribute_values[j];
			form_input.setAttributeNode(attribute_node);
			
			add_cart.appendChild(form_input); 
		}
	}
	
function calc_quantity(){
	ch_quantity = document.theform.chquantity.value;
	chfr_quantity = document.theform.chfrquantity.value;
	chw_quantity = document.theform.chwquantity.value;
	chlt_quantity = document.theform.chltquantity.value;
	chut_quantity = document.theform.chutquantity.value;
	chcc_quantity = document.theform.chccquantity.value;
}

function calc_tax(amount) {
	if (document.theform.x_State.options[document.theform.x_State.selectedIndex].value == "TN") {
		return amount * 0.095;
	}
	return 0;
}

function individual_price() {
	var price_ship_tax = 0;
	var attribute_values = new Array();
	var ch_name_cc = ch_name;
	if (ch_quantity > 0) {
		i++;
		chamount =  ch_quantity * chprice;
		chshipping = 49 * ch_quantity;
		chtaxes = calc_tax(chprice) * ch_quantity;
		price_ship_tax = chprice + 49 + calc_tax(chprice);
		cc_selected = document.theform.coupon_code.value;
		if (cc_verify(cc_selected)) {
			cc_amount = ch_quantity * cc_value;
			chamount_cc = chamount - cc_amount;
			price_ship_tax -= cc_value;
			ch_name_cc += " (Coupon Code: " + cc_selected + ")";
		}
		attribute_values = [ch_name_cc, price_ship_tax, ch_quantity];
		add_cart_element(attribute_values);
	}
	
	if (chfr_quantity > 0) {
		i++;
        chframount = chfr_quantity * chfrprice;
		chfrshipping = 39 * chfr_quantity;
		chfrtaxes = calc_tax(chfrprice) * chfr_quantity;
		price_ship_tax = chfrprice + 39 + calc_tax(chfrprice);
		attribute_values = [chfr_name, price_ship_tax, chfr_quantity]
		add_cart_element(attribute_values);
    }
    
    if (chw_quantity > 0) {
        i++;
		chwamount = chw_quantity * chwprice;
		chwshipping = 19 * chw_quantity;
		chwtaxes = calc_tax(chwprice) * chw_quantity;
		price_ship_tax = chwprice + 19 + calc_tax(chwprice);
		attribute_values = [chw_name, price_ship_tax, chw_quantity]
		add_cart_element(attribute_values);
    }
    
    if (chlt_quantity > 0) {
        i++;
		chltamount = chlt_quantity * chltprice;
		chltshipping = 29 * chlt_quantity;
		chlttaxes = calc_tax(chltprice) * chlt_quantity;
		price_ship_tax = chltprice + 29 + calc_tax(chltprice);
		attribute_values = [chlt_name, price_ship_tax, chlt_quantity]
		add_cart_element(attribute_values);
    }
	
	if (chut_quantity > 0) {
        i++;
		chutamount = chut_quantity * chutprice;
		chutshipping = 29 * chut_quantity;
		chuttaxes = calc_tax(chutprice) * chut_quantity;
		price_ship_tax = chutprice + 29 + calc_tax(chutprice);
		attribute_values = [chut_name, price_ship_tax, chut_quantity]
		add_cart_element(attribute_values);
    }
	
	if (chcc_quantity > 0) {
        i++;
		chccamount = chcc_quantity * chccprice;
		chccshipping = 9 * chcc_quantity;
		chcctaxes = calc_tax(chccprice) * chcc_quantity;
		price_ship_tax = chccprice + 9 + calc_tax(chccprice);
		attribute_values = [chcc_name, price_ship_tax, chcc_quantity]
		add_cart_element(attribute_values);
    }
}

function calcit(){
	make_null();
	remove_child_nodes("cart_elements"); 
	calc_quantity();
    /* =4 */
	individual_price(); 
	/* =7 */
    /* =5 */
    /* =8 */
	
    totalamount = chltamount + chutamount + chamount + chwamount + chframount + chccamount;
    totalshipping = chltshipping + chutshipping + chshipping + chwshipping + chfrshipping + chccshipping;
    totaltaxes = chlttaxes + chuttaxes + chtaxes + chwtaxes + chfrtaxes + chcctaxes;
    
    dtotalamount = dollarize(totalamount);
	dcc_amount = dollarize(cc_amount);
	dtotaltaxes = dollarize(totaltaxes);
    dtotalshipping = dollarize(totalshipping);
    dtotaltotal = dollarize((totalamount - cc_amount + totaltaxes + totalshipping));
    theamount = dollarize((totalamount - cc_amount + totaltaxes));

    document.theform.subamount.value = dtotalamount;
	document.theform.discount.value = dcc_amount;
    document.theform.whatevershipping.value = dtotalshipping;
    document.theform.taxes.value = dtotaltaxes;
    document.theform.amount.value = dtotaltotal;
    
    return true;
}

function finalcheck(){
    if (checkit()) {
        document.theform.submit();
    }
}
