// JavaScript Cost Calculator Functions

// Pass form object, monthly and yearly prices, and the currency symbol
function init(form, fSms1, fServ1, fSms2, fServ2, strSymb)
{
	form.company.focus();	
	strErrors = "";
	strSMS = "";
	strFax = "";

	fSmsPriceM = fSms1;
	fSmsPriceY = fSms2;
	fServicePriceM = fServ1;
	fServicePriceY = fServ2;
	fFaxPriceM = 0;
	fFaxPriceY = 0;
	if ( fFaxPriceM.toFixed() ) {
		fFaxPriceM = fFaxPriceM.toFixed(2);
		fFaxPriceY = fFaxPriceY.toFixed(2);
	}

	strSymbol = strSymb;
	
	fNetM = 0;
	fNetY = 0;
	
	fVatY = 0;
	fVatM = 0;
	
	fTotalM = 0;
	fTotalY = 0;
	
	//Flags for services
	bSms = false
	bFax = false
}

function validateReg1(form)
{
	var url;
	var selected;
	var str;
	var sel;
	
	if ( form.company.value == "" ) return valerr("Please enter your company name");
	if ( form.add1.value == "" ) return valerr("Please enter your company's address");
	if ( form.phone.value == "" ) return valerr("Please enter your telephone number");
	
	if ( form.email.value == "" ) return valerr("Please enter your email address");
	if ( form.pwd1.value == "" ) return valerr("Please enter a password for your administrator account");
	if ( form.pwd2.value == "" ) return valerr("Please enter a password for your administrator account");
	if ( form.pwd1.value != form.pwd2.value ) return valerr("Your password entries do not match - please try again");
	if ( form.first.value == "" || form.last.value == "" )
		return valerr("Please enter your full name");
		
	if ( !form.terms.checked ) return valerr("You can only register by agreeing to our terms and conditions");


// Country
/*	sel = form.country;
	str += "&country=";
	for ( i=0; i<sel.length; i++ ) {
		if ( sel.options[i].selected ) {
			str += sel.options[i].value;
		}
	}
*/	
/*
	url="/_costcalc.asp";
    xmlHttp.open("POST",url,true);
	xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send(str + "&sid="+Math.random());	
*/
}

function changesms(form)
{
	var sel = form.insms;

	for ( i=0; i<sel.length; i++ ) {
		if ( sel.options[i].selected ) {
			str = sel.options[i].text;
		}
	}
	
	if ( str.substr(0,4) == "None" ) {
		bSms = false;
		CalcTotal();
		document.getElementById("smstxt").innerHTML = str;
		document.getElementById("smspriceM").innerHTML = "-";
		document.getElementById("smspriceY").innerHTML = "-";
	}
	else {
		bSms = true;
		CalcTotal();
		document.getElementById("smstxt").innerHTML = str;
		document.getElementById("smspriceM").innerHTML = strSymbol + fSmsPriceM;
		document.getElementById("smspriceY").innerHTML = strSymbol + fSmsPriceY;
	}
	document.getElementById("netM").innerHTML = strSymbol + fNetM;
	document.getElementById("vatM").innerHTML = strSymbol + fVatM;
	document.getElementById("totalM").innerHTML = strSymbol + fTotalM;
	
	document.getElementById("netY").innerHTML = strSymbol + fNetY;
	document.getElementById("vatY").innerHTML = strSymbol + fVatY;
	document.getElementById("totalY").innerHTML = strSymbol + fTotalY;
}


function changefax(form)
{
	var sel = form.infax;

	for ( i=0; i<sel.length; i++ ) {
		if ( sel.options[i].selected ) {
			str = sel.options[i].text;
		}
	}
	
	if ( str == "None" ) {
		bFax = false;
		CalcTotal();
		document.getElementById("faxtxt").innerHTML = str;
		document.getElementById("faxpriceM").innerHTML = "-";
		document.getElementById("faxpriceY").innerHTML = "-";
	}
	else {
		bFax = true;
		CalcTotal();
		document.getElementById("faxtxt").innerHTML = str;
		document.getElementById("faxpriceM").innerHTML = strSymbol + fFaxPriceM;
		document.getElementById("faxpriceY").innerHTML = strSymbol + fFaxPriceY;
	}
}

function CalcTotal()
{
	fNetM = fServicePriceM;
	fNetY = fServicePriceY;
	if ( bSms ) {
		fNetM += fSmsPriceM;
		fNetY += fSmsPriceY;
	}
	if ( bFax ) {
		fNetM += fFaxPriceM;
		fNetY += fFaxPriceY;
	}
	
	fVatM = fNetM * .21;
	fVatY = fNetY * .21;
	fTotalM = fNetM + fVatM;
	fTotalY = fNetY + fVatY;
	
	if ( fNetM.toFixed ) {
		fNetM = fNetM.toFixed(2);
		fVatM = fVatM.toFixed(2);
		fTotalM = fTotalM.toFixed(2);

		fNetY = fNetY.toFixed(2);
		fVatY = fVatY.toFixed(2);
		fTotalY = fTotalY.toFixed(2);
	}
	else {	
		fNetM = Math.round(fNetM*100)/100;
		fVatM = Math.round(fVatM*100)/100;
		fTotalM = Math.round(fTotalM*100)/100;

		fNetY = Math.round(fNetY*100)/100;
		fVatY = Math.round(fVatY*100)/100;
		fTotalY = Math.round(fTotalY*100)/100;
	}
/*
	// Hide the credit card billing options
	if ( fTotalM==0 && fTotalY==0 )
		document.getElementById('method').style.visibility = 'hidden';
	else
		document.getElementById('method').style.visibility = 'visible';
*/	
	
}

function GetXmlHttpObject(handler)
{ 
	var objXMLHttp = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	return objXMLHttp
}


function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		show(xmlHttp.responseText);
	else {
		show(strPleaseWait);
	}
} 

function show(str)
{
	document.getElementById("calcresult").innerHTML = str;
}

function valerr(str)
{
	alert(str);
	return false;
}
