var xmlHttp = createXmlHttp();

function pageLoad(){
	document.getElementById("newListing").style.display = "none";
	document.getElementById("listingLoader").style.display = "block";
}

function process(section, sort, searchText, equipType){
	
	if (typeof document.body.style.maxHeight != "undefined") {
	  // IE 7, mozilla, safari, opera 9
		path = "equipment/getListing.php";
	} else {
	  // IE6, older browsers
		path = "../../equipment/getListing.php";
	}
	
	
	globSection = section;
	globSort = sort;
	globText = searchText;
	globType = equipType;
	
	//alert(searchText);
	myDiv = document.getElementById("listings");
	myDiv.innerHTML = "<div style=\"text-align: center; font-weight: bold;\"><img src=\"images/equipment/loading.gif\" border=\"0\"/><br/><br/>Loading Data . . .</div>";
	

	//alert(path);
	vars = "?section="+section+"&sort="+sort+"&searchText="+searchText+"&equipType="+equipType;
	//alert(path+vars);
	//only continue if xmlHttp isn't void
	//alert(xmlHttp.readyState);
	if(xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
		//try to connect to the server
		try{
			// Initiate reading the async.txt file from the server
			xmlHttp.open("GET", path+vars, true);
			//Send the proper header information along with the request
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);

		}catch(z){
			//Display the error in case of failure
			alert("Can't Connect to the server:\n"+ z.toString());
		}
		
	}
	
	
}// END Process Function



/*************************************
***** HANDLE REQUESTS START HERE *****
*************************************/

// Function that handles the http response
function handleRequestStateChange(){
	//alert("Handling");
	// Obtain a reference to the <div> element on the page
	myDiv = document.getElementById("listings");
	myDiv2 = document.getElementById("newListing");
			//Continue only if the server response if "OK"
			if(xmlHttp.readyState == 4){
				
				if(xmlHttp.status == 200){
				try{
					//Read the message from the server
					response = xmlHttp.responseText;
					document.getElementById("listingLoader").style.display = "none";
					myDiv2.style.display = "block";
					// Display the result
					myDiv.innerHTML = response;

				}catch(z){
					alert("Error reading the response: "+ z.toString());
				}
				
			}else{
				// Display error message
				alert("There was an error getting the data:\n" + xmlHttp.statusText)
			}
			}
			
}


function changeBGover(myDiv, imgName, image){
	 myDiv.style.background = "#FF822C"
	 document[imgName].src = image;
}

function changeLink(link, page){
	browserName=navigator.appName;
	if(browserName == "Microsoft Internet Explorer"){
		path = "";
	}else{
		path = "equipment/";
	}
	if(link == "all"){
		window.location=path+page;
	}else{
		window.location=path+page+"?section="+link;
	}
}

function changeBGout(myDiv, imgName, image){
	 myDiv.style.background = "#000"
	 document[imgName].src = image;
}

function hideDiv(theDiv){
	
	//alert(theDiv);
	
	if(document.getElementById(theDiv).style.display == "none"){
		image = "images/button_minus.gif";
		document.getElementById(theDiv).style.display = "block";
		document.getElementById(theDiv+"Img").src = image;
	}else{
		image = "images/button_plus.gif";
		document.getElementById(theDiv).style.display = "none";
		document.getElementById(theDiv+"Img").src = image;
	}
	
}

function sortBy(sort){
	process(globSection, sort, globText, globType);
}

function poptastic(id){

	if (navigator.appName=="Microsoft Internet Explorer"){
		path = "details.php?id="+id;
	}else{
		path = "equipment/details.php?id="+id;
	}		

	//alert(path);
	
	newwindow=window.open(path,'name','top=100, left=300, height=800,width=850,scrollbars=1');
	
	if (window.focus) {newwindow.focus()}
}

function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(url,title);
  } else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(title,url,"");
  } else {
    alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
  }
}

function showEmail(){
	if(document.getElementById("emailDiv").style.display == "none"){
		document.getElementById("emailDiv").style.display = "block";
	}else{
		document.getElementById("emailDiv").style.display = "none";
	}
}

