function isValidQty(sText) {
	//quantity must be whole number > 0
	var ValidChars = "0123456789";
	var IsValid = true;
	var Char;
	for (i = 0; i < sText.length && IsValid == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) {
			IsValid = false;
		}
	}
	if (IsValid) {
		if (eval(sText) < 1) {	
			IsValid = false;
		}
	}
	return IsValid;
}

function getAjaxRequest() {
	try {
		oAjaxRequest = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			oAjaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				oAjaxRequest = new ActiveXOjbect("Microsoft.XMLHTTP");
			} catch (failed) {
				oAjaxRequest = false;
			}
		}
	}
	return oAjaxRequest;
}

function getCart() {
	var url = 'controller?action=getCart';
	ajaxRequest = getAjaxRequest();
	ajaxRequest.open("POST", url, true);
	ajaxRequest.onreadystatechange = updateCartResponse;
	ajaxRequest.send(null);

}

function removeRebuiltItem(index) {
	var url = 'controller?action=removeRebuiltItem';
		url += '&index='+index;
	ajaxRequest = getAjaxRequest();
	ajaxRequest.open("POST", url, true);
	ajaxRequest.onreadystatechange = updateCartResponse;
	ajaxRequest.send(null);

}
	
function addToCartRequest(formName) {
	form = document.getElementById(formName);
	if (isValidQty(form.quantity.value)) {
		var url = 'controller?action=addRebuiltItem';
			url += '&productname='+form.productname.value;
			url += '&partnumber='+form.partnumber.value;
			url += '&price='+form.price.value;
			url += '&freight='+form.freight.value;
			url += '&quantity='+form.quantity.value;
		ajaxRequest = getAjaxRequest();
		ajaxRequest.open("POST", url, true);
		ajaxRequest.onreadystatechange = updateCartResponse;
		ajaxRequest.send(null);
	} else {
		alert('Enter a valid quantity.');
	}
}
	

function updateCartResponse() {
	if (ajaxRequest.readyState == 4) {
		if (ajaxRequest.status == 200) {
			thecart = ajaxRequest.responseText;
			if (thecart.indexOf('nothingincart')==-1) {
				document.getElementById('divCart').innerHTML = thecart;
				document.getElementById('divCart').style.display = 'block';
			} else {
				document.getElementById('divCart').innerHTML = '';
				document.getElementById('divCart').style.display = 'none';
			}
			//ajaxCallFetchCart();
		} else {
			alert("HTTP Error: " + ajaxRequest.status);
		}
	}
}

function validateOrder() {
		var msgInvalid = "";
		
		if (document.getElementById("bcompany").value.length<2) {
			msgInvalid += "Billing name is required.\n";
		}
		if (document.getElementById("baddress1").value.length<2) {
			msgInvalid += "Billing address is required.\n";
		}
		if (document.getElementById("bcity").value.length<2) {
			msgInvalid += "Billing city is required.\n";
		}
		if (document.getElementById("bstate").value.length<2) {
			msgInvalid += "Billing state is required.\n";
		}
		if (document.getElementById("bpostal").value.length<2) {
			msgInvalid += "Billing zip code is required.\n";
		}
		
		if (!document.getElementById("sameinfo").checked) {
			if (document.getElementById("scompany").value.length<2) {
				msgInvalid += "Shipping name is required.\n";
			}
			if (document.getElementById("saddress1").value.length<2) {
				msgInvalid += "Shipping address is required.\n";
			}
			if (document.getElementById("scity").value.length<2) {
				msgInvalid += "Shipping city is required.\n";
			}
			if (document.getElementById("sstate").value.length<2) {
				msgInvalid += "Shipping state is required.\n";
			}
			if (document.getElementById("spostal").value.length<2) {
				msgInvalid += "Shipping zip code is required.\n";
			}
		}
		
		if (document.getElementById("phone1").value.length<3||
			document.getElementById("phone2").value.length<3||
			document.getElementById("phone3").value.length<4) {
				msgInvalid += "Phone number is required.\n";
		}
		

		if (document.getElementById("cardtype").value == "") {
			msgInvalid += "Credit card type is required.\n";
		}
		if (document.getElementById("cardnumber").value == "") {
			msgInvalid += "Credit catd number is required.\n";
		} else {
			if ((document.getElementById("cardnumber").length < 15)||isNaN(document.getElementById("cardnumber").value)) {
				msgInvalid += "Invalid credit card number.\n";
			}
		}
		if ((document.getElementById("expmonth").value == "")||(document.getElementById("expyear").value == "")) {
			msgInvalid += "Credit card expiration date is required.\n";
		}
		if (document.getElementById("cardid").value == "") {
			msgInvalid += "Credit card verification number is required.\n";
		}
		
		if (msgInvalid=="") {
			document.formCheckout.submit();
		} else {
			alert(msgInvalid);
		}
	}
	
	
	function copySameInfo() {
		if (document.getElementById('sameinfo').checked) {
			document.getElementById('sfirstname').value = document.getElementById('bfirstname').value;
			document.getElementById('slastname').value = document.getElementById('blastname').value;
			document.getElementById('saddress1').value = document.getElementById('baddress1').value;
			document.getElementById('saddress2').value = document.getElementById('baddress2').value;
			document.getElementById('scity').value = document.getElementById('bcity').value;
			for (i=0;i<document.getElementById('sstate').options.length;i++) {
				if (document.getElementById('sstate').options[i].value==document.getElementById('bstate').value) {
					document.getElementById('sstate').options[i].selected = true;
				}
			}
			document.getElementById('spostal').value = document.getElementById('bpostal').value;
		}

	}
	
	
function sendemail() {
	var msg = '';
	if (document.getElementById('name').value=='') {
		msg += 'Name is required.\n';
	}
	if (document.getElementById('email').value=='') {
		msg += 'Email is required.\n';
	}
	if (msg=='') {
		document.formcontact.submit();
	} else {
		alert(msg);
	}
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// JavaScript Document

//startList = function() {
//if (document.all&&document.getElementById) {
//navRoot = document.getElementById("nav");
//for (i=0; i<navRoot.childNodes.length; i++) {
//node = navRoot.childNodes[i];
//if (node.nodeName=="LI") {
//node.onmouseover=function() {
//this.className+=" over";
//  }
//  node.onmouseout=function() {
//  this.className=this.className.replace(" over", "");
//   }
//   }
//  }
// }
//}
//window.onload=startList;


//http://www.htmldog.com/articles/suckerfish/dropdowns/example/vertical.html#

var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;

sfHover = function() {
	if (!IE6) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



