//This file loads some functions for use in our mapping system.
//Created by: Max Sklar

// cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);  
		return true;  
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);  
		return r;  
	} else {
		elm['on' + evType] = fn;
	}
}

//Utility functions used to Grab Files
//----------------------------------------------

//This utility function inputs a filename and a function.  It uses the xmlhttprequest object to grab the file, and takes the text that it received back,
// and feeds that text into the function.
function doWithFile(filename, isXML, whatToDo) {
	var request = GXmlHttp.create();
	request.open('GET', filename, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (isXML) whatToDo(request.responseXML);
			else whatToDo(request.responseText);
		}
	}
	request.send(null);
}

//This is for posting information to a file.
function postWithFile(filename, isXML, whatToSend, whatToDo) {
	var request = GXmlHttp.create();
	request.open('POST', filename, true);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			if (isXML) whatToDo(request.responseXML);
			else whatToDo(request.responseText);
		}
	}
	
	//This allows us to read post data.
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	
	//Send the request.
	request.send(whatToSend);
}

//This prepares information to be posted to the website.
//For example, if we want to go to www.stickymap.com/test.php?name=max%20sklar&a=12345
//We might call createPostStatement("name", "Max Sklar") and this will generate
// name=max%20sklar& along with the escape sequence and the ampersand to prepare for the next statement
function createPostStatement(name, value) {
	return name + "=" + escape(value) + "&";
}

//This function will help us submit forms to php using AJAX.
//Originally from http://www.devarticles.com/c/a/XML/XML-in-the-Browser-Submitting-forms-using-AJAX/5/
//fobj: the form object, valfun: a validation function as a string (null if we are not validating)
function getFormValues(fobj) {
	var str = "";
	for(var i = 0;i < fobj.elements.length;i++)
		str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
	str = str.substr(0,(str.length - 1));  //Get rid of last ampersand
	return str;
}

//These functions help us print out html
//------------------------------------------------------------------------

//Make a button in html
//label - what is the button labeled (a string)
//onClick - a string containing the javascript function to call when the button is pressed.
function makeButton(label, onClick) {
	return "<input " + makeAttribute("type", "button") + makeAttribute("value", label) + makeAttribute("onClick", onClick) + ">";
}

//Make a hidden variable in a form in html.
// value - initial value of the data.
// name - name of the hidden input object.
function makeHidden(name, value) {
	return "<input" + makeAttribute("type", "hidden") + makeAttribute("name",name) + makeAttribute("value", value) + ">";
}

//This function makes the string name="value"
function makeAttribute(name, value) {
	return " " + name + "=\"" + value + "\"";
}

//Change Text From visible to invisible.
function toggleVisible(id) {
  var item = document.getElementById(id);
  var value = item.style.display ? '' : 'none';
  item.style.display = value;
}

//Make a section visible
function makeVisible(id) {
	document.getElementById(id).style.display = "";
}

//Make a section invisible
function makeInvisible(id) {
	document.getElementById(id).style.display = "none";
}