// AJAX routines

var fromDisabled	= 0;
var toDisabled	= 0;

var countryMsg	= "Select Country/State..."; 	
var countryMsgVal	= "0";
 	
var anyMsg		= "Any";
var anyMsgVal	= "0";

var anyTownMsg		= "Any Town";
var anyTownMsgVal		= "0";

var anySuburbMsg		= "Any Suburb";
var anySuburbMsgVal	= "0";

var otherMsg		= "other..."; 
var otherMsgVal		= "999999999"; 

var countryEntry		= countryMsg + "|" + countryMsgVal;
var anyEntry		= anyMsg + "|" + anyMsgVal;
var anyTownEntry		= anyTownMsg + "|" + anyTownMsgVal;
var anySuburbEntry	= anySuburbMsg + "|" + anySuburbMsgVal;
var otherEntry		= otherMsg + "|" + otherMsgVal;


//FUNC --------------------------------------------------------------------------------ajaxInitialLoad
function ajaxInitialLoad()
{
	inf = document.inpform;

	//FROM --------------------------------------------------------------------------
	//set the country dropdown

	if (fromCountryId == "")
		fromCountryId = 0;

	setSelectedDropdown(inf,"i_from_country",fromCountryId);

	//reload and set the town/city dropdown
	setTownFields("from");

	if (!fromDisabled)
		{
		var targetDropdown	= document.forms["inpform"].elements["i_from_town"];
		fillDropdown(fromTownArray,targetDropdown,anyTownEntry,otherEntry);
		}

	//reload and set the suburb dropdowns
	setSuburbFields("from");

	if (!fromDisabled)
		{
		var targetDropdown	= document.forms["inpform"].elements["i_from_suburb"];
		fillDropdown(fromSuburbArray,targetDropdown,anySuburbEntry,"");
		}

	if (fromTownId== "")
		fromTownId = 0;

	setSelectedDropdown(inf,"i_from_town",fromTownId);

	if (fromTownId == otherMsgVal)
		try {showNewTown("from");} catch(e) {}

	setSelectedDropdown(inf,"i_from_suburb",fromSuburbId);

	//TO ----------------------------------------------------------------------------
	//set the country dropdown

	if (toCountryId == "")
		toCountryId = 0;

	setSelectedDropdown(inf,"i_to_country",toCountryId);

	//reload and set the town/city dropdown
	setTownFields("to");

	if (!toDisabled)
		{
		//alert(1);alert(anyTownEntry);alert(otherEntry);
		var targetDropdown	= document.forms["inpform"].elements["i_to_town"];
		fillDropdown(toTownArray,targetDropdown,anyTownEntry,otherEntry);	
		}

	//reload and set the suburb dropdowns
	setSuburbFields("to");

	if (!toDisabled)
		{
		var targetDropdown	= document.forms["inpform"].elements["i_to_suburb"];
		fillDropdown(toSuburbArray,targetDropdown,anySuburbEntry,"");
		}

	if (toTownId == "")
		toTownId = 0;
	
	setSelectedDropdown(inf,"i_to_town",toTownId);

	if (toTownId == otherMsgVal)
		try {showNewTown("to");} catch(e) {}

	setSelectedDropdown(inf,"i_to_suburb",toSuburbId);
}


//FUNC --------------------------------------------------------------------------------ajaxTownLoader
function ajaxTownLoader(frmName, sourceField, targetField)
{
	var idx			= sourceField.selectedIndex;
  	var selectedText		= sourceField[idx].text;
  	var selectedVal		= sourceField[idx].value;
  
  	var targetDropdown	= document.forms[frmName].elements[targetField];
  
  	var country 		= selectedVal;
  	var countryName 		= selectedText;

  	if (countryName.indexOf("Select") == -1) 	// a country has been selected
		{
 		Http.get({	url: townNamesUrl + "country_" +  country + ".txt",	callback: fillTownDropdown,cache: Http.Cache.Get}, [targetDropdown]);
 		targetDropdown.selectedIndex = 0;	// initialise subsequent dropdowns
		}
	else
 		targetDropdown.selectedIndex = -1;	// initialise subsequent dropdowns
}


//FUNC --------------------------------------------------------------------------------ajaxSuburbLoader
function ajaxSuburbLoader(frmName, sourceField, targetField, countryField)
{
	var idx			= sourceField.selectedIndex;
  	var selectedText		= sourceField[idx].text;
  	var selectedVal		= sourceField[idx].value;

  	var targetDropdown	= document.forms[frmName].elements[targetField];
  
  	var temp_town_val 	= selectedVal;
  	var townName 		= selectedText;
  	var temp_country_val 	= document.forms[frmName].elements[countryField].value;

  	if (townName != anySuburbMsg)
		{
		Http.get({	url: townNamesUrl + "country_" +  temp_country_val + "_town_" + temp_town_val + ".txt",	callback: fillSuburbDropdown,cache: Http.Cache.Get}, [targetDropdown]);
	 	targetDropdown.selectedIndex = 0;	//initialise subsequent dropdowns
		}		
	else
	 	targetDropdown.selectedIndex = -1;	//initialise subsequent dropdowns
}



//FUNC --------------------------------------------------------------------------------fillTownDropdown
function fillTownDropdown(xmlreply, elementID)
{
	if (xmlreply.status == Http.Status.OK)
		{
		var tempResponse	= xmlreply.responseText;
		fillDropdown(tempResponse,elementID,anyTownEntry,otherEntry);
		}
	else
  		{
  		//alert("Cannot handle the AJAX call.");
  		elementID.length 		= 2;
  		elementID[0].text 	= anyTownMsg;
		elementID[0].value	= anyTownMsgVal;  		
  		elementID[1].text 	= otherMsg;
		elementID[1].value	= otherMsgVal;  
		elementID.selectedIndex	= -1;	//neither selected which avoids conflict with the special combobox processing	
  		}
}


//FUNC --------------------------------------------------------------------------------fillSuburbDropdown
function fillSuburbDropdown(xmlreply, elementID)
{
	if (xmlreply.status == Http.Status.OK)
		{
		var tempResponse	= xmlreply.responseText;
		fillDropdown(tempResponse,elementID,anySuburbEntry,"");
		}
	else
  		{
	   	//alert("Cannot handle the AJAX call.");
  		elementID.length 		= 1;
  		elementID[0].text 	= anySuburbMsg;
		elementID[0].value	= anySuburbMsgVal;  
		elementID.selectedIndex	= 0;			
  		}
}


//FUNC --------------------------------------------------------------------------------fillDropdown
function fillDropdown(varArray, elementID,startEntry, endEntry)
{
	var tempSplit 	= startEntry.split("|");
	startText 		= tempSplit[0];
	startValue		= tempSplit[1];

	var tempSplit 	= endEntry.split("|");
	endText		= tempSplit[0];
	endValue		= tempSplit[1];

	if (startText > "")
		extraOptionsAddedAtStart 	= 1;
	else
		extraOptionsAddedAtStart 	= 0;

	if (endText > "")
		extraOptionsAddedAtEnd 		= 1;
	else
		extraOptionsAddedAtEnd 		= 0;
//alert("start="+extraOptionsAddedAtStart); 
//alert("end="+extraOptionsAddedAtEnd); 
	var tempResponse	= varArray;
		
	var tempSplit 	= tempResponse.split("|");
	elementID.length	= 1;
	elementID.length	= tempSplit.length + extraOptionsAddedAtStart + extraOptionsAddedAtEnd;
		
	if (extraOptionsAddedAtStart) //add an extra element at the start 
		{	
		elementID[0].text 	= startText;
		elementID[0].value	= startValue;	
		}
		
	nextEntryToAddPos	 = extraOptionsAddedAtStart; 
	
	for (i=0; i < tempSplit.length; i++)
		{
		var tempSplit2 = tempSplit[i].split("#");
		if (trimAll(tempSplit2[0]) != "")
			{
			elementID[i + nextEntryToAddPos].text	= tempSplit2[0];
			elementID[i + nextEntryToAddPos].value	= tempSplit2[1];
			}
		else
			elementID.length = elementID.length - 1;			
		}
		
	if (extraOptionsAddedAtEnd) //add an extra element at the end
		{	
		elementID[elementID.length -1].text 	= endText;
		elementID[elementID.length -1].value	= endValue;			
		}
}




//FUNC --------------------------------------------------------------------------------checkTownEntry
function checkTownEntry(fType)
{
	if (fType == "from")
		{
		temp_country	= getSelectedDropdownText("inpform","i_from_country");
		townObjID 		= document.forms["inpform"].elements["i_from_town"];
		suburbObjID 	= document.forms["inpform"].elements["i_from_suburb"];
		newTownObjID 	= document.forms["inpform"].elements["i_new_from_town"];
		}
	else
		{
		temp_country 	= getSelectedDropdownText("inpform","i_to_country");
		townObjID 		= document.forms["inpform"].elements["i_to_town"];
		suburbObjID 	= document.forms["inpform"].elements["i_to_suburb"];
		newTownObjID 	= document.forms["inpform"].elements["i_new_to_town"];
		}

	fVal = townObjID.value;

	if (fVal == otherMsgVal)
		{
		try {hideSuburb(fType);} catch(e) {}
		showNewTown(fType);;
		newTownObjID.select();
		newTownObjID.focus();
		}
	else
		{
		try {hideNewTown(fType);} catch(e) {}
		if (temp_country.indexOf("Australia") == -1) // non-OZ
			try {hideSuburb(fType);} catch(e) {}
		else
			try {showSuburb(fType);} catch(e) {}

		}
}


//FUNC --------------------------------------------------------------------------------setFields
function setFields(fType)
{
	if (fType == "from")
		{
		temp_country	= getSelectedDropdownText("inpform","i_from_country");
		temp_town 		= getSelectedDropdownText("inpform","i_from_town");
		temp_suburb 	= getSelectedDropdownText("inpform","i_from_suburb");
		}
	else
		{
		temp_country 	= getSelectedDropdownText("inpform","i_to_country");
		temp_town 		= getSelectedDropdownText("inpform","i_to_town");
		temp_suburb 	= getSelectedDropdownText("inpform","i_to_suburb");	
		}	

	if (temp_country.indexOf("Select") == -1) // a country has been selected
		{
		enableTown(fType);
		setTownFields(fType);
		setSuburbFields(fType);
		try {hideNewTown(fType);} catch(e) {}
		}
	else
		{
		disableTown(fType);
		if (temp_country.indexOf("Australia") == -1) // non-OZ
			try {hideSuburb(fType);} catch(e) {}
		else
			try {disableSuburb(fType);} catch(e) {}
		try {hideNewTown(fType);} catch(e) {}
		}
}


//FUNC --------------------------------------------------------------------------------setTownFields
function setTownFields(fType)
{
	if (fType == "from")
		{
		temp_country = getSelectedDropdownText("inpform","i_from_country");
		}
	else
		{
		temp_country = getSelectedDropdownText("inpform","i_to_country");
		}

	if (temp_country.indexOf("Select") == -1) // a country has been selected
		{
		enableTown(fType);
		checkTownEntry(fType);
		}
	else
		{
		disableTown(fType);
		}	
}	
 
//FUNC --------------------------------------------------------------------------------setSuburbFields
function setSuburbFields(fType)
{
	if (fType == "from")
		{
		temp_country = getSelectedDropdownText("inpform","i_from_country");
		}
	else
		{
		temp_country = getSelectedDropdownText("inpform","i_to_country");
		}

  	if (temp_country.indexOf("Australia") == -1) // not oz country/state
		{
		try {hideSuburb(fType);} catch(e){}
		}
	else
		{
		enableSuburb(fType);
		}
}		

 
//FUNC --------------------------------------------------------------------------------hideNewTown
function hideNewTown(fType)
{
	if (fType == "from")
		{
		visibleOff("newfromtown");
		}
	else
		{
		visibleOff("newtotown");
		}
}

//FUNC --------------------------------------------------------------------------------showNewTown
function showNewTown(fType)
{
	if (fType == "from")
		{
		visibleOn("newfromtown");
		}
	else
		{
		visibleOn("newtotown");
		}
}

//FUNC --------------------------------------------------------------------------------disableTown
function disableTown(fType)
{
	if (fType == "from")
		{
		objID	= "i_from_town";
		fromDisabled = 1;
		}
	else
		{
		objID	= "i_to_town";
		toDisabled = 1;
		}

	elementID = document.forms["inpform"].elements[objID];
	
	elementID.length 		= 1;
	elementID[0].text 	= "";
	elementID[0].value	= "";//anyMsgVal;

	dom=findDOM(objID);
	dom.disabled = "disabled";

	//used by special combobox only
	try {visibleOff("txt"+objID);} 	catch(e) {}
	try {visibleOff("frame"+objID);} 	catch(e) {}
	
}

//FUNC --------------------------------------------------------------------------------enableTown
function enableTown(fType)
{
	if (fType == "from")
		{
		objID		= "i_from_town";
		fromDisabled = 0;
		}
	else
		{
		objID	= "i_to_town";
		toDisabled = 0;
		}

	elementID = document.forms["inpform"].elements[objID];
	
	elementID.length 		= 1;
	elementID[0].text 	= anyTownMsg;
	elementID[0].value	= anyTownMsgVal;

	dom=findDOM(objID);
	dom.disabled = "";

	//used by special combobox only
	try {visibleOn("txt"+objID);} 	catch(e) {}
	try {visibleOn("frame"+objID);} 	catch(e) {}

}

//FUNC --------------------------------------------------------------------------------disableSuburb
function disableSuburb(fType)
{
	if (fType == "from")
		{
		objID	= "i_from_suburb";
		fromDisabled = 1;
		}
	else
		{
		objID	= "i_to_suburb";
		toDisabled = 1;
		}

	elementID = document.forms["inpform"].elements[objID];

	elementID.length 		= 1;
	elementID[0].text 	= "";
	elementID[0].value	= anySuburbMsgVal;
	
	dom=findDOM(objID);
	dom.disabled = "disabled";
}

//FUNC --------------------------------------------------------------------------------enableSuburb
function enableSuburb(fType)
{
	if (fType == "from")
		{
		objID	= "i_from_suburb";
		fromDisabled = 0;
		}
	else
		{
		objID	= "i_to_suburb";
		toDisabled = 0;
		}

	elementID = document.forms["inpform"].elements[objID];

	elementID.length 		= 1;
	elementID[0].text 	= anySuburbMsg;
	elementID[0].value	= anySuburbMsgVal;

	dom=findDOM(objID);
	dom.disabled = "";
}

//FUNC --------------------------------------------------------------------------------hideSuburb
function hideSuburb(fType)
{
	if (fType == "from")
		{
		visibleOff("fromsuburb1");
		visibleOff("fromsuburb2");;
		}
	else
		{
		visibleOff("tosuburb1");
		visibleOff("tosuburb2");
		}
}

//FUNC --------------------------------------------------------------------------------showSuburb
function showSuburb(fType)
{
	if (fType == "from")
		{
		visibleOn("fromsuburb1");
		visibleOn("fromsuburb2");;
		}
	else
		{
		visibleOn("tosuburb1");
		visibleOn("tosuburb2");
		}
}


//FUNC --------------------------------------------------------------------------------showDates
function showDates(objID)
{
	inf=document.inpform;

	if (objID.checked)
		visibleOn("daterange");
	else 
		{
		visibleOff("daterange");
		dom=findDOM("show_fd");
		dom.innerHTML = "select FROM date";
		dom=findDOM("show_td");
		dom.innerHTML = "select TO date";
		inf.i_availability_start_ts.value	= "0000-00-00 00:00:00";
		inf.i_availability_end_ts.value	= "0000-00-00 00:00:00";
		}
}



//#############################################################################################################

// old and unused



// AJAX routines

//FUNC --------------------------------------------------------------------------------ajaxCityLoader
function ajaxTownLoader2XXX(frmName, sourceField, targetField)
{
  var idx				= sourceField.selectedIndex;
  var selectedText	= sourceField[idx].text;
  var selectedVal		= sourceField[idx].value;
  
  var targetDropdown	= document.forms[frmName].elements[targetField];
  
  var country 		= selectedVal;

  if (country != "Select country")
	{
 	Http.get({	url: "[TOWNNAMES_URL]country_" +  country + ".txt",	callback: fillTownDropdown2,cache: Http.Cache.Get}, [targetDropdown]);

	//initialise subsequent dropdowns
 	targetDropdown.selectedIndex = -1;
 	if (sourceField.name == "i_to_country")
 		document.forms[frmName].elements["i_to_suburb"].selectedIndex = -1;
	else		 
 		document.forms[frmName].elements["i_from_suburb"].selectedIndex = -1;
	}
}


//FUNC --------------------------------------------------------------------------------ajaxSuburbLoader
function ajaxSuburbLoader2XXXXXXXXXx(frmName, sourceField, targetField, countryField)
{
		
	var idx				= sourceField.selectedIndex;
  	var selectedText		= sourceField[idx].text;
  	var selectedVal		= sourceField[idx].value;
 
  	var targetDropdown	= document.forms[frmName].elements[targetField];
  
  	var temp_town_val 		= selectedVal;
  	var townName 				= selectedText;
  	var temp_country_val 	= document.forms[frmName].elements[countryField].value;


  	var temp_country_val 	= document.forms[frmName].elements[countryField].value;

  	if (temp_town_val != "Select suburb")
		{
		Http.get({	url: "[TOWNNAMES_URL]country_" +  temp_country_val + "_town_" + temp_town_val + ".txt",	callback: fillSuburbDropdown2,cache: Http.Cache.Get}, [targetDropdown]);

		//initialise subsequent dropdowns
	 	targetDropdown.selectedIndex = -1;
		}		
}


//FUNC --------------------------------------------------------------------------------fillCityDropdown
function fillTownDropdown2XXXXXXXX(xmlreply, elementID)
{
	if (xmlreply.status == Http.Status.OK)
		{
		var tempResponse	= xmlreply.responseText;
		
		fillDropdown2(tempResponse,elementID,comboboxMsg,"Any Town or City");
		}
	else
  		{
  		//alert("Cannot handle the AJAX call.");
  		elementID.length = 1;
  		elementID[0].text 	= "Any Town";
		elementID[0].value	= "0";  		
  		}
}


//FUNC --------------------------------------------------------------------------------fillSuburbDropdown
function fillSuburbDropdown2XXXXXXXXXX(xmlreply, elementID)
{
	if (xmlreply.status == Http.Status.OK)
		{
		var tempResponse	= xmlreply.responseText;
		
		fillDropdown2(tempResponse,elementID,"Any Suburb","");
		}
	else
  		{
	   //alert("Cannot handle the AJAX call.");
  		elementID.length = 1;
  		elementID[0].text 	= "Any Suburb";
		elementID[0].value	= "0";  		
  		}
}


//FUNC --------------------------------------------------------------------------------fillDropdown
function fillDropdown2XXXXXXXXXXXX(varArray, elementID,startMsg, endMsg)
{
	if (startMsg > "")
		extraOptionsAddedAtStart 	= 1;
	else
		extraOptionsAddedAtStart 	= 0;

	if (endMsg > "")		
		extraOptionsAddedAtEnd 		= 1;
	else
		extraOptionsAddedAtEnd 		= 0;

	var tempResponse	= varArray;
		
	var tempSplit 	= tempResponse.split("|");
	elementID.length	= 1;
	elementID.length	= tempSplit.length + extraOptionsAddedAtStart + extraOptionsAddedAtEnd;
		
	if (startMsg > "") //add an extra element at the start 
		{	
		elementID[0].text 	= startMsg;
		elementID[0].value	= "";	
		}
		
	nextEntryToAddPos	 = extraOptionsAddedAtStart; 
	
	for (i=0; i < tempSplit.length; i++)
		{
		var tempSplit2 = tempSplit[i].split("#");
		if (trimAll(tempSplit2[0]) != "")
			{
			elementID[i + nextEntryToAddPos].text		= tempSplit2[0];
			elementID[i + nextEntryToAddPos].value	= tempSplit2[1];
			}
		else
			elementID.length = elementID.length - 1;			
		}
		
	if (endMsg > "")	 //add an extra element at the end
		{	
		elementID[elementID.length -1].text 	= endMsg;
		elementID[elementID.length -1].value	= "0";			
		}
}



//FUNC --------------------------------------------------------------------------------setNewSuburb
function setNewSuburb2XXXXXXX(newSuburb, newPostcode, newSuburbID)
	{
	inf 						= document.inpform;	
	inf.i_suburb.value 		= newSuburb.replace(/\\/g,"");
	inf.x_suburb.value 		= newSuburb.replace(/\\/g,"");
	inf.x_suburb_id.value 	= newSuburbID;
	processSearchForm();
	} 


//FUNC --------------------------------------------------------------------------------ajaxInitialLoad
function ajaxInitialLoad2XXXXXXXXXX()
{

	inf=document.inpform;
	
	//set the country dropdowns

	setSelectedDropdown(inf,"i_from_country","[FROM_COUNTRY_ID]");
	setSelectedDropdown(inf,"i_to_country","[TO_COUNTRY_ID]");

	//reload and set the town/city dropdowns

	var targetDropdown	= document.forms["inpform"].elements["i_from_town"];
	fillDropdown("[FROM_TOWN_ARRAY]",targetDropdown,comboboxMsg,"Any Town or City");

	setSuburbFields("from")

	var targetDropdown	= document.forms["inpform"].elements["i_to_town"];
	fillDropdown("[TO_TOWN_ARRAY]",targetDropdown,comboboxMsg,"Any Town or City");	
	
	setSuburbFields("to")
	
	fromTownId = "[FROM_TOWN_ID]";
	
	if (fromTownId == "")
		fromTownId = 0;

	setSelectedDropdown(inf,"i_from_town",fromTownId );

	toTownId = "[TO_TOWN_ID]";
	
	if (toTownId == "")
		toTownId = 0;
	
	setSelectedDropdown(inf,"i_to_town",toTownId );

	//reload and set the suburb dropdowns
	
	if ("[FROM_SUBURB_ID]" != "")
		{
		var targetDropdown	= document.forms["inpform"].elements["i_from_suburb"];
		fillDropdown("[FROM_SUBURB_ARRAY]",targetDropdown,"Any Suburb","");
		setSelectedDropdown(inf,"i_from_suburb","[FROM_SUBURB_ID]");
		}
	
	if ("[TO_SUBURB_ID]" != "")
		{
		var targetDropdown	= document.forms["inpform"].elements["i_to_suburb"];
		fillDropdown("[TO_SUBURB_ARRAY]",targetDropdown,"Any Suburb","");
		setSelectedDropdown(inf,"i_to_suburb","[TO_SUBURB_ID]");
		}
		
}


//FUNC --------------------------------------------------------------------------------setSuburbFields
function setSuburbFields2XX()
{
	temp_from_country = getSelectedDropdownText("inpform","i_from_country");

  	if (temp_from_country.indexOf("Australia") > -1) // oz country/state
		{
		dom=findDOM("i_from_suburb");
		dom.disabled = "";
		//dom=findDOM("txti_from_suburb");
		//dom.disabled = "";
		//dom.visibility = "visible"	;	
		//dom.display = "block";
		}
	else	
		{
		dom=findDOM("i_from_suburb");
		dom.disabled = "disabled";
		//dom=findDOM("txti_from_suburb");
		//dom.disabled = "disabled";
		//dom.readOnly = "readonly";
		//dom.visibility = "hidden";
		//dom.display = "none";
		}

	temp_to_country = getSelectedDropdownText("inpform","i_to_country");

  	if (temp_to_country.indexOf("Australia") > -1) // oz country/state
		{
		dom=findDOM("i_to_suburb");
		dom.disabled = "";
		//dom=findDOM("txti_to_suburb");
		//dom.disabled = "";
		//dom.display = "block";
		//dom.visibility = "visible"	;		
		}
	else	
		{
		dom=findDOM("i_to_suburb");
		dom.disabled = "disabled";
		//dom=findDOM("txti_to_suburb");
		//dom.disabled = "disabled";
		//dom.readOnly = "readonly";
		//dom.display = "none";
		//dom.visibility = "hidden";	
		}
}


//FUNC --------------------------------------------------------------------------------getStyle
function getStyleXXX(el, property) {
	if (!el) { return null; }
 
	if (el.currentStyle) {
		var tmp = property.split("-");
		property = tmp[0];
 
		for (var i = 1; i < tmp.length; i++) {
			property += tmp[i].slice(0, 1).toUpperCase() + tmp[i].slice(1);
		}
 
		return el.currentStyle[property];
	} else if (window.getComputedStyle) {
		return document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
	}
 
	return null;
}	


