/* AJAX Calls for new-post module */

// Ajax holders //
var xmlHttp;

// Ajax functions //

function getRegionStates(str, prefix, appPath){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}
	
	if(str == '')
		return;
		
	// generate request handler
	var url= appPath + "inc/ajax_get_states.php";
	url = url + "?q=" + str + "&prefix=" + prefix + "&declare=1" + "&appPath=" + appPath;
	//url = url + "&sid=" + Math.random();

	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){			
			
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			// update the parent page like so: document.getElementById("txtHint").innerHTML=xmlHttp.responseText			
			if(xmlHttp.responseText != "FAILED" && xmlHttp.responseText != ""){ // custom return message
				document.getElementById(prefix.toString() + '-states').innerHTML = xmlHttp.responseText;
				document.getElementById(prefix.toString() + '-cities').innerHTML = "";
			}else{				
				document.getElementById(prefix.toString() + '-states').innerHTML = "";
				document.getElementById(prefix.toString() + '-cities').innerHTML = "";
				alert('No States found for this country.\nYou can set the country as a default search region.\nLogged in users can suggest a missing region from their Edit Profile page.');
			}			
		}
	}// end function
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}// end ajax function

function getRegionCities(str, prefix, appPath){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}
	
	if(str == '')
		return;
		
	// generate request handler
	var url= appPath + "inc/ajax_get_cities.php";
	url = url + "?q=" + str + "&prefix=" + prefix + "&declare=1";
	//url = url + "&sid=" + Math.random();

	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){			
			
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			// update the parent page like so: document.getElementById("txtHint").innerHTML=xmlHttp.responseText
			
			if(xmlHttp.responseText != "FAILED" && xmlHttp.responseText != ""){ // custom return message
				document.getElementById(prefix.toString() + '-cities').innerHTML = xmlHttp.responseText;
			}else{
				document.getElementById(prefix.toString() + '-cities').innerHTML = "";
				alert('No Cities found for this state.\nYou can set the state as a default search region.\nLogged in users can suggest a missing region from their Edit Profile page.');
			}			
		}
	}// end function
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}// end ajax function

function populateFavoritesDropdown(appPath){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Sorry, Your Browser does not support HTTP Request")
		return;
	}
	
	// generate request handler
	var url= appPath + "inc/ajax_populate_favorites_dropdown.php";	

	// do it
	xmlHttp.onreadystatechange = function stateChanged(){
		// while processing
		if(!(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")){
			// Effect.Appear('ajaxWorking');
		}
		if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			// update the parent page like so: document.getElementById("txtHint").innerHTML=xmlHttp.responseText
			// Effect.Fade('ajaxWorking');
			if(xmlHttp.responseText == "FAILED"){ // custom return message				
				document.getElementById('favoritesDropdownList').innerHTML = "<li>You have no favorite posts...</li>";
			}else{			
				document.getElementById('favoritesDropdownList').innerHTML = xmlHttp.responseText;
			}
			// now update prices as well			
		}
	}// end function
	
	showFavoritesDropdown();
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}// end ajax function

// Main Ajax GetXML function
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch(e){
		//Internet Explorer
		 try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 }
		 catch(e){
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		 }
	}
	return xmlHttp;
}// end function