function calculate(vp_book)
{
	var vl_bookName_str = vp_book.getAttribute('name');
	vl_bookName_str = vl_bookName_str.substring(3,vl_bookName_str.length - 4);
	var vl_price = document.getElementById(vl_bookName_str + '_Total');
	var vl_qty_i = Math.abs(Math.round(vp_book.value) * 1);

	if(isNaN(vl_qty_i))
	{
		vp_book.value = "0";
		vl_price.innerHTML = "0.00";
		
	}
	else
	{
		var vl_price_f = document.getElementById(vl_bookName_str + '_Price').innerHTML * 1;		
		vp_book.value = vl_qty_i;
		vl_price.innerHTML = convertToCurrency('' + (vl_qty_i * vl_price_f));
	}
	
	subtotal();
}

function calculateMisc(vp_book)
{
	if(isNaN(vp_book.value))
	{
		vp_book.value = "0.00";
	}
	else
	{
		var vl_value_str = "" + Math.abs(vp_book.value * 1);
		vp_book.value = convertToCurrency(vl_value_str);
		subtotal();
	}
}

function focusNextTextBox(vp_current)
{
	if((event.which==13) || (window.event.keyCode==13))
	{
		vl_index = vp_current.sourceIndex;
		for(vl_index = vp_current.sourceIndex + 1; vl_index < document.all.length; vl_index++)
		{
			if(document.all[vl_index].tagName == "INPUT")
			{
				document.all[vl_index].focus();
				break;
			}
		}
	}
}

function subtotal()
{
	var vl_subtotal_f = 0;
	var vl_qty_i = 0;
	
	var vl_span_a = document.getElementsByTagName("span");
	var vl_input_a = document.getElementsByTagName("input");
	
	var vl_curSpan;
	var vl_curSpanName;
	for(var vl_counter_i=0; vl_counter_i<vl_span_a.length; vl_counter_i++)
	{
		vl_curSpan = vl_span_a.item(vl_counter_i);
		vl_curSpanName_str = vl_curSpan.getAttribute("id");
		
		if((vl_curSpanName_str != null) && (vl_curSpanName_str.indexOf("_Total") > -1))
		{
			vl_subtotal_f += parseFloat(vl_curSpan.innerHTML);
		}
	}
	
	var vl_curInput;
	for(var vl_counter_i=0; vl_counter_i<vl_input_a.length; vl_counter_i++)
	{
		vl_curInput = vl_input_a[vl_counter_i];
		
		if(vl_curInput.getAttribute("name").indexOf("_Qty") > -1)
		{
			if(!isNaN(vl_curInput.value) && (vl_curInput.value != ""))
			{
				vl_qty_i += parseInt(vl_curInput.value);
			}
			else
			{
				vl_curInput.value = 0;
			}
		}
		else if(vl_curInput.getAttribute("name").indexOf("_Total") > -1)
		{
			if(!isNaN(vl_curInput.value) && (vl_curInput.value != ""))
			{
				vl_subtotal_f += parseFloat(vl_curInput.value);
			}
			else
			{
				vl_curInput.value = 0;
			}
		}
	}
	
	document.getElementById("Total_Price").innerHTML = convertToCurrency('' + vl_subtotal_f);
	document.getElementById("Total_Qty").innerHTML = vl_qty_i;
}

function convertToCurrency(vp_value_str)
{
	if(vp_value_str.indexOf('.') == -1)
	{
		vp_value_str += '.00';
	}
	else if(vp_value_str.indexOf('.') == (vp_value_str.length - 2))
	{
		vp_value_str += '0';
	}
	else if((vp_value_str.length - vp_value_str.indexOf('.')) > 3)
	{
		vp_value_str = vp_value_str.substring(0,vp_value_str.indexOf('.') + 3);
	}
	
	return vp_value_str;
}

function checkForm()
{
	var vl_form = document.orderform;
	var vl_error_z = false;
	var vl_error_str = "Your order could not be sent because of the following error(s):";
	
	if((document.getElementById("Total_Qty").innerHTML == "0") &&
	   (document.getElementById("Total_Price").innerHTML == "0.00"))
	{
		vl_error_str += "\n - You have not specified any items";
		vl_error_z = true;
	}
	if((vl_form.MC_Title.selectedIndex == 0) ||
	   ((vl_form.MC_Title.selectedIndex == 5) &&
	    (vl_form.MC_Title_Other.value == "")))
	{
		vl_error_str += "\n - You must enter your title";
		vl_error_z = true;
	}
	if(vl_form.MC_First_Name.value == "")
	{
		vl_error_str += "\n - You must enter your first name or initials";
		vl_error_z = true;
	}
	if(vl_form.MC_Surname.value == "")
	{
		vl_error_str += "\n - You must enter your surname";
		vl_error_z = true;
	}
	if(vl_form.address.value == "")
	{
		vl_error_str += "\n - You must enter your address";
		vl_error_z = true;
	}
	if(vl_form.postcode.value == "")
	{
		vl_error_str += "\n - You must enter your postcode";
		vl_error_z = true;
	}
	if((vl_form.email.value == "") &&
	  (vl_form.tel.value == "") &&
		(vl_form.fax.value == ""))
	{
		vl_error_str += "\n - You must enter either your e-mail address, telephone number or fax number";
		vl_error_z = true;
	}
	if((vl_form.email.value != "") &&
	   (validateEmail(vl_form.email.value) == false))
	{
		vl_error_str += "\n - The e-mail address entered is invalid";
		vl_error_z = true;
	}
	
	if(vl_error_z)
	{
		alert(vl_error_str);
		return false;
	}
	else
	{
		if(document.getElementById("Total_Price").innerHTML == "0.00")
		{
			// Skip payment page
			vl_form.action = "no_payment.php";
		}
		return true;
	}
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function validateEmail(address)
{
	var at="@"
	var dot="."
	var lat=address.indexOf(at)
	var lstr=address.length
	var ldot=address.indexOf(dot)
	if (address.indexOf(at)==-1){
	   return false
	}

	if (address.indexOf(at)==-1 || address.indexOf(at)==0 || address.indexOf(at)==lstr){
	   return false
	}

	if (address.indexOf(dot)==-1 || address.indexOf(dot)==0 || address.indexOf(dot)==lstr){
	    return false
	}

	 if (address.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (address.substring(lat-1,lat)==dot || address.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (address.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (address.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}