// JavaScript Document
var t = false;
var counterTopPrice = '129.99';
var plexiGlassPrice = '39.99';
var performancePrice = '79.99';
var triSwatchPrice = '0.20';
var salesBookPrice = '4.99';
var dripBottlesPrice = '0.40';
var emptyKitPrice = '0.50';

function updateTotal() {
	var counterQty = document.getElementById('counterTop').value;
	var plexiGlassQty = document.getElementById('plexiglassBottle').value;
	var performanceQty = document.getElementById('performanceBoards').value;
	var triSwatchesQty = document.getElementById('triSwatches').value;
	var salesBooksQty = document.getElementById('salesBooks').value;
	var dripBottlesQty = document.getElementById('dripBottles').value;
	var emptyKitQty = document.getElementById('emptyKit').value;
	
	var total = (counterQty * counterTopPrice) + (plexiGlassQty * plexiGlassPrice) + (performancePrice * performanceQty) + (triSwatchPrice * triSwatchesQty) + (salesBookPrice * salesBooksQty) + (dripBottlesQty * dripBottlesPrice) + (emptyKitQty * emptyKitPrice);
	
	document.getElementById('totalSum').innerHTML = '$'+total.toFixed(2);
	document.getElementById('good_url').value = 'products.php?action=order&value='+total.toFixed(2);
}

function getCompany() {
	if (t) {
		window.clearTimeout(t);
	}
	if (document.forms.orderform.retailer.value != '') {
		t = window.setTimeout('getCompanyTimed()',200);
	} else {
		document.getElementById('LSResult').style.display = 'none';
	}
}
function getCompanyTimed() {
	ajax.elementObj = document.getElementById('LSResult');
	ajax.requestFile = 'ajaxLookup.php';	// Specifying which file to get
	ajax.onCompletion = showContent;	// Specify function that will be executed after file has been found
	//ajax.onLoading = showWaitMessage;	// Action when AJAX is loading the file
	ajax.URLString = '';
	ajax.method = 'GET';
	ajax.runAJAX('type=company&search='+document.forms.orderform.retailer.value);		// Execute AJAX function	
}
function showContent()	// Displaying content in the content <div>
{
	ajax.elementObj.style.display = 'block';
	ajax.elementObj.innerHTML = ajax.response;	// ajax.response is a variable that contains the content of the external file	
//	document.getElementById('ajaxLoading').style.display = 'none';
	ajax.elementObj = '';
}
function getCompanyPrice(company) {
	ajax.elementObj = null;
	document.getElementById('LSResult').style.display = 'none';
	ajax.requestFile = 'ajaxLookup.php';	// Specifying which file to get
	ajax.onCompletion = updateCompanyPrice;	// Specify function that will be executed after file has been found
	//ajax.onLoading = showWaitMessage;	// Action when AJAX is loading the file
	ajax.URLString = '';
	ajax.method = 'GET';
	ajax.runAJAX('type=prices&companyid='+company+'&search='+document.forms.orderform.retailer.value);		// Execute AJAX function
	return false;
}

function updateCompanyPrice() {
	var ctpResponse = '';
	var response = ajax.responseXML;
	document.forms.orderform.retailer.value = response.getElementsByTagName("company")[0].firstChild.nodeValue;
	
	xmlResponse = response.getElementsByTagName("counterTop")[0].firstChild;
	var textbox = document.getElementById('counterTop');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		counterTopPrice = ''
		textbox.value = '';
	} else {
		counterTopPrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('counterTopsPrice').innerHTML = ((xmlResponse != null) ? '$'+counterTopPrice.toFixed(2) : '<span class="notAvailable">Product Not Available</span>');

	xmlResponse = response.getElementsByTagName("plexiGlass")[0].firstChild;
	textbox = document.getElementById('plexiglassBottle');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		plexiGlassPrice = ''
		textbox.value = '';
	} else {
		plexiGlassPrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('plexiglassBottlePrice').innerHTML = ((xmlResponse != null) ? '$'+plexiGlassPrice.toFixed(2) : '<span class="notAvailable">Product Not Available</span>');

	xmlResponse = response.getElementsByTagName("performance")[0].firstChild;
	textbox = document.getElementById('performanceBoards');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		performancePrice = ''
		textbox.value = '';
	} else {
		performancePrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('performanceBoardsPrice').innerHTML = ((xmlResponse != null) ? '$'+performancePrice.toFixed(2) : '<span class="notAvailable">Product Not Available</span>');

	xmlResponse = response.getElementsByTagName("triSwatch")[0].firstChild;
	textbox = document.getElementById('triSwatchesPrice');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		triSwatchPrice = ''
		textbox.value = '';
	} else {
		triSwatchPrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('triSwatchesPrice').innerHTML = ((xmlResponse != null) ? '$'+triSwatchPrice.toFixed(2) : '<span class="notAvailable">Product Not Available</span>');

	xmlResponse = response.getElementsByTagName("salesBook")[0].firstChild;
	textbox = document.getElementById('salesBooks');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		salesBookPrice = ''
		textbox.value = '';
	} else {
		salesBookPrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('salesBooksPrice').innerHTML = ((xmlResponse != null) ? '$'+salesBookPrice.toFixed(2) : '<span class="notAvailable" style="font-size:0.8em;">Sales books are not available to your company. Please contact UV3 directly.</span>');

	xmlResponse = response.getElementsByTagName("dripBottles")[0].firstChild;
	textbox = document.getElementById('dripBottles');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		dripBottlesPrice = ''
		textbox.value = '';
	} else {
		dripBottlesPrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('dripBottlesPrice').innerHTML = ((xmlResponse != null) ? '$'+dripBottlesPrice.toFixed(2) : '<span class="notAvailable">Product Not Available</span>');

xmlResponse = response.getElementsByTagName("emptyKit")[0].firstChild;
	textbox = document.getElementById('emptyKit');
	textbox.disabled = (xmlResponse == null);
	if (xmlResponse == null) {
		dripBottlesPrice = ''
		textbox.value = '';
	} else {
		dripBottlesPrice = parseFloat(xmlResponse.nodeValue);
	}
	document.getElementById('emptyKitPrice').innerHTML = ((xmlResponse != null) ? '$'+emptyKitPrice.toFixed(2) : '<span class="notAvailable">Product Not Available</span>');
}
