/*
this file contains the functions necessary for building the connection object 
associated with the web server - KJP

Created June 5,2006
*/

//globals
var xmlHttp;
var answer = "";

function createRequest(){
	if (window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	} else {
        window.alert('Your browser needs to be updated in order to take advantage of the page functionality');
    }
}

function getInfo(sURL, sVars, box){
	createRequest();
	
	if (!xmlHttp) return null;
	try {
		xmlHttp.open("POST", sURL, true);
		xmlHttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = function handleStateChange()	{
			if (xmlHttp.readyState == 4) {
			
				if (xmlHttp.status == 200) {
				
					var comp = "text";
					if (box==comp){
						document.getElementById(box).innerHTML = xmlHttp.responseText;
					} else {
						if (box=="cartQuantity"){
							displayContents();
						} else if (box=="cartRemoveItem"){
							displayContents();
						} else if (box=="validateCustomer"){
							customerResults();
						} else if (box=="customerInfo"){
							completeCustomer();
						} else if (box=="completeSale"){
							completeSale();
						} else if (box=="customerEmail"){
							customerEmail();
						} else if (box=="orderEmail"){
							orderEmail();
						} else {
							alert(box);
						}
						
					}
					box = "";
				}
			}
		};
		xmlHttp.send(sVars);
	} catch(z) {return false;}
}

function openNewWindow(webaddress) {
  popupWin = window.open(webaddress)
}

function goSearch() {
  document.showCategory.submit();
}

function zeros(num){
	//see how many decimal places there are
	text = new String(num)
	one = text.indexOf(".")
	two = text.length
	if (one!=-1){
		differ = parseFloat(two)-parseFloat(one)
		if (differ == 3){
			return text
		}
		if (differ == 2){
			return text+"0"
		}
		if (differ == 1){
			return text+"00"
		}
	} else {
		return text+".00"
	}
}

function convert(num){
	text = new String(num)
	one = text.indexOf(".");
	two = text.length;
	//check to see if there are more than two decimal places
	differ = two-one;
	if (one == -1 || differ<=3){
		return text
	} else {
		//rounding is necessary
		ne = text.substring(0,one+4)
		k = ne.length
		mn = ne.substring(k-1,k)
		//check truncated substring third digit to round up or down
		if (mn<5) {
			return ne.substring(0,k-1);
		} else {
			placehold = "";
			placehold1 = "";
			for (hy = k; hy>=0; hy--){
				//check to see if loop is exhausted
				if (hy == "1"){
					by = "1";
					return by+ne.substring(0,hy-2)+placehold+placehold1;
					break;
				}
				//loop through each character place to check for nine's
				//take decimal point into account and skip over it
				if (ne.substring(hy-2,hy-1) == '.'){
				} else {
					if (ne.substring(hy-2,hy-1) != '9'){
						// the hundredths place is not nine
						ni = parseInt(ne.charAt(hy-2));
						ni += 1;
						return ne.substring(0,hy-2)+ni+placehold+placehold1;
						break;
					} else {
						//if the hundreds place is nine then loop
						placehold += "0";
						//add condition for more than two zeros (add decimal point)
						if (placehold == "00" && placehold1 == ""){
							//break apart to add decimal places
							placehold1 = ".00";
							placehold = "";
						}
					}
				}
			}
		}
	}
}
