/**************************************************************/
// START: Basic DOM Functions & Global Variables
/**************************************************************/

// function to return object based on its id
function g(o) { return document.getElementById(o); }

// function to hide an ojbect
function h(o) { if (typeof o == "string") o = g(o); o.style.visibility = "hidden"; o.style.display = "none"; }

// function to show an object
function s(o) { if (typeof o == "string") o = g(o); o.style.visibility = "visible"; o.style.display = "block"; }

// function to set innerHTML of object
function ih(o,d) { o.innerHTML = d; }

// cro = Create xmlRequest Object
function cro() {
  // declare the variable to hold the object
	var request_o;
	try {
		request_o = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		try {  
			request_o = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (oc) {
			request_o = null;
		} 
	}
	if (!request_o && typeof XMLHttpRequest != 'undefined') 
		request_o = new XMLHttpRequest();
	return request_o;
}

var loaded = false;
var xmr = null;
function onloadFunc() {
	if (!xmr) {
			xmr = cro();
			if (!xmr || !document.getElementById) {
				alert("You must be using IE 5.5 > or an update Firefox/Mozilla browser.");
			} else {
				loaded=true;
			}
	}
}

function handleIncoming() {
  	/* Make sure that the transaction has finished. The XMLHttpRequest object 
  	has a property called readyState with several states:
  	0: Uninitialized
  	1: Loading
  	2: Loaded
  	3: Interactive
  	4: Finished */

  	if(xmr.readyState == 4){ //Finished loading the response
  		
  		// Check that a successful server response was received
      if (xmr.status == 200) {
    		/* We have got the response from the server-side script,
    			let's see just what it was. using the responseText property of 
    			the XMLHttpRequest object. */
    		var response = xmr.responseText;
    		
    		// reset the content in the main div area		
    		var al = g('map_info');
    		al.innerHTML = response;
  		}
  	}
  }

function loadInfo(city) {
	if (!xmr) {
		xmr = cro();
		if (xmr) loaded=true;
	}
	
	if (loaded) {
		xmr.open('get',"getInfo.php?city="+city);
    	xmr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    	xmr.onreadystatechange = handleIncoming;
    	xmr.send(null);
	} else {
		alert("Sorry your browser does not support this :(");
	}
}

function updateDates(t) {
if (t.value == "Live") {
	h('dt_3');
	h('dt_4');
	g('dt_2').innerHTML = "&nbsp;... to the present";
	g('dt_0').innerHTML = "from";
	s('dt_2');
	s('dt_1');
	s('dt_0');
} else if (t.value == "Lived") {
	h('dt_2');
	s('dt_3');
	s('dt_4');
	g('dt_0').innerHTML = "from";
	s('dt_1');
	s('dt_0');
} else if (t.value == "Visited") {
	g('dt_0').innerHTML = "in ";
	h('dt_2');
	h('dt_3');
	h('dt_4');
}
}

function submitForm() {
if (g('who').value == "") {
	alert("Missing who!");
} else if (g('city').value == "") {
	alert("Missing city!");
} else {
		url = 'checkCity.php?city='+encodeURIComponent(g('city').value);
		var state = "";
		if (g('state_us').value != "") state = g('state_us').value;
		if (g('state_canada').value != "") state = g('state_canada').value;
		if (g('state_world').value != "") state = g('state_world').value;
		if (state != "") {
			url = url + "&state=" + state;
			xmr.open('get', url);
			xmr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
        	xmr.onreadystatechange = handleState;
        	xmr.send(null);
		} else {
			alert("Please enter a state,province or country");
		}
}
}

function handleState() {
 	/* Make sure that the transaction has finished. The XMLHttpRequest object 
    	has a property called readyState with several states:
    	0: Uninitialized
    	1: Loading
    	2: Loaded
    	3: Interactive
    	4: Finished */

  if(xmr.readyState == 4){ //Finished loading the response
    		
    		// Check that a successful server response was received
    if (xmr.status == 200) {
      		/* We have got the response from the server-side script,
      			let's see just what it was. using the responseText property of 
      			the XMLHttpRequest object. */
    	var response = xmr.responseText;
			if (response == "0") {
				alert("Invalid City State/Country/Province Combination.");
			} else {
				g('city_id').value = response;
				document.formInfo.submit();
			}
		}
	}
}

// Creates a marker whose info window displays the letter corresponding to
// the given index
function cM(point, city_id, markercolor) {
	var marker = new GMarker(point, markercolor);
	GEvent.addListener(marker, "click", function() {
		loadInfo(city_id);
	});
	return marker;
}