// Order form scripts

//FORM VALIDATION

// Validates a form using the 'validate' methods set for it's input fields (see below) and
// provides feedback in the form of an alert message detailing any invalid fields.
//	eg. To invoke this function when the user tries to sumbit the form 'my_form':
//			<form name='my_form' onsubmit="return formValidate(this);" ...>

function dollarFormat(dolFloat)
{
	if( dolFloat == 0 )
	{
		return '0.00';
	}
	var string = new String(Math.floor((dolFloat * 100)));
	return string.substring(0, string.length - 2) +"." + string.substring(string.length - 2, string.length)
}

function formValidate(form) 
{
	if(this.elements) form = this;
	if(form.elements)
	{
		var is_valid = true;
		var feedback ='Sorry, there are some problems with the order:';
		var crnt_feedback = '';

		if(subtotalCost == 0) 
		{
			is_valid = false;
			feedback += '\n - enter a quantity of wine';
		}

		for(elem_num=0; elem_num < form.elements.length; elem_num++) 
		{
			if(form.elements[elem_num].validate) 
			{
				crnt_feedback = form.elements[elem_num].validate();
				if(crnt_feedback !== true) 
				{
					is_valid = false;
					feedback += '\n - \''+ form.elements[elem_num].name +'\' '+ crnt_feedback;
				}
			}
		}

		if(is_valid) 
		{
			return true;
		}
		else
		{
			alert(feedback);
			return false;
		}
	}
	return true;
}

//METHODS - VALIDATE
// For each form field requiring validation add the required function below as it's 'validate' method.
//	eg. To validate that the text fields 'name' and 'email' in the form 'my_form', are not empty;
//		following the closing </form> tag add:
//		<script language="javascript">
//			document.my_form.name.validate = formValidateFieldNotEmpty;
//			document.my_form.email.validate = formValidateFieldNotEmpty;
//		</ script>

function formValidateFieldNotEmpty() 
{
	if(this.value != '') 
	{
		return true;
	}
	return 'Cannot be left empty';
}

function formValidateFieldNotZero() 
{
	if(this.value != '0') 
	{
		return true;
	}
	return 'Cannot be zero';
}

function formValidateFieldIsEmail() 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(this.value)) 
	{
		return true;
	}
	return 'Must be a valid email address';
}

function formSubmitOrder(orderForm)
{
	if(!orderForm) var orderForm = document.orderForm;
	orderForm.OrderID.value = orderForm.Email_Address.value;

	if(formValidate(orderForm))	{
		orderForm.submit();
	}
}

// Update displayed totals
var wineSubtotals = new Object();
function updateCost(sender, price, target)
{
	var wineSubtotal = sender.value * price;
	wineSubtotals[ sender.name ] = wineSubtotal;
	var wineSubtotalElement = document.getElementById( target );
	wineSubtotalElement.innerHTML = dollarFormat( wineSubtotal );

	updateSubtotal();
}

var subtotalCost;
function updateSubtotal()
{
	subtotalCost = 0;
	for(var key in wineSubtotals)
	{
		subtotalCost += wineSubtotals[ key ];
	}
	var subtotalCostElement = document.getElementById( 'sub_total' );
	subtotalCostElement.innerHTML = dollarFormat( subtotalCost );
	document.orderForm['SubTotal_NZD'].value = dollarFormat( subtotalCost );
	updateTotal();
}

var northIslandFreight = 8;
var southIslandFreight = 13;
var freightCost = 0;
function updateFreight( sender )
{
	var selectedRegion = sender[ sender.selectedIndex ].value;
	if( selectedRegion.search(/north island/i) == -1 )
	{
		freightCost = southIslandFreight;
	}
	else
	{
		freightCost = northIslandFreight;
	}
	var freightCostElement = document.getElementById( 'freight_cost' );
	freightCostElement.innerHTML = dollarFormat( freightCost );
	document.orderForm['TotalFreight_NZD'].value = dollarFormat( freightCost );
	updateTotal();		
}

function updateTotal()
{
	var totalCost = subtotalCost  + freightCost;
	var totalCostElement = document.getElementById( 'display_total' );
	totalCostElement.innerHTML = dollarFormat( totalCost );
	document.orderForm['Total_NZD'].value = dollarFormat( totalCost );
}
