// JavaScript Document

function updateSiteCost()
{
	
	var theVal;
	
	for (var i=0; i < document.appForm.site_group.length; i++){
	   if (document.appForm.site_group[i].checked){
		  theVal = document.appForm.site_group[i].value;
		 }
	 }
	
	var singleCost = 440;
	var intCost = 590;
	var doubleCost = 750;
	var superCost = 990;
	
	if(theVal == "Single" ){
		document.appForm.total_siteFee.value = singleCost;
	}else if(theVal == "Intermediate" ){
		document.appForm.total_siteFee.value = intCost;
	}else if(theVal == "Double" ){
		document.appForm.total_siteFee.value = doubleCost;
	}else if(theVal == "Super" ){
		document.appForm.total_siteFee.value = superCost;
	}
}



function updateEftCost()
{
	if(document.appForm.check_reqEft.checked == true){
		document.appForm.total_eft.value = 15;
	}else{
		document.appForm.total_eft.value = 0;
	}
}

/*
function updateInsCost()
{
	if(document.appForm.check_reqIns.checked == true){
		//document.appForm.total_MdmInsure.value = 33;
		document.appForm.total_MdmInsure.value = 'please contact';
	}else{
		document.appForm.total_MdmInsure.value = 'no';
	}
}

*/

function updatePowerCost()
{
	if((document.appForm.power_three.checked == true)&&(document.appForm.power_single.checked == true)){
		document.appForm.total_powerAccess.value = 420;
	}else if((document.appForm.power_three.checked == true)&&(document.appForm.power_single.checked == false)){
		document.appForm.total_powerAccess.value = 350;
	}else if((document.appForm.power_three.checked == false)&&(document.appForm.power_single.checked == true)){
		document.appForm.total_powerAccess.value = 70;
	}else{
		document.appForm.total_powerAccess.value = 0;
	}
}

function updateExtras()
{	
	var chair_price = 5;
	var table_price = 20;
	var carpet_price = 5;
	
	document.appForm.total_chairs.value = document.appForm.qty_chairs.value*chair_price;
	document.appForm.total_tables.value = document.appForm.qty_tables.value*table_price;
	document.appForm.total_carpet.value = document.appForm.qty_carpet.value*carpet_price;
	
	
	document.appForm.total_extraFurn.value = parseFloat(document.appForm.total_chairs.value)+
											parseFloat(document.appForm.total_tables.value)+
											parseFloat(document.appForm.total_carpet.value);

}

function updateTotal()
{
   	updateSiteCost();	
	updatePowerCost();	
	updateExtras();
	updateEftCost();
	//updateInsCost();
	
	if(document.appForm.total_siteFee.value != "POA"){
		document.appForm.total_grand.value = parseFloat(document.appForm.total_siteFee.value) +
					parseFloat(document.appForm.total_extraFurn.value) +
					parseFloat(document.appForm.total_powerAccess.value) +
					parseFloat(document.appForm.total_eft.value);
	}else{
		document.appForm.total_grand.value = "Site cost POA, plus $" + (parseFloat(document.appForm.total_extraFurn.value) +
					parseFloat(document.appForm.total_powerAccess.value) +
					parseFloat(document.appForm.total_eft.value));
	}
}

// validate form
function validForm(passForm) {
	
	
	
	if(passForm.to_sell[1].checked){
		if(passForm.sellOther_descript.value.length == 0){
			passForm.sellOther_descript.focus();
			alert("Please specify other goods/services you would like to sell");
			return false;
		}
	}
	
	// validate email if it exists
	var email = passForm.bus_email.value;
	
	if (email.length != 0) {
		if (-1 == email.indexOf("@")) { 
			passForm.bus_email.focus(); 
			alert("Please enter a valid email address");  
			return false; 
		}	   	   
		if (-1 != email.indexOf(",")) { 
			passForm.bus_email.focus(); 
			alert("Please enter a valid email address");   
			return false; 
		}
		if (-1 != email.indexOf("#")) { 
			passForm.bus_email.focus(); 
			alert("Please enter a valid email address"); 
			return false; 
		}
		if (-1 != email.indexOf("!")) { 
			passForm.bus_email.focus(); 
			alert("Please enter a valid email address"); 
			return false; 
		}
		if (-1 != email.indexOf(" ")) { 
			passForm.bus_email.focus(); 
			alert("Please enter a valid email address");  
			return false; 
		}
		if (email.length == (email.indexOf("@")+1) ){
			passForm.bus_email.focus();
			alert("Please enter a valid email address"); 
			return false;
		}
	}
	
	// check for any contact details
	if ((passForm.bus_email.value.length == 0)&&(passForm.bus_tel.value.length == 0)&&(passForm.bus_mob.value.length == 0)){
		alert('Please enter one of the following contact details:\n -Business email\n -Business telephone\n -Business mobile');
		passForm.bus_email.focus();
		return false;
	}

	if (passForm.contact_name.value.length == 0){
		passForm.contact_name.focus();
		alert("Please enter a contact name");
		return false;
	}
	if (passForm.contact_mobile.value.length == 0){
		passForm.contact_mobile.focus();
		alert("Please enter a contact mobile number");
		return false;
	}
	/*
	if (passForm.contact_market.value.length == 0){
		passForm.contact_market.focus();
		alert("");
		return false;
	}
	*/
	if (passForm.bus_name.value.length == 0){
		passForm.bus_name.focus();
		alert("Please enter business name");
		return false;
	}
	if (passForm.bus_address.value.length == 0){
		passForm.bus_address.focus();
		alert("Please enter business address");
		return false;
	}
	if (passForm.product_descrip.value.length == 0){
		passForm.product_descrip.focus();
		alert("Please enter product description");
		return false;
	}
	
	if(passForm.power_single.checked || passForm.power_three.checked){
		if (passForm.power_descrip.value.length == 0){
			passForm.power_descrip.focus();
			alert("Please enter a description of your devices that require power");
			return false;
		}
	}
	
	
	if (passForm.signage.value.length == 0){
		passForm.signage.focus();
		alert("Please enter your accurate signage details");
		return false;
	}
	
	if (!passForm.select_pay[0].checked && !passForm.select_pay[1].checked && !passForm.select_pay[2].checked){
		alert("Please select your payment method");
		return false;
	}
	
	if (passForm.read_TC.checked != true){
		alert("Please cornfirm that you have read and understand the Terms and Conditions");
		return false;
	}
	
	
    return true;	  
}


function checkNumber(question) {
	var number = question.value;
	var allowed = '0123456789';
	var temp = '';
    for (var i=0; i < number.length; i++) {
        if (allowed.indexOf(number.charAt(i)) == -1) {
            alert('Please enter numbers only');
			question.value = temp;
			question.focus();
            return false;
        }
		temp = temp + number.charAt(i);
    }
}

// validate number
function checkNumberRange(question) {
	var number = question.value;
	var allowed = '0123456789.';
	var temp = '';
    for (var i=0; i < number.length; i++) {
        if (allowed.indexOf(number.charAt(i)) == -1) {
            alert('Please enter numbers only');
			question.value = temp;
			question.focus();
            return false;
        }
		temp = temp + number.charAt(i);
    }
}