/****************************************/
// Search function for routes
var xmlHttp
var xmlHttp2
var timeID = 5

function showResult(str, page, holder)
{
	if (str.length==0)
	{ 
		holder.innerHTML=""; //document.getElementById("livesearch").innerHTML="";
		return
	 }
	
	xmlHttp=GetXmlHttpObject()
	
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url= 'routes/'+page //"mapSearch.php"
	url=url+"?q="+str
	url=url+"&sid="+Math.random()
	
	//xmlHttp.onreadystatechange=stateChanged
	xmlHttp.onreadystatechange=function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{
			holder.innerHTML=xmlHttp.responseText;
			if(page == "loadRoutes.php"){
				loaded = true;
			} else if(page =="loadAllRoutes.php"){
				loaded2 = true;
			} else if(page =="mapSearch.php"){
				loaded = true;
			}
			//timerID = window.setInterval("process()",2000);
			
			//var flash = document.getElementById("vespaMap");
			//alert(flash.mapIsReady());			
		} 	
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function process(){
	window.clearInterval(timerID);
	//alert("PROCESS");
}
/*
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		holder.innerHTML=xmlHttp.responseText;
	} 
}
*/
/****************************************/
/****************************************/

function showResult2(str2, page2, holder2)
{
	if (str2.length==0)
	{ 
		holder2.innerHTML="";
		return
	 }
	
	xmlHttp2=GetXmlHttpObject2()
	
	if (xmlHttp2==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url2 = 'routes/'+page2
	url2=url2+"?q="+str2
	url2=url2+"&sid="+Math.random()

	xmlHttp2.onreadystatechange=function(){
		if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
		{
			if(xmlHttp2.status == 200) {
				if(page2 == "rateRoute.php") {
					//alert("Your rating has been saved");
					alert(xmlHttp2.responseText);
					//holder2.innerHTML=xmlHttp.responseText;
				} else if(page2 == "createRoute.php"){
					alert("Your route has been created");
				} else if(page2 == "postComment.php"){
					reloadRoute();
					alert(xmlHttp2.responseText);	
				} else if(page2 == "loadRoutesOwn.php"){
					clearTimeout(showAllintervalOwn);
					showAllintervalOwn = setTimeout("showAllOwn(xmlHttp2.responseText)", 100);
				} else if(page2 == "loadAll.php"){
					clearTimeout(showAllinterval);
					showAllinterval = setTimeout("showAll(xmlHttp2.responseText)", 100);
				} else if(page2 == "loadAllSearch.php"){
					//clearTimeout(showAllinterval);
					showAll(xmlHttp2.responseText);
					//showAllinterval = setTimeout("showAll(xmlHttp2.responseText)", 100);
				} else if(page2 == "picUpload.php"){
					alert(xmlHttp2.responseText);
					//showAllinterval = setInterval("showAll(xmlHttp2.responseText)", 1000);
				} else if(page2 == "deleteRoute.php"){
					alert("Your route is successfully deleted.");
					setTimeout("goBackOwn()", 100);
					//alert(xmlHttp2.responseText);
					//showAllinterval = setInterval("showAll(xmlHttp2.responseText)", 1000);
				}
			}
			//
			//holder2.innerHTML=xmlHttp.responseText;
		} 
	}
	xmlHttp2.open("GET",url2,true)
	xmlHttp2.send(null)
}

var showAllintervalOwn;
var showAllinterval;
function showAllOwn(xml){
	//clearInterval(showAllintervalOwn);
	var flash = document.getElementById("vespaMap");
	flash.putRoutes(xml);
}

function showAll(xml){
	//alert(xml);
	//clearInterval(showAllinterval);
	var flash = document.getElementById("vespaMap");
	flash.putRoutes(xml);
}

function alertRated(){
	alert("Your rating has been saved");	
	return;
}
/*
function stateChange() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		holder.innerHTML=xmlHttp.responseText;
		//document.getElementById(element).style.border="1px solid #A5ACB2";
	} 
}*/

/****************************************/
/****************************************/

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;
}


function GetXmlHttpObject2()
{
	var xmlHttp2=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp2=new XMLHttpRequest();
	}
		catch (e)
	{
	// Internet Explorer
	try
	{
		xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}
	return xmlHttp2;
}

/****************************************/
/****************************************/


var http_request = false;

function makePOSTRequest(urlPost, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	
	if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = alertContents;	
	http_request.open('POST', urlPost, true);	
	//alert(parameters);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	//http_request.setRequestHeader("Content-type", "multipart/form-data");	
	http_request.setRequestHeader("Content-length", parameters.length);	
	http_request.setRequestHeader("Connection", "close");	
	http_request.send(parameters);
}

function alertContents() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			//alert(http_request.responseText);
			result = http_request.responseText;
			//alert(result);
			//alert(document.getElementById('routeDetail').style.display);
			alert("Your route has been created");
			// refresh your routes
			routeCreated();
			//showResult("none", "loadRoutes.php", document.getElementById("livesearch"));
			document.getElementById('routeDetail').innerHTML = result;
		} else {
			alert('There was a problem with the request.');
		}
	}
}

function get(obj) {
	if(!getRoute()){
		alert("Please make a route");
	} else {
	var poststr = "routeName=" + encodeURI( document.getElementById("routeNameCreate").value ) +
	"&routeDesc=" + encodeURI( document.getElementById("routeDesc").value ) +
	//"&File1=" + encodeURI( document.getElementById("File1").value ) +
	"&imgName="+ encodeURI( document.getElementById('imgName').value ) + 
	"&startLat=" + flash.getStartLat() + "&startLog=" + flash.getStartLng() + "&endLat=" + flash.getEndLat() + "&endLog=" + flash.getEndLng() + "&plots=" + flash.getRoute() + "&directions=" + getDirections();
	
	makePOSTRequest('routes/createRoute.php', poststr);
	}
}


function getEdit(obj) {
	if(!getRoute()){
		alert("Please make a route");
	} else {
		//top.frames['uploadFrame'].uploadPic();
	var poststr = "routeNum=" + encodeURI( document.getElementById("routeNum").value ) + "&routeName=" + encodeURI( document.getElementById("routeNameCreate").value ) +
	"&routeDesc=" + encodeURI( document.getElementById("routeDesc").value ) +
	//"&File1=" + encodeURI( document.getElementById("File1").value ) +
	"&imgName="+ encodeURI( document.getElementById('imgName').value ) + 
	"&startLat=" + flash.getStartLat() + "&startLog=" + flash.getStartLng() + "&endLat=" + flash.getEndLat() + "&endLog=" + flash.getEndLng() + "&plots=" + flash.getRoute() + "&directions=" + getDirections();
	
	makePOSTRequest('routes/updateRoute.php', poststr);
	}
}

function getEdit2(obj) {
	if(!getRoute()){
		alert("Please make a route");
	} else {
		//top.frames['uploadFrame'].uploadPic();
	var poststr = "routeNum=" + encodeURI( document.getElementById("routeNum2").value ) + "&routeName=" + encodeURI( document.getElementById("routeNameCreate2").value ) +
	"&routeDesc=" + encodeURI( document.getElementById("routeDesc2").value ) +
	//"&File1=" + encodeURI( document.getElementById("File1").value ) +
	//"&imgName="+ encodeURI( document.getElementById('imgName2').value ) + 
	"&imgName="+ encodeURI("") + 
	"&startLat=" + flash.getStartLat() + "&startLog=" + flash.getStartLng() + "&endLat=" + flash.getEndLat() + "&endLog=" + flash.getEndLng() + "&plots=" + flash.getRoute() + "&directions=" + getDirections();
	
	makePOSTRequest('routes/updateRoute.php', poststr);
	}
}
