var tempTarget;
var tempTargetScan;
var multiArray;
var baseURL = "/";

var thisID = 0;

/*---------------------+
| Load Children
- Initiates the JS App.
- calls AJAX request for the lawyer xml
*/
function loadChildren(categoryID, selectID){


	var url = baseURL + 'searchCategories.php';
	var param = "categoryID=";

	thisID = selectID;

	param += categoryID;
	var myAjax = new Ajax.Request( url, {method: 'get', parameters: param, onSuccess: loadData, onFailure: showMessage} );

	document.getElementById("selectedCategory").value = categoryID;

	//theParentID = categoryID;
	//alert(url +"?"+ param);
}

/*---------------------+
| Load Data
- Loads the initial root element instance on AJAX Request success
*/
function loadData(originalRequest){
	var divText = originalRequest.responseText;
//	var temp = "selects";
	var splitCat = "";
	var removeSpan = "";
	var temp = "catVal";
	var i = 0;

	if(thisID > 0){
		temp += thisID;
	}

	// the div we are targetting that contains the select list
	var theDiv = document.getElementById(temp);
	//var theList = document.getElementById(temp);

	// the list itself (only one per div)
	theList = theDiv.getElementsByTagName('select')[0];

	// clear the options first so they don't pile up.
	theList.options.length = 0;

	// if there is something add, then add it.
	if(divText != ""){

		// split the individual categories
		var splitText = divText.split('|');

		// add a default
		if(thisID == 1){
			theList[0] = new Option("--- Please Select a Sub-Specialty if desired ---", "");
		}
		else if(thisID == 2){
			theList[0] = new Option("--- Please Narrow Your Search ---", "");
		}
		else{
			theList[0] = new Option("--- Please Select a Specialty ---", "");
		}

		// for each category, split it to get the ID and the name, then add the name/value to the list.
		for(i=0; i<splitText.length; i++){
			splitCat = splitText[i].split('::');
			theList[i+1] = new Option(splitCat[1]);
			theList[i+1].value=splitCat[0];
		}



		theDiv.style.display = "block";

/*
		removeSpan = "remove" + temp;
		if(document.getElementById(removeSpan)){

		}
		else if(thisID > 0){
			var y = document.createElement('span');
			y.id = removeSpan;
			y.style.color="red";
			y.appendChild(document.createTextNode('(if desired)'));
			theDiv.appendChild(y);
		}
*/

		thisID++;
		// re-assign temp with the thisID variable again here...so the first will be catVal0, and there is no such ID, so the first one will always be visible
		temp = "catVal" + thisID;
		theDiv = document.getElementById(temp);
		if(theDiv != null){
			theDiv.style.display = "none";
		}
	}
	else{
		theDiv.style.display = "none";
		thisID++;
		temp = "catVal" + thisID;
		theDiv = document.getElementById(temp);
		if(theDiv != null){
			theDiv.style.display = "none";
		}

	}

}

function checkThirdCategory(){

	var checkItem = document.getElementById("catValue2");

	if(checkItem.length > 1){
		if(checkItem.value == "--- Narrow your search further if desired ---" || checkItem.value == ""){
			alert("Please Narrow Your Search.");
			return false;
		}
	}
	return true;
}

/*---------------------+
| show Message
+ ---------------------
- shows message box, scrolls to it
- decides if error or success
- if error with xml pastes default message
- if error with submission pastes ordered list
- if success just pastes success message.
*/
function showMessage(message, type, notXML){
	/*alert("showMessage");*/
	$('messageBox').style.display = "block";
	window.scrollTo(0, 200);
	$('messageBox').className = type + "Message";
	if(!notXML && type == "errorMessage"){
		$('messaseBox').getElementsByTagName('div')[0].innerHTML = "<p>There was a problem communicating with the server, if problem continues contact us!</p>";
	}
	else{
		$('messageBox').getElementsByTagName('div')[0].innerHTML = message.responseText;
	}
}

//onload initiates the application
//window.onload = loadChildren(0,-1);

function pageShift(){
	return;
}

//window.onLoad = loadChildren(0, 0);
