/* Simple AJAX Code-Kit (SACK) v1.6.1 */
/* ©2005 Gregory Wild-Smith */
/* www.twilightuniverse.com */
/* Software licenced under a modified X11 licence,
   see documentation or authors website for more details */

function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}


var ajax = new sack();

function spQuery(page,manswitch,rand_arr)
{
	var ajaxRequest2;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest2 = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("HTTP Request Error");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest2.onreadystatechange = function(){

		if(ajaxRequest2.readyState == 4){

			var offerDisplay = document.getElementById('spOffers');
			
			offerDisplay.innerHTML = ajaxRequest2.responseText;
			
		} else {
			var offerDisplay = document.getElementById('spOffers');

			if (manswitch == '_honda') {
				offerDisplay.innerHTML = '<div class="carbonwide"><div class="carbonheadwide"><h3 class="carbonheadleft">bikes of the week</h3></div><div style="width:430px;height:105px; padding-top: 65px; text-align: center"><img src=\"http://www.bridgemotorcycles.com/templates/bridge_honda/images/ajax-loader-honda.gif\" style="vertical-align: middle" /></div><div class="carbonfooterwidebord"></div></div>';

			
			} else if (manswitch == '_triumph') {
				offerDisplay.innerHTML = '<div class="carbonwide"><div class="carbonheadwide"><h3 class="carbonheadleft">bikes of the week</h3></div><div style="width:430px;height:105px; padding-top: 65px; text-align: center"><img src=\"http://www.bridgemotorcycles.com/templates/bridge_triumph/images/ajax-loader-triumph.gif\" style="vertical-align: middle" /></div><div class="carbonfooterwidebord"></div></div>';
			
			
			} else {
			
				offerDisplay.innerHTML = '<div class="carbonwide"><div class="carbonheadwide"><h3 class="carbonheadleft">bikes of the week</h3></div><div style="width:430px;height:105px; padding-top: 65px; text-align: center"><img src=\"http://www.bridgemotorcycles.com/bikesearch_3/ajax-loader.gif\" style="vertical-align: middle" /></div><div class="carbonfooterwidebord"></div></div>';

			
			}
		}
		
	}
	
				
	if (manswitch == '_honda') {
				
		var url = "/bikesearch_3/bikes_of_the_week_honda.php";
			
	} else if (manswitch == '_triumph') {
	
		var url = "/bikesearch_3/bikes_of_the_week_triumph.php";
	
	} else {
				
		var url = "/bikesearch_3/bikes_of_the_week.php";
			
	}
	
	if (rand_arr != '') {
	
		var queryString2 = "?page=" + page + "&rand_arr=" + rand_arr;
	
	} else {
	
		var queryString2 = "?page=" + page;
		
	}

	ajaxRequest2.open("GET", url + queryString2, true);
	ajaxRequest2.send(null);

}

function queryDB(orderby,closeorderby,page,closepage,showall,manswitch,fixmake){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("HTTP Request Error");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){

		if(ajaxRequest.readyState == 4){

			// document.getElementById('content').id = 'searchResults';

			var resultDisplay = document.getElementById('content');

			resultDisplay.innerHTML = ajaxRequest.responseText;
		} else {
			var resultDisplay = document.getElementById('content');

			resultDisplay.innerHTML = '<h3 class=\"white\" style=\"border: 0\">Loading <img src=\"http://www.bridgemotorcycles.com/bikesearch_3/ajax-loader.gif\" /></h3>';
		}
	}
	
	if (document.getElementById('minprice')) {
		var minprice = document.getElementById('minprice').value;
	}
	
	if (document.getElementById('maxprice')) {
		var maxprice = document.getElementById('maxprice').value;
	}
	
	if (document.getElementById('type')) {
		var type = document.getElementById('type').value;
	}
	
	if (document.getElementById('make')) {
		var make = document.getElementById('make').value;
	} else {
		var make = "any";
	}
	
	if (document.getElementById('new')) {
		var new_bikes = document.getElementById('new').checked;
	}
	
	if (document.getElementById('used')) {
		var used = document.getElementById('used').checked;
	}

	
	if (new_bikes == true) {
		var newbike = 1;
	} else {
		var newbike = 0;
	}
	
	if (used == true) {
		var used = 1;
	} else {
		var used = 0;
	}
	
	if (document.getElementById('model')) {
		var model = document.getElementById('model').value;
	}
	
	if (document.getElementById('ccmin')) {
		var ccmin = document.getElementById('ccmin').value;
	}
	
	if (document.getElementById('ccmax')) {
		var ccmax = document.getElementById('ccmax').value;
	}

	
	if (!fixmake) {
		var fixmake = "any";
	}

	if (showall == "yes") {
		
		if(manswitch == "honda") {
		
			var queryString = "?minprice=0&maxprice=0&type=any&make=27&new=1&used=1&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=any&ccmin=0&ccmax=0";
		
		} else if (manswitch == "triumph") {
		
			var queryString = "?minprice=0&maxprice=0&type=any&make=33&new=1&used=1&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=any&ccmin=0&ccmax=0";
		
		} else {
		
			var queryString = "?minprice=0&maxprice=0&type=any&make=any&new=1&used=1&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=any&ccmin=0&ccmax=0";
		
		}
	
	} else if (showall == "offers") {
	
		var queryString = "?minprice=0&maxprice=0&type=any&make=" + fixmake +"&new=1&used=1&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=any&ccmin=0&ccmax=0&offers=offers";
		
	} else if (showall == "scooters") {
	
		var queryString = "?minprice=0&maxprice=0&type=any&make=any&new=1&used=1&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=any&ccmin=0&ccmax=0&offers=scooters";
	
	} else if (showall == "bikes") {
	
		var queryString = "?minprice=0&maxprice=0&type=any&make=any&new=1&used=1&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=any&ccmin=0&ccmax=0&offers=bikes";
		
	} else {
	
		var queryString = "?minprice=" + minprice + "&maxprice=" + maxprice + "&type=" + type + "&make=" + make + "&new=" + newbike + "&used=" + used + "&orderby=" + orderby + "&closeorderby=" + closeorderby + "&page=" + page + "&closepage=" + closepage + "&model=" + model + "&ccmin=" + ccmin + "&ccmax=" + ccmax;
		
	}

	ajaxRequest.open("GET", "/staticfiles/search_query.php" + queryString, true);
	ajaxRequest.send(null);
}

function addRating(rating,width)
{
	document.getElementById('ratingVal').value = rating;
	document.getElementById('current-rating').style.width = width+'%';
}

function getMaxPrice(sel)
{
	document.getElementById('submitsearch').disabled = true;
	document.getElementById('submitsearch').className = 'loader';
	document.getElementById('submitsearch').value = '';
	
	var minprice = document.getElementById('minprice').value;
	var selectedmax = document.getElementById('maxprice').value;
	
	document.getElementById('maxprice').options.length = 0;	// Empty Max Price select box
	
	if(minprice.length>0){
		ajax.requestFile = '/bikesearch_3/functions.php?minprice='+minprice+'&selected='+selectedmax;	// Specifying which file to get
		ajax.onCompletion = createMaxPrice;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createMaxPrice()
{
	document.getElementById('submitsearch').disabled = false;
	document.getElementById('submitsearch').className = 'submitbutton';
	document.getElementById('submitsearch').value = 'Go';
	
	var obj = document.getElementById('maxprice');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function getModel(sel)
{
	document.getElementById('submitsearch').disabled = true;
	
	document.getElementById('submitsearch').className = 'loader';
	document.getElementById('submitsearch').value = '';
	
	var make = document.getElementById('make').value;
	var type = document.getElementById('type').value;
	
	document.getElementById('model').options.length = 0;	// Empty Model select box
	
	if(make.length>0){
		ajax.requestFile = '/bikesearch_3/functions.php?make='+make+'&type='+type;	// Specifying which file to get
		ajax.onCompletion = createModel;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createModel()
{
	document.getElementById('submitsearch').disabled = false;
	
	document.getElementById('submitsearch').className = 'submitbutton';
	document.getElementById('submitsearch').value = 'Go';
	
	var obj2 = document.getElementById('model');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function loadPage()
{
	if (document.getElementById('minprice')) {
		document.getElementById('minprice').selectedIndex = 0;
		getMaxPrice();
	}
}

function resetForm()
{
	document.getElementById('make').selectedIndex = 0;
	document.getElementById('type').selectedIndex = 0;
	getModel();
}

function shopOffer(page, page2, man)
{
	var ajaxRequest3;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest3 = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest3 = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest3 = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("HTTP Request Error");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest3.onreadystatechange = function(){

		if(ajaxRequest3.readyState == 4){
		
			if (page != "" && page2 == "") {
				var offerDisplay = document.getElementById('shopOffers');
			} else if (page == "" && page2 != "") {
				var offerDisplay = document.getElementById('otherOffers');
			}

			offerDisplay.innerHTML = ajaxRequest3.responseText;
			
		}
		
	}

	if (page != "") {
		
		var queryString3 = "?offerpage=" + page;
		var url = "http://www.bridgemotorcycles.com/modules/mod_vmoffers.php";
		
	}
	if (page2 != "") {
		
		if (man == "honda")	{
			var queryString3 = "?offerpage=" + page2;
			var url = "http://www.bridgemotorcycles.com/staticfiles/offers_honda.php";
		} else if (man == "triumph") {
			var queryString3 = "?offerpage=" + page2;
			var url = "http://www.bridgemotorcycles.com/staticfiles/offers_triumph.php";
		} else {
			var queryString3 = "?offerpage=" + page2;
			var url = "http://www.bridgemotorcycles.com/staticfiles/offers.php";
		}
	}
	
	ajaxRequest3.open("GET", url + queryString3, true);
	ajaxRequest3.send(null);
	
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
