
// identify explorer - not as easy as it sounds (opera likes to pretend it's IE)
var MSIE = /^ms_/.test(document.documentElement.uniqueID);
var ieVersion = (MSIE) ? navigator.appVersion.match(/MSIE (\d\.\d)/)[1] : 0;

function _addEventListener(element, eventName, func) {
	if (element.addEventListener) element.addEventListener(eventName, func, false); // W3C
	else if (element.attachEvent) element.attachEvent('on' + eventName, func); // IE
	else element['on' + eventName] = func;
}

function submitForm(formName) {
	document[formName].submit()
};

function setFormAction(formName, actionValue) {
	document[formName].action = actionValue;
};

function openWindow(url, width, height){
	var win = window.open(url, "", "toolbar=no,location=no,directories=no,status=no,menubar=0,resizable=0,copyhistory=no,width=" + width + ",height=" + height + ",scrollbars=1");
	if (win.focus) win.focus();
};

function getWindowHeight() {
	return window.innerHeight || document.documentElement.offsetHeight;
};

// convert text to HTML so we can update innerHTML safely
function encodeHTML(text) {
	return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
};

function adSetup() {
	if (window.adDrop) adDrop();
	resize();
	if (window.roundedCorners) roundedCorners.recalc();
};

// ensure that page content expands to fill the height of the screen
var MIN_HEIGHT = (MSIE) ? "height" : "minHeight";
function resize() {
	var content = document.getElementById("pagecontent");
	var footer = document.getElementById("footer");
	if (content && footer) {
		//content.style[MIN_HEIGHT] = getWindowHeight() - content.offsetTop - footer.offsetHeight;
	}
};

// event handling

function addEventHandler(element, event, handler) {
	if (element.attachEvent) element.attachEvent(event, handler);
	else if (element.addEventListener)	element.addEventListener(event.slice(2), handler, null);
};

if (!MSIE || ieVersion > 5) addEventHandler(window, "onresize", resize);
addEventHandler(window, "onload", adSetup);

//admin site functions

function setUserAction(formName, actionField, actionValue) {
    document[formName].elements[actionField].value = actionValue;
}

function setNextStep(formName, value) {
    document[formName].nextStep.value = value;
}

// script to target parent of pop-up window

function targetopener(mylink, closeme, closeonly){
	if (! (window.focus && window.opener))return true;
	window.opener.focus();
	if (! closeonly)window.opener.location.href=mylink.href;
	if (closeme)window.close();
	return false;
	}

//car finance price-poster
function constructURL(url,formName){
	amount=document.finance.borrow.value;
	newURL = url+'?amount='+amount;
	//removed opener functionality
	//window.opener.focus();
	//window.opener.location.href=newURL;
	//window.location.href=newURL;
	raw_popup(newURL,'_blank','location=0,resizable=yes,statusbar=0,scrollbars=1, menubar=0, width=780, height=500');
	//self.resizeTo(780,500);
	return false;
}

/* popup window stuff */

function isUndefined(v) {
    var undef;
    return v===undef;
}

var _POPUP_FEATURES = 'location=0,resizable=yes, statusbar=0,scrollbars=1, menubar=0, width=680, height=500';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

function fillChildsFFV(form,name1,name2,allchilds,allparents) {
	var parentArr = new Array();
	var childArr = new Array();

parentArr[0] = "20000;Bedford";
           childArr[0] = "21000;Astra|21001;Rascal";
          parentArr[1] = "20001;BMC";
           childArr[1] = "21002;Pro518|21003;Pro625|21004;Pro628";
          parentArr[2] = "20002;Chevrolet";
           childArr[2] = "21007;GMC|21008;GS20|21009;Silverado";
          parentArr[3] = "20003;Citroen";
           childArr[3] = "21010;Berlingo|21011;C15|21012;C25|21013;Dispatch|21014;Relay";
          parentArr[4] = "20004;Dacia";
           childArr[4] = "21015;Duster";
          parentArr[5] = "20005;DAF";
           childArr[5] = "21016;CF|21017;FA";
          parentArr[6] = "20006;Daihatsu";
           childArr[6] = "21018;Extol|21019;Fourtrak|21020;Hijet|21021;Sportrak";
          parentArr[7] = "20007;Dennis";
           childArr[7] = "20007;Dennis";
          parentArr[8] = "20008;Dodge";
           childArr[8] = "21022;Dakota|21023;Magnum|21024;RAM|21025;SRT";
          parentArr[9] = "20009;ERF";
           childArr[9] = "21026;EC";
          parentArr[10] = "20011;Fiat";
           childArr[10] = "21027;Doblo|21028;Ducato|21029;Fiorino|21030;Punto|21031;Scudo";
          parentArr[11] = "20012;Foden";
           childArr[11] = "21032;Alpha";
          parentArr[12] = "20013;Ford";
           childArr[12] = "21033;Cargo|21034;Courier|21035;Escort|21036;F150|21037;Fiesta|21038;Ka|21039;Maverick|21040;Ranger|21041;Tourneo|21042;Transit|21043;Transit Connect";
          parentArr[13] = "20014;Freightliner";
           childArr[13] = "20014;Freightliner";
          parentArr[14] = "20015;Fruehauf";
           childArr[14] = "20015;Fruehauf";
          parentArr[15] = "20010;FSO";
           childArr[15] = "20010;FSO";
          parentArr[16] = "20016;GMC";
           childArr[16] = "21044;Jimmy";
          parentArr[17] = "20017;Gray & Adams";
           childArr[17] = "20017;Gray & Adams";
          parentArr[18] = "20018;Holden";
           childArr[18] = "21045;UTE";
          parentArr[19] = "20019;Hyundai";
           childArr[19] = "21046;H100";
          parentArr[20] = "20020;Isuzu";
           childArr[20] = "21047;Crew|21048;D-MAX|21049;Mega|21050;NKR|21051;Rodeo|21052;TF|21053;Trooper";
          parentArr[21] = "20021;Iveco";
           childArr[21] = "21054;Cargo|21055;Daily|21056;Eurostar|21057;Eurotech|21058;Eurotrakker";
          parentArr[22] = "20022;Jeep";
           childArr[22] = "21059;Cherokee";
          parentArr[23] = "20023;Kia";
           childArr[23] = "21060;Pregio|21061;Pride|21062;Sorento";
          parentArr[24] = "20024;Land Rover";
           childArr[24] = "21064;Defender 110|21065;Defender 130|21063;Defender 90|21066;Discovery|21067;Freelander";
          parentArr[25] = "20025;LDV";
           childArr[25] = "21068;Convoy|21069;Cub|21171;Maxus|21070;Pilot";
          parentArr[26] = "20026;Leyland";
           childArr[26] = "21071;45|21072;55|21073;65|21074;75|21075;85|21076;95|21077;Constructor|21078;Freighter|21079;Roadrunner";
          parentArr[27] = "20027;Mahindra";
           childArr[27] = "20027;Mahindra";
          parentArr[28] = "20028;MAN";
           childArr[28] = "21080;F2000|21081;L2000|21082;M2000";
          parentArr[29] = "20029;Mazda";
           childArr[29] = "21083;B2000|21084;B2200|21085;B2500|21086;E2000|21087;E2200";
          parentArr[30] = "20030;Mercedes-Benz";
           childArr[30] = "21088;108|21089;109|21090;110|21091;111|21092;112|21093;113|21094;115|21095;207|21096;208|21097;210|21098;211|21099;308|21100;310|21101;311|21102;312|21103;313|21104;316|21105;410|21106;413|21107;814|21108;817|21109;Actros|21110;Atego|21111;Vito";
          parentArr[31] = "20031;Mitsubishi";
           childArr[31] = "21112;Canter|21113;L200|21114;L300|21115;Shogun";
          parentArr[32] = "20032;Nissan";
           childArr[32] = "21116;Cabstar|21117;Interstar|21118;Kubistar|21119;Navara|21120;Primastar|21121;Sunny|21122;Terrano|21123;Vanette";
          parentArr[33] = "20033;Peugeot";
           childArr[33] = "21124;Boxer|21125;Expert|21126;Partner";
          parentArr[34] = "20034;Piaggio";
           childArr[34] = "21127;Porter";
          parentArr[35] = "20035;Proton";
           childArr[35] = "21128;Jumbuck";
          parentArr[36] = "20036;Renault";
           childArr[36] = "21129;Clio|21130;Extra|21131;Kangoo|21132;Master|21133;Maxter|21134;Midliner|21135;Premium|21136;Trafic";
          parentArr[37] = "20037;Rover";
           childArr[37] = "20037;Rover";
          parentArr[38] = "20038;Scania";
           childArr[38] = "21137;Topline";
          parentArr[39] = "20039;SEAT";
           childArr[39] = "21138;Inca|21139;Terra";
          parentArr[40] = "20040;Skoda";
           childArr[40] = "21140;Felicia";
          parentArr[41] = "20041;Subaru";
           childArr[41] = "21141;Sumo";
          parentArr[42] = "20042;Suzuki";
           childArr[42] = "21142;Carry|21143;Super Carry";
          parentArr[43] = "20043;Talbot";
           childArr[43] = "21144;Express";
          parentArr[44] = "20044;Tata";
           childArr[44] = "21145;Gurkha|21146;Loadbeta|21147;TL";
          parentArr[45] = "20045;Toyota";
           childArr[45] = "21172;Dyna|21148;Hi Ace|21149;Hi Lux|21150;Lite Ace|21151;Townace";
          parentArr[46] = "20046;Vauxhall";
           childArr[46] = "21152;Astra|21153;AstraMax|21154;AstraVan|21155;Brava|21156;Combo|21157;Corsa|21158;Midi|21159;Movano|21160;Rascal|21161;Vivaro";
          parentArr[47] = "20047;Volkswagen";
           childArr[47] = "21162;Caddy|21163;Caravelle|21173;Crafter|21164;Golf|21165;LT|21166;Polo|21167;Transporter";
          parentArr[48] = "20048;Volvo";
           childArr[48] = "21168;FH|21169;FLC|21170;FM"; 



	emptySelectBox(form, name1, allchilds);
	var childid = 0;
	var start = 1;
	var name2Length = document[form][name2].length;
	if (allchilds == true) {
		childid = 1;
	}
	if (allparents == false) {
		start = 0;
	}
    var selectedvalue = 0;
	for (var i=start;i<name2Length;i++) {
		if (document[form][name2][i].selected) {

            selectedvalue = parentArr[i-start].split(";")[0];
			var tmpArr = childArr[i-start].split("|");
			for (var j=0;j<tmpArr.length;j++) {
				var tmpArr2 = tmpArr[j].split(";");
			    document[form].elements[name1].options[childid] = new Option(tmpArr2[1],tmpArr2[0]);
                childid++;
			}
		}
	}
    if (allchilds) {
        document[form].elements[name1].options[0].value = '0';
    }
}

function emptySelectBox(form, boxname, allchilds){
	var length = Number(document.forms[form].elements[boxname].options.length);

	if (navigator.appName.indexOf("Netscape") != -1) {
		for (var i=(length); i > 0; i--) {
			document.forms[form].elements[boxname].options[i]=null;
		}
	} else if (navigator.userAgent.indexOf("Opera") != -1) {
		for(var i=(length); i > 0; i--) {
			document.forms[form].elements[boxname].options.remove(i);
		}
	} else if ((navigator.userAgent.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) >= 4)) {
		for(var i=(length); i > 0; i--) {
			document.forms[form].elements[boxname].options.remove(i);
		}
	} else if ((navigator.userAgent.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) < 4)) {
		for (var i=(length-1); i > 0; i--) {
			document.forms[form].elements[boxname].options[i].selected = false;
		}
	} else {
		for (var i=(length-1); i > 0; i--) {
			document.forms[form].elements[boxname].options[i].selected = false;
		}
	}
	if (navigator.appName.indexOf("Netscape") == -1) {
		if (allchilds) {
			document.forms[form].elements[boxname].options[0] = new Option("All","0",true,true);
		} else {
			document.forms[form].elements[boxname].options[0] = new Option("","0",true,true);
		}
		document.forms[form].elements[boxname].options[0].selected = true;
	}
}

var numb = '0123456789';

function isValid(parm,val) {
  if (parm == "") return true;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function isNum(parm) {return isValid(parm,numb);}

function openLocationWindow(formName, fieldName, parameterName, url){
    url = url + '?' + parameterName + '=' + document[formName].elements[fieldName].value + '&locationfield=' + fieldName;
	openWindow(url, '700','400');
};

function returnValueToOpener(formName, fieldName, localFormName, localFieldName, multiple) {
    if (formName != null && fieldName != null && localFormName != null && localFieldName != null) {
        var returnValue = this.document.forms[localFormName].elements[localFieldName].value;
        if (multiple) {
            var array = new Array();
            array = this.document.forms[localFormName].elements[localFieldName];
            var finished = false;
            var index = 0;
            while(!finished || index < array.length) {
                if (array[index].checked == true) {
                    returnValue = array[index].value;
                    finished = true;
                }
                index++;
            }
        }
        window.opener.document[formName].elements[fieldName].value = returnValue;        
    }
};

function closeLocationWindow(formName) {
    window.close();
    window.opener.document[formName].submit();
    window.opener.focus();
};

// Used by the <input:select> tag to remember options when the user navigates back in history
function rememberoptions_load(inputSelect) {
	if (inputSelect && inputSelect.form && inputSelect.options) {
		var hValues = inputSelect.form[inputSelect.name + '_values'];
		var hText = inputSelect.form[inputSelect.name + '_text'];
		var hSelected = inputSelect.form[inputSelect.name + '_selected'];
		if (hValues && hText && hSelected && hValues.value.length > 0) {
			// deserialize the arrays from the hidden fields
			var optionValues = hValues.value.split(';');
			var optionText = hText.value.split(';');
			var optionSelected = hSelected.value.split(';');

			// Delete all existing options.
			while (inputSelect.options.length>0) inputSelect.options.remove(0);

			for (var i=0; i<optionValues.length; ++i) {
				// Create a new option
				var option = new Option(optionText[i], optionValues[i]);
				// Add the option to the select tag
				inputSelect.options[i] = option;
				// Mark the option as selected if it should be
				for (var j=0; j<optionSelected.length; ++j) if (option.value == optionSelected[j]) {
					option.selected = true;
				}
			}
		}
	}
}

// Used by the <input:select> tag to remember options when the user navigates back in history
function rememberoptions_save(inputSelect) {
	if (inputSelect && inputSelect.form && inputSelect.options) {
		var options = inputSelect.options;
		var optionValues = new Array(), optionText = new Array(), optionSelected = new Array();

		// Store options to arrays
		for (var i=0; i<options.length; ++i) {
			var option = options[i];
			optionValues.push(option.value);
			optionText.push(option.text);
			if (option.selected) optionSelected.push(option.value);
		}

		// Serialize the arrays to hidden fields
		inputSelect.form[inputSelect.name + '_values'].value = optionValues.join(';');
		inputSelect.form[inputSelect.name + '_text'].value = optionText.join(';');
		inputSelect.form[inputSelect.name + '_selected'].value = optionSelected.join(';');
	}
}

// Used by the <input:select> tag to remember options when the user navigates back in history
function rememberoptions_registerEventHandlers(formName, selectTagName) {
	// Find formName if it's missing.
	if (!formName || formName.length==0) {
		var tags = document.getElementsByName(selectTagName);
		if (tags.length>0 && tags[0] && tags[0].form && tags[0].form.name) formName = tags[0].form.name;
	}
	var form = document.forms[formName];
	if (form) {
		var selectTag = form[selectTagName];
		if (selectTag) {
			_addEventListener(form, 'submit', function() { rememberoptions_save(selectTag); });
			_addEventListener(window, 'load', function() { rememberoptions_load(selectTag); });
		}
	}
}

function emptySelectBox(form, boxname, allchilds){
	var length = Number(document.forms[form].elements[boxname].options.length);

	if (navigator.appName.indexOf("Netscape") != -1) {
		for (var i=(length); i > 0; i--) {
			document.forms[form].elements[boxname].options[i]=null;
		}
	} else if (navigator.userAgent.indexOf("Opera") != -1) {
		for(var i=(length); i > 0; i--) {
			document.forms[form].elements[boxname].options.remove(i);
		}
	} else if ((navigator.userAgent.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) >= 4)) {
		for(var i=(length); i > 0; i--) {
			document.forms[form].elements[boxname].options.remove(i);
		}
	} else if ((navigator.userAgent.indexOf("MSIE") != -1) && (parseInt(navigator.appVersion) < 4)) {
		for (var i=(length-1); i > 0; i--) {
			document.forms[form].elements[boxname].options[i].selected = false;
		}
	} else {
		for (var i=(length-1); i > 0; i--) {
			document.forms[form].elements[boxname].options[i].selected = false;
		}
	}
	if (navigator.appName.indexOf("Netscape") == -1) {
		if (allchilds) {
			document.forms[form].elements[boxname].options[0] = new Option("All","0",true,true);
		} else {
			document.forms[form].elements[boxname].options[0] = new Option("","0",true,true);
		}
		document.forms[form].elements[boxname].options[0].selected = true;
	}
}

