/*
function getFocus() {
		var inputs = document.getElementsByTagName('input');
		var skip = false;
		for (var i=0; !(skip); i++) {
			if ((inputs.item(i) != null) && (inputs.item(i).type == 'text') && (inputs.item(i).style.display != 'none')) {
				inputs.item(i).select();
				inputs.item(i).focus();
				skip = true;
			} // end if
			else if (i >= inputs.length-1) skip = true;
		} // end for
	} // end function
*/
	function submitLogin() {
		document.form.submit();
	} // end function
	
	function chkFields(obj) {

		if (obj) {
			if (obj.value == 'Preview Order') submit();
		} // end if
		
		var ErrorMsg = '';
		var selects = document.getElementsByTagName('select');
		for (var i =0; i < selects.length; i++) {
			if (selects.item(i).value == 'choose')
				ErrorMsg += 'Please choose a value for ' + selects.item(i).title + "\n\n";
		} // end for
		
		var inputs = document.getElementsByTagName('input');
		var selects = document.getElementsByTagName('select');
		var rails = 0; var stiles = 0;
		var width = 0; var height = 0;
		for (var i =0; i < inputs.length; i++) {
			if (((inputs.item(i).value == '0') || (inputs.item(i).value == ""))
																				 && (inputs.item(i).name != 'shipping_address_two')
																				 && (inputs.item(i).name != 'billing_address_two')
																				 && (inputs.item(i).name != 'company')
																				 && ((inputs.item(i).name != 'cc_month') && (inputs.item(i).year != 'cc_month'))
																				 && (inputs.item(i).name != 'rails_center_whole')
																				 && (inputs.item(i).name != 'stiles_center_whole')
																				 && ((inputs.item(i).name != 'cc_year') && (inputs.item(i).year != 'cc_year'))
																				 && (inputs.item(i).id != 'tax')
																				 && (inputs.item(i).id != '')
																				 && (inputs.item(i).title != '')
																				 ) {
				ErrorMsg += 'Please enter a value for ' + inputs.item(i).title + "\n\n";
			} // end if
			var inputsname = inputs.item(i).name;
			if (inputsname.indexOf('rails_') != -1) {rails += parseFloat(inputs.item(i).value); }
			if (inputsname.indexOf('stiles_') != -1) {stiles += parseFloat(inputs.item(i).value);}
			if (inputsname.indexOf('width') != -1) {width += parseFloat(inputs.item(i).value); }
			if (inputsname.indexOf('height') != -1) {height += parseFloat(inputs.item(i).value);}
		} // end for
		for (var i =0; i < selects.length; i++) {
			var selectsname = selects.item(i).name;
			if (selectsname.indexOf('rails_') != -1) {rails += parseFloat(selects.item(i).value); }
			if (selectsname.indexOf('stiles_') != -1) {stiles += parseFloat(selects.item(i).value);}
			if (selectsname.indexOf('width') != -1) {width += parseFloat(selects.item(i).value); }
			if (selectsname.indexOf('height') != -1) {height += parseFloat(selects.item(i).value);}
		} // end for

		if ((rails >= height) && (rails != 0)) ErrorMsg += 'Total Rail Size cannot be larger than the height of the door.' + "\n\n";
		if ((stiles >= width) && (stiles != 0)) ErrorMsg += 'Total Stile Size cannot be larger than the width of the door.' + "\n\n";
				
		var textareas = document.getElementsByTagName('textarea');
		for (var i =0; i < textareas.length; i++) {
			if (textareas.item(i).value == "")
				ErrorMsg += 'Please enter a value for ' + textareas.item(i).title + "\n\n";
		} // end for

		if (ErrorMsg != '') {
			ErrorMsg = "MISSING DATA\n" + '----------------------------------------------------' + "\n\n" + ErrorMsg;
			alert(ErrorMsg);	
			return false;
		} else {
			obj.submit();
			return true;
		} // end else
	} // end function
	
	function retrieveInfo(elem) {
		document.getElementById('image').src = "images/thumbs/" + elem + ".jpg";
		document.getElementById('door_name').innerHTML = elem;
		document.getElementById('swatches').innerHTML = "<table><tr><td width=\"33%\"><img src=\"images/swatches/swatch001.jpg\" /></td><td width=\"33%\"><img src=\"images/swatches/swatch002.jpg\" /></td><td width=\"33%\"><img src=\"images/swatches/swatch003.jpg\" /></td></tr></table>";
	} // end function
	
	function submitRetail(obj, val) {
		obj.location.value = val;
		if (chkFields()) obj.submit();
	} // end function
	
	function setShipping(obj) {
		if (obj.checked) {
			document.getElementById('billing_address_one').value = document.getElementById('shipping_address_one').value;
			document.getElementById('billing_address_two').value = document.getElementById('shipping_address_two').value;
			document.getElementById('billing_city').value = document.getElementById('shipping_city').value;
			document.getElementById('billing_state').value = document.getElementById('shipping_state').value;
			document.getElementById('billing_zip').value = document.getElementById('shipping_zip').value;
			document.getElementById('billing_address_one').readOnly = true;
			document.getElementById('billing_address_two').readOnly = true;
			document.getElementById('billing_city').readOnly = true;
			document.getElementById('billing_state').readOnly = true;
			document.getElementById('billing_zip').readOnly = true;
			setTaxes();
		} // end if
		else {
			document.getElementById('billing_address_one').value = "";
			document.getElementById('billing_address_two').value = "";
			document.getElementById('billing_city').value = "";
			document.getElementById('billing_state').value = "";
			document.getElementById('billing_zip').value = "";
			document.getElementById('billing_address_one').readOnly = false;
			document.getElementById('billing_address_two').readOnly = false;
			document.getElementById('billing_city').readOnly = false;
			document.getElementById('billing_state').readOnly = false;
			document.getElementById('billing_zip').readOnly = false;
			setTaxes();
		} // end else
	} // end function
	
	function setPrice() {
		document.getElementById('shipping_method').value = document.getElementById('cost').innerHTML;
		var itemCost = parseFloat(document.getElementById('itemcost').innerHTML);
		if (document.getElementById('shipping_method').checked)	var shippingCost = parseFloat(document.getElementById('shipping_method').value);
		else var shippingCost = parseFloat(document.getElementById('shipping_method2').value);
		var taxesCost = parseFloat(document.getElementById('taxes').innerHTML);
		taxesCost = 1+(taxesCost/100);
		var totalTaxes = (parseInt(itemCost*parseFloat(document.getElementById('taxes').innerHTML))/100).toFixed(2);
		totalTaxes = parseFloat(totalTaxes);
		var handlingCost = parseFloat(document.getElementById('handling_method').value);
		if (isNaN(shippingCost)) shippingCost = 0;
		var totalCost = itemCost+totalTaxes+shippingCost+handlingCost;
		document.getElementById('totalcost').innerHTML = totalCost.toFixed(2);
		if (document.getElementById('cost').innerHTML == 0) document.getElementById('shipping_error').innerHTML = "We're sorry.  Due to a larger quantity of doors, shipping cannot be figured at this time.  Please <a href=\"contact.php\" target=\"_blank\">contact us</a> for shipping arrangements.";
		document.getElementById('shipping_method').value = shippingCost.toFixed(2);
		var tsh = shippingCost+handlingCost;
		if (isNaN(tsh)) { tsh = handlingCost; }
		document.getElementById('totalsandh').innerHTML = tsh.toFixed(2);
		document.getElementById('totaltaxes').innerHTML = (parseInt(itemCost*parseFloat(document.getElementById('taxes').innerHTML))/100).toFixed(2);
//		document.getElementById('tax').value = totalTaxes.toFixed(2);
	} // end function
	
	function showImage(val) { document.getElementById(val).style.visibility = 'visible'; }

	function hideImage(val) { document.getElementById(val).style.visibility = 'hidden'; }
	
function handleHttpResponse() {
  if (http.readyState == 4) {
    if (http.responseText.indexOf('invalid') == -1) {
      results = http.responseText;
			var txt = "Current Price: $"+results;
      document.getElementById('currPrice').innerHTML = txt;
      isWorking = false;
    } // end if
  } // end if
} // end function

var isWorking = false;

function updatePrice() {
	var quantity=document.getElementById('quantity').value;
	var doorid=document.getElementById('id').value;
	var width=parseFloat(document.getElementById('width').value) + parseFloat(document.getElementById('width_whole').value);
	var height=parseFloat(document.getElementById('height').value) + parseFloat(document.getElementById('height_whole').value);
	var param="quantity="+quantity+"&width="+width+"&height="+height+"&id="+doorid;
	if (!isWorking && http) {
    http.open("GET", "updateprice.php?"+escape(param), true);
    http.onreadystatechange = handleHttpResponse;
    isWorking = true;
    http.send(null);
	} // end if
} // end functino
function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object

function addDrawer() {
	var yes = confirm("Do you want to view the drawer front that goes with this door?");
	if (yes) {
		document.getElementById('loc').value = document.getElementById('loc').value + "&drawer_chkbx=true";
	} else {
		document.getElementById('loc').value = 'browse.php';
	} // end else
} // end function