// 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 fillChildsFFBI(form,name1,name2,allchilds,allparents) {
	var parentArr = new Array();
	var childArr = new Array();
    parentArr[0] = "10000;Aprilia";
           childArr[0] = "11000;Atlantic|11003;ETV 1000|11004;ETX 125|11005;Futura|11006;Habana|11009;Leonardo|11007;Mojito Custom 125/50|11008;Moto 6.5|11012;Pegaso|11014;RS|11019;RST Futura|11018;RSV|11020;RXV|11021;Scarabeo 125|11022;SL1000 Falco|11023;Sportcity|11025;SR|11028;SXV|11030;Tuono";
                      parentArr[1] = "10001;Ariel";
           childArr[1] = "11032;Arrow|11033;Hunter";
                      parentArr[2] = "10002;Baimo";
           childArr[2] = "11034;Renegade";
                      parentArr[3] = "10003;Battistinis";
           childArr[3] = "11035;Harley";
                      parentArr[4] = "10004;Benelli";
           childArr[4] = "11036;Adiva 125|11037;K2|11039;TNT|11038;Tornado";
                      parentArr[5] = "10005;Beta";
           childArr[5] = "11040;Alp|11041;Ark|11042;Rev 3|11043;Rev 50";
                      parentArr[6] = "10006;Bimota";
           childArr[6] = "11044;SB6R|11045;SB8R|11046;YB7|11047;YB8";
                      parentArr[7] = "10007;BMW";
           childArr[7] = "11048;C1 125|11049;C1 200|11050;F650|11546;F800|11051;G650|11052;K1100|11053;K1200|11054;K75|11056;R100|11058;R1100|11059;R1150|11060;R1200|11055;R80|11057;R850";
                      parentArr[8] = "10008;BSA";
           childArr[8] = "11061;A10|11062;A65|11063;B31|11064;B40|11065;B50|11066;Bantam|11067;C15|11068;Rocket";
                      parentArr[9] = "10009;Buell";
           childArr[9] = "11074;Firebolt XB12R|11073;Firebolt XB9R|11069;Lightning X1|11072;Lightning XB12S|11070;Lightning XB9S|11071;Lightning XB9SX|11075;S1|11076;S3|11077;Ulysses XB12X";
                      parentArr[10] = "10010;Cagiva";
           childArr[10] = "11078;Canyon 500|11079;Canyon 600|11080;Canyon 900|11081;Elefant 350|11082;Elefant 750|11083;Elefant 900|11084;Mito 125|11085;Navigator|11086;Planet|11088;Raptor 1000|11087;Raptor 650|11089;River 500|11090;River 600|11091;Roadster|11092;V Raptor 1000";
                      parentArr[11] = "10012;Cannondale";
           childArr[11] = "10012;Cannondale";
                      parentArr[12] = "10011;CCM";
           childArr[12] = "11093;404|11094;604|11095;644|11096;C25|11097;FT35S|11547;R30|11098;R35";
                      parentArr[13] = "10013;CH Racing";
           childArr[13] = "11099;CH509|11100;WSM50";
                      parentArr[14] = "10014;CPI";
           childArr[14] = "11101;Hussar|11102;Oliver|11103;Popcorn|11104;SuperCross|11105;SuperMoto";
                      parentArr[15] = "10015;Daelim";
           childArr[15] = "11106;Daystar|11107;Message|11108;S-Five|11109;Trans";
                      parentArr[16] = "10016;Derbi";
           childArr[16] = "11110;Atlantis|11111;GP1|11112;GPR|11113;Predator|11114;Senda";
                      parentArr[17] = "10017;Ducati";
           childArr[17] = "11131;1000|11132;1098|11133;1100|11115;500|11116;600|11117;620|11118;748|11119;749|11120;750|11121;800|11122;851|11123;860|11124;900|11125;906|11126;916|11127;950|11128;996|11129;998|11130;999|11134;FMX650|11135;FMX650|11136;S2|11137;S4|11138;ST2|11139;ST3|11140;ST4";
                      parentArr[18] = "10018;Easy Rider";
           childArr[18] = "10018;Easy Rider";
                      parentArr[19] = "10019;Enfield";
           childArr[19] = "11141;Bullet|11142;Clipper|11143;Electra|11144;Sv650s";
                      parentArr[20] = "10020;Eton";
           childArr[20] = "11145;Rascal|11146;Viper";
                      parentArr[21] = "10021;Gas Gas";
           childArr[21] = "11147;EC 200|11148;EC 250|11149;EC 300|11150;EC 400|11151;Pampera 250|11152;Pampera 280|11153;TXT 250|11154;TXT 280";
                      parentArr[22] = "10022;Gilera";
           childArr[22] = "11155;Cougar|11156;DNA|11158;GSM|11157;Ice|11159;Nexus|11160;Runner|11161;Sioux|11162;SMT|11163;Stalker";
                      parentArr[23] = "10023;Greeves";
           childArr[23] = "11164;Anglian|11165;Challender|11166;Pathfinder";
                      parentArr[24] = "10024;Harley-Davidson";
           childArr[24] = "11167;Dyna Glide|11168;Electra Glide|11169;Night Train|11170;Road King|11171;Softail|11172;Sportster|11173;Street Rod|11174;V-Rod";
                      parentArr[25] = "10025;Hartford";
           childArr[25] = "11175;HD|11176;VR";
                      parentArr[26] = "10026;Honda";
           childArr[26] = "11177;Ape|11178;CB|11179;CBF|11180;CBR|11181;CM|11182;CMX|11190;CR125|11191;CR250|11192;CR500|11189;CR85|11196;CRF100|11197;CRF150|11198;CRF230|11199;CRF250|11200;CRF450|11193;CRF50|11194;CRF70|11195;CRF80|11201;CRM|11183;CT|11184;Cub|11185;CX|11186;Dax|11187;Elite|11188;Express|11549;FXM|11202;GL|11203;MB|11204;MBT|11205;MBX|11206;Metropolitan|11207;NH|11208;NR|11209;NSR|11210;NT|11211;NT|11212;RC|11213;Reflex|11214;RS|11215;Ruckus|11216;SH|11217;ST|11218;Urban Express|11219;Valkyrie|11220;VF|11221;VFR|11222;VT|11223;VTR|11224;VTX|11226;XL|11227;XLR|11228;XLV|11225;XR|11229;Z";
                      parentArr[27] = "10027;Husaberg";
           childArr[27] = "11250;FC|11251;FE|11252;FS";
                      parentArr[28] = "10028;Husqvarna";
           childArr[28] = "11253;CR|11254;Husky Boy|11255;SM|11256;SMR|11257;SMS|11258;TC|11259;TE|11260;WR|11261;WRE|11262;WRK";
                      parentArr[29] = "10029;Hyosung";
           childArr[29] = "11263;Comet|11264;Cruise|11265;Grand Prix|11266;GT|11267;GV|11268;Prima|11269;RT|11270;RX";
                      parentArr[30] = "10030;Indian";
           childArr[30] = "11271;Lambretta|11272;Scout";
                      parentArr[31] = "10031;Italjet";
           childArr[31] = "11273;Dragster|11274;Formula|11275;Millennium|11276;Torpedo";
                      parentArr[32] = "10032;Jawa";
           childArr[32] = "11277;350|11278;500|11279;639";
                      parentArr[33] = "10033;Jialing";
           childArr[33] = "11280;JC|11281;JD|11282;JH|11283;JL";
                      parentArr[34] = "10034;JINCHENG";
           childArr[34] = "11284;JC50|11285;Jinlin|11286;Monkey Bike";
                      parentArr[35] = "10035;Kawasaki";
           childArr[35] = "11287;Eliminator|11288;ER|11289;GPZ|11290;KDX|11291;KFX|11295;KH|11292;KLE|11293;KLR|11294;KLX|11296;KR|11297;KSR|11298;KX|11299;KZ|11300;Versys|11301;Vulcan|11302;Z|11303;Zephyr|11304;ZG|11305;ZR|11306;ZRX|11307;ZX|11308;ZXR|11309;ZZR";
                      parentArr[36] = "10036;Keeway";
           childArr[36] = "11310;Focus|11311;Hurricane|11312;Matrix|11313;Superlight";
                      parentArr[37] = "10037;Kinroad-Rock";
           childArr[37] = "11314;XT";
                      parentArr[38] = "10038;KTM";
           childArr[38] = "11315;EXC|11316;LC|11317;SX";
                      parentArr[39] = "10039;KYMCO";
           childArr[39] = "11318;Agility|11319;CK|11320;Ego|11321;Hipster|11322;Movie|11323;MXer|11324;MXU|11325;People|11326;Pulsar|11327;Sector|11328;Spacer|11329;Stryker|11330;Super|11331;Venox|11332;Vivio|11333;Yup|11334;Zing|11335;ZX";
                      parentArr[40] = "10040;Lambretta";
           childArr[40] = "11336;GP|11337;Li|11338;SX|11339;TV";
                      parentArr[41] = "10041;Laverda";
           childArr[41] = "11340;750|11341;Jota";
                      parentArr[42] = "10042;Malaguti";
           childArr[42] = "11342;Ciak|11343;Drakon|11344;F10|11345;F12|11346;F15|11347;Grizzly|11348;Madison";
                      parentArr[43] = "10043;Matchless";
           childArr[43] = "11349;350|11350;G";
                      parentArr[44] = "10044;MBK";
           childArr[44] = "11351;Mach|11352;Nitro|11353;Ovetto|11354;Rocket|11355;Stunt";
                      parentArr[45] = "10045;Montesa";
           childArr[45] = "11356;Alto|11357;Cota";
                      parentArr[46] = "10046;Morini";
           childArr[46] = "11358;Corsaro";
                      parentArr[47] = "10047;Moto Guzzi";
           childArr[47] = "11359;Breva|11360;California|11361;Griso|11362;Le Mans|11363;Nevada";
                      parentArr[48] = "10048;Moto-Roma";
           childArr[48] = "11364;Grand Prix|11365;MRX|11366;Mustang|11367;Road Runner|11368;Stinger";
                      parentArr[49] = "10049;MV Agusta";
           childArr[49] = "11369;Brutale|11370;F4|11371;Raptor";
                      parentArr[50] = "10050;MZ";
           childArr[50] = "11372;Baghira|11373;Mastiff|11374;Skorpion";
                      parentArr[51] = "10051;Norton";
           childArr[51] = "11375;Atlas|11376;Commando|11377;Dominator";
                      parentArr[52] = "10052;Peugeot";
           childArr[52] = "11378;Elyseo|11379;Elystar|11380;Jet Force|11381;Looxor|11382;Ludix|11383;Metal X|11386;Speed Fight|11387;Squab|11388;Streetfight|11384;Trekker|11385;Vivacity|11389;XPS";
                      parentArr[53] = "10053;PGO";
           childArr[53] = "11390;Bugrider|11391;PMX|11392;T-Rex";
                      parentArr[54] = "10054;Piaggio";
           childArr[54] = "11393;Beverly|11394;Diesis|11395;Fly|11396;Hexagon|11397;Liberty|11398;NRG|11400;Sfera|11401;Skipper|11399;Typhoon|11402;Vespa|11403;X8|11404;X9|11405;Zip";
                      parentArr[55] = "10055;Polaris";
           childArr[55] = "11406;Phoenix|11407;Victory";
                      parentArr[56] = "10056;Regent";
           childArr[56] = "11408;Roadster";
                      parentArr[57] = "10057;Rieju";
           childArr[57] = "11409;MRX|11410;RS|11411;SMX";
                      parentArr[58] = "10058;Sachs";
           childArr[58] = "11412;B-805|11413;Madass|11414;Roadster|11415;XTC";
                      parentArr[59] = "10059;Sherco";
           childArr[59] = "11416;250|11418;4.5i|11417;500";
                      parentArr[60] = "10060;Suzuki";
           childArr[60] = "11419;AN Burgman|11420;AY|11421;Boulevard|11422;DL|11423;DR|11424;DR-Z|11425;EN|11426;FR|11427;FXR|11428;GN|11429;GS|11545;GSF|11548;GSR|11430;GSX|11432;GSX-F|11431;GSX-R|11433;GT|11550;GZ|11434;LS|11435;RC|11436;RF|11437;RG|11438;RM|11439;RM-X|11440;RM-Z|11441;RV|11442;SV|11443;T20|11444;TL|11445;TU|11446;VL|11447;VS|11448;VX|11449;VZ|11450;VZR";
                      parentArr[61] = "10061;Sym";
           childArr[61] = "11451;City Hopper|11452;DD50|11453;Fiddle|11454;Husky|11455;Jet|11456;Joyride|11457;Mask|11458;Megalo|11459;Mio|11460;Orbit|11461;Red Devil|11462;Shark|11463;Wolf";
                      parentArr[62] = "10062;TGB";
           childArr[62] = "11464;202|11465;204|11466;302|11467;303R|11468;R50X";
                      parentArr[63] = "10063;Thumpstar";
           childArr[63] = "11469;Pro";
                      parentArr[64] = "10064;Titan";
           childArr[64] = "10064;Titan";
                      parentArr[65] = "10065;TM";
           childArr[65] = "11470;EN|11471;MX|11472;SMM|11473;SMR|11474;SMX";
                      parentArr[66] = "10066;Triumph";
           childArr[66] = "11475;Adventurer|11476;Bonneville|11477;Daytona|11478;Legend|11479;Rocket III|11480;Scrambler|11481;Speed Four|11482;Speed Triple|11483;Speedmaster|11484;Sprint|11485;Thruxton|11486;Thunderbird|11487;Tiger|11488;Trident|11489;Trophy";
                      parentArr[67] = "10067;Victory";
           childArr[67] = "11490;V92|11491;Vegas";
                      parentArr[68] = "10068;Voxan";
           childArr[68] = "11492;Cafe Racer|11493;Roadster";
                      parentArr[69] = "10069;Yamaha";
           childArr[69] = "11494;CS|11495;DT|11496;DT-R|11497;FJ|11498;FJR|11499;FS|11500;FZ|11501;FZR|11502;FZS|11503;FZX|11504;GTS|11505;Jog|11506;MT|11507;Neo|11508;PW|11509;Radian|11510;RD|11511;RS|11512;RT|11513;RX|11514;RX-S|11515;SRX|11516;SZR|11517;TDM|11518;TDR|11519;TT|11520;TTR|11521;TW|11522;TX|11523;TY|11524;TZR|11525;VMax|11526;WR|11527;XC|11528;XJ|11529;XJR|11530;XJS|11531;XS|11532;XT|11533;XTX|11534;XTZ|11535;XV|11536;XVS|11537;XVZ|11538;YBR|11539;YD|11540;YMF|11541;YQ|11542;YZ|11543;YZF|11544;YZR";


	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); });
		}
	}
}

