function createAJAXObject() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function getHTML(theDIV, thePage, theID){
	var xmlHttp = createAJAXObject();
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4){
			theDIV.innerHTML = xmlHttp.responseText;
		}
	}
	
    xmlHttp.open("GET", thePage, true);
    xmlHttp.send(null);
	
}

function postForm(url, parameters, crsID){
	var xmlHttp = createAJAXObject();
	var statusNote;
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				result = xmlHttp.responseText;
				if (!isNaN(result)){
					document.getElementById('insertResults').style.display = '';
					document.getElementById('insertResults').innerHTML = "<b>" + document.getElementById("crsName" + crsID).value + "</b> Successfully Inserted";
					statusNote = window.setInterval("document.getElementById('insertResults').style.display='none';",10000);
					displayNewItem(crsID, document.getElementById("crsName" + crsID).value, result.replace(/[\r\n]/g, ""));
					document.getElementById("crsName" + crsID).value = '';
					document.getElementById("crsDesc" + crsID).value = '';
				} else {
					alert('There was a problem with the insert.');
				}
			} else {
				alert('There was a problem with the insert.');
			}
		}
	}
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	//xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}

function submitForm(crsID) {
	var poststr = "crsName=" + encodeURIComponent( document.getElementById("crsName" + crsID).value ) + "&crsDesc=" + encodeURIComponent( document.getElementById("crsDesc" + crsID).value ) + "&crsParentID=" + encodeURIComponent( document.getElementById("crsParentID" + crsID).value );
	postForm('subjectInsert.cfm', poststr, crsID);
	document.getElementById("inputBox" + crsID).className = "inactiveBox";
}

function postEditForm(url, parameters, crsID){
	var xmlHttp = createAJAXObject();
	var statusNote;
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				result = xmlHttp.responseText;
				if (!isNaN(result)){
					/*
					document.getElementById('updateResults').style.display = '';
					document.getElementById('updateResults').innerHTML = "<b>" + document.getElementById("crsEditName" + crsID).value + "</b> Successfully Updated";
					statusNote = window.setInterval("document.getElementById('updateResults').style.display='none';",10000);
					alert(document.getElementById("i" + crsID).text);
					document.getElementById("i" + crsID).text = document.getElementById("crsEditName" + crsID).value;
					*/
					document.location.href='subjects.cfm';
				} else {
					alert('There was a problem with the update.');
				}
			} else {
				alert('There was a problem with the update.');
			}
		}
	}
	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", parameters.length);
	//xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(parameters);
}

function submitEditForm(crsID) {
	var poststr = "crsName=" + encodeURIComponent( document.getElementById("crsEditName" + crsID).value ) + "&crsDesc=" + encodeURIComponent( document.getElementById("crsEditDesc" + crsID).value ) + "&crsID=" + encodeURIComponent( document.getElementById("crsEditID" + crsID).value );
	postEditForm('subjectUpdate.cfm', poststr, crsID);
	document.getElementById("crsEditBox" + crsID).className = "inactiveBox";
}


function displayNewItem(crsID, crsName, newCRSID){
	//alert("[" + crsID + "][" + newCRSID + "]");
	//var parent = document.getElementById("i" + crsID);
	
	if (!document.getElementById("p" + crsID)) {
		var parent = document.createElement("DIV");
		parent.id = "p" + crsID;
		parent.style.marginLeft=0;
		
		var parentLI = document.getElementById("i" + crsID);
		parentLI.appendChild(parent, parentLI.firstChild);
	} else {
		var parent = document.getElementById("p" + crsID)
	}
	
	var newLI = document.createElement("DIV");
	newLI.id = "i" + newCRSID;
	newLI.style.marginLeft=30;
	
	newLI.appendChild(document.createTextNode(crsName + " "));
	
	var newIMG = document.createElement("IMG");
	newIMG.src = "/images/add.gif";
	newIMG.className = "btnS";
	newIMG.onclick = function (){addSub(newCRSID);}
	newIMG.style.marginLeft = 3;
	
	newLI.appendChild(newIMG);
	
	var newEditIMG = document.createElement("IMG");
	newEditIMG.src = "/images/edit.gif";
	newEditIMG.className = "btnS";
	newEditIMG.onclick = function (){editSub(newCRSID);}
	newEditIMG.style.marginLeft = 3;
	
	newLI.appendChild(newEditIMG);
	
	var newDelIMG = document.createElement("IMG");
	newDelIMG.src = "/images/del.gif";
	newDelIMG.className = "btnS";
	newDelIMG.onclick = function (){delSub(newCRSID);}
	newDelIMG.style.marginLeft = 3;
	
	newLI.appendChild(newDelIMG);
	
	parent.appendChild(newLI, parent.firstChild); //parent.insertBefore(newLI, parent.firstChild);
}





   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }


			function setCookie (name, value) {
	var expdate = new Date ();
	expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 31));
	document.cookie = name + "=" + escape (value) + "; expires=" + expdate.toGMTString() +  "; path=/";
}
function getCookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function expand(sID, btn){
	document.getElementById("p" + sID).className='vSub';
	btn.src='/images/minus.gif';
	btn.onclick = function (){contract(sID, btn)}
	var cookieValue = getCookie("crsCookie");
	if (cookieValue != null)
		cookieValue = cookieValue + "," + sID;
	else
		cookieValue = sID;
	
	setCookie("crsCookie", cookieValue);
	//alert(cookieValue);
}

function contract(sID, btn){
	document.getElementById("p" + sID).className='hSub';
	btn.src='/images/plus.gif';
	btn.onclick = function (){expand(sID, btn)}
	
	var cookieValue = getCookie("crsCookie");
	if (cookieValue != null){
		var crsArray = cookieValue.split(",");
		var i = 0;
		while (i < crsArray.length) {
			if (crsArray[i] == sID) {
				crsArray.splice(i, 1);
			} else {
				i++;
			}
		}
		
		cookieValue = crsArray.join(",");
		
		setCookie("crsCookie", cookieValue);
		//alert(cookieValue);
	}
}
//<![CDATA[	
var map = null;
var geocoder = null;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallZoomControl());
	map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(49.1563, -121.9514), 13);
    geocoder = new GClientGeocoder();
  }
}

function showAddress(address, displayaddress) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          //alert(address + " not found");
		  document.getElementById('map').style.display='none';
        } else {
		  
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          marker.openInfoWindowHtml('<div style="text-align: left; width: 200; height:50; background-color:white;">' + displayaddress + '</div>');
        }
      }
    );
  }
}

//]]>



