// JavaScript Document
var gvdblPOL12Months = 180
var gvdblLicence12Months = 150 //$150 per user per year
var gvarrPMLPrices = new Array(3)
//This array holds 3 items, 1 for each of the "year" options
//Then each of the 3 items is an array in itself of 10 items with 
//the prices.
gvarrPMLPrices[0] = new Array(11)
gvarrPMLPrices[0][0] = 0
gvarrPMLPrices[0][1] = 199
gvarrPMLPrices[0][2] = 398
gvarrPMLPrices[0][3] = 597
gvarrPMLPrices[0][4] = 796
gvarrPMLPrices[0][5] = 995
gvarrPMLPrices[0][6] = 1940
gvarrPMLPrices[0][7] = 3781
gvarrPMLPrices[0][8] = 5522
gvarrPMLPrices[0][9] = 7164
gvarrPMLPrices[0][10] = 8706

//var gvarrPMLYear2 = new Array(10)
gvarrPMLPrices[1] = new Array(11)
gvarrPMLPrices[1][0] = 0
gvarrPMLPrices[1][1] = 169
gvarrPMLPrices[1][2] = 338
gvarrPMLPrices[1][3] = 507
gvarrPMLPrices[1][4] = 676
gvarrPMLPrices[1][5] = 845
gvarrPMLPrices[1][6] = 1649
gvarrPMLPrices[1][7] = 3213
gvarrPMLPrices[1][8] = 4693
gvarrPMLPrices[1][9] = 6089
gvarrPMLPrices[1][10] = 7400

//var gvarrPMLYear3 = new Array(10)
gvarrPMLPrices[2] = new Array(11)
gvarrPMLPrices[2][0] = 0
gvarrPMLPrices[2][1] = 143
gvarrPMLPrices[2][2] = 287
gvarrPMLPrices[2][3] = 431
gvarrPMLPrices[2][4] = 575
gvarrPMLPrices[2][5] = 720
gvarrPMLPrices[2][6] = 1400
gvarrPMLPrices[2][7] = 2731
gvarrPMLPrices[2][8] = 3989
gvarrPMLPrices[2][9] = 5175
gvarrPMLPrices[2][10] = 6290

function calculateTotal(){
		var lvdblTotal, lvstrIncGst
		lvstrIncGst = ""
		with (document.main){
			lvdblTotal = gvarrPMLPrices[Year.options[Year.selectedIndex].value-1][NumberOfPML.options[NumberOfPML.selectedIndex].value]
			//Use the year selction (minus 1) to indicate the year array, 
			//and the PML selection (minus 1) to indicate the price array.
			if (NumberOfPOL.options[NumberOfPOL.selectedIndex].value=="51"){
				alert("Please contact us for a specific quote.")
			}
			else{
				lvdblTotal += ((NumberOfPOL.options[NumberOfPOL.selectedIndex].value) * gvdblPOL12Months)
				if (country.options[country.selectedIndex].value == 'New Zealand'){
						lvdblTotal = lvdblTotal * 1.125;
						lvstrIncGst = " including GST"
						//add gst if NZ
				}
				scAMOUNT.value = toDollarsAndCents(lvdblTotal).toString()
			
				//alert(lvdblTotal)
				document.getElementById("displayTotal").innerHTML = "$" + toDollarsAndCents(lvdblTotal).toString() + lvstrIncGst
				return true;
		}
	}
}

function calculateTotalNew(){
	//alert("calc new")
	var lvdblTotal, lvstrIncGst, lvdblGST, lvdblIncGST
	lvstrIncGst = ""
	with (document.main){
		if (numberOfUsers.options[numberOfUsers.selectedIndex].value=="51"){
			alert("Please contact us for a specific quote.");
		}
		else{
			lvdblTotal = parseInt(numberOfUsers.options[numberOfUsers.selectedIndex].value) * gvdblLicence12Months;		
			if (document.main.formType){
				if(formType.value=="invoice"){
					//Add gst if it's the invoice page
					lvdblTotal = lvdblTotal * 1.125;
					lvstrIncGst = " including GST";
				}
			}
			else{
				//if (newzealand[0].checked){
				lvdblIncGST = lvdblTotal * 1.125;
				lvdblGST = lvdblIncGST - lvdblTotal; //Work out GST component
				document.getElementById("licenceFees").innerHTML = "$" + toDollarsAndCents(lvdblTotal).toString();
				document.getElementById("gstTotal").innerHTML = "$" + toDollarsAndCents(lvdblGST).toString();
				lvdblTotal = lvdblIncGST; //Assign total to gst inclusive amount
				//lvstrIncGst = " including GST";				
				//}
			}
			scAMOUNT.value = toDollarsAndCents(lvdblTotal).toString();
		
			//alert(lvdblTotal)
			
			document.getElementById("displayTotal").innerHTML = "$" + toDollarsAndCents(lvdblTotal).toString() + lvstrIncGst;
			return true;
		}
	}
}

function calculateTotalNewer(){
	//alert("calc new")
	var lvdblTotal, lvstrIncGst, lvdblGST, lvdblIncGST
	lvstrIncGst = ""
	with (document.main){
		if (numberOfUsers.options[numberOfUsers.selectedIndex].value=="51"){
			alert("Please contact us for a specific quote.");
		}
		else{
			lvdblTotal = parseInt(numberOfUsers.options[numberOfUsers.selectedIndex].value) * gvdblLicence12Months;		
			if (document.main.formType){
				if(formType.value=="invoice"){
					//Add gst if it's the invoice page
					lvdblTotal = lvdblTotal * 1.125;
					lvstrIncGst = " including GST";
				}
			}
			else{
				if (newzealand[0].checked){
				lvdblIncGST = lvdblTotal * 1.125;
				lvdblGST = lvdblIncGST - lvdblTotal; //Work out GST component
				//document.getElementById("licenceFees").innerHTML = "$" + toDollarsAndCents(lvdblTotal).toString();
				//document.getElementById("gstTotal").innerHTML = "$" + toDollarsAndCents(lvdblGST).toString();
				lvdblTotal = lvdblIncGST; //Assign total to gst inclusive amount
				//lvstrIncGst = " including GST";				
				}
			}
			scAMOUNT.value = toDollarsAndCents(lvdblTotal).toString();
		
			//alert(lvdblTotal)
			
			document.getElementById("displayTotal").innerHTML = "$" + toDollarsAndCents(lvdblTotal).toString() + lvstrIncGst;
			return true;
		}
	}
}

function validateOrder(){
		with (document.main){
		var problem = 0
		var message = ""
		var cartRef=""
		var curDate
		var productType = ""
		if (name.value==""){
			problem++
			message+="\nName is required."
		}
		if (addressPostal.value==""){
			problem++
			message+="\nPostal address is required."
		}
		if (document.main.country[0]){
			if (country.options[country.selectedIndex].value=="None"){
				problem++
				message+="\nCountry is required."
			}
		}
		if (!verifyEmail(email.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone.value==""){
			problem++
			message+="\nTelephone number is required."
		}		
		if (document.main.orderNumber){
			if (orderNumber.value==""){
				problem++
				message+="\nYour order number is required."
			}
		}
		if (!agreeLicence.checked){
			problem++
			message+="\nYou must accept the software licence agreement to proceed."
		}		
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			calculateTotalNew()
		}
	}
}

function validateOrderNew(){
		with (document.main){
		var problem = 0
		var message = ""
		var cartRef=""
		var curDate
		var productType = ""
		if (name.value==""){
			problem++
			message+="\nName is required."
		}
		if (addressPostal.value==""){
			problem++
			message+="\nPostal address is required."
		}
		if (document.main.country){
			if (document.main.country[0]){
				if (country.options[country.selectedIndex].value=="None"){
					problem++
					message+="\nCountry is required."
				}
			}
		}
		if (!verifyEmail(email.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone.value==""){
			problem++
			message+="\nTelephone number is required."
		}		
		if (document.main.orderNumber){
			if (orderNumber.value==""){
				problem++
				message+="\nYour order number is required."
			}
		}
		if (!agreeLicence.checked){
			problem++
			message+="\nYou must accept the software licence agreement to proceed."
		}		
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			calculateTotalNewer()
		}
	}
}

function validateOrderInv(){
		with (document.main){
		var problem = 0
		var message = ""
		var cartRef=""
		var curDate
		var productType = ""
		if (name.value==""){
			problem++
			message+="\nName is required."
		}
		if (company.value==""){
			problem++
			message+="\nCompany is required."
		}
		if (addressPostal.value==""){
			problem++
			message+="\nPostal address is required."
		}
		if (document.main.country){
			if (document.main.country[0]){
				if (country.options[country.selectedIndex].value=="None"){
					problem++
					message+="\nCountry is required."
				}
			}
		}
		if (!verifyEmail(email.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone.value==""){
			problem++
			message+="\nTelephone number is required."
		}		
		if (document.main.orderno){
			if (orderno.value==""){
				problem++
				message+="\nYour order number is required."
			}
		}
		if (!agreeLicence.checked){
			problem++
			message+="\nYou must accept the software licence agreement to proceed."
		}		
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			calculateTotalNewer()
		}
	}
}


function validateFreeTrial(){
		with (document.main){
		var problem = 0
		var message = ""
		var cartRef=""
		var curDate
		var productType = ""
		if (name.value==""){
			problem++
			message+="\nName is required."
		}
		if (addressPostal.value==""){
			problem++
			message+="\nPostal address is required."
		}
		if (country.options[country.selectedIndex].value=="None"){
			problem++
			message+="\nCountry is required."
		}
		if (!verifyEmail(email.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone.value==""){
			problem++
			message+="\nTelephone number is required."
		}				
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function validateContact(){
		with (document.main){
		var problem = 0
		var message = ""		
		if (name01.value==""){
			problem++
			message+="\nName is required."
		}		
		if (!verifyEmail(email02.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone03.value==""){
			problem++
			message+="\nTelephone number is required."
		}				
		if (comment05.value==""){
			problem++
			message+="\nYour comment/question is required."
		}				
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function validateDistributor(){
		with (document.main){
		var problem = 0
		var message = ""		
		if (name01.value==""){
			problem++
			message+="\nName is required."
		}		
		if (address02.value==""){
			problem++
			message+="\nAddress is required."
		}				
		if (!verifyEmail(email04.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone06.value==""){
			problem++
			message+="\nTelephone number is required."
		}				
		if (businessstrengths08.value==""){
			problem++
			message+="\nBusiness description is required."
		}				
		if (targetmarket09.value==""){
			problem++
			message+="\nTarget market is required."
		}
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}

function validateFreeTrialNew(){
		with (document.main){
		var problem = 0
		var message = ""		
		if (name01.value==""){
			problem++
			message+="\nName is required."
		}		
		if (address03.value==""){
			problem++
			message+="\nAddress is required."
		}				
		if (!verifyEmail(email05.value)){
			problem++
			message+="\nA valid email address is required."
		}
		if (phone06.value==""){
			problem++
			message+="\nTelephone number is required."
		}				
		if (!agreement11.checked){
			problem++
			message+="\nAcceptance of the software licence agreement must be checked in order to proceed."
		}		
		if (problem > 0){
			alert(message);
			return false;
		}
		else{
			return true;
		}
	}
}




function verifyEmail(addr) { 
    var atPos = addr.indexOf('@'); 
    var dotPos = addr.lastIndexOf('.') 
    if (atPos>0 && dotPos>atPos+1) return true; 
    else
    	return false;
} 

function roundNumber(paramNumber) {
	var rlength = 2; // The number of decimal places to round to
	var newnumber = Math.round(paramNumber*Math.pow(10,rlength))/Math.pow(10,rlength);
	return newnumber;
}

function toDollarsAndCents(n) {
  var s = "" + Math.round(n * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t
}