/**
 * Construction Calculator
 *
 * Author: Chris Knowles <chris@ckweb.com.au>
 *
 * $Id: main.js 3 2008-07-23 08:09:48Z Chris $
 */

(function(){
	$E.onDomload(
		function(){
			Calc.load(); 
		}  
	);
})();

var Calc = {
		   
	selects: {'location': null, 'sector': null, 'type': null, 'finish': null, 'per': null},
		   
	valid: false,

	suffix : ['m<sup>2</sup>', 'm<sup>2</sup>', 'units'],

	range: ['200:7500', '200:7500', '1:250', '150:450'],

	location: [
		'Adelaide',
		'Brisbane',
		'Hobart',
		'Melbourne',
		'Perth',
		'Sydney'
	],
		
	sector: [
		'Residential',
		'Commercial',
		'Industrial'
	],

	type: [
		'House',
		'Townhouse',
		'Villa',
		'Units low rise',
		'Units mid rise',
		'Units high rise',
		'Offices low rise',
		'Offices mid rise',
		'Offices high rise',
		''
	],

	finish: [
		'Low',
		'Medium',
		'High'
	],

	per: [
		'GFA',
		'NLS',
		'UNIT'
	],

	load: function(){

		$('#resultinfo').hide();

		// the form
		Calc.form = $D.id('calcForm');
		Calc.form.onsubmit = function(){
			Calc.calculate()
			return false;
		}

		Calc.selects.sector = new $Form.Select('sector', '');
		Calc.selects.sector.disable();

		Calc.selects.type = new $Form.Select('type', '');
		Calc.selects.type.disable();

		Calc.selects.finish = new $Form.Select('finish', '');
		Calc.selects.finish.disable();

		Calc.selects.per = new $Form.Select('per', '');
		Calc.selects.per.disable();

		Calc.qty = $D.id('qty');
		Calc.qty.value = "";
		Calc.qty.disabled = 'disabled';

		Calc.button = $D.id('calculate');
		Calc.button.className = 'disabled';

		// set location
		Calc.selects.location = new $Form.Select('location', '');
		var http = new $H;
		http.post('fetch.php', 'action=location', function(){
			
			var elms = eval(http.text);
			Calc.selects.location.populateKeys(elms);
		});

		$E.listen('location', 'change', function(){ 
			Calc.quantityClear();
			Calc.resultClear();
			Calc.selects.sector.disable(); 
			Calc.selects.sector.loading();
			Calc.selects.type.clear();
			Calc.selects.type.disable(); 
			Calc.selects.finish.clear();
			Calc.selects.finish.disable(); 
			Calc.selects.per.clear();
			Calc.selects.per.disable();
			Calc.qty.disabled = 'disabled';
			Calc.qty.value = "";
			Calc.button.className = 'disabled';
			Calc.loadSector();
		});

		// set sector
		$E.listen('sector', 'change', function(){	
			Calc.quantityClear();
			Calc.resultClear();
			Calc.selects.type.disable(); 
			Calc.selects.type.loading();
			Calc.selects.finish.clear();
			Calc.selects.finish.disable(); 
			Calc.selects.per.clear();
			Calc.selects.per.disable();
			Calc.qty.disabled = 'disabled';
			Calc.qty.value = "";
			Calc.button.className = 'disabled';
			Calc.loadType();
		});

		// set type
		$E.listen('type', 'change', function(){   
			Calc.quantityClear();
			Calc.resultClear();
			Calc.selects.finish.disable(); 
			Calc.selects.finish.loading();
			Calc.selects.per.clear();
			Calc.selects.per.disable();
			Calc.qty.disabled = 'disabled';
			Calc.qty.value = "";
			Calc.button.className = 'disabled';
			Calc.loadFinish();
		});

		// set finish
		$E.listen('finish', 'change', function(){ 
			Calc.quantityClear();
			Calc.resultClear();
			Calc.selects.per.disable();
			Calc.selects.per.loading();
			Calc.qty.disabled = 'disabled';
			Calc.qty.value = "";
			Calc.button.className = 'disabled';
			Calc.loadPer();
		});

		// set per
		$E.listen('per', 'change', function(){ 
			Calc.quantityClear();
			Calc.resultClear();
			Calc.qty.disabled = 'disabled';
			Calc.qty.value = "";
			Calc.button.className = 'disabled';
			Calc.loadQty();
		});

		// set qty
		$E.listen('qty', 'keyup', function(){
			Calc.validateQty();
		});

		// calculate the form when the button is clicked
		$E.listen('calculate', 'click', function(e){
			Calc.calculate();
			$E.kill(e);
		});
		
	},

	quantityClear: function() {
		$D.setContent('qtyLabel', "");
		$D.setContent('qtyError', "");
	},

	resultClear: function() {
		$D.setContent('result', "");
	},

	loadSector: function() {
		var loc = Calc.selects.location.getValue();
		var http = new $H;
		http.post('fetch.php', 'action=sector&location='+loc, function(){
			
			var elms = eval(http.text);
			Calc.selects.sector.populateKeys(elms);
			Calc.selects.sector.enable();
		});
	},

	loadType: function() {
		var loc = Calc.selects.location.getValue();
		var sec = Calc.selects.sector.getValue();
		var http = new $H;
		http.post('fetch.php', 'action=type&location='+loc+'&sector='+sec, function(){
			
			var elms = eval(http.text);
			if (elms == "9:") {
				Calc.loadFinish(true);
				Calc.selects.type.clear();
			} else {
				Calc.selects.type.populateKeys(elms);
				Calc.selects.type.enable();
			}
		});
	},  

	loadFinish: function(noType) {
		var loc = Calc.selects.location.getValue();
		var sec = Calc.selects.sector.getValue();
		if (noType) {
			var type = 9;
		} else {
			var type = Calc.selects.type.getValue();
		}
		var http = new $H;
		http.post('fetch.php', 'action=finish&location='+loc+'&sector='+sec+'&type='+type, function(){
			
			var elms = eval(http.text);
			Calc.selects.finish.populateKeys(elms);
			Calc.selects.finish.enable();
		});
	},  

	loadPer: function() {
		var loc = Calc.selects.location.getValue();
		var sec = Calc.selects.sector.getValue();
		var type = Calc.selects.type.getValue();
		if (!type) {
			type = 9;
		} 
		var finish = Calc.selects.finish.getValue();
		var http = new $H;
		http.post('fetch.php', 'action=per&location='+loc+'&sector='+sec+'&type='+type+'&finish='+finish, function(){
			
			var elms = eval(http.text);
			Calc.selects.per.populateKeys(elms);
			Calc.selects.per.enable();
		});
	},  

	loadQty: function() {
		var per = Calc.selects.per.getValue();
		var suffix = Calc.suffix[per];
		$D.setContent('qtyLabel', suffix);
		Calc.qty.disabled = false;
		$D.setContent('qtyError', Calc.getQtyMessage());
	},

	splitParts: function(http, type) {

		var list = bits[0].split(",");
		for (index in list) {
			Calc.curList.push(Calc.type);
		}
		Calc.cost = bits[1];
	},

	getQtyMessage: function() {

		// sector, type, per == 0 then use different range
		if (Calc.selects.sector.getValue() == 0 && Calc.selects.type.getValue() == 0 && Calc.selects.per.getValue() == 0) {
			var parts = Calc.range[3].split(":");
		} else {
			var per = Calc.selects.per.getValue();
			var parts = Calc.range[per].split(":");
		}

		var lower = parts[0];
		var upper = parts[1];
		var text = '<div>Please enter a number between ' + lower + ' and ' + upper + '</div>';
		return text + "<div id='info'>(If your project is outside these numbers please contact <a href='mailto:costplanning@washingtonbrown.com.au'>Cost Planning</a> for further information)</div>";
	},

	validateQty: function() {
		var qty = Calc.qty.value;

		if (Calc.selects.sector.getValue() == 0 && Calc.selects.type.getValue() == 0 && Calc.selects.per.getValue() == 0) {
			var parts = Calc.range[3].split(":");
		} else {
			var per = Calc.selects.per.getValue();
			var parts = Calc.range[per].split(":");
		}

		var lower = parseInt(parts[0]);
		var upper = parseInt(parts[1]);
		if (qty >= lower && qty <= upper) {
			Calc.button.className = 'highlight';
		} else {
			Calc.button.className = 'disabled';
		}		
		$D.setContent('result', '');
	},

	formatPrice: function(value) {
		value = value.toString();
		var formatted = "";
		for (var i = value.length - 1, j = 0; i > -1; i--, j++) {
			if (j % 3 === 0 && j !== 0) {
				formatted = "," + formatted;
			}
			formatted = value.charAt(i) + formatted;
		}

		if (formatted[0] === ","){
			formatted = formatted.substr(1);
		}
		return formatted;
	},

	calculate: function(){

		if (Calc.button.className == 'disabled') {
			return false;
		}

		// get the price
		var loc = Calc.selects.location.getValue();
		var sec = Calc.selects.sector.getValue();
		var type = Calc.selects.type.getValue();
		if (!type) {
			type = 9;
		} 
		var finish = Calc.selects.finish.getValue();
		var per = Calc.selects.per.getValue();
		var http = new $H;
		http.post('fetchPrice.php', 'action=price&location='+loc+'&sector='+sec+'&type='+type+'&finish='+finish+'&per='+per, function(){
			
			var price = parseInt(http.text);
			
			// calculate total
			var result = Calc.formatPrice(price * parseInt(Calc.qty.value));
//			/result = price * parseInt(Calc.qty.value);
			
			// display total
			var output = '<strong>Result:</strong> $' + result + ' <span>(excluding GST)</span>';
			$('#resultinfo').fadeIn();
			$D.setContent('result', output);

		});
		
	}
	
}