
var root = '/';

/**
 * Create Ajax request object
 */
function ajaxinit(){
	
	var xmlHttp;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e)	{
		// Internet Explorer
		try	{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)	{
			try	{
	  		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  		} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
	  		}
		}
	}
	
	return xmlHttp;
}

/**
 * Function used for home page search
 * Params serialized using JQuery and posted to ajax.php
 * Response from ajax.php is loaded into #contents div
 * Section 0 for public, 1 for private
 */
function ajaxvillasearch(form,section){		
	
	var xmlHttp = ajaxinit();
	if(!xmlHttp) alert("Your browser does not support Ajax");
	
	//Serialise form element into qs format, requires JQuery
	var params = $('form').serialize();
	
	//URL containing get information
	var url = root+'includes/ajax.php?';		
	url += 'action=search&';
	url += 'section='+section+'&';
	url += params;	
	
	xmlHttp.onreadystatechange = function(){		
		//Processing
		if (xmlHttp.readyState==2){
			//Loading image while info is in transit
			$('#contents').html('<img src="'+root+'images/layout/ajax-loader.gif" alt="" width="32" height="32"/>');
		}
		//Complete
		if (xmlHttp.readyState==4){			
			var response = xmlHttp.responseText;
			$('#contents').html($.evalJSON(response).content);
			$('#propertyresultscount').html($.evalJSON(response).count);
		}
	}
	xmlHttp.open("get",url,true);
	xmlHttp.send(null);	
	
	
}

/**
 * Function used on villasfocus.php
 * id of image to load is passed, this is then passed to ajax.php
 */
function loadImage(image,id){
	
	var xmlHttp = ajaxinit();
	if(!xmlHttp) alert("Your browser does not support Ajax");
	
	var url = root+'includes/ajax.php?';		
	url += 'action=image&';
	url += 'id='+id+"&";
	url += 'image='+image;
	
	xmlHttp.onreadystatechange = function(){		
		//Processing
		if (xmlHttp.readyState==2){
			$('#mainimage').html('<img src="'+root+'images/layout/ajax-loader.gif" alt="" width="32" height="32"/>');
		}
		//Complete
		if (xmlHttp.readyState==4){		
			$('#mainimage').html(xmlHttp.responseText);
		}
	}
	xmlHttp.open("get",url,true);
	xmlHttp.send(null);
	
}
