String.prototype.trim = function () {
	return this.replace(/^\s*/, '').replace(/\s*$/, ''); //replace(/^\s*|\s*$/,"");
}
var xmlhttp;
function SetupNewLocationDialog()
{
	document.getElementById('txtTitle').value = "";
	document.getElementById('txtField1').value = "";
	document.getElementById('txtField2').value = "";
	document.getElementById('txtNotes').value = "";
	document.getElementById('hidLocationID').value = "-1";
	showDialog('divAddNewLocation');
}
function GetLocationEditDetails(LocationID)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url = "ajax/GetLocationEditDetails.cfm?LocationID=" + LocationID;
	xmlhttp.onreadystatechange=DisplayLocationEditDetails;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function DisplayLocationEditDetails()
{
	if (xmlhttp.readyState==4) {
		var details = xmlhttp.responseText.trim().split('~');
		var sel = document.getElementById('selLocationType');
		for(var i=0; i < sel.length; i++)
			if(sel.options[i].value.split('~')[0] == details[1]) {
				sel.selectedIndex = i;
				break;
			}
		document.getElementById('txtTitle').value = details[0];
		document.getElementById('txtField1').value = details[2];
		document.getElementById('txtField2').value = details[3];
		document.getElementById('txtNotes').value = details[4];
		document.getElementById('hidLocationID').value = details[5];
		showDialog('divAddNewLocation');
	}
}
function SaveDefaults(LocationID, SessionSize, MentorID)
{
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null) {
		alert("Your browser does not support XMLHTTP!");
		return;
	}
	var url = "ajax/SaveDefaults.cfm";
	var params = "LocationID=" + LocationID + "&SessionSize=" + SessionSize + "&MentorID=" + MentorID;
	xmlhttp.onreadystatechange=OnSaveDefaults;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}
function OnSaveDefaults()
{
	if (xmlhttp.readyState==4) {
		window.location.reload();
		// we do this so the schedule table will be reloaded with the default location
	}
}
function GetLocations(MentorID)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url = "ajax/GetLocations.cfm?MentorID=" + MentorID;
	xmlhttp.onreadystatechange=DisplayLocations;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function DisplayLocations()
{
	if (xmlhttp.readyState==4) {
		document.getElementById('divLocations').innerHTML = xmlhttp.responseText;
		GetLocationDetails(document.getElementById('selLocation').value);
	}
}
function GetLocationDetails(LocationID)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url = "ajax/GetLocationDetails.cfm?LocationID=" + LocationID;
	xmlhttp.onreadystatechange=DisplayLocationDetails;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
function DisplayLocationDetails()
{
	if (xmlhttp.readyState==4) {
		document.getElementById('divLocationDetails').innerHTML = xmlhttp.responseText;
		var OwnerID = document.getElementById('hidSelectedLocationOwnerID').value;
		if(OwnerID == "-1") {
			document.getElementById('btnEdit').style.display = "none";
			document.getElementById('btnDelete').style.display = "none";
		}
		else {
			document.getElementById('btnEdit').style.display = "inline";
			document.getElementById('btnDelete').style.display = "inline";	
		}
	}
}
function InsertLocation(Title,LocationTypeID,Field1,Field2,Notes,OwnerID,LocationID)
{
	xmlhttp=GetXmlHttpObject();
	if(xmlhttp==null) {
		alert("Your browser does not support XMLHTTP!");
		return;
	}
	var url = "ajax/InsertLocation.cfm";
	var params = "Title=" + Title + "&LocationTypeID=" + LocationTypeID + "&Field1=" + Field1 + "&Field2=" + Field2 + "&Notes=" + Notes + "&OwnerID=" + OwnerID + "&LocationID=" + LocationID;
	xmlhttp.onreadystatechange=LoadLocations;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}
function DeleteLocation(LocationID)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null) {
		alert ("Your browser does not support XMLHTTP!");
		return;
	}
	var url = "ajax/DeleteLocation.cfm";
	var params = "LocationID=" + LocationID;
	xmlhttp.onreadystatechange=LoadLocations;
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}
function LoadLocations()
{
	if (xmlhttp.readyState==4) {
		window.location.reload();
		GetLocationDetails(document.getElementById('selLocation').value);
	}
}


// Support Functions
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}
