/* JavaScript for Jalon Villa Rentals */

/* The Ajax essentials */
var xhr;

function xhr_object()
{
	var req = null;
	if ( window.XMLHttpRequest )	// Firefox, Netscape, Opera, Safari - and IE7+
		req = new XMLHttpRequest();
	else if ( window.ActiveXObject )	{	// IE6 and earlier
		req = new ActiveXObject( "Msxml2.XMLHTTP" );	// more modern
		if ( !req )
			req = new ActiveXObject( "Microsoft.XMLHTTP" );	// older
	}
	return req;	// if still null, will return 'null'
}

function init_ajax()
{
	xhr = xhr_object();
}

/* Availability calendar functions */

function display_month()
{
	var hold = document.getElementById( "calendar" );
	if (( xhr.readyState == 4 ) && ( xhr.status == 200 || xhr.status == 304 ))
		hold.innerHTML = xhr.responseText;
}

function current_month( villa )
{
	var func = 'jvr-calendar.php?id=' + villa;
	if ( xhr )	{
		xhr.open( 'GET', func, true );
		xhr.onreadystatechange = display_month;
    	xhr.send( null );
	}
	else
		alert( "Ajax not working!" );
}

function new_month( villa, month, year )
{
	var func = 'jvr-calendar.php?id=' + villa + '&month=' + month + '&year=' + year;
	if ( xhr )	{
		xhr.open( 'GET', func, true );
		xhr.onreadystatechange = display_month;
    	xhr.send( null );
	}
	else
		alert( "Ajax not working!" );
}

/* Property suggestion function */

function display_suggestions()
{
	var slist = document.getElementById( "suggestions" );
	if (( xhr.readyState == 4 ) && ( xhr.status == 200 || xhr.status == 304 ))	{
		slist.innerHTML = xhr.responseText;
		slist.style.display = "block";
		setTimeout( 'search()', 1000 );
	}
}

function new_suggestions( instring )
{
	var func = 'replace-suggestions.php?s=' + encodeURIComponent( instring );
	if ( xhr )	{
		xhr.open( 'GET', func, true );
		xhr.onreadystatechange = display_suggestions;
    	xhr.send( null );
	}
	else
		alert( "Ajax not working!" );
}

function search()
{
	var str = document.getElementById( "property" ).value;
	if ( str != "" )
		new_suggestions( str );
	else
		setTimeout( 'search()', 500 );
}

function init_search()
{
	init_ajax();
	document.seeker.property.focus();
	search();
}

function deliver( obj )
{
	var field_val = obj.firstChild.firstChild.nodeValue;
	document.getElementById( "property" ).value = field_val;
	document.getElementById( "seeker" ).submit();
}

/* popup */

function popup( doc )	// for later
{
	swin = window.open( doc, "searchWin", "width=600,height=400,top=120,left=120,scrollbars=1" );
	swin.focus();
}

/* highlight and restore for candidate strings */

function highlight( obj )
{
	obj.style.backgroundColor = "black";
	obj.style.color = "white";
	obj.style.cursor = "pointer";
}

function restore( obj )
{
	obj.style.backgroundColor = "white";
	obj.style.color = "black";
}

