//==========================================
// create the request
//==========================================
function createRequest()
{
    var request = null; 
    try
    {
        request = new XMLHttpRequest();
    }
    catch(trymicrosoft)
    {
	try
	{
	    request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(othermicrosoft)
	{
	    try
	    {
	        request = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch(failed)
	    {
	        request = null;
	    }
	}
    }

    if (request == null)
    {
        alert ("Error creating request object!");
    }
    else
    {
	return request;
    }
}

var request = createRequest();


//==========================================
// get new units list (new)
//==========================================
function updateUnits2(v_categoryId) {
	//alert ("inside updatePageTitles");
	var categoryId = v_categoryId.value;
	//alert ("categoryId: " + categoryId);
	//showDiv('unitDiv');
	if (categoryId > 0) {
    	document.cmsForm.categoryId.value = categoryId;
    	var url = "includes/updateUnitsList.php?categoryId=" + categoryId;
    	url = url + "&dummy=" + new Date().getTime();
    	request.open("GET", url, true);
    	request.onreadystatechange = updateUnitsList;
    	request.send(null);
	} else {
    	document.cmsForm.action = "showAllJobs.php";
		//alert("show all jobs");
    	document.cmsForm.submit();
	}
}


//==========================================
// get new units list
//==========================================
function updateUnits (v_categoryId) {
//alert ("inside updatePageTitles");
var categoryId = v_categoryId.value;
//alert ("categoryId: " + categoryId);
    document.cmsForm.categoryId.value = categoryId;
    var url = "includes/updateUnitsList.php?categoryId=" + categoryId;
    url = url + "&dummy=" + new Date().getTime();
    request.open("GET", url, true);
    request.onreadystatechange = updateUnitsList;
    request.send(null);
}


//==========================================
// update page select list
// (NOT using FCKeditor)
//==========================================
function updateUnitsList() {
	//alert (request.readyState);
	if (request.readyState == 4) {
		//alert (request.status);
    	if (request.status == 200) {
			var A_units = new Array();
			var unitName = "";
			var unitId = "";
			//========================================
			// new list of teachers names and ids
			//========================================
			var newList = request.responseText;
			//alert ("newList: " + newList);
	
			//========================================
			// get page names and ids
			//========================================
    		var A_newList = newList.split(";");
        	//alert ("size of A_newList: " + A_newList.length);
			var lgth = document.cmsForm.unitId.options.length; // db queries
			document.cmsForm.unitId.options.length = 0; // db queries
			clearDiv("unitId");
			parentEl = document.getElementById("unitId");
   			optionNode = document.createElement("option");
			optionNode.setAttribute("value", "");
			optionNodeText = document.createTextNode("Select Unit");
    		optionNode.appendChild(optionNodeText);
    		parentEl.appendChild(optionNode);
			lgth = document.cmsForm.unitId.options.length; // db queries
			for (var i = 0; i <  A_newList.length; i++) {
	    		A_unitInfo = A_newList[i].split(":");
	    		unitId = A_unitInfo[0];
	    		unitName = A_unitInfo[1];
            	//alert ("unitId: " + unitId);
            	//alert ("unitName: " + unitName);
	    		optionNode = document.createElement("option");
	    		optionNode.setAttribute("value", unitId);
	    		optionNodeText = document.createTextNode(unitName);
	    		optionNode.appendChild(optionNodeText);
	    		parentEl.appendChild(optionNode);
	    		//document.cmsForm.unitId.options[i] = new Option(unitName, unitId); // use this for db queries
			}

			document.getElementById("unitDiv").style.display = "block";
			//lgth = document.cmsForm.unitId.options.length; // use this for db queries
			//document.cmsForm.unitId.options.length = lgth; // use this for db queries
    	}
  	}
}

//==========================================
// clears DIVs from their sub nodes
//==========================================
function clearDiv(divToClear) {
    var firstChild = "";
    var clearDiv = document.getElementById(divToClear);
    while (clearDiv.hasChildNodes()) {
        firstChild = clearDiv.firstChild;
        clearDiv.removeChild(firstChild);
    }
}
