var _orderForm;
var totalPriceNum;
var priceNode;
//get if we're a weekday - 0 and 6 are Sunday and Saturday
var _dayOfWeekInt;
//generic time str
var _genericTimeStr = "Choose A Time";
var _genericDayVal;
var _genericMonthVal;

//an array of all our required items
var _requiredTxtArray = [['txt_Name','Please enter a name'],
					  ['txt_Phone','Please enter a phone number.'],
					  ['txt_Email','Please enter an email address'],
					  ['txt_Address','Please enter an address.'],
					  ['txt_ZipCode','Please enter a zip code.']
					  ];
var _errorMsg;

//an array of our required dropdowns
var _requiredSandwichArray = [
								['sel_GrilledChicken',12],
								['sel_SoftNoodle',13],
								['sel_Combo',14],
								['sel_Steak',13],
								['sel_BunCha',13],
								['sel_Prawn',13],
								['sel_Cat',13],
								['sel_Drunken',13],
								['sel_Eggplant',12],
								['sel_Vegroll',7],
								['sel_Chixroll',7],
								['sel_Prawnroll',8],
								['sel_Papaya',8],
								['sel_Ribs',10],
								['sel_LongBeans',10],
								['sel_Tofu',10]
								];

// vars end
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// methods start
function onInitHandler($event){
	totalPriceNum = 0;
	_errorMsg = document.getElementById('errorMsg');
	
	//set our generic values to check against
	_genericTimeStr = document.getElementById('sel_SpecificTime')[0].value;
	_genericDayVal = document.getElementById('sel_SpecificDay')[0].value;
	_genericMonthVal = document.getElementById('sel_SpecificMonth')[0].value;
	
	priceNode = document.getElementById('totalcost');
	_orderForm = document.getElementById('form_order');
	//add a handler to our submit button
	InsideDown.EVENTS.addEventHandler(_orderForm, 'submit',onSubmitHandler, false);
	//loop through our sandwich form and add events
	//not attaching correctly; added directly to fields for now
	for(var i=0;i<_requiredSandwichArray.length;i++){
		var formStr = _requiredSandwichArray[i][0];
		var selElement = document.getElementById(formStr);
		//add a listener to our dropdowns
		InsideDown.EVENTS.addEventHandler(selElement,"change",onSandwichChange,false);
	}
	//add a handler to our checkbox
	var specificTime = document.getElementById('sel_SpecificTime');
	InsideDown.EVENTS.addEventHandler(specificTime,"change",onTimeChangeHandler,false);
	//add a handler to our day/month
	var selMonth = document.getElementById("sel_SpecificMonth");
	var selDay = document.getElementById("sel_SpecificDay");
	InsideDown.EVENTS.addEventHandler(selMonth,"change",onWeekdayHandler,false);
	InsideDown.EVENTS.addEventHandler(selDay,"change",onWeekdayHandler,false);
	//dispatch an event
	InsideDown.EVENTS.dispatchEventHandler(selMonth,"change");
	//check our sandwich pricing
	setSandwichPrice();
}

// constructor/init end
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// methods start

function allowSubmit($submitArray){
	if($submitArray.length>0){
		var txtStr = "Please fix the following errors:\n------\n";
		for(var i=0;i<$submitArray.length;i++){
			txtStr += $submitArray[i];
		}
		alert(txtStr);
		_errorMsg.style.display = "block";
		return false;
	}else{
		_errorMsg.style.display = "none";
		return true;
	}
}

//get the value of our delivery options
function getDeliveryVal(){
	//check our delivery options
	var freeDeliveryVal = document.getElementById('chk_FreeDelivery').checked;
	var timeVal = document.getElementById('sel_SpecificTime').value;
	var returnStr = "";
	if(!freeDeliveryVal && timeVal==_genericTimeStr){
		returnStr += "Please select either free delivery or specific delivery.\n";	
	}
	var dayVal = document.getElementById('sel_SpecificDay').value;
	var monthVal = document.getElementById('sel_SpecificMonth').value;
	if(dayVal== _genericDayVal || monthVal == _genericMonthVal){
		returnStr += "Please select a day and month for delivery.\n";
	}
	return returnStr;
}

function setSandwichPrice(){
	//reset our total price
	totalPriceNum = 0;
	//check to see if we have a specific time selected
	var timeVal = document.getElementById('sel_SpecificTime').value;
	if(timeVal!=_genericTimeStr) totalPriceNum+=10;
	//loop through all our sandwiches to get the total price
	for(var i=0;i<_requiredSandwichArray.length;i++){
		var formStr = _requiredSandwichArray[i][0];
		var selElement = document.getElementById(formStr);
	
		//make sure we're a number to calculate the price
		if(!isNaN(selElement.value)){
			totalPriceNum += selElement.value * _requiredSandwichArray[i][1];
		}
	}
	priceNode.innerHTML = "$"+totalPriceNum;	
}

// methods end
// :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// handlers start

function onSandwichChange($event){
	setSandwichPrice();
}

function onSubmitHandler($event){
	InsideDown.EVENTS.preventDefault($event);
	//hold an array for submissions
	var submissionArray = new Array();
	//loop through our input text fields
	for(var i=0;i<_requiredTxtArray.length;i++){
		var formStr = _requiredTxtArray[i][0];
		var formElement = document.getElementById(formStr);
		//loop through our text fields
		if(formElement.value=="" || formElement.value==undefined || formElement.length==0){
			submissionArray.push(_requiredTxtArray[i][1]+"\n");
		}
	}
	
	//loop through our sandwich fields to make sure we've ordered something
	for(var i=0;i<_requiredSandwichArray.length;i++){
		var formStr = _requiredSandwichArray[i][0];
		var selElement = document.getElementById(formStr);
		if(!isNaN(selElement.value) && selElement.value!=0){
			break;
		}
		//if we've reached the end of the array and haven't broken
		if(i==_requiredSandwichArray.length-1) submissionArray.push("Please select a sandwich.\n");
	}
	
	//grab our error msg
	var dayOfWeekError = document.getElementById("weekendErrorMsg");
	//if our day of week error message is visible, we have an incorrect day selected
	if(dayOfWeekError.style.display!="none") submissionArray.push("Please select a weekday for delivery\n");
	//check our delivery options
	var deliveryStr = getDeliveryVal();
	if(deliveryStr.length>0) submissionArray.push(deliveryStr);

	//allow or don't allow submission
	var allowBool = allowSubmit(submissionArray);
	if(allowBool) _orderForm.submit();
	
}
function onTimeChangeHandler($event){
	setSandwichPrice();	
}

//validate the current selection is a weekday
function onWeekdayHandler($event) {
	var curDate = new Date();
	//get our values for month and day	
	var monthVal = document.getElementById("sel_SpecificMonth").value;
	if(monthVal.length > 2) monthVal = "0"+monthVal;
	var dayVal = document.getElementById("sel_SpecificDay").value;
	//grab our error msg
	var dayOfWeekError = document.getElementById("weekendErrorMsg");
	var curYear = curDate.getFullYear();
	var dateStr = monthVal+"/"+dayVal+"/"+curYear;
	//hold full date
	var fullDate = new Date(dateStr);
	
	//get if we're a weekday - 0 and 6 are Sunday and Saturday
	_dayOfWeekInt = fullDate.getDay();
	if(_dayOfWeekInt==0 || _dayOfWeekInt==6){
		dayOfWeekError.style.display="block";
	}else{
		dayOfWeekError.style.display="none";
	}
}


//init
InsideDown.EVENTS.addEventHandler(window,"load",onInitHandler,false);
