/*******************************************************************************************
* Customer javascript functions - created by Customer Community
******************************************************************************************/

function ViewLarge(ImageSource)
{
	//alert("start");
	if (document.getElementById("LViewImage"))
	{
		//alert("before change");
		//document.getElementById("Preview_top").innerHTML = '<img height="245" alt="" src="' + ImageSource + '" width="245" border="0" />'
		document.getElementById("LViewImage").src = ImageSource;
	}
	else
	{
		alert("Div not found");

	}

}

function PricingAjaxCall(pricingForm)
{
	var modelCategory1, postCode, typeOfUse;
	
	modelCategory1 = pricingForm.ModelCategory1.value;
	postCode = pricingForm.Postcode.value;

	for (var i=0; i < pricingForm.PrivateOrBusinessRadio.length; i++)
	{
		if (pricingForm.PrivateOrBusinessRadio[i].checked)
		{
			var typeOfUse = pricingForm.PrivateOrBusinessRadio[i].value;
		}
	}
	
	if (!postCode || postCode == "")
	{
		alert("Please enter postcode.");
		return false;
	}
	else
	{
		if(postCode.length != 4 || !IsNumeric(postCode))
		{
			alert("Postcode is invalid.");
			return false;
		}
	}

	if (!typeOfUse || typeOfUse == "")
	{
		alert("Please select type of use.");
		return false;
	}
	
	if (!modelCategory1 || modelCategory1 == "" || modelCategory1 == "ALL")
	{
		var pricingURLACT = "admin/includes/CallPricingAJAX.asp";

		pricingURLACT += "?Model=ACT";
		pricingURLACT += "&PostCode=" + postCode;
		pricingURLACT += "&TypeOfUse=" + typeOfUse;

		//ajax_loadContent('PricingOutPut_ACT', pricingURLACT, '');
		getDataACT(pricingURLACT);
		
		var pricingURLKYR = "admin/includes/CallPricingAJAX.asp";

		pricingURLKYR += "?Model=KYR";
		pricingURLKYR += "&PostCode=" + postCode;
		pricingURLKYR += "&TypeOfUse=" + typeOfUse;

		//setTimeout("ajax_loadContent('PricingOutPut_KYR', '" + pricingURLKYR + "', '')", 100);
		getDataKYR(pricingURLKYR);

		var pricingURLREX = "admin/includes/CallPricingAJAX.asp";

		pricingURLREX += "?Model=REX";
		pricingURLREX += "&PostCode=" + postCode;
		pricingURLREX += "&TypeOfUse=" + typeOfUse;

		//setTimeout("ajax_loadContent('PricingOutPut_REX', '" + pricingURLREX + "', '')", 100);
		getDataREX(pricingURLREX);

		var pricingURLSTA = "admin/includes/CallPricingAJAX.asp";

		pricingURLSTA += "?Model=STA";
		pricingURLSTA += "&PostCode=" + postCode;
		pricingURLSTA += "&TypeOfUse=" + typeOfUse;

		//setTimeout("ajax_loadContent('PricingOutPut_STA', pricingURLSTA, '')", 100);
		getDataSTA(pricingURLSTA);

		var pricingURLACS = "admin/includes/CallPricingAJAX.asp";

		pricingURLACS += "?Model=ACS";
		pricingURLACS += "&PostCode=" + postCode;
		pricingURLACS += "&TypeOfUse=" + typeOfUse;

		//setTimeout("ajax_loadContent('PricingOutPut_ACS', pricingURLACS, '')", 100);
		getDataACS(pricingURLACS);
	}
	else
	{	
		var pricingURL = "admin/includes/CallPricingAJAX.asp";
	
		pricingURL += "?Model=" + modelCategory1;
		pricingURL += "&PostCode=" + postCode;
		pricingURL += "&TypeOfUse=" + typeOfUse;

		//ajax_loadContent('PricingOutPut_' + modelCategory1, pricingURL, '')
		getDataGeneric(pricingURL, 'PricingOutPut_' + modelCategory1);
	}

	return false;
}

var ajaxGeneric = new sack();
var genericDivID;
function getDataGeneric(theScript, divID)
{
		ajaxGeneric.requestFile = theScript;			// Specifying which file to get
		genericDivID = divID;
		ajaxGeneric.onLoading = function() { doOnLoading(genericDivID) };
		ajaxGeneric.onCompletion = showClientDataGeneric;     // Specify function that will be executed after file has been found
		ajaxGeneric.runAJAX();							// Execute AJAX function                        
}

function doOnLoading(divID)
{
	var loadingMessage =  '<img align="middle" alt="Loading" src="images/ajax-loader.gif" /><span style="padding-top:5px;">&nbsp;&nbsp;Please wait while your request is being processed...</span>';
	document.getElementById(divID).innerHTML = loadingMessage;
}

function showClientDataGeneric()
{
	document.getElementById(genericDivID).innerHTML = ajaxGeneric.response;
}

var ajaxACT = new sack();
function getDataACT(theScript)
{
		ajaxACT.requestFile = theScript;			// Specifying which file to get
		ajaxACT.onLoading = function() { doOnLoading("PricingOutPut_ACT") };
		ajaxACT.onCompletion = showClientDataACT;     // Specify function that will be executed after file has been found
		ajaxACT.runAJAX();							// Execute AJAX function                        
}

function showClientDataACT()
{
	document.getElementById("PricingOutPut_ACT").innerHTML = ajaxACT.response;
}


var ajaxKYR = new sack();
function getDataKYR(theScript)
{
		ajaxKYR.requestFile = theScript;			// Specifying which file to get
		ajaxKYR.onLoading = function() { doOnLoading("PricingOutPut_KYR") };
		ajaxKYR.onCompletion = showClientDataKYR;     // Specify function that will be executed after file has been found
		ajaxKYR.runAJAX();							// Execute AJAX function                        
}

function showClientDataKYR()
{
	document.getElementById("PricingOutPut_KYR").innerHTML = ajaxKYR.response;
}

var ajaxREX = new sack();
function getDataREX(theScript)
{
		ajaxREX.requestFile = theScript;			// Specifying which file to get
		ajaxREX.onLoading = function() { doOnLoading("PricingOutPut_REX") };
		ajaxREX.onCompletion = showClientDataREX;     // Specify function that will be executed after file has been found
		ajaxREX.runAJAX();							// Execute AJAX function                        
}

function showClientDataREX()
{
	document.getElementById("PricingOutPut_REX").innerHTML = ajaxREX.response;
}

var ajaxSTA = new sack();
function getDataSTA(theScript)
{
		ajaxSTA.requestFile = theScript;			// Specifying which file to get
		ajaxSTA.onLoading = function() { doOnLoading("PricingOutPut_STA") };
		ajaxSTA.onCompletion = showClientDataSTA;     // Specify function that will be executed after file has been found
		ajaxSTA.runAJAX();							// Execute AJAX function                        
}

function showClientDataSTA()
{
	document.getElementById("PricingOutPut_STA").innerHTML = ajaxSTA.response;
}

var ajaxACS = new sack();
function getDataACS(theScript)
{
		ajaxACS.requestFile = theScript;			// Specifying which file to get
		ajaxACS.onLoading = function() { doOnLoading("PricingOutPut_ACS") };
		ajaxACS.onCompletion = showClientDataACS;     // Specify function that will be executed after file has been found
		ajaxACS.runAJAX();							// Execute AJAX function                        
}

function showClientDataACS()
{
	document.getElementById("PricingOutPut_ACS").innerHTML = ajaxACS.response;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   
   return IsNumber;
   
}

/* 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();
}