function str_replace(search, replace, subject, count) {
	var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
	        f = [].concat(search),
	        r = [].concat(replace),
	        s = subject,
	        ra = r instanceof Array, sa = s instanceof Array;
	s = [].concat(s);
	if (count) {
	    this.window[count] = 0;
	}

	for (i=0, sl=s.length; i < sl; i++) {
	    if (s[i] === '') {
	        continue;
	    }
	    for (j=0, fl=f.length; j < fl; j++) {
	        temp = s[i]+'';
	        repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
	        s[i] = (temp).split(f[j]).join(repl);
	        if (count && s[i] !== temp) {
	            this.window[count] += (temp.length-s[i].length)/f[j].length;}
	    }
	}
	return sa ? s : s[0];
}

var xmlHttp
function GetXmlHttpObject(){
	var xmlHttp = null;
	try{
	// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		// Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

//adds another location
function add_location(){
	var loccount = document.getElementById('loccount').value;
	var al = ['0','A','B','C','D'];
	if(loccount < 4){
		loccount++;
		document.getElementById('loccount').value = loccount;
		var element = '<table cellpadding="0" cellspacing="5" align="right" width="100%"><tr><td class="mark">' + al[loccount] + '</td><td><input type="text" name="loc' + loccount + '" id="loc' + loccount + '" class="text" size="33" /></td></tr></table><br class="clear"/>';
		//var element = '<div><label>Destination</label><input type="text" name="loc' + loccount + '" id="loc' + loccount + '" size="36"/></div>';
		var container = document.getElementById('divloc');
		var new_element = document.createElement('div');
		new_element.innerHTML = element;
		container.appendChild(new_element);
	}else{
		alert('You may only have 2 additional destinations. If you have more please call.');
	}
}

function display(){
	var miles = directions.getDistance().html;
	alert(miles);
	if(miles) return miles;
	else alert('One or more of the address/s are incorrect. Please try again.');
}

function subcheck(){
	if(! str_replace(' ','',document.getElementById('loc1').value)){
		alert('Please enter your location.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('loc2').value)){
		alert('Please enter your destination.');
		return false;
	}else{
		var map,directions;
		var loc1element = document.getElementById("loc1").value;
		var loc2element = document.getElementById("loc2").value;
		var loc3element = document.getElementById("loc3");
		var loc4element = document.getElementById("loc4");
		var from = "from: " + loc1element + " to: " + loc2element;
		if(loc3element != null){
			loc3element = document.getElementById("loc3").value;
			loc3element = str_replace('&','',loc3element);
			if(str_replace(' ','',loc3element)){
				from = from + " to: " + loc3element;
			}
		}
		if(loc4element != null){
			loc4element = document.getElementById("loc4").value;
			loc4element = str_replace('&','',loc4element);
			if(str_replace(' ','',loc4element)){
				from = from + " to: " + loc4element;
			}
		}
		loc1element = str_replace('&','',loc1element);
		loc2element = str_replace('&','',loc2element);
		
		map = new GMap2(document.getElementById("map_canvas"));
		//directionsPanel = document.getElementById("route");
		directions = new GDirections(map);
		//directionsPanel = document.getElementById("route");
		//directions = new GDirections(map);
		GEvent.addListener(directions,"load", function() {
			var d = directions.getDistance().html;
			if(d){
				xmlHttp = GetXmlHttpObject()
				if(xmlHttp == null){
					alert ("Your browser does not support AJAX!");
					return;
				}
				var url = "index.php";
				var params = "act=calc&distance="+d+"&loc1="+loc1element+"&loc2="+loc2element+"&loc3="+loc3element+"&loc4="+loc4element;
				xmlHttp.open("POST",url,true);
				xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				xmlHttp.setRequestHeader("Content-length", params.length);
				xmlHttp.setRequestHeader("Connection", "close");
				xmlHttp.onreadystatechange = function(){
					if(xmlHttp.readyState == 4){
						var resu = xmlHttp.responseText;
						if(resu){
							document.getElementById('locinfo').innerHTML = resu;
						}
					}
				}
				xmlHttp.send(params);
			}
		});
		//GEvent.addListener(directions,"load",display);
		//distance = map.getDistance(to).html;
		map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
		directions.load(from);
		//alert(distance);
		return false;
	}
	return false;
}

function subcheckfields(){
	if(! str_replace(' ','',document.getElementById('name').value)){
		alert('Please enter your name.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('phone').value)){
		alert('Please enter your phone number.');
		return false;
	}else if(document.destform.oconfirm[2].checked==true && ! str_replace(' ','',document.getElementById('tmphone').value)){
		alert('Please enter a text message phone.');
		return false;
	}else if(document.destform.oconfirm[2].checked==true && ! document.getElementById('tmcarrier').value){
		alert('Please select a text message carrier.');
		return false;
	}else if(document.destform.oconfirm[2].checked==true && document.getElementById('tmcarrier').value=='other' && ! str_replace(' ','',document.getElementById('tmcarrier_other').value)){
		alert('Please enter a text message carrier.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('captcha_code').value)){
		alert('Please enter a captcha code.');
		return false;
	}else{
		xmlHttp = GetXmlHttpObject()
		if(xmlHttp == null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url = "reserve.php";
		var params = "captcha_code=" + document.getElementById('captcha_code').value + "&act=capcheck";
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				var resu = xmlHttp.responseText;
				if(resu == 'true'){
					xmlHttp1 = GetXmlHttpObject()
					var url1 = "reserve2.php";
					var params1 = "date=" + document.destform.date.value + "&hour=" + document.getElementById('hour').value + "&minute=" + document.getElementById('minute').value + "&timestate=" + document.getElementById('timestate').value + "&act=timecheck";
					xmlHttp1.open("POST",url1,true);
					xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					xmlHttp1.setRequestHeader("Content-length", params1.length);
					xmlHttp1.setRequestHeader("Connection", "close");
					xmlHttp1.onreadystatechange = function(){
						if(xmlHttp1.readyState == 4){
							var resu1 = xmlHttp1.responseText;
							if(resu1 == 'true'){
								document.getElementById('destform').submit();
							}else{
								alert('The date/time must be at least 30 minutes ahead of the current time. Please call the office at (503) 256 5400.');
								document.getElementById('captcha_code').value='';
								document.destform.date.focus();
								document.getElementById('captcha').src = '/si/securimage_show.php?' + Math.random();
							}
						}
					}
					xmlHttp1.send(params1);
				}else if(resu == 'false'){
					alert('Please check the captcha code and try again.');
					document.getElementById('captcha_code').value='';
					document.getElementById('captcha_code').focus();
					document.getElementById('captcha').src = '/si/securimage_show.php?' + Math.random();
				}
			}
		}
		xmlHttp.send(params);
	}
	return false;
}

function subcheckfields1(){
	if(! str_replace(' ','',document.getElementById('pickup').value)){
		alert('Please enter a pick-up.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('dropoff').value)){
		alert('Please enter a drop-off.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('name').value)){
		alert('Please enter your name.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('phone').value)){
		alert('Please enter your phone number.');
		return false;
	}else if(document.destform.oconfirm[2].checked==true && ! str_replace(' ','',document.getElementById('tmphone').value)){
		alert('Please enter a text message phone.');
		return false;
	}else if(document.destform.oconfirm[2].checked==true && ! document.getElementById('tmcarrier').value){
		alert('Please select a text message carrier.');
		return false;
	}else if(document.destform.oconfirm[2].checked==true && document.getElementById('tmcarrier').value=='other' && ! str_replace(' ','',document.getElementById('tmcarrier_other').value)){
		alert('Please enter a text message carrier.');
		return false;
	}else if(! str_replace(' ','',document.getElementById('captcha_code').value)){
		alert('Please enter a captcha code.');
		return false;
	}else{
		xmlHttp = GetXmlHttpObject()
		if(xmlHttp == null){
			alert ("Your browser does not support AJAX!");
			return;
		}
		var url = "reserve.php";
		var params = "captcha_code=" + document.getElementById('captcha_code').value + "&act=capcheck";
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState == 4){
				var resu = xmlHttp.responseText;
				if(resu == 'true'){
					xmlHttp1 = GetXmlHttpObject()
					var url1 = "reserve2.php";
					var params1 = "date=" + document.destform.date.value + "&hour=" + document.getElementById('hour').value + "&minute=" + document.getElementById('minute').value + "&timestate=" + document.getElementById('timestate').value + "&act=timecheck";
					xmlHttp1.open("POST",url1,true);
					xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					xmlHttp1.setRequestHeader("Content-length", params1.length);
					xmlHttp1.setRequestHeader("Connection", "close");
					xmlHttp1.onreadystatechange = function(){
						if(xmlHttp1.readyState == 4){
							var resu1 = xmlHttp1.responseText;
							if(resu1 == 'true'){
								document.getElementById('destform').submit();
							}else{
								alert('The date/time must be at least 30 minutes ahead of the current time. Please call the office at (503) 256 5400.');
								document.getElementById('captcha_code').value='';
								document.destform.date.focus();
								document.getElementById('captcha').src = '/si/securimage_show.php?' + Math.random();
							}
						}
					}
					xmlHttp1.send(params1);
				}else if(resu == 'false'){
					alert('Please check the captcha code and try again.');
					document.getElementById('captcha_code').value='';
					document.getElementById('captcha_code').focus();
					document.getElementById('captcha').src = '/si/securimage_show.php?' + Math.random();
				}
			}
		}
		xmlHttp.send(params);
	}
	return false;
}

function tmcarrier_check(){
	if(document.getElementById('tmcarrier').value == 'other'){
		document.getElementById('tmcarrier_other').disabled = false;
	}else{
		document.getElementById('tmcarrier_other').disabled = true;
	}
}

function tmtype_check(){
	if(document.destform.oconfirm[2].checked==true){
		document.getElementById('tmphone').disabled = false;
		document.getElementById('tmcarrier').disabled = false;
	}else{
		document.getElementById('tmphone').disabled = true;
		document.getElementById('tmcarrier').disabled = true;
	}
}

/*xmlHttp1 = GetXmlHttpObject()
					var url1 = "reserve2.php";
					var params1 = "date=" + document.getElementById('name').value + "&hour=" + document.getElementById('name').value + "&minute=" + document.getElementById('name').value + "&timestate=" + document.getElementById('name').value + "&act=timecheck";
					xmlHttp1.open("POST",url1,true);
					xmlHttp1.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					xmlHttp1.setRequestHeader("Content-length", params1.length);
					xmlHttp1.setRequestHeader("Connection", "close");
					xmlHttp1.onreadystatechange = function(){
						if(xmlHttp1.readyState == 4){
							var resu1 = xmlHttp1.responseText;
							if(resu1 == 'true'){
								alert(resu1);
								//document.getElementById('destform').submit();
							}else{
								alert('The date/time must be at least 30 minutes ahead of the current time. Please try again.');
							}
						}
					}
					xmlHttp1.send(params1);*/