// JavaScript Document


function selectedBike(specialOffer)
{
	if(bar1){
		bar1.showBar();
	}
	// selectName is the id of the select that fired the event
	// divID is the id of the DIV to contain the returned data
	var selectObj = document.getElementById("modelSelect");
	
	var modelMCN = selectObj.options[selectObj.selectedIndex].value;
	
	makeRequest("/cgi-bin/aspPages/theBikes/usedBikes/getBikeList.asp", "?modelMCN=" + modelMCN + "&specialOffer=" + specialOffer, "returnedBikes");

	//document.location = "#returnedBikes";
}

function selectedManufacturer(manufacturer, divID)
{
	if(bar1){
		bar1.showBar();
	}
	// set divID to loading status
	//document.getElementById(divID).innerHTML = document.getElementById("loading").innerHTML;
	
	// set the retrieved bikes div back to its default
	var content = document.getElementById("returnedBikesBlank").innerHTML;
	document.getElementById("returnedBikes").innerHTML = content;
	
	//replaceDiv('returnedBikes','returnedBikesBlank');
	
	makeRequest("/cgi-bin/aspPages/theBikes/usedBikes/getManufacturer.asp", "?manufacturer=" + manufacturer, divID);

}

function showBikesFullDetails(bikeRef){

	// first we need to move the current contents of the mainUsedBikes div to the tempHolder div
	if(bar1){
		bar1.showBar();
	}
	var content = document.getElementById("mainUsedBikes").innerHTML;
	
	document.getElementById("tempHolder").innerHTML = content;
	
	//document.getElementById("mainUsedBikes").innerHTML = "TEST";
	makeRequest("/cgi-bin/aspPages/theBikes/usedBikes/showFullBikeDetails.asp", "?bikeRef=" + bikeRef, "mainUsedBikes");

	document.location='#top';

}

function returnToUsedBikesListing(blank){

// need to overwrite the mainUsedBikes div with the contents of tempHolder

var content = document.getElementById("tempHolder").innerHTML;

document.getElementById("mainUsedBikes").innerHTML = content;


}


function makeRequest(url, queryString, divID) {
	
	//document.getElementById("miniLoadingMessage").style.display = "inline";
	//alert("Making Request: " + url + queryString + ", " + divID); 
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		  http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/html');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
		
		http_request.onreadystatechange = function() { responseHandler(http_request, divID); };
        http_request.open('GET', url + queryString, true);
		http_request.send(null);
				
}

function responseHandler(http_request, divID) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				
				//populate tempHolder div with the AJAX response
				if(divID!="null"){
				document.getElementById(divID).innerHTML = "";
				document.getElementById(divID).innerHTML = http_request.responseText;
				//document.getElementById("miniLoadingMessage").style.display="none";

				}
				// fade in/out
				
				//replaceDiv(divID,'tempHolder');
				if(bar1){
					bar1.hideBar();
				}

            } else {
               alert('There was a problem with the request.');
			   bar1.hideBar();
            }
        }
		
}

// swaps the images in the main bike details
function changeImage(image){

	//get mainImage obj
	var mainImage = document.getElementById("mainImage");
	mainImage.src = image;

}

function emailRegardingUsedBike(formRef, divID){
	
	// we need to get all the data from the form to create the queryString
	var queryString = "?";
	var first = true;
	var form = document.getElementById(formRef);
	var bikeModel;
	for (var i=0;i<form.length;i++)
	{
	if(form.elements[i].type != "button" & form.elements[i].type != "reset" & form.elements[i].id != "legend")
	{
		if(!first){
				queryString += "&";	
		}
		
		if(form.elements[i].name == "bikeModel"){
			bikeModel = form.elements[i].value;	
		}
			
		queryString+=form.elements[i].name + "=" + form.elements[i].value;
			first = false;
		}
	}
	//alert(queryString);

	makeRequest("/cgi-bin/aspPages/theBikes/usedBikes/emailRegardingUsedBike.asp", queryString, divID);
		
	alert("Thankyou for contacting us about the " + bikeModel + ".  We will contact you shortly, normally within 48 hours.");
	
	

}

function updateUsedBikeLoading(){
}

function showHPIDetails(registration, divID){
	
	makeRequest("/hpi/" + registration + ".htm","",divID);
	
}




/*
// JavaScript Document
// fades out div1, replaces its content with div2s and fades div1 back in
// call javascript:replaceDiv('div1','div2')


function replaceDiv(div1, div2){

fadeOut(div1);

setTimeout("replaceContent('" + div1 + "', '" + div2 + "');",1000);

setTimeout("fadeIn('" + div1 + "');", 2000);

}


function fadeIn(divId){

for (var i=0;i<11;i++)
{
setTimeout("setOpacity('" + divId + "', " + i + ");",100*i);
}

}


function fadeOut(divId){

for (var i=0;i<11;i++)
{
setTimeout("setOpacity('" + divId + "', " + (10-i) + ");",100*i);
}

}


function setOpacity(iDivId, opacity2){

	var obj = document.getElementById(iDivId);
	obj.style.opacity = opacity2/10;
	obj.style.filter = 'alpha(opacity=' + opacity2*10 + ')';
}


function replaceContent(div1, div2)
{
var tempContent = document.getElementById(div1).innerHTML;

document.getElementById(div1).innerHTML = document.getElementById(div2).innerHTML;
//document.getElementById(div2).innerHTML = tempContent;
}

*/