// set up globals for the request
var mybutton; var itdisplay; var myframe;
var loc = document.location.hostname;
var path = document.location.pathname;
var query = document.location.search;
if (loc.indexOf("127.0.0.1")>-1||loc.indexOf("localhost")>-1)
{
 	var url="http://127.0.0.1/welcome";
	if (loc.indexOf("localhost")>-1) document.location.href="http://127.0.0.1"+path+query;
}
else if (loc.indexOf("nancy")>-1) 	var url="http://nancy/welcome";
else if (loc.indexOf("www.welcometostratford.com")>-1) var url="http://www.welcometostratford.com";
else if (loc.indexOf("www")<0) document.location.href="http://www.welcometostratford.com"+path+query;
//else if (loc.indexOf("savourstratford.com")>-1) document.location.href="http://www.welcometostratford.com"+path;


//var requrl;
var req=false; var req2=false;
function getObjs(x)
{
	if (document.getElementById)
	{
	    if (document.getElementById('listframe'))
	    {
	        myframe = document.getElementById('listframe');
            if (myframe.contentDocument) mybutton = myframe.contentDocument.getElementById("button"+x);
 	    	else if (myframe.Document)  mybutton = myframe.Document.getElementById("button"+x);
        }
	    else mybutton = document.getElementById("button"+x);
	    itdisplay = document.getElementById("itinerary");
	}
	else if(document.all)
	{
        if (document.all('listframe'))
		{
            myframe = document.all('listframe');
			mybutton = myframe.Document.all("button"+x);
        }
		else mybutton = document.all("button"+x);
		itdisplay = document.all("itinerary");
	}
}
// Uses asynchronous request to save the note to a session var without leaving the page.
// Cool or what?
function saveItem(type,cat,id,x)
{
	requrl = url+"/itinerary.php?type="+type+"&subtype="+cat+"&ID="+id;
    if (window.XMLHttpRequest) {   req = new XMLHttpRequest();  }
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject) { req = new ActiveXObject("Microsoft.XMLHTTP");  }
	if (req) {
		getObjs(x); // sets value of global mybutton
		mybutton.innerHTML="<span style=\"color:#FFFFFF\">SAVING....</span>";
		foo = window.setTimeout('wait()',800);
		// pause a little, just so it looks like something is happening...
	}
	else  alert("Your note could not be saved.");
}


function processReqChange()
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			mybutton.innerHTML="<span style=\"color:#FFFFFF\">ITEM SAVED</span>";
			if (req.responseText==0) resp = "no";
			else resp = req.responseText;
			itdisplay.innerHTML = "You currently have "+resp + " items in your itinerary.";
		}
		else {
          alert("There was a problem saving this item:\n" + req.statusText);
        }
    }
}


function wait()
{
	req.onreadystatechange = processReqChange;
    req.open("GET", requrl, true);
    req.send(null);
}

function processReqChange2()
{
    // only if req shows "complete"
    if (req2.readyState == 4) {
        // only if "OK"
	  //	alert(req2.status);
        if (req2.status == 200) {
			if (req2.responseText==0) resp = "no";
			else resp = req2.responseText;
			itdisplay.innerHTML = "You currently have "+resp + " items in your itinerary.";
		}
		else {
          alert("There was a problem getting your items:\n" + req2.statusText);
        }
    }
}



function initPlanner()
{
	getObjs(0);
	if (itdisplay!="undefined")
	{
		requrl2 = url+"/itinerary.php";
   //		alert (requrl2);
       	if (window.XMLHttpRequest) {
          //alert("Http");
			req2 = new XMLHttpRequest();
			req2.onreadystatechange = processReqChange2;
       		req2.open("GET", requrl2, true);
        	req2.send(null);
    // branch for IE/Windows ActiveX version
    	} else if (window.ActiveXObject) {
         // alert("ActiveX");
			req2 = new ActiveXObject("Microsoft.XMLHTTP");
        	if (req2) {
                req2.onreadystatechange = processReqChange2;
				req2.open("GET", requrl2, true)
            	req2.send();
        }
  	  }
  	else  alert("there was a problem getting your data.");
	}
}
