//generates an HTTP request
function getXMLHttpReq() { 
	var req = null;
 
	if (window.XMLHttpRequest)
   		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
   		req = new ActiveXObject(Microsoft.XMLHTTP);
	
	return req;
} //END getXMLHttpReq()

//queries the server to return a pages editable content
function loadContents()
{
    //get the name of the page first
    var sPath = window.location.pathname;
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    
    //change sPage in case we're on the base URL
    if(sPage == "")
    {
    	sPage = "index.html";
    }
    //change sPage if we're in edit mode
    if(sPage == "editWebPage.php")
    {
    	sPage = window.actualPageValue;
    }
	
	//load the page name into the URL for the GET request that grabs its contents
	var params = "pageName=" + sPage;
	var url = "php/loadPageContents.php?" + params;
    
    var xhr = getXMLHttpReq();

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        var result = xhr.responseText;
       
        var lastIndex = 0;
        
        //step through the returned string to parse out multiple zone contents
        for(var i = 0; i < result.length; i++)
        {
        	//zone contents are separated by the & character
        	//continue reading until we find one, then start parsing
        	if(result.charAt(i) == '^')
        	{
        		//substring the current content out from the whole return
        		var temp = result.substring(lastIndex, i-1);
        		var tempArray = new Array();
        		
        		//split the content into its zone and actual content
        		tempArray = temp.split('|');
        		
        		//load the content to the correct zone
        		document.getElementById(tempArray[0]).innerHTML = tempArray[1];
        		
        		//cut the return string down if there's more left to parse
        		//then continue searching for the next zone's contents
        		if(i+1!=result.length)
        		{
        			result = result.substr(i+1);
        			i = 0;
        		}
        	}
        }
      }
    } //END anonymous function
   
    xhr.open("GET", url, true);
    xhr.send(); //send the name of the page in the AJAX request
} //END loadContents()
