/**
 * JavaScript file to drive the online estimate system for the Swale website
 * (c) Chris Cook & Michelle Britton
 */

/**
 * This function selects which sub-form to show
 *
 * @param string
 * @return void
 */
var form;
function show_form(form) {
	if(form == 'boiler_change') {
		
		document.getElementById('boiler_change').style.display = 'inline';
		document.getElementById('new_system').style.display = 'none';
		show_cylinder_options('hide')
		
	} else if (form == 'new_system') {
		
		document.getElementById('boiler_change').style.display = 'none';
		document.getElementById('new_system').style.display = 'inline';
		
	} else {
		
		document.getElementById('boiler_change').style.display = 'none';
		document.getElementById('new_system').style.display = 'none';		
		
	}
}

document.getElementsByClassName = function(class_name) {
    var docList = this.all || this.getElementsByTagName('*');
    var matchArray = new Array();

    /*Create a regular expression object for class*/
    var re = new RegExp("(?:^|\\s)"+class_name+"(?:\\s|$)");
    for (var i = 0; i < docList.length; i++) {
        if (re.test(docList[i].className) ) {
            matchArray[matchArray.length] = docList[i];
        }
    }

	return matchArray;
}//eof annonymous function

/**
 * Selectively hides or shows the 
 */
function show_cylinder_options(selected_option) {
	
	var elements = document.getElementsByClassName('tank');
	//alert(elements);
	
	if(selected_option == "combination") {
		//alert(typeof(elements));
		for(keyVar in elements) {
			elements[keyVar].style.display = 'none';
		}
		
	} else if(selected_option == "conventional") {
		
		for(keyVar in elements) {
			elements[keyVar].style.display = 'inline';
		}		
		
	} else {
		
		for(keyVar in elements) {
			elements[keyVar].style.display = 'none';
		}			
		
	}
}

/**
 * Calculates the total estimated cost for a boiler change
 */
function get_change_price() {
	
	var estimated_price;
	var valve_price = 22;
	var radiator_price = 180;
	
	//Grab the info we need
	var boiler_type = document.getElementById('boiler_type').value;
	var model = document.getElementById('model').value;
	var valves = document.getElementById('valves').value;
	var airing_control = document.getElementById('airing_control').checked;
	var roomstat = document.getElementById('remote_roomstat').checked;
	var additional_radiators = document.getElementById('additional_radiators').value;
	var feed_expansion = document.getElementById('feed_expansion').checked;
	var cylinder = document.getElementById('cylinder').checked;
	var powerflush = document.getElementById('powerflush').checked;
	var magnaclean = document.getElementById('magnaclean').checked;
	
	//Work it all out
	switch(boiler_type) {		
		case 'combination':
			//Get the right price for the right model
			switch(model) {
				case 'baxi':
					estimated_price = 1700;
					break;
				case 'vaillant':
					estimated_price = 1850;
					break;
				case 'worcester':
					estimated_price = 1700;
					break;			
			}
			break;    
		case 'conventional':
			//get the right price for the right model
			switch(model) {
				case 'baxi':
					estimated_price = 1675;
					break;
				case 'vaillant':
					estimated_price = 1750;
					break;
				case 'worcester':
					estimated_price = 1575;
					break;			
			}
			break;
	}
	
	//Add on the price of the thermostatic valves
	estimated_price += valves * valve_price;
	
	//Airing cupboard control
	if(airing_control == true) {
		estimated_price += 350;
	}
	
	//Remote roomstat
	if(roomstat == true) {
		estimated_price += 170;
	}
	
	//Add on the price of the extra radiators
	estimated_price += additional_radiators * radiator_price;
	
	if(feed_expansion == true) {
		estimated_price += 95;
	}
	
	if(cylinder == true) {
		estimated_price += 275;
	}
	
	if(powerflush == true) {
		estimated_price += 250;
	}
	
	if(magnaclean == true) {
		estimated_price += 120;
	}
	
	if(isNaN(estimated_price)) {
		//Update the page
		document.getElementById('change_estimate').innerHTML = '<span>Please check you have completed all details!</span>';		
	} else {
		//Update the page
		document.getElementById('change_estimate').innerHTML = '<span>ESTIMATED TOTAL &pound;' + estimated_price + '</span>';		
	}
}

/**
 * Calculates the total estimated cost for a boiler change
 */
function get_new_system_price() {
	
	var estimated_price;
	var radiator_price = 180;
	
	//Grab the info we need
	var boiler_type = document.getElementById('ns_boiler_type').value;
	var model = document.getElementById('ns_model').value;
	var additional_radiators = document.getElementById('ns_additional_radiators').value;

	//Work it all out
	switch(boiler_type) {		
		case 'combination':
			//Get the right price for the right model
			switch(model) {
				case 'baxi':
					estimated_price = 4300;
					break;
				case 'vaillant':
					estimated_price = 4450;
					break;
				case 'worcester':
					estimated_price = 4250;
					break;			
			}
			break;    
		case 'conventional':
			//get the right price for the right model
			switch(model) {
				case 'baxi':
					estimated_price = 4450;
					break;
				case 'vaillant':
					estimated_price = 4450;
					break;
				case 'worcester':
					estimated_price = 4300;
					break;			
			}
			break;
	}

	//Add on the price of the extra radiators
	estimated_price += additional_radiators * radiator_price;

	if(isNaN(estimated_price)) {
		//Update the page
		document.getElementById('ns_change_estimate').innerHTML = '<span>Please check you have completed all details!</span>';		
	} else {
		//Update the page
		document.getElementById('ns_change_estimate').innerHTML = '<span>ESTIMATED TOTAL &pound;' + estimated_price + '</span>';		
	}
}