

document.write("<style> .nojs, .reqd { display: none;} #field14 * {display:inline;} </style>");
 
 function $(id){
 	return document.getElementById(id);
 }
 
 function autosubmit_results(){
	if (document.getElementById('carresults')){
		var inputs = document.getElementById('carresults').getElementsByTagName('INPUT');
		for(var i=0; i < inputs.length; i++){
			if(inputs[i].type == "radio"){
				inputs[i].onclick = function(){ 
					return document.results.submit(); 
				}
			}
		}
	}
}
 
 
/* set up table cells in the search box */ 
 function processTableRow(el){
 
	var i = el.getElementsByTagName('TD')[1] || el.getElementsByTagName('TD')[0];
	debug(i);
		
	var input =  i.getElementsByTagName('select')[0] || i.getElementsByTagName('input')[0] || i.getElementsByTagName('a')[0];
	el.input = input;
		
 
	if (el.className && el.className.substring(0, 4) == "req_"){
 		// id of parent element
 		var id = el.className.replace("req_node_type_", "").replace(" reqd", "");
 		

		// get parent as an object
		var parent = $("row" + id);
		
		if (parent.dependantChildren){
 			parent.dependantChildren.push(el);
 		} else {
	 		parent.dependantChildren = new Array();
 			parent.dependantChildren.push(el); 				
 		}
		
		var i = parent.getElementsByTagName('TD')[1] || parent.getElementsByTagName('TD')[0] ;
		debug(i);
		
		//console.log(i);
		
		var input =  i.getElementsByTagName('select')[0] || i.getElementsByTagName('input')[0] || i.getElementsByTagName('a')[0];
		
		parent.input = input;
 		// assign event handlers
		
		if (input.tagName == "A"){
		
		//todo: need to make this = 1 if any children are showing.
			if ((parent.input && parent.input.value == 1) || !(el.input.value == 0 || el.input.value == "")){
				/*console.log(parent.input && parent.input.value == 1) 
				console.log(el.input.value == 0 || el.input.value == "")
				console.log("seetting to 1 ", parent, input, el);*/
				parent.input.value = 1;
			} else {
				//console.log("seetting to 0 ", parent, input, el);			
				parent.input.value = 0;
			}
		
			input.onclick = function(){
				parent.input.value = (parent.input.value==0) ? 1 : 0;
				
				change(parent);
			}
		
		} else {
		
			
			
			
			input.onchange = function(){
				change(parent);
			}
			input.onkeyup = function(){
				change(parent);
			}
		}
		// initial call 
		change(parent);
		//console.log("parent", parent)
 		
 	}
 
 }


function hide(e){
	//if (e.style){
		//console.log(e.input);
		
		
		
		
		e.input.value = e.input.tagName == "SELECT" ? 0 : "";
		
		
		e.style.display = "none";
		//}
}

function show(e){
	if (e.style){
		//IE doesn't suppor table-cell and block displays incorrectly in firefox
		e.style.display = !(window.attachEvent && !window.opera) ? "table-row" : "block";
	}
}



// call this function when a form element changes

 function change(e){
	//console.log(e.input)
 	if (e.input.value == 0 || e.input.value == ""){
		debug(e.input)
		if (e.input.tagName == "A"){
			e.input.innerHTML = 'More options';
			e.input.className = 'more';
			//var img = e.getElementsByTagName('IMG')[0];
			//img.src="gfx/more-options.gif";

		} else {
		//console.log(e.input);
		$('field2').innerHTML = '<input type="text" id="field_2" name="cs_2"/> ';
		} 
		for (var i= 0; i < e.dependantChildren.length; i++){
 			//e.dependantChildren[i].style.background = "#FFEEEE";
			hide(e.dependantChildren[i]);
 		}
 	} else {
		if (e.input.tagName == "A"){
			e.input.innerHTML = 'Fewer options';
			e.input.className = 'fewer';
			
			//debug(e);
			//var img = e.getElementsByTagName('IMG')[0];
			//img.src="gfx/fewer-options.gif";
		} else {
			if ($('field_1').value != '' && $('field_1').value != 0){
				var x = createXHR();
				x.onreadystatechange=function(){
	  				if(x.readyState==4){
	    				$('field2').innerHTML=x.responseText;
	    			}
	  			}
	  			x.open("GET","models.php?make=" + $('field_1').value + '&selected=' + $('field_2').value,true);
				x.send(null);
			} else {
				$('field2').innerHTML = '<input type="text" id="field_2" name="cs_2"/> ';
			}
		}
		
 		for (var i= 0; i < e.dependantChildren.length; i++){
			//e.dependantChildren[i].style.background = "#FFFFFF";
 			show(e.dependantChildren[i]);
 			var prev  = e.dependantChildren[i].previousSibling;
 			while(prev && prev.tagName !== "TD"){
 				prev = prev.previousSibling;
 			}
			if(prev){
				show(prev);
				}
 			
  		}
 	}
  }
function createXHR(){
	var x;
	try {
		// Firefox, Opera 8.0+, Safari
		x=new XMLHttpRequest();
	} catch (e) {
    // Internet Explorer
    try {
		x=new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
      try
        {
        x=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        document.write("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    return x;
 }

function models_dropdown(){
	$('field_1').onchange = function(){
		if ($('field_1').value != '' && $('field_1').value != 0){
			var x = createXHR();
			x.onreadystatechange=function(){
  				if(x.readyState==4){
    				$('field2').innerHTML=x.responseText;
    			}
  			}
  			x.open("GET","models.php?make=" + $('field_1').value,true);
			x.send(null);
		}
	
	}

}
  
 var webx = new Object();

function debug(m){
//console.log(m);
}

 
 /* initialise the search box inputs */
function initialise(){
	

	try {
	 	var table = $("group1"); //.getElementsByTagName("table")[0];
	 	var rows = table.getElementsByTagName("tr");
	 	for(var i=0; i < rows.length; i++){
	 		processTableRow(rows[i]);		
	 	}
		
	} catch (e) {
		//console.log(e);
		//alert('error on searchbox');
	} 	
	
	
	var links = document.getElementsByTagName('A');
	for(var i = 0; i < links.length; i++){
		fixlink(links[i]);
	}
	
	opera_labels();	
		
	//models_dropdown();
	autosubmit_results();

	
}



function opera_labels(){
	var labels = document.getElementsByTagName('LABEL');
		
	for(var i = 0; i < labels.length; i++){
		var l = labels[i];
		opera_label(l);
		/*if (l.hasAttribute && l.hasAttribute('for')){
			debug(l);
				
			if (window.opera || navigator.vendor.indexOf('Apple') >= 0 || navigator.userAgent.indexOf('MSIE') >= 0){
				opera_label(l);
			}
		}*/
	}
}

function opera_label(l) {
	l.onclick = function(e){
		if(e){
			e.stopPropagation();
		}
		//alert(l.htmlFor);
		var input = document.getElementById(l.htmlFor);
		//console.log(input);
		//alert('p'); 
		debug(l, input);
		input.click();
		return true;
	}
}


function fixlink(l){
	//console.log(l);
	if (l.href.substring(l.href.length -1) =="#"){
		l.onclick = function(){alert('Coming Soon'); return false;}
	}
}

 
 
window.onload = initialise;

