var vehicles = {
	"car": {"Mini": Array('Chevrolet Matiz', 'Hyundai I 10', 'Citroen c1'),  
			"Economy": Array('Vauxhall Corsa', 'Volkswagen Polo', 'Nissan Micra' ),
			"Compact": Array('Ford focus','Volkswagen golf'),	
			"Intermediate": Array('Ford Mondeo','Vauxhall Insigna','Volkswagen Passat' )		
	},
	"van": {"Short wheel base": Array('Ford Transit SWB','Volkswagen Transporter SWB'),  
			"Long wheel base": Array('LDV Maxus','Ford Transit LWB'),
			"4m": Array('Mercedes Sprinter 4m','Volkswagen Crafter 4m'),
			"Luton": Array('Ford Transit Luton','Volkswagen Crafter Luton', 'Mercedes Sprinter Luton')
	},
	"minibus":{"7 seat": Array('Volkswagen Caddy Maxi', 'Volkswagen Sharan' ),  
			"9 seat": Array('Volkswagen Transporter')					
	}
};		
		
function show_vehicles_list(type) {
	var model_list = document.getElementById('select_model');			
	clear_select(model_list);
	var default_option = document.createElement('option');
	default_option.value = "";
	default_option.text = "Select model...";
	default_option.innerText = "Select model...";
	model_list.appendChild(default_option);
	var which = vehicles[type];
	for (var type in which) {
		var category = document.createElement('optgroup');
		category.label = type;
		for (var i=0; i< which[type].length; i++) {
			var option = new Option(which[type][i], which[type][i],false,false);
			option.innerText = which[type][i];
			category.appendChild(option);
		}
						
		model_list.appendChild(category);
	}
}

function book_show_vehicles_list(type) {
	show_vehicles_list(type);
	//show_wii();	
}

//Fix Firefox stupid behaviour with optiongroups not removing themselves when you set the length to 0
function clear_select(select) {
	while (select.hasChildNodes()) {
		select.removeChild(select.childNodes[0]);
	}
}

function set_value_for_select(select,value) {
	for (var i=0; i< select.options.length; i++) {
		if (select.options[i].value == value) {
			select.selectedIndex = i;
			return;
		}
	}
}

function show_wii() {
	var model_list = document.getElementById('select_model');
	var wii_option = document.getElementById('div_wii');
	var vehicle = model_list.options[model_list.selectedIndex].value;
	var elite_cars = vehicles.car['Elite'];
	for (var i=0; i<elite_cars.length; i++) {
		if (elite_cars[i] == vehicle) {
			wii_option.style.display = 'block';
			return;
		}
	}
	wii_option.style.display = 'none';
}


if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.id == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].id == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

function clearForm()
{
        var form = document.getElementById("book_form");
        for (var i=0; i<form.elements.length;i++){
                if( (form.elements[i].type=="text")||(form.elements[i].type=="textarea") ){
                form.elements[i].value="";
                };
                if(form.elements[i].type=="checkbox"){
                form.elements[i].checked=false;
                };
        }
}

function validate_form()
{
	var fname=document.getElementById('name');
	var phone=document.getElementById('phone');
	var email=document.getElementById('email');
	var vehicle=document.getElementById('select_car');
	var model=document.getElementById('select_model');
	var pickup_month=document.getElementById('pick_up_date_Month_ID');
	var pickup_day=document.getElementById('pick_up_date_Day_ID');
	var pickup_year=document.getElementById('pick_up_date_Year_ID');
	var drop_month=document.getElementById('drop_off_date_Month_ID');
	var drop_date=document.getElementById('drop_off_date_Day_ID');
	var drop_year=document.getElementById('drop_off_date_Year_ID');
	var find_us=document.getElementById('how');
	
	if(fname.value=="")
	{
		alert("Please enter your name");
		fname.focus();
		return false;
	}
	else if(phone.value=="")
	{
		alert("Please enter your phone number");
		phone.focus();
		return false;
	}
	else if(email.value=="")
	{
		alert("Please enter your Email address");
		email.focus();
		return false;
	}
	else if(!emailValidate(email.value))
	{
		alert("Please enter correct Email address");
		email.focus();
		return false;
	}
	else if(model.value=="")
	{
		alert("Please select your "+ vehicle.value +" model");
		model.focus();
		return false;
	}
	else if(find_us.value=="")
	{
		alert("Please enter How to find us");
		find_us.focus();
		return false;
	}
	
	else
	{
		return true;
	}	
}

function emailValidate(email) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,3})$/;
   return reg.test(email);
}

