var ajax;
var container;
var pos;

function initAjax()
{
	ajax = getAjax();
	
	if ( !ajax ) {
		//alert("Seu browser não possui o objeto XMLHttp para a utilização do Ajax.");
		ajax = null;
		return false;
	}
    
	container = document.getElementById("dvLogin");
	box = document.getElementById("dvLoginBox");
}

function logar()
{

	var contentType = "application/x-www-form-urlencoded; charset=ISO-8859-1";

    if ( !ajax ) {
        //document.frmLoginBox.submit();
        return;
    }
    
    box.style.display = "none";
    
	ajax.open("POST", "login.aspx?", true );
    	
    ajax.setRequestHeader("Content-Type", contentType);
	ajax.onreadystatechange = ajaxStatus;
    ajax.send( getParam(document.frmLoginBox) );
	
}

function ajaxStatus()
{
	if ( ajax.readyState == 4 ){
//		alert(ajax.responseText)
		container.innerHTML = ajax.responseText //getHtml(ajax.responseText);
		installScripts(ajax.responseText);
	}
	else{
		container.innerHTML = "Aguarde ...<br /><br />"
	}

}

function installScripts(content)
{
    var search = content; 
    var script; 
          
    while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i)) 
    { 
      search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length); 
       
      if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break; 
       
      block = search.substr(0, search.indexOf(RegExp.$1)); 
      search = search.substring(block.length + RegExp.$1.length); 
       
      var oScript = document.createElement('script'); 
      oScript.text = block; 
      document.getElementsByTagName("head").item(0).appendChild(oScript); 
    } 

}
function getAjax()
{
	var xmlhttp=false;
   /* running locally on IE5.5, IE6, IE7 */                                              ; /*@cc_on
     if(location.protocol=="file:"){
      if(!xmlhttp)try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;}
      if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;}
     }                                                                                ; @cc_off @*/
   /* IE7, Firefox, Safari, Opera...  */
     if(!xmlhttp)try{ xmlhttp=new XMLHttpRequest(); }catch(e){xmlhttp=false;}
   /* IE6 */
     if(typeof ActiveXObject != "undefined"){
      if(!xmlhttp)try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;}
      if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;}
     }
   /* IceBrowser */
     if(!xmlhttp)try{ xmlhttp=createRequest(); }catch(e){xmlhttp=false;}

	return xmlhttp;
}


function getParam(obj) {

  var getstr = "" ;// = "?";
  for (i=0; i<obj.elements.length; i++) {
     if (obj.elements[i].tagName.toUpperCase() == "INPUT") {
        
        var t = obj.elements[i].type.toLowerCase();

        if ( t == "text" || t == "password" ) {
           getstr += obj.elements[i].name + "=" + encodeURIComponent(obj.elements[i].value) + "&";
        }
        if ( t == "checkbox") {
           if (obj.elements[i].checked) {
              getstr += obj.elements[i].name + "=" + encodeURIComponent(obj.elements[i].value) + "&";
           } else {
              getstr += obj.elements[i].name + "=&";
           }
        }
        if ( t == "radio") {
           if (obj.elements[i].checked) {
              getstr += obj.elements[i].name + "=" + encodeURIComponent(obj.elements[i].value) + "&";
           }
        }
     }   
     if (obj.elements[i].tagName.toUpperCase() == "SELECT") {
        var sel = obj.childNodes[i];
        getstr += sel.name + "=" + encodeURIComponent(sel.options[sel.selectedIndex].value) + "&";
     }
     
  }
  return getstr;
}
function cancelarLogin()
{
	container.innerHTML = "";
	box.style.display=''
	document.frmLoginBox.txtSenha.value = "";
	document.frmLoginBox.txtUsuario.value = "";
}
function testaSubmit(e,f)
{
    if(e.keyCode == 13)
    {
        logar();
    }
}