var LatLngUrl = new Array();  // String to hold the marker LatLng position when adding new place

function loadScript(page) {
	if(document.getElementById(page+'Script')) {
		return;
	} else {
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement('script');
		script.id = page + 'Script';
		script.type = 'text/javascript';
		script.src = page + ".js";
		head.appendChild(script);
	}
}


function loadPage(page) {

loadScript(page);

if(page == "") page = "home";

			  		if(page == "home") {
			  			GUnload();
						loadMap();
					}
					if(page == "add") {
						GUnload();
						addPage();
					}
}

function addPage() {
if (GBrowserIsCompatible()) {
		addmap = new GMap2(document.getElementById("mapAdd"));
        addmap.addControl(new GSmallMapControl());
        addmap.addControl(new GMapTypeControl());
        addmap.setCenter(new GLatLng(13.753214, 100.531780), 15);
        
        GEvent.addListener(addmap,"click",function(overlay,point) {
        	if(!overlay) {
        		addmap.clearOverlays();
        		var marker = new GMarker(point);
				var tempS = marker.getLatLng().toUrlValue();
				LatLngUrl = tempS.split(",");
        		addmap.addOverlay(marker);
			}
		});
	}
}

function addNewPlace() {
	var placeName = $("addName").value;
	var parameters = "name=" + encodeURIComponent(placeName);
	if($("addCat1").value == "other") { parameters += "&cat1=" + $("other1").value; }
	else { parameters += "&cat1=" + document.getElementById("addCat1").value; }
	if($("addCat2").value == "other") { parameters += "&cat2=" + $("other2").value; }
	else { parameters += "&cat2=" + document.getElementById("addCat2").value; }
	parameters += "&price=" + document.getElementById("addPrice").value;
	parameters += "&desc=" + encodeURIComponent(document.getElementById("addDesc2").value);
	parameters += "&desc2=" + encodeURIComponent(document.getElementById("addDesc").value);
	parameters += "&address=" + encodeURIComponent(document.getElementById("addAddress").value);
	parameters += "&tel=" + encodeURIComponent(document.getElementById("addTel").value);
	parameters += "&email=" + encodeURIComponent(document.getElementById("addEMail").value);
	parameters += "&times=" + encodeURIComponent(document.getElementById("addTimes").value);
	parameters += "&owner=" + document.getElementById("addOwner").checked;
	parameters += "&lat=" + LatLngUrl[0]; 
	parameters += "&lng=" + LatLngUrl[1];
	parameters += "&pic1=" + encodeURIComponent(document.getElementById("pic1").value);
	parameters += "&pic2=" + encodeURIComponent(document.getElementById("pic2").value);
	parameters += "&pic3=" + encodeURIComponent(document.getElementById("pic3").value);
	parameters += "&pic4=" + encodeURIComponent(document.getElementById("pic4").value);
	parameters += "&menu=" + encodeURIComponent(document.getElementById("addMenu").value);
	postRequest(parameters, "addNewPlace.php",null,null,"index.php?p=add/thankyou");
	//window.location = "index.php?p=add/thankyou";
	
}

function newOrder() {
	var placeName = $("orderName").value;
	var parameters = "name=" + encodeURIComponent(placeName);
	parameters += "&email=" + encodeURIComponent($("orderEMail").value);
	parameters += "&comm=" + encodeURIComponent($("orderMSG").value);
	parameters += "&logo=" + $("orderLogo").checked;
	parameters += "&info=" + $("orderInfo").checked;
	
	postRequest(parameters, "order/thankyou.php",null,null,"index.php?p=order/thankyou");
	//window.location = "index.php?p=order/thankyou";
}
	
function postRequest(parameters, page, div, loading, redirect) {
	
	if(loading == "show") HS.showLoading();
	
	var xmlHttp;
			try
			  {
			  // Firefox, Opera 8.0+, Safari
				  xmlHttp=new XMLHttpRequest();
				  }
				catch (e)
				  {
				  // Internet Explorer
				  try
				    {
				    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
				    }
				  catch (e)
				    {
			    try
				      {
			      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			      }
			    catch (e)
			      {
			      alert("Your browser does not support AJAX!");
			      return false;
			      }
			    }
			  }
			  xmlHttp.onreadystatechange=function()
			    {
			    if(xmlHttp.readyState==4 && xmlHttp.status == 200)
			      {
			  		if(div) {
						$(div).innerHTML = xmlHttp.responseText;
					}
					HS.hideLoading();
					if(redirect) {
						window.location = redirect;
					}
			      }
			    }
			  xmlHttp.open("POST",page,true);
			  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
     		  xmlHttp.setRequestHeader("Content-Length", parameters.length);
      		  if(!document.all) {
			  	xmlHttp.setRequestHeader("Connection", "close");
			  }
			  xmlHttp.send(parameters);
}

