// JavaScript Document

function setQueryString(){
queryString="";
var frm = document.forms[0];
var numberElements = frm.elements.length;
for(var i = 0; i < numberElements; i++) {
if(i < numberElements-1) {
queryString += frm.elements[i].name+"="+
encodeURIComponent(frm.elements[i].value)+"&";
} else {
queryString += frm.elements[i].name+"="+
encodeURIComponent(frm.elements[i].value);
}

}
}

var request;
var queryString; //will hold the POSTed data
function sendData(){
setQueryString();
var url="designplate_process.php";
httpRequest("POST",url,true);
}

/* Initialize a request object that is already constructed.
Parameters:
reqType: The HTTP request type, such as GET or POST.
url: The URL of the server program.
isAsynch: Whether to send the request asynchronously or not. */
function initReq(reqType,url,isAsynch){
/* Specify the function that will handle the HTTP response */
request.onreadystatechange=handleResponse;
request.open(reqType,url,isAsynch);
/* Set the Content-Type header for a POST request */
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8");
request.send(queryString);
}

/* 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
//initialization succeeded
if(request){
initReq(reqType,url,asynch);
} else {
alert("Your browser does not permit the use of all "+
"of this application's features!");
}
}

//event handler for XMLHttpRequest
function handleResponse( ){
if(request.readyState == 4){
if(request.status == 200){
results = request.responseText;
//alert(results);
var data=results.split("=")

document.getElementById('image1').src = data[0];
document.getElementById('image2').src = data[1];
var view=data[2];
if(view=="Both")
{
document.getElementById('image1').style.display="";
document.getElementById('image2').style.display=""
}
if(view=="Front")
{
document.getElementById('image1').style.display="none";
document.getElementById('image2').style.display=""
}
if(view=="Rear")
{
document.getElementById('image1').style.display="";
document.getElementById('image2').style.display="none"
}
document.getElementById('price').innerHTML=data[3];
} else {
  // Tony Byng of The Company Merchant Limited commented out this alert 23/04/09  because on Firefox,
  // if the screen is moved onto the next screen whilst the ajax is still running the user
  // gets to see this message because the original script is no longer available
 //alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
}
}//end outer if
} 