/******************************/
/***      AJAX SETTINGS     ***/
/******************************/
var AjaxRequest = new Object();
var PostAjaxSuccessFunctionCall;
var AjaxToReturnXml;

function AjaxHTTPRequestObjectCreate()
{
     if ( window.XMLHttpRequest ) return new XMLHttpRequest();   
     else return new ActiveXObject("Microsoft.XMLHTTP");
}
function AjaxDoConnection( Url , OnloadFunc , CallMethod , SendVars , ReturnXml )
{
    if ( CallMethod == '' ) CallMethod = 'GET';
    if ( SendVars == '' ) SendVars = null;
    AjaxRequest[ OnloadFunc ] = new AjaxHandler();
    AjaxRequest[ OnloadFunc ].run( Url , OnloadFunc , CallMethod , SendVars , ReturnXml );
}
function AjaxEventHandler()
{
    if ( AjaxRequest.readyState == 4 )
    {
        if ( AjaxToReturnXml ) ResponseValue = AjaxRequest.responseXML;
        else ResponseValue = AjaxRequest.responseText;
        eval ( PostAjaxSuccessFunctionCall + '(  ResponseValue );' );       
    }
}
var isLocked = false;
var currentConnection = ""; 
function test( Url , OnloadFunc , CallMethod , SendVars , ReturnXml )
{
    AjaxDoConnection( Url , OnloadFunc , CallMethod , SendVars , ReturnXml );
}
var i = 0;
function AjaxHandler()
{
    this.oAjaxRequest   = ''; 
    this.callBackMethod = '';
    this.returnXml      = '';
    
    this.run = function ( Url , OnloadFunc , CallMethod , SendVars , ReturnXml ) 
    {
        this.callBackMethod = OnloadFunc;
        this.returnXml      = ReturnXml;
        this.oAjaxRequest   = AjaxHTTPRequestObjectCreate();
        this.doConnection( Url , OnloadFunc , CallMethod , SendVars );
    }
    this.doConnection = function( Url , OnloadFunc , CallMethod , SendVars , ReturnXml  )
    {
        if( !isLocked )
        {
            isLocked = true; 
            currentConnection = OnloadFunc;
            this.oAjaxRequest.onreadystatechange =  this.ajaxEventHandler;
            this.oAjaxRequest.open( CallMethod , Url , true );  
            this.oAjaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            this.oAjaxRequest.send( SendVars );
        }  
        else
        {
			var callBack  = "AjaxRequest[OnloadFunc].doConnection( '" + Url +"' , '" + OnloadFunc +"' , '" + CallMethod +"' , '" + SendVars +"' , '" + ReturnXml +"' )";
            window.setTimeout( callBack , 100 );  
        }  
    }    
    this.ajaxEventHandler = function(  )
    {
        if ( AjaxRequest[ currentConnection ].oAjaxRequest.readyState == 4 )
        {
            if ( AjaxRequest[ currentConnection ].returnXml ) var ResponseValue = AjaxRequest[ currentConnection ].oAjaxRequest.responseXML;
            else var  ResponseValue = AjaxRequest[ currentConnection ].oAjaxRequest.responseText;
            eval ( AjaxRequest[ currentConnection ].callBackMethod + '(  ResponseValue );' );    
            isLocked = false;   
        }
    }
}
function EncodeSendVarsChars( EncodedChars )
{
     EncodedChars = EncodedChars.replace(/=/g,"%3D");
     EncodedChars = EncodedChars.replace(/&/g,"%26");
     EncodedChars = EncodedChars.replace(/@/g,"%40");
     return EncodedChars;
}
function AjaxResponseText( AjaxObj )
{
    return AjaxObj.responseText;
}
function AjaxResponseXML( AjaxObj )
{
    return AjaxObj.responseXML;
}
function GetSimpleNodeValue( XMLResponse , Tagname , TagNodeId , ChildNodeId )
{
    if ( TagNodeId == '' ) TagNodeId = 0;
    if ( ChildNodeId == '' ) ChildNodeId = 0;

    return XMLResponse.getElementsByTagName( Tagname )[TagNodeId] . childNodes[ ChildNodeId ] . nodeValue;
}
function parseScript(_source)
{
	var source = _source;
	var scripts = new Array();		
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1)
	{
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	// Loop through every script collected and eval it
	for(var i=0; i<scripts.length; i++)
	{
		try
		{
			eval(scripts[i]);
		}
		catch(ex)
		{
			// do what you want here when a script fails
		}
	}
	// Return the cleaned source
	return source;
}


function CheckErrorMessage( obj )
{
	if( obj.value == "This field is mandatory" || obj.value == "Email format is not valid" || obj.value == "Email address not registered" )	
	{
		obj.style.borderColor = "#BFBFBF";
		obj.style.color = "#000000";
		obj.value = "";
	}
}


/**** RESERVE UNIT ****/
function ReserveUnit( unit_id )
{
	if( confirm("Do you want to reserve the unit?") )
	{	
		var SendVars = 'mode=ReserveUnit&unit_id=' + unit_id;
		AjaxDoConnection( 'ajax-functions.php' , 'FillResults_ReserveUnit' , 'POST' , SendVars , false );
	}
}
function FillResults_ReserveUnit( result )
{
	var temp = result.split("$");
	var unit_id = temp[0];
	var agent_name = temp[1];
		
	document.getElementById('status_'+unit_id).className = "status-reserved";
	document.getElementById('status_'+unit_id).innerHTML = "Reserved<br/><span class='grey-text'>by "+agent_name+"</span>"
	document.getElementById('reserve_'+unit_id).innerHTML = "<a href='javascript: UnreserveUnit("+unit_id+")'>UNRESERVE</a>";
}


/**** UNRESERVE UNIT ****/
function UnreserveUnit( unit_id )
{
	if( confirm("Do you want to unreserve the unit?") )
	{	
		var SendVars = 'mode=UnreserveUnit&unit_id=' + unit_id;
		AjaxDoConnection( 'ajax-functions.php' , 'FillResults_UnreserveUnit' , 'POST' , SendVars , false );
	}
}
function FillResults_UnreserveUnit( unit_id )
{
	if( !isNaN(unit_id) )
	{
		document.getElementById('status_'+unit_id).className = "status-available";
		document.getElementById('status_'+unit_id).innerHTML = "Available"
		document.getElementById('reserve_'+unit_id).innerHTML = "<a href='javascript: ReserveUnit("+unit_id+")'>RESERVE</a>";
	}
}


/**** EDIT UNIT ****/
function EditUnit( unit_id )
{
	var SendVars = 'mode=EditUnit&unit_id=' + unit_id;
	AjaxDoConnection( 'ajax-functions.php' , 'FillResults_EditUnit' , 'POST' , SendVars , false );
}
function FillResults_EditUnit( result )
{
	var temp = result.split("$");
	var html_select = temp[0];
	var sale_price = temp[1];
	var unit_id = temp[2];
	
	document.getElementById('sale_price_'+unit_id).innerHTML = "<input type='text' name='sale_price_input_"+unit_id+"' id='sale_price_input_"+unit_id+"' value='"+sale_price+"' style='width:70px; border:1px solid #BFBFBF; padding:2px;'/>"
	document.getElementById('status_'+unit_id).innerHTML = html_select;
	document.getElementById('reserve_'+unit_id).innerHTML = "<a href='javascript: SaveUnit("+unit_id+")'>SAVE</a>";
}


/**** SAVE UNIT ****/
function SaveUnit( unit_id )
{
	var sale_price_input = document.getElementById('sale_price_input_'+unit_id).value * 1;
	var status_id = document.getElementById('status_select_'+unit_id).value * 1;
	
	var SendVars = 'mode=SaveUnit&unit_id='+unit_id+'&sale_price='+sale_price_input+'&status_id='+status_id;
	AjaxDoConnection( 'ajax-functions.php' , 'FillResults_SaveUnit' , 'POST' , SendVars , false );
}
function FillResults_SaveUnit( result )
{
	var temp = result.split("$");
	var unit_id = temp[0];
	var sale_price = temp[1];
	var status = temp[2];
	var status_class = temp[3];
	var status_id = temp[4];


	document.getElementById('sale_price_'+unit_id).innerHTML = "&euro;&nbsp;" + sale_price;
	document.getElementById('status_'+unit_id).innerHTML = status;
	document.getElementById('status_'+unit_id).className = status_class;
	document.getElementById('reserve_'+unit_id).innerHTML = "<a href='javascript: EditUnit("+unit_id+")'>EDIT</a>";
}


/**** REMOVE AGENT ****/
function RemoveAgent( agent_id )
{
	if( confirm("Do you want to remove the agent?") )
	{	
		var SendVars = 'mode=RemoveAgent&agent_id='+agent_id;
		AjaxDoConnection( 'ajax-functions.php' , 'FillResults_RemoveAgent' , 'POST' , SendVars , false );	
	}
}
function FillResults_RemoveAgent( result )
{
	if( !result.length ) window.location.reload();
	else alert(result);
}
