var timeoutId, request;
var box_element;
var queryString;
var which_get = "search"
old_results = "";
function handleCheck(){
	  if(request.readyState == 4){
   		 clearTimeout(timeoutId);
    	if(request.status == 200){
      		//Implement document object in DOM
      		xmlReturnVal = request.responseText;
					if(which_get == "search") {
					if (box_element == "tire") {
					show_hide('none', 'by_tire_title');
			show_hide('block', 'result_title');
						show_hide('none', 'loading_box_II');
						if(document.getElementById("content_results")) {
						old_results = document.getElementById("content_results").innerHTML;
						}
						document.getElementById("results_box").innerHTML = "<div id=\"content_results\" style=\"height: 100%; overflow-y: scroll; overflow-x: hidden;\">"+xmlReturnVal+old_results+"</div>";
			show_hide('none', "by_tire_box");
			show_hide('block', "results_box");
			document.getElementById('content_results').scrollTop = 0;
					} else if (box_element == "vehicle") {
					show_hide('none', 'by_vehicle_title');
			show_hide('block', 'result_title');
						show_hide('none', 'loading_box');
						if(document.getElementById("content_results")) {
						old_results = document.getElementById("content_results").innerHTML;
						}
			document.getElementById("results_box").innerHTML = "<div id=\"content_results\" style=\"height: 100%; overflow-y: scroll; overflow-x: hidden;\">"+xmlReturnVal+old_results+"</div>";
			show_hide('none', "by_vehicle_box");
			show_hide('block', "results_box");
			document.getElementById('content_results').scrollTop = 0;
					} else {
			document.getElementById(box_element).innerHTML = xmlReturnVal;
			show_hide('block', box_element);
				if (document.getElementById(box_element+'_label')) {
					show_hide('block', box_element+"_label");
				}
				if (box_element == "model_box") {
					show_hide('block', 'submit_box');
					show_hide('block', 'vehicle_submit');
				}
					show_hide('none', 'loading_box');
			}
				} else if (which_get == "item") {
					document.getElementById("item_content").innerHTML = xmlReturnVal;
					show_hide('none', 'item_loading_box');
					show_hide('block', 'item_content');
					which_get = "search";
					document.getElementById('item_content').scrollTop = 0;
				}
     } else {   
      alert("A problem occurred with communicating between the XMLHttpRequest object and the server program. " + request.status);
    }
  }//end outer if
 }
 
 function warn(){
   request.abort();
   alert("A problem occurred with communicating with the server program. Please make sure you are connected to the Internet and try again in a few moments.");
 }

function initReq(reqType,url,bool){
    try{
        /* Specify the function that will handle the HTTP response */
       request.onreadystatechange=handleCheck; 
        request.open(reqType,url,bool);
		 request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
    	request.send(null);
    } catch (errv) {

        alert(
                "The application cannot contact the server at the moment. "+
                "It may be a problem with cross-domain restrictions." );
    }
}

/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
function httpRequest(reqType,url,asynch) {
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
       initReq(reqType,url,asynch);
    }  else {
       alert("Your browser does not permit the use of all "+
       "of this application's features!");
			 }
}

function next_step(mode,iteration) {
if (mode == "tire") {
	document.getElementById('mode').value = "tire";
		show_hide('none', 'by_store_box');
		show_hide('none', 'by_store_title');
		show_hide('none', 'by_vehicle_box');
		show_hide('none', 'by_vehicle_title');
		show_hide('block', 'by_tire_box');
		show_hide('block', 'by_tire_title');
		show_hide('none', 'results_box');
		show_hide('none', 'result_title');
		show_hide('block', 'tire_submit');
} else if (mode == "vehicle") {
if(iteration == 0) {
	document.tire_form.make.selectedIndex = 0;
		show_hide('none', 'mode');
		show_hide('none', 'by_tire_box');
		show_hide('none', 'by_tire_title');
		show_hide('none', 'by_store_box');
		show_hide('none', 'by_store_title');
		show_hide('block', 'by_vehicle_box');
		show_hide('block', 'by_vehicle_title');
		show_hide('none', 'results_box');
		show_hide('none', 'result_title');
		show_hide('none', 'year_box');
		show_hide('none', 'model_box');
		show_hide('none', 'submit_box');
		show_hide('none', 'year_box_label');
		show_hide('none', 'model_box_label');
} else {
	/*for(i = 0; i < document.tire_form["store"].length; i++)
	{
		if( document.tire_form["store"][i].checked == true ) {
			store = document.tire_form["store"][i].value;
		break;
		}
		}*/
		store = document.getElementById('store').value;
if (iteration == 1) {
	box_element = "year_box";
	var make = document.getElementById('make').value;
	queryString = "&make="+encodeURIComponent(make)+"&store="+encodeURIComponent(store);
		httpRequest("GET","get_stock.php?mode=vehicle"+queryString,true);
		show_hide('none', 'year_box');
		show_hide('none', 'model_box');
		show_hide('none', 'submit_box');
		show_hide('none', 'year_box_label');
		show_hide('none', 'model_box_label');
		show_hide('block', 'loading_box');
} else if (iteration == 2) {
	box_element = "model_box";
		show_hide('none', 'model_box');
		show_hide('none', 'submit_box');
		show_hide('none', 'model_box_label');
		show_hide('block', 'loading_box');
	var make = document.getElementById('make').value;
	var year = document.getElementById('year').value;
		queryString = "&make="+encodeURIComponent(make)+"&year="+encodeURIComponent(year)+"&store="+encodeURIComponent(store);
		httpRequest("GET","get_stock.php?mode=vehicle"+queryString,true);
			}
		}
	}
}
function submit_get(get_type) {
	/*for(i = 0; i < document.tire_form["store"].length; i++)
	{
		if( document.tire_form["store"][i].checked == true ) {
			store = document.tire_form["store"][i].value;
		break;
		}
	}*/
			store = document.getElementById('store').value;

if (get_type == "tire") {
	var height = document.getElementById('height').value;
	var ratio = document.getElementById('ratio').value;
	var diameter = document.getElementById('diameter').value;
	box_element = "tire";
		queryString = "&height="+encodeURIComponent(height)+"&ratio="+encodeURIComponent(ratio)+"&diameter="+encodeURIComponent(diameter)+"&store="+encodeURIComponent(store);
		httpRequest("GET","get_stock.php?mode=tire"+queryString,true);
		document.getElementById('tire_submit').style.display = "none";
		show_hide('block', 'loading_box_II');
} else if (get_type == "vehicle") {
	box_element = "vehicle";
	var make = document.getElementById('make').value;
	var year = document.getElementById('year').value;
	var model = document.getElementById('model').value;
		queryString = "&make="+encodeURIComponent(make)+"&year="+encodeURIComponent(year)+"&store="+encodeURIComponent(store)+"&model="+encodeURIComponent(model);
		httpRequest("GET","get_stock.php?mode=submit"+queryString,true);
		show_hide('none', 'vehicle_submit');
		show_hide('block', 'loading_box');
}
}
function show_stuff(stuff_type) {
	if (stuff_type == "store") {
		show_hide('block', 'by_store_box');
		show_hide('block', 'by_store_title');
		show_hide('none', 'by_vehicle_box');
		show_hide('none', 'by_vehicle_title');
		show_hide('none', 'by_tire_box');
		show_hide('none', 'by_tire_title');
		show_hide('none', 'result_title');
		show_hide('none', 'results_box');
	} else if (stuff_type == "tire") {
		next_step('tire');
	} else if (stuff_type == "vehicle") {
		next_step('vehicle',0);
	} else if (stuff_type == "results") {
		show_hide('none', 'by_store_box');
		show_hide('none', 'by_store_title');
		show_hide('none', 'by_vehicle_box');
		show_hide('none', 'by_vehicle_title');
		show_hide('none', 'by_tire_box');
		show_hide('none', 'by_tire_title');
		show_hide('block', 'result_title');
		show_hide('block', 'results_box');
	}
}
function item_pop(id) {
	which_get = "item";
		queryString = "&id="+id;
		httpRequest("GET","get_stock.php?mode=item"+queryString,true);
		show_hide('none', 'item_content');
		show_hide('block', 'gray_fade');
		show_hide('block', 'item_loading_box');
		show_hide('block', 'item_display');
}
function show_hide(s_h, elem) {
	document.getElementById(elem).style.display = s_h;
}
