var nfnopt=false;
var nfnsel=false;
var xpath;
var lpath;
var ops;
var opp=new Array('m','n','s','e','w','c');

function changeicon(){
	var iconsel="/cgi-bin/pdbsub2.pl"+document.pixsearch.pixlist.value;
	updateicons(iconsel);
	setTimeout ("finishIt();",400);
	setTimeout ("finishIt();",800);
	setTimeout ("finishIt();",1600);
}
function finishIt(){
	var pixch, idsub;
	for (var lpx=1; lpx<100; lpx++){
		idsub="0"+lpx;
		pixch=document.getElementById("pix"+idsub.substr(idsub.length-2,2));
		if(pixch) Reflection.add(pixch); else lpx=999;
	}
}
function showlist(){
	document.getElementById('total').innerHTML ="Searching&#8230;";
	document.body.style.cursor='wait';
	var result=document.pixsearch;
	result.text.value=result.text.value.toUpperCase();
	var listpath='/cgi-bin/pixlist.pl?make='+result.make.value+'&model='+result.model.value+'&type='+result.type.value+'&opm='+iniop+'&show='+result.show.checked+'&nfn='+result.nfn.checked+'&text='+result.text.value+'&fleet='+fleet;
	var listrequest;
	if (window.ActiveXObject)
		listrequest = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		listrequest = new XMLHttpRequest();
	else
		alert("not supported");		
	
	listrequest.onreadystatechange = listshow;
	function getlist(){
		if(listrequest.readyState==0 || listrequest.readyState==4){
			listrequest.open("GET",listpath,true);
			listrequest.send(null);
		}
	}
	function listshow(){
		if(listrequest.readyState == 4) {
			document.getElementById('list').innerHTML = listrequest.responseText;
			if(document.pixsearch.pixlist.value=="" && document.pixsearch.pixlist.length!=0) {
				document.pixsearch.pixlist[0].selected=true;
			}
			findtotal(document.pixsearch.pixlist.length);
			changeicon();
		}
	}	
	getlist();
}
// 								Update Icons Start
function updateicons(vehtag) {
	var iconpath=vehtag;
	var iconrequest;		
	if (window.ActiveXObject)
		iconrequest = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		iconrequest = new XMLHttpRequest();
	else
		alert("not supported");		
	
	iconrequest.onreadystatechange = iconshow;
	function geticon(){
		if(iconrequest.readyState==0 || iconrequest.readyState==4){
			iconrequest.open("GET",iconpath,true);
			iconrequest.send(null);
		}
	}
	function iconshow(){
		if(iconrequest.readyState == 4) {
			document.getElementById('icnlst').innerHTML = iconrequest.responseText;
			showdata(iconrequest.responseText);
			finishIt();
		}
	}	
	geticon();
}
//						Update icons end

function change(){
	var result=document.pixsearch;
	var make=result.make.value;
	var model=result.model.value;
	var type=result.type.value;
	for(var lz=0; lz<=5; lz++){if(result.opm[lz].checked==true) iniop=opp[lz];}
	if (!iniop) iniop="m";
	lpath="";
	ops="";
	xpath="";
	sndop="";
	nfnsel=document.pixsearch.nfn.checked;
	nfnopt=!document.pixsearch.nfn.disabled;
	if(make==""){
		ops="mnsewc";
		if(iniop=="w") nfnopt=true; else nfnopt=false;
	} else {
		if (make=="bmmo"){
			lpath="bmmo/"+type;
			ops="m";
			nfnopt=false;
		}
		if(model==""){
			if (make=="leyland"){
				ops="mnsewc";
				if(iniop=="m") nfnopt=false; else nfnopt=true;
			}
			if (make=="dennis" || make=="alexdennis" || make=="merc" || make=="volvo"){
				ops="w";
				nfnopt=true;
			}
			if (make=="ford"){
				ops="mnwe";
				nfnopt=false;
			}
		} else {
			if (model=="ln"){
				lpath="leyland/national";
				ops="mnsew";
				nfnopt=false;
			}
			if (model=="ld8"){
				lpath="leyland/titan";
				ops="m";
				nfnopt=false;
			}
			if (model=="arab"){
				lpath="guy/arab";
				ops="m";
				nfnopt=false;
			}
			if (model=="ll"){
				lpath="leyland/leopard";
				ops="mnsewc";
				nfnopt=false;
			}
			if (model=="lx"){
				lpath="leyland/lynx";
				ops="w";
				nfnopt=true;
			}
			if (model=="vr"){
				lpath="bristol/vr";
				ops="sew";
				nfnopt=false;
			}
			if (model=="df"){
				lpath="daimler/fleetline";
				if(type=="all") ops="mnsewc";
				if(type=="d11") ops="m";
				if(type=="d12")	ops="msew";
				if(type=="d13") ops="mnse";
				if(type=="d14") ops="mn";
				if(type=="other") ops="mnsewc";
				nfnopt=false;
			}
			if (model=="r192"){
				lpath="ford/r192";
				if(type=="all") ops="mnw";
				if(type=="f1") ops="mn";
				if(type=="f2" || type=="f3" || type=="other") ops="m";
				if(type=="m2") ops="m";
				if(type=="m3") ops="mw";
				nfnopt=false;
			}
			if (model=="transit"){
				lpath="ford/transit";
				if(type=="m1") ops="m";
				if(type=="all") ops="mne";
				if(type=="misc") ops="ne";
				nfnopt=false;
			}
			if (model=="dl"){
				lpath="dennis/lance";
				ops="w";
				nfnopt=true;
			}
			if (model=="dart"){
				lpath="dennis/dart";
				ops="w";
				nfnopt=true;
			}
			if (model=="trident"){
				lpath="dennis/trident";
				ops="w";
				nfnopt=true;
			}
			if (model=="enviro300"){
				lpath="dennis/enviro";
				ops="w";
				nfnopt=true;
			}
			if (model=="merc608"){
				lpath="mercedes/608";
				ops="w";
				nfnopt=false;
			}
			if (model=="merc609"){
				lpath="mercedes/609";
				ops="w";
				nfnopt=true;
			}
			if (model=="merc709"){
				lpath="mercedes/709";
				ops="w";
				nfnopt=true;
			}
			if (model=="merc0814"){
				lpath="mercedes/0814";
				ops="w";
				nfnopt=true;
			}
			if (model=="citaro"){
				lpath="mercedes/citaro";
				ops="w";
				nfnopt=false;
			}
			if (model=="lt"){
				ops="newc";
				lpath="leyland/tiger";
				if (iniop=="w") nfnopt=true; else nfnopt=false;
			}
			if (model=="olympian"){
				lpath="leyland/olympian";
				ops="nsw";
				if (iniop=="w" || iniop=="s") nfnopt=true; else nfnopt=false;
			}
			if (model=="solo"){
				lpath="optare/solo";
				ops="w";
				nfnopt=true;
			}
			if (model=="b7"){
				lpath="volvo/b7";
				ops="w";
				nfnopt=true;
			}
			if (model=="b10m"){
				lpath="volvo/b10m";
				ops="w";
				nfnopt=true;
			}
			if (model=="sg192"){
				lpath="man/sg192";
				ops="nw";
				nfnopt=false;
			}
			if (model=="mcw"){
				lpath="mcw/metroliner";
				ops="wc";
				nfnopt=false;
			}
			if (model=="iveco"){
				lpath="fiat/iveco";
				ops="w";
				nfnopt=false;
			}
			if (model=="bmc"){
				lpath="bmc/1100fe";
				ops="w";
				nfnopt=true;
			}
		}
	}
	if(ops) valop();
	if(iniop=="m") sndop=""; else sndop="mr"+iniop+"_";
	xpath=lpath+"/"+sndop+"pix.htm";
	if(nfnopt==false) nfnsel=false;
	makevar(iniop,ops);
	setnfn(nfnopt,nfnsel);
	showlist();
	changemenu(xpath);
}
//	Validate operator selection
function valop(){
	var vi;
	while(ops.search(iniop)==-1){
		for(vi=1; vi<=5; vi++){if(iniop==opp[vi]) iniop=opp[vi-1];}
		if(iniop==opp[0]) break;
	}
	while(ops.search(iniop)==-1){
		for(vi=4; vi>=0; vi--){if(iniop==opp[vi]) iniop=opp[vi+1];}
	}
}

// new bit to change sidemenu	
function changemenu(xpath){	
	var menupath='/cgi-bin/fleetmenu2.pl?path=/fleet/'+xpath;
	var request;		
	if (window.ActiveXObject)
		request = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		request = new XMLHttpRequest();
	else
		alert("not supported");		
	
	request.onreadystatechange = sidemenu;
	function getmenu(){
		if(request.readyState==0 || request.readyState==4){
			request.open("GET",menupath,true);
			request.send(null);
		}
	}
	function sidemenu(){
		if(request.readyState==4) wright(request.responseText,'smp');
	}	
	getmenu();		
}
function makevar(iniop,ops){
	for (var lz=0; lz<=5; lz++) {
		disable(lz,RegExp(opp[lz]).test(ops));
		document.pixsearch.opm[lz].checked=(iniop==opp[lz]);	
	}	
}

function setnfn(nfnopt,nfnsel){
	var nftag='nf';
	var nfcolor = new Array("#000","#CCC");
	document.pixsearch.nfn.disabled=!nfnopt;
	if(nfnopt)
		document.getElementById(Object(nftag)).style.color=nfcolor[0];
	else
		document.getElementById(Object(nftag)).style.color=nfcolor[1];
	document.pixsearch.nfn.checked=nfnsel;
/*																	New bit to switch on/off the Type code box  */
	if(document.pixsearch.type.value=="")
		document.getElementById(Object('tc')).style.color=nfcolor[1];
	else
		document.getElementById(Object('tc')).style.color=nfcolor[0];
	document.pixsearch.type.disabled=(document.pixsearch.type.value=="");
}

function disable(itm,state){
var tags = new Array('mroc','mrn','mrs','mre','mrw',"mrc");
var color = new Array("#000","#CCC");
	document.pixsearch.opm[itm].disabled=!state;
	if(state)
	document.getElementById(Object(tags[itm])).style.color=color[0];
	else
	document.getElementById(Object(tags[itm])).style.color=color[1];
}

var ns6=document.getElementById&&!document.all
var ie=document.all

function findtotal(number){
	var thetext=number+" vehicle";
	if(number!=1) thetext=thetext+"s";
	thetext=thetext+" found";
	wright(thetext,'total');
	document.body.style.cursor='default';
}

function showdata(data){
	var words=data.split("<!-- Hidden Data ");
	words=words[1].split(" -->");
	words=words[0].split("---");
	
	var pf = words[0].split("=");
	var ptext = pf[1]+" photograph";
	if(pf[1]!="1") ptext=ptext+"s";
	ptext=ptext+" found";
	wright(ptext,'prslt');
	if(pf[1]=="0") pf[1]="";
	wright(pf[1],'pics');
	
	var tp = words[1].split("=");
	wright(tp[1],'vt');
	
	var nm = words[2].split("=");
	if(nm[1]==" ()") nm[1]="";
	wright(nm[1],'flt');
	
	var af = words[3].split("==");
	wright(af[1],'altf');

	var ar = words[4].split("==");
	wright(ar[1],'altrn');
	
}
function wright(what,where){
	if (ie) eval("document.all."+Object(where)).innerHTML=what
	else if (ns6) document.getElementById(Object(where)).innerHTML=what
}
