// JavaScript Document

function $2() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function GetXmlHttp2(){
	var xmlhttp = new Object();
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType("text/xml");
	} else if (window.ActiveXObject) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function LoadPickupLocations(region_code){
	var xmlhttp = new Object();
	var xmlhttp = GetXmlHttp2();
	
	$2('loading').style.display='block';
	$2('region').disabled=true;
	$2('pickup_location').disabled=true;
	$2('dropoff_location').disabled=true;
	
	var location_text = new Array();
	var location_value = new Array();	
	
	xmlhttp.open("POST", "/get_pickup_locations", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {

			var locations = xmlhttp.responseXML.getElementsByTagName('location');	
			for(p = 0; p < locations.length; p++) {
				location_value.push(locations[p].getAttribute("code"));
				location_text.push((locations[p].firstChild.data != null)?locations[p].firstChild.data:"");
			}
			
			LoadSelect('pickup_location',location_value,location_text,'Select Pickup Location','');
			LoadSelect('dropoff_location',[],[],'Select Dropoff Location','');
		} else {
			LoadSelect('pickup_location',[],[],'Select Pickup Location','');
			LoadSelect('dropoff_location',[],[],'Select Dropoff Location','');	
		}
	}	
	
	$2('loading').style.display='none';
	$2('region').disabled=false;
	$2('pickup_location').disabled=false;
	$2('dropoff_location').disabled=false;
	
	region_code = escape(region_code);
	
	var params = "region_code=" + region_code;
	xmlhttp.send(params);
	
}

function LoadDropoffLocations(pickup_location){
	var xmlhttp = new Object();
	var xmlhttp = GetXmlHttp2();
	
	$2('loading').style.display='block';
	$2('region').disabled=true;
	$2('pickup_location').disabled=true;
	$2('dropoff_location').disabled=true;
	
	var location_text = new Array();
	var location_value = new Array();	

	xmlhttp.open("POST", "/get_dropoff_locations", true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {

			var locations = xmlhttp.responseXML.getElementsByTagName('location');
			
			for(p = 0; p < locations.length; p++) {
				location_value.push(locations[p].getAttribute("code"));
				location_text.push((locations[p].firstChild.data != null)?locations[p].firstChild.data:"");
			}
			
			LoadSelect('dropoff_location',location_value,location_text,'Select Dropoff Location',unescape(pickup_location));
		} else {
			LoadSelect('dropoff_location',[],[],'Select Dropoff Location','');	
		}
	}
	
	
	
	$2('loading').style.display='none';
	$2('region').disabled=false;
	$2('pickup_location').disabled=false;
	$2('dropoff_location').disabled=false;
	
	pickup_location = escape(pickup_location);
	
	var params = "pickup_location=" + pickup_location;
	xmlhttp.send(params);
	
}




function LoadSelect(dest_object,value_array,text_array,title_text,selected){
	$2(dest_object).length = 0;
	j = 0;r = 0;
	if(title_text != ''){
		$2(dest_object).options[j++] = new Option(title_text,'');
	}
	var len = value_array.length + j;
	for(i = j; i < len; i++){
		$2(dest_object).options[i] = new Option(text_array[r],value_array[r]);
		if(value_array[r] == selected){
			$2(dest_object).options[i].selected = true;
		}
		r++;
	}
}

function UpdateDates(){
	
	var from_day = $2('from_day').value;
	var from_month = $2('from_month').value;
	var from_year = $2('from_year').value;
	var from_time = $2('from_time').value;
	
	var from_date = new Date(from_year,from_month - 1,from_day);	
	var to_date = new Date(from_year,from_month-1,from_day);
	to_date.setDate(to_date.getDate() + 7);
	
	$2('to_day').value = to_date.getDate();
	$2('to_month').value = to_date.getMonth() + 1;
	$2('to_year').value = to_date.getFullYear();

}
