///<summary>remove all the options´ itens of a drop down object</summary>
///<param name="o">the drop down object</param>
function clearDropDown( o )
{
	try
	{
		for ( var i=o.length;i>0;i-- ) o.options[i-1] = null;
	}
	catch( ex )
	{
	}
}

///<summary>make a background http request</summary>
///<param name="m">request mode {"GET"|"POST"}</param>
///<param name="u">url to request</param>
///<param name="p">parameters to send on request</param>
///<returns>a xml object or an exception</returns>
function getXMLData( m,u,p )
{
	//if ( u.indexOf("http://"+window.location.host+"/") != 0 ) throw "Host inválido \"" + u + "\"";
	if ( u.indexOf("http://") >= 0 ) throw "Endereço inválido \"" + u + "\"\n\nO domínio não deve ser especificado!";
	u = "http://" + window.location.host + (u.substr(0,1)!="/"?"/":"") + u;

	if ( typeof(m) != "string" ) m = "GET"; else m = m.toUpperCase();
	if ( m != "POST" && m != "GET" ) m = "GET";
	
	try
	{
		var oHttp = XmlHttp.create();
		//oHttp.open("GET","http://<%=Request.ServerVariables("SERVER_NAME")%>/asp/data/cities.asp?uf="+uf,false);
		if ( m == "GET" )
		{
			u += (p!=null?"?"+p:"");
			oHttp.open(m,u,false);
			oHttp.setRequestHeader("Content-Type","text/html; charset=\"ISO-8859-1\"");
			p = null;
		}
		else
		{
			oHttp.open(m,u,false);
			oHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=\"ISO-8859-1\"");
		}
		oHttp.setRequestHeader("Accept-Language","pt-BR");
		oHttp.setRequestHeader("Cache-Control","no-cache");
		oHttp.setRequestHeader("User-Agent",window.navigator.userAgent);
		//oHttp.onreadystatechange = function xmlhttpChange(){window.alert(oHttp);};
		oHttp.send(p);
		if ( oHttp.status == "404" ) throw "HTTP Error 404 - the url \"" + u + "\" does not exist."
		return oHttp.responseXML;
	}
	catch (ex)
	{
		//window.alert( "Não foi possível estabelecer a conexão com o servidor devido ao erro abaixo!\n\n" + ex );
		throw "A conexão com o servidor falhou devido ao seguinte erro:\n\n" + ex;
	}
   //return null;
}

function getUser( eml )
{
   var oXml = null;
   var id = "", name = "", ddd = "", phone = "", state = "";
   
   if ( eml == "" ) return;

	try
	{
		oXml = getXMLData("POST","/empresa/contact/utils/data.asp","eml="+eml);
		if ( oXml == null ) throw "Os dados retornados pela fonte não são válidos!";
	}
	catch (ex)
	{
		//window.alert( "Os dados recebidos do servidor não estão corretos!" );
		window.alert( ex );
		return;
	}

   try
   {  
		//window.alert( oXml.readyState );
		if ( window.navigator.appName.indexOf("Explorer") > -1  )
		{
			if ( oXml.childNodes.length == 2 )
			{
				var nodes = oXml.childNodes[1].childNodes;
				for ( var i=0;i<nodes.length;i++)
				{
					if ( nodes[i].childNodes.length )
					{
						if ( nodes[i].nodeName == "id" ) id = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "name" ) name = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "ddd" ) ddd = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "phone" ) phone = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "state" ) state = nodes[i].firstChild.nodeValue;
					}
				}
			}

		}
		else
		{
			if ( oXml.childNodes.length == 1 )
			{
				var nodes = oXml.childNodes[0].childNodes;
				for ( var i=0;i<nodes.length;i++)
				{
					if ( nodes[i].childNodes.length )
					{
						if ( nodes[i].nodeName == "id" ) id = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "name" ) name = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "ddd" ) ddd = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "phone" ) phone = nodes[i].firstChild.nodeValue;
						if ( nodes[i].nodeName == "state" ) state = nodes[i].firstChild.nodeValue;
					}
				}
			}
		}
		var frm = window.document.forms["f1"];
		if ( id.length > 0 ) frm.elements["txtId"].value = id;
		if ( name.length > 0 ) frm.elements["txtName"].value = name;
		if ( ddd.length > 0 ) frm.elements["txtDDD"].value = ddd;
		if ( phone.length > 0 ) frm.elements["txtPhone"].value = phone;
		for ( var j=0;j<frm.elements["cboStates"].length;j++ )
		{
			//alert( frm.elements["cboStates"].options[j].value + " | " + state );
			if ( frm.elements["cboStates"].options[j].value == state )
			{
				frm.elements["cboStates"].selectedIndex = j;
				break;
			}
		}
/*
		//window.alert( nodes.length );
		var oCbo = window.document.forms["f1"].elements["cbo"];
		for ( var i=0;i<nodes.length;i++)
		{
			//window.document.forms["f1"].elements["txt"].value += nodes[i].firstChild.nodeValue + "\n";
			//oCbo.options[i] = new Option( nodes[i].firstChild.nodeValue, "0" );
			oCbo.options[i] = new Option( nodes[i].attributes[1].nodeValue, nodes[i].attributes[0].nodeValue );
		}
*/
	}
	catch (ex)
	{
		window.alert( "A busca pelo usuário falhou devido ao seguinte erro:\n\n" + ex );
		return;
	}
}