// JavaScript Document

var sub1,sub2 = 0;
var single1,single2,single3 = 0;
var cd1 = 0;

// current prices - update as necessary
var subprice1 = 45.00;
var subprice2 = 165.00;
var singleprice1= 30.00;
var singleprice2= 25.00;
var singleprice3= 25.00;
var cdprice1= 18.00;

//Money format function
function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

// calculate all single ticket fields
function calcSingle(choice){
	
	if (choice==1){
		
		if (document.form1.singleamount1.value != "")
		{	single1 = document.form1.singleamount1.value;
			document.form1.singletotal1.value = formatAsMoney(single1 * singleprice1);	
			grandTotal();
		}
		else {
			document.form1.singletotal1.value = "0.00";
			grandTotal();
		}
	}
	
	if (choice==2){
		if (document.form1.singleamount2.value != "")
		{	single2 = document.form1.singleamount2.value;
			document.form1.singletotal2.value = formatAsMoney(single2 * singleprice2);		
			grandTotal();
		}
		else {
			document.form1.singletotal2.value = "0.00";
			grandTotal();
		}
	}
	
	if (choice==3){
		if (document.form1.singleamount3.value != "")
		{	single3 = document.form1.singleamount3.value;
			document.form1.singletotal3.value = formatAsMoney(single3 * singleprice3);		
			grandTotal();
		}
		else {
			document.form1.singletotal3.value = "0.00";
			grandTotal();
		}
	}
}

// calculate all cd sale fields
function calcCD(choice){
	
	if (choice==1){
		
		if (document.form1.cdamount1.value != "")
		{	cd1 = document.form1.cdamount1.value;
			document.form1.cdtotal1.value = formatAsMoney(cd1 * cdprice1);		
			grandTotal();
		}
		else {
			document.form1.cdtotal1.value = "0.00";
			grandTotal();
		}
	}	
}

//calculates total when donation field changes
function calcDonation() {
	grandTotal();
}

// senior and student discount - update prices here if changes made as well
function discountCheck(){
	
	if (document.form1.discount.checked==true)
	{
		singleprice1= 15.00;
		singleprice2= 15.00;	
		singleprice3= 25.00;	
	
		calcSingle("1");
		calcSingle("2");
		calcSingle("3");
	}
	else
	{
		singleprice1= 30.00;
		singleprice2= 25.00;
		singleprice3= 25.00;
	
		calcSingle("1");
		calcSingle("2");
		calcSingle("3");
	}
}

//calcualte total amount of all fields thoughout form
function grandTotal(){
	document.form1.total.value = formatAsMoney(parseFloat(document.form1.singletotal1.value) + parseFloat(document.form1.singletotal2.value) + parseFloat(document.form1.singletotal3.value) + parseFloat(document.form1.cdtotal1.value) + parseFloat(document.form1.donation.value) + parseFloat(document.form1.donation2.value));
}

// Required fields validation
/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("total", "name", "address", "apt", "city", "state", "zip", "phone", "email", "match");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("Select your order options above", "Name", "Address", "Apt.", "City", "State", "Zip", "Phone", "E-mail", "Will your company match your donation?");
	// dialog message
	var alertMsg = "Please complete the following fields:\n\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null || obj.value == "0.00"){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}