// JavaScript Document
// EGi Calculator Code by Michal Cwierz
// April 2009


function Pick() {
	D = document.getElementById('desks_number').value;
	Calculate(D);
}
function Calculate(D) {
		// MAIN CALCULATIONS
		CalculatePro(D);
		CalculateProLegal(D);
		CalculateEss(D);
		CalculateEssLegal(D);
}

function CalculatePro(D) {
	proCost = ProCost(D);
	premierCost = PremierCost(D);
	if(proCost>premierCost){ calculatedPro = "See Premier"; } else { calculatedPro = "£"+Comma(proCost);}
	//alert("PRO: "+calculatedPro);
	if(calculatedPro == "See Premier"){ document.getElementById('result_pro').style.fontSize = '11px';  } else { document.getElementById('result_pro').style.fontSize = '14px'; }
	document.getElementById('result_pro').firstChild.nodeValue = calculatedPro;
}

function CalculateProLegal(D) {
	prolegCost = ProlegCost(D);
	premierCost = PremierCost(D);
	if(prolegCost>premierCost){ calculatedProleg = "See Premier"; } else { calculatedProleg = "£"+Comma(prolegCost);}
	//alert("PROLEG: "+calculatedPro);
	if(calculatedProleg == "See Premier"){ document.getElementById('result_pro_plus').style.fontSize = '11px';  } else { document.getElementById('result_pro_plus').style.fontSize = '14px'; }
	document.getElementById('result_pro_plus').firstChild.nodeValue = calculatedProleg;
}


function CalculateEss(D) {
	essCost = EssCost(D);
	premierCost = PremierCost(D);
	if(essCost>proCost){ 
		if(proCost>premierCost) { calculatedEss = "See Premier"; }
		else { calculatedEss = "See Pro"; }
	} else { calculatedEss = "£"+Comma(essCost);}
	//alert("ESS: "+calculatedEss);
	if(calculatedEss == "See Premier" || calculatedEss == "See Pro"){ document.getElementById('result_essential').style.fontSize = '11px';  } else { document.getElementById('result_essential').style.fontSize = '14px'; }
	document.getElementById('result_essential').firstChild.nodeValue = calculatedEss;
}

function CalculateEssLegal(D) {
	esslegCost = EsslegCost(D);
	prolegCost = ProlegCost(D);
	premierCost = PremierCost(D);
	if(esslegCost>prolegCost){ 
		if(prolegCost>premierCost) { calculatedEssleg = "See Premier"; } else { calculatedEssleg = "see PRO+ Legal"; }
	} else { calculatedEssleg = "£"+Comma(esslegCost);}
	//alert("ESSLEG: "+calculatedPro);
	if(calculatedEssleg == "See Premier" || calculatedEssleg == "see PRO+ Legal"){ document.getElementById('result_essential_plus').style.fontSize = '11px';  } else { document.getElementById('result_essential_plus').style.fontSize = '14px'; }
	document.getElementById('result_essential_plus').firstChild.nodeValue = calculatedEssleg;
}


function ProCost(D) {
	bandMinimum = pro["minimum"][3];
	if(D>pro["band1"][1]) {
			band1 = (pro["band1"][1] - pro["band1"][0] + 1) * pro["band1"][2];
		} else {
			if(D<pro["band1"][0]){ band1 = 0 }
			else { band1 = (D - pro["minimum"][1]) * pro["band1"][2] }
		}
	if(D>pro["band2"][1]) {
			band2 = (pro["band2"][1] - pro["band2"][0] + 1) * pro["band2"][2]
		} else {
			if(D<pro["band2"][0]){ band2 = 0 }
			else { band2 = (D - pro["band1"][1]) * pro["band2"][2] }
		}
	if(D>pro["band3"][1]) {
			band3 = (pro["band3"][1] - pro["band3"][0] + 1) * pro["band3"][2]
		} else {
			if(D<pro["band3"][0]){ band3 = 0 }
			else { band3 = (D - pro["band2"][1]) * pro["band3"][2] }
		}
	if(D>pro["band4"][1]) {
			band4 = (pro["band4"][1] - pro["band4"][0] + 1) * pro["band4"][2]
		} else {
			if(D<pro["band4"][0]){ band4 = 0 }
			else { band4 = (D - pro["band3"][1]) * pro["band4"][2] }
		}
	if(D>pro["band5"][1]) {
			band5 = (pro["band5"][1] - pro["band5"][0] + 1) * pro["band5"][2]
		} else {
			if(D<pro["band5"][0]){ band5 = 0 }
			else { band5 = (D - pro["band4"][1]) * pro["band5"][2] }
		}

	proCost = bandMinimum+band1+band2+band3+band4+band5;
	return proCost;
}

function ProlegCost(D) {
	legMinimum = proleg["minimum"][2] * 5;
	if(D>pro["minimum"][1]) {
		if(D<pro["band2"][0]) {
			leg1 = (D - pro["minimum"][1]) * proleg["leg1"][2];
		} else { leg1 = (pro["band1"][1] - pro["minimum"][1]) * proleg["leg1"][2]; }
	} else { leg1 = 0 }
	
	if(D>pro["band1"][1]) {
		if(D<pro["band3"][0]) {
			leg2 = (D - pro["band1"][1]) * proleg["leg2"][2];
		} else { leg2 = (pro["band2"][1] - pro["band1"][1]) * proleg["leg2"][2]; }
	} else { leg2 = 0 }
	
	if(D>pro["band2"][1]) {
		if(D<pro["band4"][0]) {
			leg3 = (D - pro["band2"][1]) * proleg["leg3"][2];
		} else { leg3 = (pro["band3"][1] - pro["band2"][1]) * proleg["leg3"][2]; }
	} else { leg3 = 0 }
	
	if(D>pro["band3"][1]) {
		if(D<pro["band5"][0]) {
			leg4 = (D - pro["band3"][1]) * proleg["leg4"][2];
		} else { leg4 = (pro["band4"][1] - pro["band3"][1]) * proleg["leg4"][2]; }
	} else { leg4 = 0 }
	
	if(D>pro["band4"][1]) {
		leg5 = (D - pro["band4"][1]) * proleg["leg5"][2];
	} else { leg5 = 0 }


	prolegCost = legMinimum+leg1+leg2+leg3+leg4+leg5;
	return prolegCost;
}

function EssCost(D) {
	bandMinimum = essential["minimum"][3];
	if(D>essential["band1"][1]) {
			band1 = (essential["band1"][1] - essential["band1"][0] + 1) * essential["band1"][2];
		} else {
			if(D<essential["band1"][0]){ band1 = 0 }
			else { band1 = (D - essential["minimum"][1]) * essential["band1"][2] }
		}
	if(D>essential["band2"][1]) {
			band2 = (essential["band2"][1] - essential["band2"][0] + 1) * essential["band2"][2]
		} else {
			if(D<essential["band2"][0]){ band2 = 0 }
			else { band2 = (D - essential["band1"][1]) * essential["band2"][2] }
		}
	if(D>essential["band3"][1]) {
			band3 = (essential["band3"][1] - essential["band3"][0] + 1) * essential["band3"][2]
		} else {
			if(D<essential["band3"][0]){ band3 = 0 }
			else { band3 = (D - essential["band2"][1]) * essential["band3"][2] }
		}
	if(D>essential["band4"][1]) {
			band4 = (essential["band4"][1] - essential["band4"][0] + 1) * essential["band4"][2]
		} else {
			if(D<essential["band4"][0]){ band4 = 0 }
			else { band4 = (D - essential["band3"][1]) * essential["band4"][2] }
		}
	if(D>essential["band5"][1]) {
			band5 = (essential["band5"][1] - essential["band5"][0] + 1) * essential["band5"][2]
		} else {
			if(D<essential["band5"][0]){ band5 = 0 }
			else { band5 = (D - essential["band4"][1]) * essential["band5"][2] }
		}

	essCost = bandMinimum+band1+band2+band3+band4+band5;
	return essCost;
}
//============================
function EsslegCost(D) {
	legMinimum = essleg["minimum"][2];
	if(D>essential["minimum"][1]) {
		if(D<essential["band2"][0]) {
			leg1 = (D - essential["minimum"][1]) * essleg["leg1"][2];
		} else { leg1 = (essential["band1"][1] - essential["minimum"][1]) * essleg["leg1"][2]; }
	} else { leg1 = 0 }
	
	if(D>essential["band1"][1]) {
		if(D<essential["band3"][0]) {
			leg2 = (D - essential["band1"][1]) * essleg["leg2"][2];
		} else { leg2 = (essential["band2"][1] - essential["band1"][1]) * essleg["leg2"][2]; }
	} else { leg2 = 0 }
	
	if(D>essential["band2"][1]) {
		if(D<essential["band4"][0]) {
			leg3 = (D - essential["band2"][1]) * essleg["leg3"][2];
		} else { leg3 = (essential["band3"][1] - essential["band2"][1]) * essleg["leg3"][2]; }
	} else { leg3 = 0 }
	
	if(D>essential["band3"][1]) {
		if(D<essential["band5"][0]) {
			leg4 = (D - essential["band3"][1]) * essleg["leg4"][2];
		} else { leg4 = (essential["band4"][1] - essential["band3"][1]) * essleg["leg4"][2]; }
	} else { leg4 = 0 }
	
	if(D>essential["band4"][1]) {
		leg5 = (D - essential["band4"][1]) * essleg["leg5"][2];
	} else { leg5 = 0 }


	esslegCost = legMinimum+leg1+leg2+leg3+leg4+leg5;
	return esslegCost;
}

function PremierCost(D) {
	bandMinimum = premier["minimum"][3];
	if(D>premier["band1"][1]) {
			band1 = (premier["band1"][1] - premier["band1"][0] + 1) * premier["band1"][2]
		} else {
			if(D<premier["band1"][0]){ band1 = 0 }
			else { band1 = (D - premier["minimum"][1]) * premier["band1"][2] }
		}
	if(D>premier["band2"][1]) {
			band2 = (premier["band2"][1] - premier["band2"][0] + 1) * premier["band2"][2]
		} else {
			if(D<premier["band2"][0]){ band2 = 0 }
			else { band2 = (D - premier["band1"][1]) * premier["band2"][2] }
		}
	if(D>premier["band3"][1]) {
			band3 = (premier["band3"][1] - premier["band3"][0] + 1) * premier["band3"][2]
		} else {
			if(D<premier["band3"][0]){ band3 = 0 }
			else { band3 = (D - premier["band2"][1]) * premier["band3"][2] }
		}
	if(D>premier["band4"][1]) {
			band4 = (premier["band4"][1] - premier["band4"][0] + 1) * premier["band4"][2]
		} else {
			if(D<premier["band4"][0]){ band4 = 0 }
			else { band4 = (D - premier["band3"][1]) * premier["band4"][2] }
		}
	if(D>premier["band5"][1]) {
			band5 = (premier["band5"][1] - premier["band5"][0] + 1) * premier["band5"][2]
		} else {
			if(D<premier["band5"][0]){ band5 = 0 }
			else { band5 = (D - premier["band4"][1]) * premier["band5"][2] }
		}
	if(D>premier["band6"][1]) {
			band6 = (premier["band6"][1] - premier["band6"][0] + 1) * premier["band6"][2]
		} else {
			if(D<premier["band6"][0]){ band6 = 0 }
			else { band6 = (D - premier["band5"][1]) * premier["band6"][2] }
		}
	if(D>premier["band7"][1]) {
			band7 = (premier["band7"][1] - premier["band7"][0] + 1) * premier["band7"][2]
		} else {
			if(D<premier["band7"][0]){ band7 = 0 }
			else { band7 = (D - premier["band6"][1]) * premier["band7"][2] }
		}
	premierCost = bandMinimum+band1+band2+band3+band4+band5+band6+band7;
	return premierCost;
}

// MODEL ESSENTIAL COSTS
essential=new Array(5);
// [0] = FROM  [1] = TO  [2] = PRICE  [3] = COSTBAND  [4] = CUMMULATIVE
essential["minimum"] = [1,1,750,750,750];
essential["band1"] = [2,2,665,665,1415];
essential["band2"] = [3,3,645,645,2060];
essential["band3"] = [4,4,645,645,2705];
essential["band4"] = [5,5,630,630,3335];
essential["band5"] = [6,10000,630,"unlimted",0];

// MODEL PRO COSTS
pro=new Array(5);
// [0] = FROM  [1] = TO  [2] = PRICE  [3] = COSTBAND  [4] = CUMMULATIVE
pro["minimum"] = [0,5,750,3750,3750];
pro["band1"] = [6,10,675,3375,7125];
pro["band2"] = [11,15,610,3050,10175];
pro["band3"] = [16,20,550,2750,12925];
pro["band4"] = [21,25,495,2475,15400];
pro["band5"] = [26,10000,495,"unlimted",0];	

// MODEL PREMIER COSTS
premier=new Array(5);
// [0] = FROM  [1] = TO  [2] = PRICE  [3] = COSTBAND  [4] = CUMMULATIVE
premier["minimum"] = [0,50,565,28250,28250];
premier["band1"] = [51,100,510,0,28250];
premier["band2"] = [101,150,460,23000,51250];
premier["band3"] = [151,200,320,16000,67250];
premier["band4"] = [201,250,225,11250,78500];
premier["band5"] = [251,300,160,8000,86500];
premier["band6"] = [301,350,80,4000,90500];
premier["band7"] = [351,10000,40,"unlimted",0];	

// MODEL PRO LEGAL COSTS
proleg=new Array(3);
// [0] = LO  [1] = LRR  [2] = LEGAL+  
proleg["minimum"] = [1495,950,1250];
proleg["leg1"] = [1045,665,1125];
proleg["leg2"] = [730,465,1015];
proleg["leg3"] = [510,325,810];
proleg["leg4"] = [355,225,565];
proleg["leg5"] = [250,160,465];	

// MODEL ESSENTIAL LEGAL COSTS
essleg=new Array(3);
// [0] = LO  [1] = LRR  [2] = LEGAL+  
essleg["minimum"] = [1817,1155,1950];
essleg["leg1"] = [1731,1100,1750];
essleg["leg2"] = [1648,1047,1600];
essleg["leg3"] = [1570,998,1400];
essleg["leg4"] = [1495,950,1250];
essleg["leg5"] = [1045,665,1125];	

function Comma(number) {
number = '' + number;
if (number.length > 3) {
var mod = number.length % 3;
var output = (mod > 0 ? (number.substring(0,mod)) : '');
for (i=0 ; i < Math.floor(number.length / 3); i++) {
if ((mod == 0) && (i == 0))
output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
else
output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
}
return (output);
}
else return number;
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   Pick();
   return false;
   }
else
   return true;
}



		  

