var updating1=true;
var updating2=true;

function updateOptions() {
	updating1=true;

         if (window.XMLHttpRequest)
         {// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
         }
         else
         {// code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }  

	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			document.getElementById("make").innerHTML=xmlhttp.responseText;
			updateModels();
			updating1=false;
		}
	}

	xmlhttp.open("GET", "./php/gains.php", true);

	xmlhttp.send();
}

function updateModels() {
	updating2=true;

         if (window.XMLHttpRequest)
         {// code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
         }
         else
         {// code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }  

	xmlhttp.onreadystatechange=function() {
		if(xmlhttp.readyState==4) {
			document.getElementById("model").innerHTML=xmlhttp.responseText;
			showGains();
			updating2=false;
		}
	}

	if(document.getElementById("drp_make")) {
		xmlhttp.open("GET", "./php/gains.php?make=" + document.getElementById("drp_make").value, true);

		xmlhttp.send(null);
	}
}

function showGains() {

	var make = document.getElementById("drp_make").value;
	var model = document.getElementById("drp_model").value;

	if ((!updating1) && (!updating2) && (make!="blank") && (model!="blank")) {		

		document.getElementById("selMake").innerHTML=make;
		document.getElementById("selModel").innerHTML=model;

		showPopup();

        	 if (window.XMLHttpRequest)
	         {// code for IE7+, Firefox, Chrome, Opera, Safari
        	          xmlhttp=new XMLHttpRequest();
	         }
        	 else
	         {// code for IE6, IE5
        	          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	         }  

		xmlhttp.onreadystatechange=function() {
			if(xmlhttp.readyState==4) {
				document.getElementById("gains").innerHTML=xmlhttp.responseText;
			}
		}

		xmlhttp.open("GET", "./php/gains.php?make=" + make + "&model=" + model, true);

		xmlhttp.send(null);
	}
	else if ((!updating1) && (!updating2)) {
		alert("Please select both a make and model before clicking Continue.");
	}

}

function updateGains() {

}

function showPopup() {
	if(document.getElementById("drp_make")) {
		document.getElementById("drp_make").selectedIndex=0;
		updateModels();
	}

         // Define size of the popup window
         popupHeight = 350;
         popupWidth = 400;

         // Get the dimensions of the screen and of the document - use largest to ensure backdrop covers visible area
         screenHeight = document.documentElement.clientHeight;
         screenWidth = document.documentElement.clientWidth;

         docHeight = document.body.offsetHeight + 50;
         docWidth = document.body.offsetWidth;

         // Set backdrop dimensions as the max of the above
         backdropHeight = Math.max(screenHeight, docHeight);
         backdropWidth = Math.max(screenWidth, docWidth);

         // Define top left corner coords to centre the popup
         xPos = (screenWidth - popupWidth) * 0.5;
         yPos = (screenHeight - popupHeight) * 0.5;

         // Size backdrop
         document.getElementById("backdrop").style.height = backdropHeight;
         document.getElementById("backdrop").style.width = backdropWidth;

         // Position and size popup
         document.getElementById("popup").style.height = popupHeight;
         document.getElementById("popup").style.width = popupWidth;
         document.getElementById("popup").style.left = xPos;
         document.getElementById("popup").style.top = yPos;

         // Display the overlaying elements
	document.getElementById("popup").style.visibility="visible";
	document.getElementById("backdrop").style.visibility="visible";
         fadeToBlack(5);
}

function hidePopup() {
	document.getElementById("popup").style.visibility="hidden";
	document.getElementById("gains").innerHTML="<img src='graphix/loading.gif'>";

	fadeThroughBlack(75);
	updateOptions();
}

function fadeToBlack(i) {
	document.getElementById("backdrop").style.filter="alpha(opacity="+i+")";
	document.getElementById("backdrop").style.opacity=""+(i/10);

	if (i<75) {
		t = setTimeout("fadeToBlack("+(i+10)+")", 50);
         }
	else {
		clearTimeout();
         }
}

function fadeThroughBlack(i) {
	document.getElementById("backdrop").style.filter="alpha(opacity="+i+")";
	document.getElementById("backdrop").style.opacity=""+(i/10);
	
	if (i>5) {
		t = setTimeout("fadeThroughBlack("+(i-10)+")", 50);
	}
	else {
		clearTimeout();
		document.getElementById("backdrop").style.visibility="hidden";
	}
}

function growBox() {
	document.getElementById("drp_model").style.width="250px";
}

function shrinkBox() {
	document.getElementById("drp_model").style.width="115px";
}
