/* javascript function to update form field
 *  field		form field that is being counted
 *  count		form field that will show characters left
 *  maxchars 	maximum number of characters
*/
function characterCount(field, count, maxchars) {
  var realchars = field.value.replace(/\t|\r|\n|\r\n/g,'');
  var excesschars = realchars.length - maxchars;
  if (excesschars > 0) {
		field.value = field.value.substring(0, maxchars);
		alert("Error:\n\n- You are only allowed to enter up to"+maxchars+" characters.");
	} else {
		count.value = maxchars - realchars.length;
	}
}


function show_p(p_id){
	if(p_info[p_id] && image_array[p_id]){
		document.getElementById('show_p_image').innerHTML='<img src="' + image_array[p_id].m + '" />';
		document.getElementById('p_name').innerHTML=p_info[p_id].products_name;
		document.getElementById('p_op').innerHTML=p_info[p_id].op;
		document.getElementById('p_price').innerHTML='Price:'+p_info[p_id].products_price;
		document.getElementById('p_bott').innerHTML='<input type="hidden" value="' + p_id + '" name="products_id">';
		document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_cart.gif";
		}else{
			document.getElementById('show_p_image').innerHTML='<img src="' + image_array[main_p].m + '" />';
			document.getElementById('p_name').innerHTML=(''!=p_info[main_p].main_title ? p_info[main_p].main_title:p_info[main_p].products_name);
			document.getElementById('p_op').innerHTML='';
			document.getElementById('p_price').innerHTML='';
			document.getElementById('p_bott').innerHTML='';
			document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_flas.gif";
			}
	}
function smb(){
	if(document.getElementById('p_bott').innerHTML==''){
		alert('Please select... ...');
		return false;
		}else{
			return true;
			}
	 
	}
	
function p_v(v_id){
	
	var out_p_id=new Array(p_to_v.length);
for(var i=0; i<p_to_v.length; i++){
	for(var j=0; j<p_to_v[p_to_v[i]].length; j++){
		if(p_to_v[p_to_v[i]][j]==v_id){
			
			out_p_id[i]=p_id[p_to_v[i]];
			//alert(p_to_v[i]);
			break;
			} 
		}
	}
	return out_p_id;
}


function b_of_model(brand_id){
	if(0<brand_id){
		document.getElementById('show_model').innerHTML='Model:<select name="op_model" onChange="m_of_year(this.value)"><option value="0" >Select... ...</option>'+brand_of_model['b'+brand_id]+'</select>';
		document.getElementById('show_year').innerHTML='';
		document.getElementById('p_bott').innerHTML='';
		document.getElementById('show_products').innerHTML='';
		document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_flas.gif";
		document.getElementById('p_name').innerHTML=(''!=p_info[main_p].main_title ? p_info[main_p].main_title:p_info[main_p].products_name);
		}else{
			document.getElementById('show_model').innerHTML='';
			document.getElementById('show_year').innerHTML='';
			document.getElementById('p_bott').innerHTML='';
			document.getElementById('show_products').innerHTML='';
			document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_flas.gif";
			document.getElementById('p_name').innerHTML=(''!=p_info[main_p].main_title ? p_info[main_p].main_title:p_info[main_p].products_name);
			}
	}
	
function m_of_year(model_name){
		if('0'!=model_name){
		document.getElementById('show_year').innerHTML='Year:<select name="op_year" onChange="select_v()"><option value="0" >Select... ...</option>'+model_of_year[model_name]+'</select>';	
			}else{
				document.getElementById('show_year').innerHTML='';
				}
		document.getElementById('show_products').innerHTML='';
		document.getElementById('p_bott').innerHTML='';
		document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_flas.gif";
		document.getElementById('p_name').innerHTML=(''!=p_info[main_p].main_title ? p_info[main_p].main_title:p_info[main_p].products_name);
	}	
	
function select_v(){
	var out_products_html='';
	if(document.getElementsByName('op_year') && document.getElementsByName('op_brand') && document.getElementsByName('op_model')){
		var year=document.getElementsByName('op_year');
		var brand=document.getElementsByName('op_brand');
		var model=document.getElementsByName('op_model');
		
		var out_show_p=new Array();
		if(0!=year[0].value && 0!=brand[0].value && 0!=model[0].value){
			for(var i=0; i<vehicle_info.length; i++){
				
				if(brand[0].value==vehicle_info[vehicle_info[i]].vehicle_brand_id && year[0].value==vehicle_info[vehicle_info[i]].vehicle_year && model[0].value==vehicle_info[vehicle_info[i]].vehicle_model){
					var out_p_id=p_v(vehicle_info[vehicle_info[i]].vehicle_id);
					for(var j=0; j<out_p_id.length; j++){
						if(out_p_id[j]){
							//alert(out_p_id[j]);
							out_products_html+='<option value="' + out_p_id[j] + '" >'+p_info[out_p_id[j]].products_name+'</option>';
							}
						}
					
					}
				}
				
				if(''!=out_products_html){
					document.getElementById('show_products').innerHTML='Products:<select name="op_products" onChange="show_p(this.value)"><option value="0" >Select... ...</option>'+out_products_html+'</select>';
					
					}else{ document.getElementById('show_products').innerHTML='No products'; }
					
			}else{
				document.getElementById('show_products').innerHTML=''; document.getElementById('p_bott').innerHTML=''; 
			        document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_flas.gif"; 
					document.getElementById('p_name').innerHTML=(''!=p_info[main_p].main_title ? p_info[main_p].main_title:p_info[main_p].products_name);
					}
		}else{ 
		document.getElementById('show_products').innerHTML='No products'; document.getElementById('p_bott').innerHTML=''; 
		document.getElementById("to_cart").src="includes/templates/classic/buttons/english/button_in_flas.gif";
		document.getElementById('p_name').innerHTML=(''!=p_info[main_p].main_title ? p_info[main_p].main_title:p_info[main_p].products_name);
		}
	}
