var GLOBAL_CLICKED = false;

//Global Javascript page
function populateSeries(formName, makeField, seriesField, arrayName, carType) {
	
	with (eval("document." + formName)) {
		
		varMakeField = eval(makeField);
    if (varMakeField.options) {
      tempNum = varMakeField.selectedIndex;
    } else {
  		varMakeFieldANO = eval(makeField+'ANO');
  		tempNum = varMakeFieldANO.value;
    }

		//empty out series box
		varSeriesBox = eval(seriesField);
		for (i=0;i<varSeriesBox.lenght;i++) {
			varSeriesBox.options[i]=null
		}
		varSeriesBox.options.length = 0;
		
		//create new options
		varCarArray = new Array();
		varCarArray = eval(arrayName);
		
		//split new and used 0 = New, 1 = Used
		if (carType == 0) {
			for (j=0;j<varCarArray[tempNum].length;j++) {
				seriesString = varCarArray[tempNum][j];
				var tempOption = new Option(seriesString, seriesString);
				varSeriesBox.options[j] = tempOption;
			}
		} else {
			for (j=0;j<varCarArray[tempNum].length;j++) {
				seriesString = varCarArray[tempNum][j];
				starPos = seriesString.indexOf("*");
				if (parseInt(starPos) > 0) {
					displayString = seriesString.substring(0,parseInt(starPos));
					valueString = seriesString.substring(parseInt(starPos) + 1,seriesString.length);
				} else {
					displayString = seriesString;
					valueString = "All";
				}
				var tempOption = new Option(displayString, valueString);
				varSeriesBox.options[j] = tempOption;
			}
		}
		
	}
}

function populateSeriesComm(formName, makeField, seriesField, arrayName, carType) {
	with (eval("document." + formName)) {
		
		varMakeField = eval(makeField);
    if (varMakeField.options) {
      tempNum = varMakeField.selectedIndex;
    } else {
  		varMakeFieldANO = eval(makeField+'ANO');
  		tempNum = varMakeFieldANO.value;
    }

		//empty out series box
		varSeriesBox = eval(seriesField);
		for (i=0;i<varSeriesBox.lenght;i++) {
			varSeriesBox.options[i]=null
		}
		varSeriesBox.options.length = 0;
		
		//create new options
		varCarArray = new Array();
		varCarArray = eval(arrayName);
		
		//split new and used 0 = New, 1 = Used
		if (carType == 0) {
			for (j=0;j<varCarArray[tempNum].length;j++) {
				seriesString = varCarArray[tempNum][j];
				var tempOption = new Option(seriesString, seriesString);
				varSeriesBox.options[j] = tempOption;
			}
		} else {
			for (j=0;j<varCarArray[tempNum].length;j++) {
				seriesString = varCarArray[tempNum][j];
				starPos = seriesString.indexOf("*");
				if (parseInt(starPos) > 0) {
					displayString = seriesString.substring(0,parseInt(starPos));
					valueString = seriesString.substring(parseInt(starPos) + 1,seriesString.length);
				} else {
					displayString = seriesString;
					valueString = "All";
				}
				var tempOption = new Option(displayString, displayString);
				varSeriesBox.options[j] = tempOption;
			}
		}
		
	}
}

//generic populate scripts
function populateMakes(formName, makeField, seriesField, arrayName, carType) {
	with (eval("document." + formName)) {
		varFormField = eval(makeField);
    //alert(varFormField.options);
    if (varFormField.options) {
  		
  		for (i=0;i<varFormField.lenght;i++) {
  			varFormField.options[i]=null
  		}
  		
  		varFormField.options.length = 0;
  		varCarArray = new Array();
  		varCarArray = eval(arrayName);
  		
  		for (i=0;i<varCarArray.length;i++) {
  			var tempOption = new Option(varCarArray[i].value, varCarArray[i].value)
  			varFormField.options[i] = tempOption;
  		}
    
    }
		
		//populate series box
		populateSeries(formName, makeField, seriesField, arrayName, carType);
	}
}


//Coenie se functions
  function setCookie(name, value, expires, path, domain, secure) 
  {
    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
    document.cookie = curCookie;
  }
  
  function getCookie(name) 
  {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
    } else
      begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
      end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
  }
 
  function deleteCookie(name, path, domain) 
  {
    if (getCookie(name)) 
    {
      document.cookie = name + "=" + 
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
  }
 
  function readCookie(aName)
  {
    //alert(document.cookie);
    var start = document.cookie.indexOf(aName+"=");
    if (start == -1){return ''} 
    start = document.cookie.indexOf("=",start)+1;
    var end =  document.cookie.indexOf(";",start);
    if (end == -1){return ''}
    return unescape(document.cookie.substring(start,end));
  }

  function fixDate(date) 
  {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
      date.setTime(date.getTime() - skew);
  }
  
  function writeCookie(aName,aValue)
  {
    var exDate = new Date(2050,1,1,1,1,1,1);
    var start = document.cookies.indexOf(aName+"=");
    var end =  document.cookies.indexOf(";e",start)-1;
    if ((end == -1)||(start == -1))
    {
      document.cookies = document.cookies+''+aName+'='+aValue+';expires='+exDate+';';
    }
    else
    {
      s = document.cookies.substring(0,start)+
      aName+"="+aValue+";expires="+exDate+";"+                  
      document.cookies.substring(end,99999);
    }
  }
  
 function popMessage(arrayNum) {
  	if (arrayNum != null) {
		tempMessage = messageArray[arrayNum];
		if (arrayNum == 0) {
			window.status = "";
			if (parent.botbar) {
				//t = window.setTimeout("parent.botbar.document.messageForm.helpMessage.value = tempMessage;", 1000);
			}
		} else {
			//parent.botbar.document.messageForm.helpMessage.value = tempMessage;
		}
	}
 }
  
function out(obj,color){obj.style.backgroundColor = color;}
function over(obj,color){obj.style.backgroundColor = color;cursor(obj);}
function cursor(obj){obj.style.cursor = 'hand'; }
function clicked(obj){if ((obj.cells[1].children[0])&&(!GLOBAL_CLICKED)){if (obj.cells[1].children[0].tagName == "A"){location.href = obj.cells[1].children[0].href;}}}

function addList(cons, carID, Type, abbr) {
	if (Type == "New") {
		varString = "carID";
	} else {
		varString = "adnumber";
	}
	//alert(carID);
	parent.botbar.location.href = "botbar.cfm?" + varString + "=" + carID + "&Type=" + Type + "&t=" + abbr;
	var tempImageObject;
	tempImageObject = eval("document.all." + cons + carID);
	tempImageObject.innerHTML = "<b style=\"color: ##333333\">Added</b>";
}

function goFinance(sURL) {
	if (!sURL) {
		URL = "https://www.wesbank.co.za/fastapp/callacar/index.asp";
	} else {
		URL = sURL;
	}
	ID = "Finance";
	t = window.open(URL, ID, 'width=780, height=480, scrollbars=yes');
	t.focus();
}

function viewDetailUsed(USID, page, abbr) {
	sURL = "viewVehicle.cfm?t="+abbr+"&RequestTimeout=99999&USID=" + USID + page;
	ID = "detailWin";
	t = window.open(sURL, ID, 'width=650, height=495, scrollbars=yes');
	t.focus();
}
function viewDetailNewSpecial(carID, page, price) {
	sURL = "viewVehicle.cfm?RequestTimeout=99999&carID=" + carID + page + "&sPrice=" + price + "&special=1";
	ID = "detailWin";
	t = window.open(sURL, ID, 'width=650, height=440, scrollbars=yes');
	t.focus();
}
function viewDetailUsedSpecial(carID, page, price) {
	sURL = "viewVehicle.cfm?RequestTimeout=99999&adnumber=" + carID + page + "&sPrice=" + price + "&special=1";
	ID = "detailWin";
	t = window.open(sURL, ID, 'width=650, height=440, scrollbars=yes');
	t.focus();
}
function viewSpecial(adnumber, page, price, specialID) {
	sURL = "viewSpecial.cfm?RequestTimeout=99999&adnumber=" + adnumber + page + "&sPrice=" + price + "&specialID=" + specialID;
	ID = "detailWin";
	t = window.open(sURL, ID, 'width=650, height=440, scrollbars=yes');
	t.focus();
}

//varChecked = false;

function ShowErrorFixError(aObject,aMessage)
{
	 alert(aMessage);
	 aObject.value = '0';	
	 aObject.focus();
}

//calculator functions----------------------------------------------------
function calculate() {
	with (document.calculatorForm) {
		//validate all fields
		varCalc = true;
		if ((price.value == "")||(isNaN(price.value))) {
			ShowErrorFixError(price,"Please enter a valid price!")
			varCalc = false;
		} else if ((deposit.value == "")||(isNaN(deposit.value))) {
			ShowErrorFixError(deposit,"Please enter a valid deposit!")
			varCalc = false;
		} else if ((interest.value == "")||(isNaN(interest.value))) {
			ShowErrorFixError(interest,"Please enter a valid interest rate!")
			varCalc = false;
		} else if ((payments.value == "")||(isNaN(payments.value))) {
			ShowErrorFixError(payments,"Please enter a valid number of payments!")
			varCalc = false;
		}
		
		if (varCalc == true) {
			//all values are set, do calculations
			if (interest.value > 1.0) {
	        	varInterest = parseFloat(interest.value)/100.0;
	    	}
			varInterest = parseFloat(varInterest)/12;
			varPow = 1;
			for (i = 0; i < payments.value; i++) {
	        	varPow = varPow * (1 + varInterest);
			}
			tempMonthly = ((parseInt(price.value) - parseInt(deposit.value)) * varPow * varInterest) / (varPow - 1);
			monthly.value = twodec(tempMonthly);
		}
	}
}

function twodec(value) {
	tempValue = parseInt(value*100)/100;
	return tempValue;
}

function doDeposit(tempDeposit) {
	cons = parseInt(tempDeposit)/100;
	depositAmount1 = parseInt(document.calculatorForm.price.value) * cons;
	document.calculatorForm.deposit.value = Math.round(depositAmount1);
	calculate();
}

function viewLargeImage(imageName) 
{
	URL = "../includes/cfm/largeImage.cfm?image=" +imageName;
	showModalDialog(URL,"","font-family:Verdana; font-size:12; dialogWidth:370px; dialogHeight:370px; status=0");
}

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

///////////////////////////////////////////////////////////////////////////////
//Global vars
//////////////////////////////////////////////////////////////////////////////

// whitespace characters
var whitespace = " \t\n\r";
//All the ilegal characters in a email adress i think! 

var ilegal = "!\"#$%&'()*+,/:;<=>?[\\]^`{|}~ ";
// "

var g_strFromDB = "";
// Check whether string s is empty.
function isEmpty(s)
{ 
  return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)
{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isEmail (s,canBeEmpty)
{   if (isEmpty(s)) 
       if (!canBeEmpty) return false;
       else return true;
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
   while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    //look for a dubble @ 
	 if (s.indexOf("@") != -1)
	 {
		if (s.indexOf("@",s.indexOf("@")+1) != -1)
		{
			return false;
		}
	 }
	 // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

// checks for ilegal chars 
function isLegal (s)
{   var i;

	//Well if the string is not a valid email adress then  add the @ sign
	if (isEmail(s)){ilegal1 = ilegal}else{ilegal1 = ilegal + "@"} 	
    // Search through string's characters one by one
    // until we find a ilegal character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (ilegal1.indexOf(c) != -1) return false;
    }

    // All characters are whitespace.
	 return true;
}
/*
function checkMake() {
  if (document.usedSearch.selectmake.value == 'All') {
    alert('Please select a make');
    document.usedSearch.selectmake.focus();
    return false;
  }
}
*/