var obj_locat;
var obj_state;var req;

var _arrid   = new Array();
var _arrtext = new Array();
var _arrtype = new Array();


function do_checkemail(strmail)
{
	if (strmail == "")
		return;

	loadXMLDoc("./getemailstatus.php?mail="+strmail);
}

function do_newlist(stateValue, objLocation, objState)
{
	if (stateValue == "")
		return;
	obj_locat = objLocation;
	obj_state = objState;
	obj_state.disabled = true;
	obj_locat.disabled = true;
	
	loadXMLDoc("./getdestfromstate.php?state="+stateValue);
}

function set_dis(loc)
{
	loc.length=0;
	loc.options[0] = new Option('No Selected', '0');
	loc.disabled=true;
}

function loadXMLDoc( url )
{
	if( window.ActiveXObject )
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if( req )
		{
			req.open("GET", url, true );
			req.onreadystatechange = processReqChange;
			req.send();
		}
	} 
	else if( window.XMLHttpRequest ) 
	{
		try
		{
			req = new XMLHttpRequest();
			req.open( "GET", url, true);
			req.onreadystatechange = processReqChange;
			req.send( null );
		}
		catch(e)
		{
			alert(e);
		}
	}
}
function processReqChange()
{
	if( req.readyState==4 )
	{
		if( req.status==200 )
		{
			var result = req.responseXML;
			put_result(result);
		}
	}
}
function put_result(objXML)
{
	_res = "";

	try
	{
		var results = objXML.getElementsByTagName("OptType");
	}
	catch(e)
	{
		window.status = "done!";
		_bCanRun = true;
		return;
	}

	if (results.length == 1)
	{
		if (results.item(0).firstChild.nodeValue == "EmailStatus")
		{
			try
			{
				var subres = objXML.getElementsByTagName("LocationDetailInfo");
			}
			catch(e)
			{
				return;
			}

			var _email_status   = GetNodeValue(subres.item(0), "OptResult", 0);
			if (_email_status == 0)
			{
				document.getElementById("reg_area").style.display = 'block';
				document.getElementById("chkReg").checked = true;
			}
			else
			{
				document.getElementById("reg_area").style.display = 'none';
				document.getElementById("chkReg").checked = false;
			}
			return;
		}
	}

	try
	{
		var results = objXML.getElementsByTagName("LocationDetailInfo");
	}
	catch(e)
	{
		alert("exception");
		window.status = "done!";
		_bCanRun = true;
		return;
	}

	if (results.length > 0)
	{
		_arrid = new Array();
		_arrtype = new Array();
		_arrtext = new Array();
		for(i = 0; i < results.length; i++)
		{
			var _LocationCode   = GetNodeValue(results.item(i), "LocationCode", 0);
			var _LocationType   = GetNodeValue(results.item(i), "LocationType", 0);
			var _LocationString = GetNodeValue(results.item(i), "LocationString", 0);

			_res += div_span_item(_LocationCode, _LocationType, _LocationString);

			_arrid[i]   = _LocationCode;
			_arrtype[i] = _LocationType;
			_arrtext[i] = _LocationString;
		}

		var _inputBox = _obj_location;
		var _x=_inputBox.offsetLeft;
		var _y=_inputBox.offsetTop;
		while(_inputBox=_inputBox.offsetParent)
		{
			_x+=_inputBox.offsetLeft;
			_y+=_inputBox.offsetTop;
		}
		
		_smartbox=document.getElementById('mytextbox');
		_smartbox.style.left=_x.toString() + "px";
		_smartbox.style.top=(_y+20).toString() + "px";
		_smartbox.innerHTML = _res;
		_smartbox.style.display = 'block';
		
		var _divBG = document.getElementById("smartboxBG");
		_divBG.style.width = _smartbox.offsetWidth;
		_divBG.style.height = _smartbox.offsetHeight;
		_divBG.style.top = _smartbox.style.top;
		_divBG.style.left = _smartbox.style.left;
		_divBG.style.display = 'block';
		_cursel = 0;
		keysInit(keypressed);
		setChoise(0);
		_isSmartBoxOpened = true;
	}
	else
	{
		_cursel = -1;
		keysInit(null);
		closeSmartBox();
	}
	_bCanRun = true;
	window.status = "done!";
}

function setChoise(nValue)
{
	var _box = document.getElementById('mytextbox');;
	var _n = _box.childNodes.length;
	if (_n > 0)
	{
		if (_prevsel >= 0)
		{
			var prev = _box.childNodes[_prevsel];
			if (prev != null)
				prev.className = "item";
		}
		if (nValue >= 0)
		{
			var _new = _box.childNodes[nValue];
			_new.className = "item_new";
			_prevsel = nValue;
		}
	}
}

function GetNodeValue(parent, item, index)
{
	var result = parent.getElementsByTagName(item).item(index);
	if (result)
	{
		//3 is text node
		if (result.firstChild.nodeType != 3)
			return "";
		else
			return result.firstChild.nodeValue;
	}
	else { 
		return ""; 
	}
}
function div_span_item(_code, _type, _str){
	_divb = "<div class='item' onmousedown='mouse_sel(this)' onmouseover='mouse_move(this)' onmouseout='mouse_out(this)'>";
	_dive = "<\/div>";

	_spanb = "<div class='itemlabel'>";
	_spane = "<\/div>";
	
	if (_type == 1)
		_span_air = "<div style='float:left;margin-top:4px;'><img src='./image/airport_icon.gif'></div>";
	else
		_span_air = "";

	return _divb + _span_air + _spanb + _str + _spane + _dive; 
}