// JavaScript Document
// INIT VARIABLES
price_one_shirt = 15;
price_two_shirt = 25;
price_three_shirt = 30;

shipping = 0;

tax_rate = 0;


// INIT THE ARRAYS

//below is a function in case if the user hits the enter key
function checkKey(){
	var key = event.keyCode;
	if (key == 13){
		return (false);
	}
}

// ------- BOF: FUNCTIONS FOR DETERMINING THE ORDER TOTAL -----------------
function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) num = "0";
    cents = Math.floor((num * 100 + 0.5) % 100);
    num = Math.floor((num * 100 + 0.5) / 100).toString();
    if(cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    num = num.substring(0,num.length - (4 * i + 3))+','+num.substring(num.length-(4 * i + 3));
    return ("$" + num + "." + cents);
}

// --- BOF: INFORMATION FOR THE BILLING AND SHIPPING INFORMATION ---
// array for the fields for the billing and shipping information
var fld_billing = new Array();
fld_billing[0] = new Array("FNAME","Billing Address: Your First Name");
fld_billing[1] = new Array("LNAME","Billing Address: Your Last Name");
fld_billing[2] = new Array("ADDRESS","Billing Address: Address");
fld_billing[3] = new Array("CITY","Billing Address: City");
fld_billing[4] = new Array("ZIP","Billing Address: Zip Code");

var fld_shipping = new Array();
fld_shipping[0] = new Array("SHIP_FNAME","Shipping Address: Your First Name");
fld_shipping[1] = new Array("SHIP_LNAME","Shipping Address: Your Last Name");
fld_shipping[2] = new Array("SHIP_ADDRESS","Shipping Address: Address");
fld_shipping[3] = new Array("SHIP_CITY","Shipping Address: City");
fld_shipping[4] = new Array("SHIP_ZIP","Shipping Address: Zip Code");

var t_shirts = new Array("SHIRT_01","SHIRT_02","SHIRT_03");

// function to fill in all the fields in the shipping address as they are in the billing address
function ship_billing(){ //
	// get the value of the checkbox
	if (document.store["SHIP_SAME"].checked){ //evaluates to true.
		// now we will fill them in.			
		for (j=0; j<fld_billing.length; j++){ // fill in all the text fields
			document.store[fld_shipping[j][0]].value = document.store[fld_billing[j][0]].value;
		}

		// fill in the state location
		document.store["SHIP_STATE"].selectedIndex = document.store["STATE"].selectedIndex;

		// although not required, we will prefill the telephone number in the mix
		document.store["SHIP_PHONE"].value = document.store["PHONE"].value;
	}
}

function updateCart(prod_val){
	// reset the variables each time this function is called

	document.store["ORDER_INFORMATION"].value = "";
	shirt_count = 0;
	sub_ttl = 0;

	
	// loop through the values of the t-shirt products to determine the price
	for (t=0; t<t_shirts.length; t++){
		if (document.store[t_shirts[t]].selectedIndex != ""){
			document.store["ORDER_INFORMATION"].value += document.store[t_shirts[t]].name + ": " + document.store[t_shirts[t]].options[document.store[t_shirts[t]].selectedIndex].value +"\n";
			shirt_count++;
		}
	}

	// update the price
	if (shirt_count == 1) {
		sub_ttl = price_one_shirt;
	} else if (shirt_count == 2){
		sub_ttl = price_two_shirt;
	} else if (shirt_count == 3){
		sub_ttl = price_three_shirt;
	}

	// price evaluated.. now display in the field
	document.store["ORDER_INFORMATION"].value += "\n ------- \n Total Price: " + formatCurrency(sub_ttl);
}

function personal_info(){
	// checks to make sure the fields in the billing information section are filled out
	b_missing = "";
	for (b=0; b<fld_billing.length; b++){
		//alert (document.store[fld_billing[b][0]].value + " is the value of the field for the field: " + fld_billing[b][0]);
		if (document.store[fld_billing[b][0]].value == ""){
			b_missing += fld_billing[b][1] + "\n";
		}
	}
	

	s_missing = "";
	if (!document.store["SHIP_SAME"].checked) { // if not shipping to the same billing address, then we need to check the values
		for (s=0; s<fld_shipping.length; s++){
			if (document.store[fld_shipping[s][0]].value == ""){
				s_missing += fld_shipping[s][1] + "\n";
			}
		}
	}

	return b_missing + s_missing;

}


function validation(){
	missing = "";

	// what is the shirt count variable evaluated as?
	// loop through the values of the t-shirt products to determine the price
	shirt_ct = 0;
	for (t=0; t<t_shirts.length; t++){
		if (document.store[t_shirts[t]].selectedIndex != ""){
			shirt_ct++;
		}
	}
	if (shirt_ct == 0) missing += "Please select a product\n";

	// ck for blank fields
	missing += personal_info();

	// ck the type of payment method
	// a. get the value of the type of payment
	var radio_choice = "";
	for (counter = 0; counter < document.store["PAYMENT_TYPE"].length; counter++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.store["PAYMENT_TYPE"][counter].checked){
			radio_choice = document.store["PAYMENT_TYPE"][counter].value;
		}
	}

	if (radio_choice == ""){
		missing+= "Payment Method not selected\n";
	}

	if (radio_choice == "m" || radio_choice == "v"){
		// a - get the values for the month and the day dropdown figures
		the_month = window.document.store.CC_MONTH;
		for (m=0; m<the_month.length;m++){
			if (the_month.options[m].selected) {
				month_value = the_month.options[m].value;
			}
		};

		// b - get the values for the year
		the_year = window.document.store.CC_YEAR;
		for (y=0; y<the_year.length;y++){
			if (the_year.options[y].selected) {
				year_value = the_year.options[y].value;
			}
		}

		// return missing:
		cc_valid = validateCard(document.store['CC_NUMBER'].value,radio_choice,month_value,year_value);
		if (cc_valid != ""){ missing+=cc_valid; };
	};

	// FINALE: is anything missing?
	if (missing != ""){
		missing_hdr = "The Following Errors Have Occurred:\n";
		alert (missing_hdr + missing);
		return false;
	} else {	
		return true;
	}
}