var note; var notepad; var map; var ntab; var mtab;
var mytab;
var listing;  var ltab;
// set up globals for the request
var req=false;
var mybutton;
/*var loc = document.location.hostname;
var path = document.location.pathname;
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+"/";
}
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("dev.welcometostratford.com")>-1) var url="http://dev.welcometostratford.com";
else if (loc.indexOf("www")<0) document.location.href="http://www.welcometostratford.com"+path+"/";

var requrl;*/

function tabs()
{
	if (ntab) ntab.className="tab";
	mtab.className="tab";
     	if (ltab!=null) 	ltab.className="tab";
	mytab.className="tabselect";
}

function init(choice,x,mapurl)
{
	if (choice=="l") listingSwapper(x);
	else if (choice=="m") mapSwapper(x,mapurl);
	else if (choice=="n") noteSwapper(x);
}

function sectionSetup(choice,x)
{
	if (document.getElementById&&!document.all)
	{
		if (document.getElementById("listing"+x)) listing = document.getElementById("listing"+x);
		if (document.getElementById("notepad"+x))
		{
			notepad = document.getElementById("notepad"+x);
			note = document.getElementById("note"+x);
			ntab = document.getElementById("n_tab_"+x);
		}
		map = document.getElementById("map"+x);
		mytab = document.getElementById(choice+"_tab_"+x);
		mtab = document.getElementById("m_tab_"+x);
	    if (document.getElementById("l_tab_"+x))	ltab = document.getElementById("l_tab_"+x);

	}
	else if(document.all)
	{
		if (document.all("listing"+x)) listing = document.all("listing"+x);
		if (document.all("notepad"+x))
		{
			notepad = document.all("notepad"+x);
			note = document.all("note"+x);
			ntab = document.all("n_tab_"+x);
		}
		map = document.all("map"+x);
		mytab = document.all(choice+"_tab_"+x);
		mtab = document.all("m_tab_"+x);
		if ( document.all("l_tab_"+x)) ltab = document.all("l_tab_"+x);
	}
}

function mapSwapper(x,mapurl)
{
	sectionSetup('m',x);
	tabs();
    if (listing!=null) listing.style.display="none";
	if (notepad!=null) notepad.style.display="none";
	map.style.display="inline";
	map.innerHTML='<iframe src="'+mapurl+'" width="520" height="330" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe>';
}

function noteSwapper(x)
{
	sectionSetup('n',x);
	tabs();
	listing.style.display="none";
	map.innerHTML='';
	map.style.display="none";
	if (notepad) notepad.style.display="inline";
}

function listingSwapper(x)
{
	sectionSetup('l',x);
	tabs();
	map.innerHTML='';
	map.style.display="none";
	if (notepad) notepad.style.display="none";
	listing.style.display="inline";
}

// Uses asynchronous request to save the note to a session var without leaving the page.
// Cool or what?
function saveNote(reqdata,count,formobj,x)
{
	requrl = url+"/notesaver.php?note="+reqdata+"&count="+count;
	// change that later!
	try { req = new XMLHttpRequest();
    }  catch (trymicrosoft) {
	  try {
	    req = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (othermicrosoft) {
	    try {
	      req = new ActiveXObject("Microsoft.XMLHTTP");
	    } catch (failed) {
	      req = false;
	    }
	  }
	}
	if (req) {
		mybutton=formobj;
		mybutton.className="darkbutton";
		mybutton.value="SAVING....";
		foo = window.setTimeout('wait()',1000);
		// pause a little, just so it looks like something is happening...
		mybutton.value="SAVE NOTE";
		sectionSetup(null,x);
		note.innerHTML="<strong>Note:</strong> "+reqdata;
	}
	else  alert("Your note could not be saved.");
	listingSwapper(x);
}

function processReqChange()
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			//alert("Your note was saved.");
			mybutton.className="button";
			mybutton.value="NOTE SAVED";
		}
		else {
           alert("There was a problem saving the note:\n" + req.statusText);
        }
    }
}

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

