// ***************************************************
// Common javascript functions using Ajax for WakingTheGiant
// Author: Rohland Lablache de Charmoy
// Creation Date: 14-02-2005
// Last Modified: 

// Copyright: AMPT 2006
// ***************************************************


function AjaxLocationCallBack(response)
{
	// Strip the response into TEXT,ID
	var data = response.split('|');
	
	if (data.length <= 1)
		return;	// No Proper Data returned
	
	var responsearray = data[1].split(',');
	
	
	selobj = document.getElementById(data[0]);
	
	// First clear the List
	for (i = 0; i < selobj.length;i++)
	{
		selobj.options[i] = null;
		i--;
	}
	
	if (responsearray.length > 1)
	{
		for (i = 0; i < responsearray.length-1;i++)
		{
			var values = responsearray[i].split(':');
			// new option ('text','value')
			selobj.options[i] = new Option(values[0],values[1]);
		}
	}
	else
	{
		// Add 1 Item
		selobj.options[0] = new Option('No items',-1);
	}
	
	// check the type and reset appropriate select boxes
	if (data[0] == 'country')
	{
		// Disable all the other Selection Boxes
		psel = document.getElementById('province');
		csel = document.getElementById('city');
		//ssel = document.getElementById('suburb');
		psel.disabled = true;psel.options[0] = new Option('Loading');
		csel.disabled = true;csel.options[0] = new Option('Loading');
		//ssel.disabled = true;ssel.options[0] = new Option('Loading');
		
		// Initiate a selection change to reset province
		csel = document.getElementById('country');
		locationSelectionChanged(csel);
	}
	else if (data[0] == 'province')
	{
		psel = document.getElementById('province');
		locationSelectionChanged(psel);
	}
	else if (data[0] == 'city')
	{
		csel = document.getElementById('city');
		locationSelectionChanged(csel);
	}
	else if (data[0] == 'suburb')
	{
		// Disable all the other Selection Boxes
		psel = document.getElementById('province');
		csel = document.getElementById('city');
		//ssel = document.getElementById('suburb');
		psel.disabled = false;
		csel.disabled = false;
		//ssel.disabled = false;
	}

}

function locationSelectionChanged(obj)
{
	if (obj.selectedIndex >=0)
			valueId = obj.options[obj.selectedIndex].value;
		else
			valueId = -1;
	
	if (obj.id == 'country')
	{
		cAjax(urlLocation + '?type=province&countryid=' + valueId,AjaxLocationCallBack);
	}
	else if (obj.id == 'province')
	{
		// Disable all the other Selection Boxes
		psel = document.getElementById('province');
		csel = document.getElementById('city');
		//ssel = document.getElementById('suburb');
		psel.disabled = false;
		csel.disabled = true;csel.options[0] = new Option('Loading');
		//ssel.disabled = true;ssel.options[0] = new Option('Loading');
		cAjax(urlLocation + '?type=city&provinceid=' + valueId,AjaxLocationCallBack);
	}
	else if (obj.id == 'city')
	{
		// Disable all the other Selection Boxes
		psel = document.getElementById('province');
		csel = document.getElementById('city');
		//ssel = document.getElementById('suburb');
		psel.disabled = false;
		csel.disabled = false;
		//ssel.disabled = true;ssel.options[0] = new Option('Loading');
		//cAjax(urlLocation + '?type=suburb&cityid=' + valueId,AjaxLocationCallBack);
	}
	else if (obj.id == 'suburb')
	{
	}
}
