var tempTarget;
var tempTargetScan;
var multiArray;
var baseURL = "/";
var theState = "";

/*---------------------+
| Load Children
- Initiates the JS App.
- calls AJAX request for the lawyer xml
*/
function loadMenus(state){
	var url = baseURL + 'stateCity.php';

	var param = "";
		
	if(state != ""){
		param = "state=" + state;
		
//		theState = state;
	
		document.getElementById("selectedState").value = state;
		
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: param, onSuccess: loadCities, onFailure: showMessage} );
	}
	else{
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: param, onSuccess: loadStates, onFailure: showMessage} );
	}
	
	//alert(url +"?"+ param);
}

/*---------------------+
| Load States
- Loads the initial root states element instance on AJAX Request success 
*/
function loadStates(originalRequest){
	var divText = originalRequest.responseText;
	var splitText = "", theID = "", optionText = "";
	var j = 0;

	theID = "stateVal";
	optionText = "--- Please select a state ---";

	var theList = document.getElementById(theID);
	if(divText != ""){
		splitText = divText.split('|');
		theList[0] = new Option(optionText);
		theList[0].value = "--- Please select a state ---";
		for(i=0; i<splitText.length; i++){
			j = i+1;
			theList[i+1] = new Option(splitText[i]);
			theList[i+1].value=splitText[i];
		}
		theList.style.display = "block";
	}
	else{
		theList.style.display = "none";
	}
}

/*---------------------+
| Load Cities
- Loads the cities elements of the states instance on AJAX Request success 
*/
function loadCities(originalRequest){
	var divText = originalRequest.responseText;
	var splitText = "", theID = "", optionText = "";
	var j = 0;
	
	theID = "cityVal";
	optionText = "--- Please select a city ---";
	
	var theList = document.getElementById(theID);
	theList.options.length = 0;
	if(divText != ""){
		splitText = divText.split('|');
		theList[0] = new Option(optionText);
		theList[0].value = "";
		for(i=0; i<splitText.length; i++){
			j = i+1;
			theList[j] = new Option(splitText[i]);
			theList[j].value = splitText[i];
		}
		theList.style.display = "block";		
	}
	else{
		theList.style.display = "none";
	}
}

/*---------------------+
| 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 = loadMenus("");

function pageShift(){
	return;
}
