//////////////// DIRECTORY FUNCTIONS ////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
function changeDirectoryState( newSessionState, sPageName ){
	window.location = "/"+sPageName+"/"+newSessionState; 
}
function changeDirectoryCounty( objState, objCounty, sPageName ){
	var newSessionState = document.getElementById(objState).value;
	var objCounty_selected_index = objCounty.selectedIndex;
	var objCountyText = objCounty.options[objCounty_selected_index].text;
	var newSessionCounty = objCountyText.replace(/\ /g,"-");
	window.location = "/"+sPageName+"/"+newSessionState+"/"+newSessionCounty; 
}
function changeDirectoryCity( objState, objCityValue, sPageName, sURLfixed ){
	var newSessionState = document.getElementById(objState).value;
	var sQuery = "SELECT Description, County FROM StateCity WHERE StateCityID="+ objCityValue;
	var sFixed = "";
	if( sURLfixed ) { 
		sFixed = "../../";
	}
	jQuery.post(sFixed+"../admin/dirFunctions/dbQuery.php",{option:1,query:sQuery}, 
		function( data ) { 
			if ( data ){
				var ArrayInfo = eval('(' + data + ')');
				var newSessionCity = ArrayInfo[0].Description.replace(/\ /g,"-");
				var newSessionCounty = ArrayInfo[0].County.replace(/\ /g,"-");
				window.location = "/"+sPageName+"/"+newSessionState+"/"+newSessionCounty+"/"+newSessionCity;
				return true;
			}
		}
	);
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function fillObjCounty( objList, sobjStateValue ){
	document.getElementById(objList).options.length = 0;
	var sQuery = "SELECT CountyID, Description FROM County WHERE StateID=\""+ sobjStateValue +"\" ORDER BY 2";
	jQuery.post("../admin/dirFunctions/dbQuery.php",{option:1,query:sQuery}, 
		function( data ) { 
			var OptionList = document.createElement("option");
			document.getElementById(objList).options.add(OptionList); 
			OptionList.text = "Select County";
    		OptionList.value = "";
			if ( data ){
				var ArrayInfo = eval('(' + data + ')');
				var nInfoLenght = ArrayInfo.length;
				for ( var i = 0; i < nInfoLenght; i++ ){
					if ( ArrayInfo[i].CountyID && ArrayInfo[i].Description ){
						OptionList = document.createElement("option");
						document.getElementById(objList).options.add(OptionList); 
						OptionList.value = ArrayInfo[i].CountyID;
						OptionList.text = ArrayInfo[i].Description;
					}
				}
			}
		}
	);
}
//////////////////////////////////////////////////////////////////////////////////////////////
function fillObjCountySelected( objList, sobjStateValue, selectedValue ){
	document.getElementById(objList).options.length = 0;
	var sQuery = "SELECT CountyID, Description FROM County WHERE StateID=\""+ sobjStateValue +"\" ORDER BY 2";
	jQuery.post("../admin/dirFunctions/dbQuery.php",{option:1,query:sQuery}, 
		function( data ) { 
			var OptionList = document.createElement("option");
			document.getElementById(objList).options.add(OptionList); 
			OptionList.text = "Select County";
    		OptionList.value = "";
			if ( data ){
				var ArrayInfo = eval('(' + data + ')');
				var nInfoLenght = ArrayInfo.length;
				for ( var i = 0; i < nInfoLenght; i++ ){
					if ( ArrayInfo[i].CountyID && ArrayInfo[i].Description ){
						OptionList = document.createElement("option");
						document.getElementById(objList).options.add(OptionList); 
						OptionList.value = ArrayInfo[i].CountyID;
						OptionList.text = ArrayInfo[i].Description;
						if( ArrayInfo[i].CountyID == selectedValue ){
							OptionList.selected = true;
						}
					}
				}
			}
		}
	);
}
//////////////////////////////////////////////////////////////////////////////////////////////
function fillObjCity( objList, sobjStateValue ){
	document.getElementById(objList).options.length = 0;
	var sQuery = "SELECT A.StateCityID as s1,A.Description as s2,B.Description as s3,count(C.Description) as n1 FROM StateCity A, County B, StateCity C WHERE A.CountyID=B.CountyID and A.Description=C.Description and A.StateID=\""+ sobjStateValue +"\" GROUP BY 1,2,3 ORDER BY 2";
	jQuery.post("../admin/dirFunctions/dbQuery.php",{option:1,query:sQuery}, 
		function( data ) { 
			var OptionList = document.createElement("option");
			document.getElementById(objList).options.add(OptionList); 
			OptionList.text = "Select City";
    		OptionList.value = "";
			if ( data ){
				var ArrayInfo = eval('(' + data + ')');
				var nInfoLenght = ArrayInfo.length;
				for ( var i = 0; i < nInfoLenght; i++ ){
					if ( ArrayInfo[i].s1 && ArrayInfo[i].s2 ){
						OptionList = document.createElement("option");
						document.getElementById(objList).options.add(OptionList); 
						OptionList.value = ArrayInfo[i].s1;
						if(  ArrayInfo[i].n1 == 1 ){
							OptionList.text = ArrayInfo[i].s2;
						}
						else{
							OptionList.text = ArrayInfo[i].s2+" - "+ArrayInfo[i].s3 ;
						}
					}
				}
			}
		}
	);
}
//////////////////////////////////////////////////////////////////////////////////////////////
function fillObjCitySelected( objList, sobjStateValue, selectedValue ){
	document.getElementById(objList).options.length = 0;
	var sQuery = "SELECT A.StateCityID as s1,A.Description as s2,B.Description as s3,count(C.Description) as n1 FROM StateCity A, County B, StateCity C WHERE A.CountyID=B.CountyID and A.Description=C.Description and A.StateID=\""+ sobjStateValue +"\" GROUP BY 1,2,3 ORDER BY 2";
	jQuery.post("../admin/dirFunctions/dbQuery.php",{option:1,query:sQuery}, 
		function( data ) { 
			var OptionList = document.createElement("option");
			document.getElementById(objList).options.add(OptionList); 
			OptionList.text = "Select City";
    		OptionList.value = "";
			if ( data ){
				var ArrayInfo = eval('(' + data + ')');
				var nInfoLenght = ArrayInfo.length;
				for ( var i = 0; i < nInfoLenght; i++ ){
					if ( ArrayInfo[i].s1 && ArrayInfo[i].s2 ){
						OptionList = document.createElement("option");
						document.getElementById(objList).options.add(OptionList); 
						OptionList.value = ArrayInfo[i].s1;
						if(  ArrayInfo[i].n1 == 1 ){
							OptionList.text = ArrayInfo[i].s2;
						}
						else{
							OptionList.text = ArrayInfo[i].s2+" - "+ArrayInfo[i].s3 ;
						}
						if( ArrayInfo[i].s1 == selectedValue ){
							OptionList.selected = true;
						}
					}
				}
			}
		}
	);
}
//////////////// MISCELANEOUS FUNCTIONS //////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
function changeSearchBox( objName ){
	if ( objName == "zip" ){
		document.getElementById("div_search_zip").style.display = "block";
		document.getElementById("div_search_state").style.display = "none";
		document.getElementById("div_search_county").style.display = "none";
		document.getElementById("div_search_city").style.display = "none";
		document.getElementById("btn_search_zip").style.display = "none";
		document.getElementById("btn_search_city").style.display = "inline";
		document.getElementById("btn_search_county").style.display = "inline";
	}
	else if ( objName == "county" ){
		document.getElementById("div_search_zip").style.display = "none";
		document.getElementById("div_search_state").style.display = "block";
		document.getElementById("div_search_county").style.display = "block";
		document.getElementById("div_search_city").style.display = "none";
		document.getElementById("btn_search_zip").style.display = "inline";
		document.getElementById("btn_search_city").style.display = "inline";
		document.getElementById("btn_search_county").style.display = "none";
	}
	else if ( objName == "city" ){
		document.getElementById("div_search_zip").style.display = "none";
		document.getElementById("div_search_state").style.display = "block";
		document.getElementById("div_search_county").style.display = "none";
		document.getElementById("div_search_city").style.display = "block";
		document.getElementById("btn_search_zip").style.display = "inline";
		document.getElementById("btn_search_city").style.display = "none";
		document.getElementById("btn_search_county").style.display = "inline";
	}
	document.getElementById("search_option").value = objName;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function inputTextCaption( nOption, objInputText, sText ){
	if ( nOption == 1 ){
		if( document.getElementById(objInputText).value == sText ){
			document.getElementById(objInputText).value = "";
			document.getElementById(objInputText).style.color = '#000000';
		}
	}
	else if ( nOption == 2 ){
		var objValue = document.getElementById(objInputText).value;
		if( objValue.replace (/\s+/g, '') == "" ){
			document.getElementById(objInputText).value = sText;
			document.getElementById(objInputText).style.color = '#666666';
		}
	}
}
