
var tab_div	=["Gallery", "Options", "Features", "Specs", "Warranty", "Safety/Recall"];

function switch_div(div_name){
	labels_code='<table border="0" width="500" cellpadding="0" cellspacing="0"><tr>';
	for(i=0;i<tab_div.length;i++){
		if(tab_div[i]==div_name){
              labels_code+='<td class="tab_cars_on" width="90">'+tab_div[i]+'</a></td>';
    		  labels_code+='<td width="3"></td>';
			document.getElementById(tab_div[i]).style.display='';
		}
		else{
			labels_code+='<td class="tab_cars_off" width="100"><a href="javascript:switch_div(\''+tab_div[i]+'\');" class="menu_cars_off_link">'+tab_div[i]+'</a></td>';
  			labels_code+='<td width="3"></td>';
			document.getElementById(tab_div[i]).style.display='none';
		}
	}
	labels_code+='</tr></table>';
	document.getElementById("labels").innerHTML =labels_code;
}

function change_picture(picture){
	document.getElementById("main_pic").innerHTML="<center><IMG src='"+picture+"'></center>";
}

function checkAll(){
	for (i = 0; i < document.optionForm.checkBoxes.length; i++)
		document.optionForm.checkBoxes[i].checked = true ;
}

function uncheckAll(){
	for (i = 0; i < document.optionForm.checkBoxes.length; i++)
		document.optionForm.checkBoxes[i].checked = false ;
	//document.getElementById("optionDiv").innerHTML ='NONE';
	document.getElementById("opts").value =0;
	document.getElementById("total_price").innerHTML="$"+document.getElementById("msrp").value;
	document.getElementById("total_options").innerHTML="$0";
	document.getElementById("nopts").value=0;
	document.getElementById("number_options").innerHTML="0";
	//document.getElementById("removeDiv").style.display='none';
	//document.getElementById("optionsTitle").style.display='none';
}

//remove all occurences of a substring in a string
function remove(s, t) {
	i = s.indexOf(t);
	r = "";
	if (i == -1) return s;
	r += s.substring(0,i) + remove(s.substring(i + t.length), t);
	return r;
}
function ltrim(s){
	var l=0;
	while(l < s.length && s[l] == ' '){l++;}
	return s.substring(l, s.length);
}

function checkModified(indice,option_desc,msrp){
	indice=parseInt(indice);
	var price=parseFloat(document.getElementById("opts").value);
	var car_msrp=parseFloat(document.getElementById("msrp").value);
	/*var list=document.getElementById("optionDiv").innerHTML;
	list=remove(list,'\n');list=remove(list,'\r');list=remove(list,'NONE');
	list=remove(list,'<table width="100%">');list=remove(list,'</table>');
	list=remove(list,'<TABLE width="100%">');list=remove(list,'</TABLE>');
	list=remove(list,'<tbody>');list=remove(list,'</tbody>');
	list=remove(list,'<TBODY>');list=remove(list,'</TBODY>');
	list='<table width="100%">'+list;*/
	if(document.optionForm.checkBoxes[indice-1].checked==1){
		document.getElementById("opts").value = price+parseFloat(msrp);
		document.getElementById("nopts").value=parseFloat(document.getElementById("nopts").value)+1;
		//list+='<tr><td width="80%">'+option_desc+'</td><td>$'+msrp+'</td></tr>';
	}
	else{
		document.getElementById("opts").value = price-parseFloat(msrp);
		document.getElementById("nopts").value=parseFloat(document.getElementById("nopts").value)-1;
		//option_desc=option_desc.replace("&","&amp;");
		//list = remove(list,'<tr><td width="80%">'+option_desc+'</td><td>$'+msrp+'</td></tr>');
		//list = remove(list,'<TR><TD width="80%">'+option_desc+'</TD><TD>$'+msrp+'</TD></TR>');
	}
	document.getElementById("number_options").innerHTML=document.getElementById("nopts").value;
	document.getElementById("total_options").innerHTML="$"+document.getElementById("opts").value;
	document.getElementById("total_price").innerHTML="$"+(parseFloat(document.getElementById("opts").value)+car_msrp);
	/*
	document.getElementById("removeDiv").innerHTML="<a href='javascript: uncheckAll();'>Remove All</a>";
	list+='</table>';
	if(list=='<table width="100%"></table>'){
		document.getElementById("removeDiv").style.display='none';
		document.getElementById("optionsTitle").style.display='none';
		list='NONE';
	}
	else{
		document.getElementById("removeDiv").style.display='';
		document.getElementById("optionsTitle").style.display='';
	}
	document.getElementById("optionDiv").innerHTML = list;
	*/
}

/* AJAX TRIM SELECTION */

function refresh_trim(var_style_id){
	jQuery.noConflict();
	
	//this is a customisable array that will decide which divs to put a loading gif into
	var tab = new Array('specs_header', 'features', 'specifications', 'safety', 'options', 'photos');
	
	//we first replace the divs by an animated loading gif
	for(var i=0; i<tab.length;i++){
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id',tab[i]+'Div');
		newDiv.innerHTML = "<center><img src='/images/circle.gif'></center>";
		var parent = document.getElementById(tab[i]+'Div').parentNode;
		parent.removeChild(document.getElementById(tab[i]+'Div'));
		parent.appendChild(newDiv);
	}
	
	// GET THE CONTENT OF THE DIVS
	jQuery.get('spip.php?page=get_specs_header&var_mode=recalcul',{style_id: var_style_id},function(c) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id','specs_headerDiv');
		newDiv.innerHTML = c;
		var parent = document.getElementById('specs_headerDiv').parentNode;
		parent.removeChild(document.getElementById('specs_headerDiv'));
		parent.appendChild(newDiv);
	});
	
	jQuery.get('spip.php?page=get_features&var_mode=recalcul',{style_id: var_style_id},function(c) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id','featuresDiv');
		newDiv.innerHTML = c;
		var parent = document.getElementById('featuresDiv').parentNode;
		parent.removeChild(document.getElementById('featuresDiv'));
		parent.appendChild(newDiv);
	});
	
	jQuery.get('spip.php?page=get_specifications&var_mode=recalcul',{style_id: var_style_id},function(c) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id','specificationsDiv');
		newDiv.innerHTML = c;
		var parent = document.getElementById('specificationsDiv').parentNode;
		parent.removeChild(document.getElementById('specificationsDiv'));
		parent.appendChild(newDiv);
	});
	
	jQuery.get('spip.php?page=get_safety&var_mode=recalcul',{style_id: var_style_id},function(c) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id','safetyDiv');
		newDiv.innerHTML = c;
		var parent = document.getElementById('safetyDiv').parentNode;
		parent.removeChild(document.getElementById('safetyDiv'));
		parent.appendChild(newDiv);
	});
	
	jQuery.get('spip.php?page=get_options&var_mode=recalcul',{style_id: var_style_id},function(c) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id','optionsDiv');
		newDiv.innerHTML = c;
		var parent = document.getElementById('optionsDiv').parentNode;
		parent.removeChild(document.getElementById('optionsDiv'));
		parent.appendChild(newDiv);
	});
	
	jQuery.get('spip.php?page=get_photos&var_mode=recalcul',{style_id: var_style_id},function(c) {
		var newDiv = document.createElement('div');
		newDiv.setAttribute('id','photosDiv');
		newDiv.innerHTML = c;
		var parent = document.getElementById('photosDiv').parentNode;
		parent.removeChild(document.getElementById('photosDiv'));
		parent.appendChild(newDiv);
		initScrollbars2();
	});
	document.getElementById('mainSelectTrim').value=var_style_id;
}
/* END AJAX TRIM SELECTION */

//get the tab index from the string contained by the hash
function get_tab_index_from_url(tabs){
	var indice = 0;
	//get tab from url
	var tab = window.location.hash;
	if(tab!=''){
		var selected_tab = '';
		for(var i=0; i<tab.length;i++)
			if(isNaN(tab.charAt(i)) && tab.charAt(i)!='#') selected_tab = selected_tab+tab.charAt(i);
		if(selected_tab!='') indice=tabs[selected_tab];
		else indice=0;
	}
	else indice=0;
	
	return indice;
}

//get the style code from the hash
function get_style_id_from_url(){
	//get style_id from url
	var style_id = window.location.hash;
	if(style_id!=''){
		var selected_style_id = '';
		for(var i=0; i<style_id.length;i++)
			if(!isNaN(style_id.charAt(i)) && style_id.charAt(i)!='#') selected_style_id = selected_style_id+style_id.charAt(i);
	}
	else return '';
	return selected_style_id;
}

//change the url hash string, keeping the number
function set_hash_number(number){
	var hash = window.location.hash;
	if(hash!=''){
		var hash_string = '';
		for(var i=0; i<hash.length;i++)
			if(isNaN(hash.charAt(i)) && hash.charAt(i)!='#') hash_string = hash_string+hash.charAt(i);
		location.hash='#'+hash_string+number;
	}
	else location.hash='#'+number;
}

//change the url hash number, keeping the aplhabetical string
function set_hash_string(string){
	var hash = window.location.hash;
	if(hash!=''){
		var hash_number = '';
		for(var i=0; i<hash.length;i++)
			if(!isNaN(hash.charAt(i)) && hash.charAt(i)!='#') hash_number = hash_number+hash.charAt(i);
		location.hash='#'+string+hash_number;
	}
	else location.hash='#'+string;
}