function tog(theId){
	if(document.getElementById(theId)){
		if(document.getElementById(theId).style.display=='none'){
			document.getElementById(theId).style.display='';
		}else{
			document.getElementById(theId).style.display='none';
		}
	}
}
function getTerms(theID){
	if($(theID)){
		if($(theID).style.display=='none'){
			$(theID).style.display='';
			dynamic(theID,'inc/php/calcTerms.php','');
		}else{
			$(theID).style.display='none';
		}
	}
}

function $(id){
	return document.getElementById(id);
}
function addOnClick(){ 
	for(var i=1; i<=11; i++){
		if(document.getElementById('a'+i)){
			var lnk = document.getElementById('a'+i).href;
			//alert(document.getElementById('a'+i).href);
			$('tab'+i).onclick = Function('link("'+lnk+'")'); 
			$('tab'+i).onmouseover = Function('chg("tab'+i+'","on")');
			$('tab'+i).onmouseout = Function('chg("tab'+i+'","off")');
		}
	}
}

function showDetails(detail){
	$('tab1').className='';
	$('tab2').className='';
	$('tab3').className='';
	$('tab4').className='';
	$('tab01').style.display='none';
	$('tab02').style.display='none';
	$('tab03').style.display='none';
	$('tab04').style.display='none';
	if($('tab5')){//specials tab - not always rendered
		$('tab5').className='';
		$('tab05').style.display='none';
	}

	$('tab' + detail).className='active';
	$('tab0' + detail).style.display='';
}

function dynamic(theID,url,vars){
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			document.getElementById(theID).innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET",url + "?" + vars,true);
	xmlHttp.send(null);
}

function commSearch(make,model,body,priceMin,priceMax,page){
	var xmlHttp;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			document.getElementById('commercialSearch').innerHTML=xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET","inc/php/search-box.php?make=" + make + "&model=" + model + "&body=" + body + "&priceMin=" + priceMin + "&priceMax=" + priceMax + "&page=" + page,true);
	xmlHttp.send(null);
}

function submitCommSearch(page){
	if(page){
		var page = page;
	}else{
		var page = 1;
	}
	var theForm = document.forms['commercialSearch'];
	var make = theForm.elements['make'].value;
	var model = theForm.elements['model'].value;
	var body = theForm.elements['body'].value;
	var priceMin = theForm.elements['priceMin'].value;
	var priceMax = theForm.elements['priceMax'].value;

	commSearch(make,model,body,priceMin,priceMax,page);
}
function updatePrice(price,theID){
	var currentPrice = $('vehPrice').value.replace(/,/,'');
	if($(theID).checked == true){
		$('vehPrice').value = parseInt(currentPrice) + parseInt(price);
	}else{
		$('vehPrice').value = parseInt(currentPrice) - parseInt(price);
	}
	var newPrice = $('vehPrice').value;
	$('vehPrice').value = addCommas($('vehPrice').value);
	$('vehPrice2').innerHTML = $('vehPrice').value;
	updateOptions();
	dynamic('finance','inc/php/calculator.php','price='+newPrice);
}
function killModel(){
	document.forms['commercialSearch'].elements['model'].value = '';
}

function updateOptions(){
	var sCheckBoxValues = "";
	var inputs = document.getElementsByTagName('input');
    for (var i = 0; i < inputs.length; i++) {
		if(inputs[i].checked){
			sCheckBoxValues += inputs[i].value + "^";
		}
    }
	 document.forms['enq'].elements['options'].value=sCheckBoxValues;
}

function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)){
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}