document.write('<script type="text/javascript" src="xmlhttp.js"></script>');

function swapImage(imageId,newImageFile) {
	var buttonOver = new Image();
	buttonOver.src = newImageFile;
	document[imageId].src = buttonOver.src;
}
function preloadImages(imageArray) {
	var images = new Array(imageArray.length);
	for (i=0;i<imageArray.length;i++) {
		images[i] = new Image();
		images[i].src = imageArray[i];
	}
}	
function resetOptions(optionalOnly) {
	var j=0;
	var selectElements = new Array();
			var optEquipId = document.getElementById('optional_equipment');
	var optEquipQuantId = document.getElementById('opt_equip_quantities');
	var optEquipPriceId = document.getElementById('opt_equip_prices');

	selectElements[j] = document.getElementById('model');
		j++;
	selectElements[j] = document.getElementById('voltage');
	j++;
	numOptEquipSelects = getNumOptEquipSelects();

	//reset optional equipment
	for (i=0;i<numOptEquipSelects;i++) {
		//remove all optional equipment selects except the first one
		if (i!=0) {
			optEquipId.removeChild(document.getElementById('optional[' + i + ']'));
			optEquipQuantId.removeChild(document.getElementById('opt_equip_quant[' + i + ']'));
			optEquipPriceId.removeChild(document.getElementById('opt_equip_price[' + i + ']'));
		}
		else {
			//add optional equipment to select elements array to reset
			selectElements[(i+j)] = document.getElementById('optional[' + i + ']');
		}
	}
		//reset select element options
	for (var i=0;i<selectElements.length;i++) {
		resetSelectOptions(selectElements[i]);
	}		if (document.getElementById('model_price').innerHTML != "$0.00") {
		document.getElementById('model_price').innerHTML = "$0.00";
		document.getElementById('opt_equip_price[0]').innerHTML = "$0.00";
		document.getElementById('total_price').innerHTML = "$0.00";
	}		if (document.getElementById('machine_type').value == "33") {
		document.getElementById('fob_location').value = "Savage, MN";
	}	
	populateOptions(selectElements);
}

function addOptEquipSelect() {
	var optEquipId = document.getElementById('optional_equipment');
	var optEquipQuantId = document.getElementById('opt_equip_quantities');
	var optEquipPriceId = document.getElementById('opt_equip_prices');

	//add optional select
	var arrayKey = addArraySelect(optEquipId,'optional');

	//add optional equip quants
	var quantityInput = document.createElement('INPUT');

	quantityInput.id = 'opt_equip_quant[' + arrayKey + ']';
	quantityInput.name = quantityInput.id;	quantityInput.value = '1';
	quantityInput.size = '5';
	quantityInput.setAttribute("onchange","updatePrices();document.getElementById('addLink').style.display = 'block';");
	optEquipQuantId.appendChild(quantityInput);

	//add optional equip price
	var priceDiv = document.createElement('DIV');
	priceDiv.id = 'opt_equip_price[' + arrayKey + ']';
	priceDiv.appendChild(document.createTextNode(' $0.00'));
	priceDiv.style.marginTop = '6px';
	optEquipPriceId.appendChild(priceDiv);
	
}
function deleteOptEquipSelect(id) {
	var optEquipId = document.getElementById('optional_equipment');
	var optEquipQuantId = document.getElementById('opt_equip_quantities');
	var optEquipPriceId = document.getElementById('opt_equip_prices');
		var selectToDelete = document.getElementById('optional[' + id + ']');
	var quantToDelete = document.getElementById('opt_equip_quant[' + id + ']');
	var priceToDelete = document.getElementById('opt_equip_price[' + id + ']');
		optEquipId.removeChild(selectToDelete);	optEquipQuantId.removeChild(quantToDelete);
	optEquipPriceId.removeChild(priceToDelete);
}

function populateOptions(selectElements) {
	var machineId = document.getElementById('machine_type').value;
	var modelId = document.getElementById('model').value;
	//if machine has not been selected
	if (machineId == 0) {
		//give everything their default options
		for (var i=0;i<selectElements.length;i++) {
			addDefaultSelectOption(selectElements[i]);
		}
		//if single element was passed in, instead of an array...
		if (selectElements.length == 0) {
			addDefaultSelectOption(selectElements);
		}
	}
	else if (modelId == 0) {
		//if machine has been selected, but not model, give everything except model its default options
		for (var i=0;i<selectElements.length;i++) {
			if(selectElements[i].id != 'model') {
				addDefaultSelectOption(selectElements[i]);
			}
		}
		//get model options
		ajaxLoad('getQuoteOptions.php?mode=models&machine_id=' + document.getElementById('machine_type').value,'GET',getModelOptions);
		}
	else {
		//if machine and model have been selected, load optional equipement and voltage
		getVoltageOptions();
	}
}

function getModelOptions(data) {		
	var selectOption;
		var selectElement = document.getElementById('model');			
	selectOption = createOption('Choose one:',0);
	selectElement.options.add(selectOption);		
	//parse data into array of objects using json parser
	if (typeof(data) != 'undefined' && data != '') {	
		var jsonData = eval('(' + data + ')');	
					
		if(typeof(jsonData.models) != 'undefined') {	
			for (var i=0; i<jsonData.models.length; i++) {
				selectOption = createOption(jsonData.models[i].desc,jsonData.models[i].id);
				selectElement.options.add(selectOption);
			}
		}
	}
}

function getVoltageOptions() {	
	var voltageId = document.getElementById('voltage');
	var selectedVoltage = document.getElementById('voltage').value;
	resetSelectOptions(voltageId);
	ajaxLoad('getQuoteOptions.php?mode=model_voltage&voltage=' + selectedVoltage + '&model_id=' + document.getElementById('model').value, 'GET', getVoltage);}function checkVoltage() {

	if (document.getElementById('voltage').value == "220V Single Phase") {
				//find 220 Volt Single Phase in Optional Equipment and add it automagically
		var i = 0;
		while (document.getElementById('optional[' + i + ']') != null && document.getElementById('optional[' + i + ']').value != 0 && document.getElementById('optional[' + i + ']').value != "108") {
			i++;
		}
		if (document.getElementById('optional[' + i + ']') == null) {
			addOptEquipSelect();
		}
		//select 220V in the available optional equipment dropdown
		for (var j=0;j<document.getElementById('optional[' + i + ']').length;j++) {
			if (document.getElementById('optional[' + i + ']').options[j].value == 108) {
				document.getElementById('optional[' + i + ']').selectedIndex = j;
				//update prices	
				quantity = document.getElementById('opt_equip_quant[' + i + ']').value;
				ajaxLoad('getQuoteOptions.php?mode=opt_equip_price&array_id=0&opt_equip_id=' + document.getElementById('optional[0]').value + '&quantity=' + quantity,'GET',getOptEquipPrices);
				//add new optional equipment list
				document.getElementById('addLink').style.display = "block";
				break;
			}
		}
	}
	else {
		//check to see if 220 Volt is already in optional equipment and if so remove it
		i=0;
		while(document.getElementById('optional[' + i + ']') != null) {	
			if (document.getElementById('optional[' + i + ']').value == '108') {
				//delete select dropdown
				deleteOptEquipSelect(i);
			}
			i++;
		}
	}
}

function getVoltage(data) {
	if (typeof(data) != 'undefined' && data != '') {	
		var jsonData = eval('(' + data + ')');	
		if(typeof(jsonData.model) != 'undefined' && jsonData.model.length > 0) {
			modelPriceId = document.getElementById('voltage');			selectedId = 0;
			for (i=0;i<jsonData.model.length;i++) {						if (jsonData.model[i].selected == "true") {					selectedId = i;				}
				selectOption = createOption(jsonData.model[i].voltage,jsonData.model[i].voltage);
				modelPriceId.options.add(selectOption);
			}						modelPriceId.selectedIndex = selectedId;		}	}
	//setup optional equipement dropdown
	ajaxLoad('getQuoteOptions.php?mode=opt_equip&model_id=' + document.getElementById('model').value,'GET',getOptEquipOptions);	
}
function getOptEquipOptions(data) {
	var selectOption;	
	var selectElement;
	for (var j=0;j<getNumOptEquipSelects();j++) {
		selectElement = document.getElementById('optional[' + j + ']');	
		if(selectElement.value == 0) {
			resetSelectOptions(selectElement);
			selectOption = createOption('Choose one:',0);
			selectElement.options.add(selectOption);
		}
	}			
	//parse data into array of objects using json parser
	if (typeof(data) != 'undefined' && data != '') {
		var jsonData = eval('(' + data + ')');
		if(typeof(jsonData.equipment) != 'undefined') {
			for (i=0; i<jsonData.equipment.length; i++) {
				for (var j=0;j<getNumOptEquipSelects();j++) {
					selectElement = document.getElementById('optional[' + j + ']');	
					if(j == (getNumOptEquipSelects() - 1)) {
						selectOption = createOption(jsonData.equipment[i].desc,jsonData.equipment[i].id);
						selectElement.options.add(selectOption);
					}
				}
			}	
		}			
	}
	updatePrices();	
}

function updatePrices() {
	//get model price
	ajaxLoad('getQuoteOptions.php?mode=model_price&model_id=' + document.getElementById('model').value,'GET',getModelPrices);	
}

function getModelPrices(data) {
	if (typeof(data) != 'undefined' && data != '') {
		var jsonData = eval('(' + data + ')');	
		if(typeof(jsonData.model) != 'undefined' && jsonData.model.length > 0) {
			modelPriceId = document.getElementById('model_price');
			modelPriceId.replaceChild(document.createTextNode(jsonData.model[0].price),modelPriceId.firstChild);
		}
	}	
	quantity = document.getElementById('opt_equip_quant[0]').value;	
	ajaxLoad('getQuoteOptions.php?mode=opt_equip_price&array_id=0&opt_equip_id=' + document.getElementById('optional[0]').value + '&quantity=' + quantity,'GET',getOptEquipPrices);	
}

function getOptEquipPrices(data) {
	if (typeof(data) != 'undefined' && data != '') {	
		var jsonData = eval('(' + data + ')');	
		if(typeof(jsonData.equipment) != 'undefined' && jsonData.equipment.length > 0) {
			optEquipPriceId = document.getElementById('opt_equip_price[' + parseInt(jsonData.equipment[0].arrayId) + ']');
			optEquipPriceId.replaceChild(document.createTextNode(jsonData.equipment[0].price),optEquipPriceId.firstChild);			
			nextSelect = document.getElementById('optional[' + (parseInt(jsonData.equipment[0].arrayId) + 1) + ']');
			if(nextSelect != null) {
				quantity = document.getElementById('opt_equip_quant[' + (parseInt(jsonData.equipment[0].arrayId) + 1) + ']').value;
				ajaxLoad('getQuoteOptions.php?mode=opt_equip_price&array_id=' + (parseInt(jsonData.equipment[0].arrayId) + 1) + '&opt_equip_id=' + nextSelect.value + '&quantity=' + quantity,'GET',getOptEquipPrices);
			}
		}
	}
	getTotalPrice();
}


function getTotalPrice() {
	totalPriceId = document.getElementById('total_price');
	total = dollarStringToNum(document.getElementById('model_price').firstChild.nodeValue);
	
	for (i=0;i<getNumOptEquipSelects();i++) {
		if ((document.getElementById('opt_equip_price[' + i + ']') != null) && dollarStringToNum(document.getElementById('opt_equip_price[' + i + ']').firstChild.nodeValue) > 0) {
			total = (parseFloat(total) + parseFloat(dollarStringToNum(document.getElementById('opt_equip_price[' + i + ']').firstChild.nodeValue)));
		}
	}
	if (document.getElementById('other_quant') && document.getElementById('other_price_amt')) {
		total = (parseFloat(total) + parseFloat(dollarStringToNum(document.getElementById('other_quant').value)) *  parseFloat(dollarStringToNum(document.getElementById('other_price_amt').value)));
	}
	if(document.getElementById('freight').value) {
		total = (parseFloat(total) + parseFloat(dollarStringToNum(document.getElementById('freight').value)));
	}
	if(document.getElementById('discount_percent').value) {
		total = (parseFloat(total) - parseFloat(total) * parseFloat(dollarStringToNum(document.getElementById('discount_percent').value)) / 100);
	}
	if(document.getElementById('discount_amount').value) {
		total = (parseFloat(total) - parseFloat(dollarStringToNum(document.getElementById('discount_amount').value)));
	}	
	totalPriceId.replaceChild(document.createTextNode('$' + total),totalPriceId.firstChild);
}

function dollarStringToNum(string) {	
	var newString = '';
	for (var i=0;i<string.length;i++) {
		if (string.charAt(i) != '$' && string.charAt(i) != ',') {
			newString += string.charAt(i);
		}
	}
	return parseFloat(newString);
}

function getNumOptEquipSelects() {
	var i=0;
	var selectElement = eval("document.getElementById('optional[0]')");
	while (selectElement != null) {
		i++;		
		selectElement = document.getElementById('optional[' + i + ']');
	}
	return i;
}
function addArraySelect(divElement,arrayId) {
	var i=0;
	var selectElement = eval("document.getElementById('" + arrayId + "[0]')");
	if(typeof(selectElement) != 'undefined') {
		while (selectElement != null) {			
			i++;	
			selectElement = eval("document.getElementById('" + arrayId + "[" + i + "]" + "')");
		}
		selectElement = document.createElement('SELECT');
		selectElement.id = arrayId + '[' + i + ']';
		selectElement.name = selectElement.id;
		selectElement.style.width = '300px';
		selectElement.onchange = function(e){
			updatePrices();
			document.getElementById('addLink').style.display = 'block';
		}
		if (selectElement.captureEvents) selectElement.captureEvents(Event.CHANGE);
		divElement.appendChild(selectElement);
		populateOptions(selectElement);	
		return i;	
	}	
}

function createOption(title,val) {	
	//create new optional equipment menu
	var myOption = document.createElement('OPTION');
	myOption.text = title;	myOption.value = val;
	return myOption;}

function addDefaultSelectOption(selectElement) {
	switch(selectElement.id) {
		case 'model':
 			optionElement = createOption('Choose a machine first',0);
			break;
		default:
 			optionElement = createOption('Choose a model first',0);
	}
	selectElement.options.add(optionElement);
}

function resetSelectOptions(selectElement) {
	var numOptions = selectElement.options.length;
	for (var i=0;i<numOptions;i++) {
		selectElement.remove(0);
	}
}