var xmlHTTPLogin;
var xmlHTTPLoginFF;

/*/***************************BEGIN - Adicionar funções de w3c compatibility****************/
/**************************BEGIN - FUNÇÔES UTILIZADAS PARA CONVERSÃO MULTIBROWSER***********************/
// Função para detectar os dados do browser para registar nos logs 
// BC 2010-03-09
function getbrowser()
	{
		     var OSName="Unknown OS";
			if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
			if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
			if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
			if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
			
			var nVer = navigator.appVersion;
			var nAgt = navigator.userAgent;
			var browser  = navigator.appName;
			var fullVersion  = ''+parseFloat(navigator.appVersion); 
			var majorVersion = parseInt(navigator.appVersion,10);
			var nameOffset,verOffset,ix;

			// In MSIE, the true version is after "MSIE" in userAgent
			if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
			browserName = "Microsoft Internet Explorer";
			fullVersion = nAgt.substring(verOffset+5);
			}
			// In Opera, the true version is after "Opera" 
			else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
			browserName = "Opera";
			fullVersion = nAgt.substring(verOffset+6);
			}
			// In Chrome, the true version is after "Chrome" 
			else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
			browserName = "Chrome";
			fullVersion = nAgt.substring(verOffset+7);
			}
			// In Safari, the true version is after "Safari" 
			else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
			browserName = "Safari";
			fullVersion = nAgt.substring(verOffset+7);
			}
			// In Firefox, the true version is after "Firefox" 
			else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
			browserName = "Firefox";
			fullVersion = nAgt.substring(verOffset+8);
			}
			// In most other browsers, "name/version" is at the end of userAgent 
			else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 
			{
			browserName = nAgt.substring(nameOffset,verOffset);
			fullVersion = nAgt.substring(verOffset+1);
			if (browserName.toLowerCase()==browserName.toUpperCase()) {
			browserName = navigator.appName;
			}
			}
			// trim the fullVersion string at semicolon/space if present
			if ((ix=fullVersion.indexOf(";"))!=-1) fullVersion=fullVersion.substring(0,ix);
			if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);
			majorVersion = parseInt(''+fullVersion,10);
			if (isNaN(majorVersion)) {
			fullVersion  = ''+parseFloat(navigator.appVersion); 
			majorVersion = parseInt(navigator.appVersion,10);
			}
			
			var osxml ='<xml><os>'+OSName +'</os><browser>'+browser +'</browser><agent>'+nAgt +'</agent><version>'+fullVersion+'</version><resolution>'+ screen.width +'x'+ screen.height +'</resolution></xml>';
		return osxml
	
	}
function W3CNewObject(strObjectName)
{
    var obj;
    if (strObjectName == 'xmlhttp') 
    {

        if (typeof XMLHttpRequest == "undefined") XMLHttpRequest = function() 
        {
            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) { }
            try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) { }
            throw new Error("This browser does not support XMLHttpRequest.");
        }
        else 
        {
            obj = new XMLHttpRequest;
        }
    }
    else 
    {
        if (strObjectName == 'xml') 
        {

            try //Internet Explorer
            {
                obj = new ActiveXObject("MSXML2.DomDocument");
                obj.async = false;
            }
            catch (e) {
                try //Internet Explorer
                {
                    obj = new ActiveXObject("Microsoft.XMLDOM");
                    obj.async = false;
                }
                catch (e) {
                    try //Internet Explorer
                    {
                        obj = new ActiveXObject("Microsoft.DomDocument");
                        obj.async = false;
                    }
                    catch (e) {
                        try //Firefox, Mozilla, Opera, etc.
                        {
			    obj = document.implementation.createDocument("","",null);
                            obj.async = false;
                        }
                        catch (e) {
                            alert(e.message);
                        }
                    }
                }
            }
        }
    }
    return obj;
}


function W3CXMLLoad(varobj , strxml)
{
	if ( window.ActiveXObject)
	{
		varobj.loadXML(strxml);
	}
	else
	{
		parser=new DOMParser();
    		varobj=parser.parseFromString(strxml,"text/xml");
	}

	return varobj;
}

function W3CXSLLoad(varobj , strxsl)
{
	if ( window.ActiveXObject)
	{
		varobj.loadXML(strxml);
	}
	else
	{
		parser=new DOMParser();
    	varobj=parser.parseFromString(strxml,"text/xml");
	}

	return varobj;
}

//Autoria de Hugo e Sergio
function W3CXMLtoString(objXML)
{
	var strObj = (new XMLSerializer()).serializeToString(objXML);
	//alert(strObj);
	return strObj;
}

function W3CNodeTransform(oxml,oxsl)
{
	var obj;
		
	if (window.ActiveXObject)
  	{
		obj=oxml.transformNode(oxsl);
	}
	else
	{
		xsltProcessor=new XSLTProcessor();
  		xsltProcessor.importStylesheet(oxsl);
  		obj = xsltProcessor.transformToDocument(oxml);		
	}

	return obj; 	
}

function W3CXMLList(obj)
{
   var x=obj;
   var str='';



   if ( !window.ActiveXObject)
   {
   	while (x!= null)
   	{
		
		if (x.nodeType != 3 && x.nodeType != 9)
		{
		    str= str + '<' +  x.nodeName + ' ';
	       	    if (x.hasAttributes())
		    {
				
		        y = x.attributes;
                    	
			for (i=0; i < y.length; i++)
		    	{
                                
				str = str + y.item(i).nodeName + "='" + y.item(i).nodeValue + "' "; 
		    	}		
		    }
		    str = str + '>';
		
		    
		}
		if (x.nodeValue != null && x.nodeValue != '' ) 
		{
			
			str = str + x.nodeValue;
		}
	
   	
   	
		if (x.hasChildNodes()) 
		{
			x = x.firstChild; 
		}
		else 
		{
			if (x.nextSibling!=null) 
			{
				if (x.nodeType != 3 && x.nodeType != 9) str= str + '</' +  x.nodeName + '>';
				x=x.nextSibling; 
	
			}
			else 
			{
				if (x.nodeType != 3 && x.nodeType != 9) str= str + '</' +  x.nodeName + '>';
				x = x.parentNode;
				if (x.nodeType != 3 && x.nodeType != 9) str= str + '</' +  x.nodeName + '>';
				x = x.nextSibling;
			}
		}
   	}
   	
	return str;
   }
   else return obj.xml;	
   
}


/*/***************************END - Adicionar funções de w3c compatibility****************/


function login(user, pwd, method,strbrowser)
{
	var xmlDoc;
	var elm;
	var att;
		  
	//If internet explorer
	if(window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("MSXML2.DOMDocument");
		//xmlDoc = W3CNewObject("xml");
		xmlDoc.loadXML("<xml/>");
		//xmlDoc = W3CXMLLoad(xmlDoc, '<xml></xml>');
		elm = xmlDoc.createProcessingInstruction("xml", 
				"version=\"1.0\" encoding=\"windows-1252\"");
		xmlDoc.insertBefore(elm, xmlDoc.childNodes.item(0));
		elm = xmlDoc.createElement("login");
		  
		// Username
		att = xmlDoc.createAttribute("user");
		att.text = user;
		elm.attributes.setNamedItem(att);
		  
		// Password
		att = xmlDoc.createAttribute("pwd");
		att.text = pwd;
		elm.attributes.setNamedItem(att);
		
		// Browser
		att = xmlDoc.createAttribute("browser");
		att.text = strbrowser;
		elm.attributes.setNamedItem(att);
		  
		window.status = "Autenticando...";
		  
		xmlDoc.documentElement.appendChild(elm);
		  
		try 
		{
			 xmlHTTPLogin = new ActiveXObject("msxml2.xmlHTTP"); 
		}
		catch (e) 
		{ 
			xmlHTTPLogin = new ActiveXObject("Microsoft.xmlHTTP"); 
		}
		xmlHTTPLogin.onreadystatechange = eval(method);
		xmlHTTPLogin.open("POST", "/crm/_code/xhtCRMLogin.asp", true);
		xmlHTTPLogin.send(xmlDoc.xml);
	}
	else
	{
		//xmlDoc = new ActiveXObject("msxml2.DOMDocument");
		xmlDoc = W3CNewObject("xml");
		//xmlDoc.loadXML("<xml/>");
		xmlDoc = W3CXMLLoad(xmlDoc, '<xml></xml>');
		/*elm = xmlDoc.createProcessingInstruction("xml", 
				"version=\"1.0\" encoding=\"windows-1252\"");
		xmlDoc.insertBefore(elm, xmlDoc.childNodes.item(0));*/
		elm = xmlDoc.createElement("login");
		  
		// Username
		att = xmlDoc.createAttribute("user");
		att.value = user;
		elm.attributes.setNamedItem(att);
		  
		// Password
		att = xmlDoc.createAttribute("pwd");
		att.value = pwd;
		elm.attributes.setNamedItem(att);
		
		// Browser
		att = xmlDoc.createAttribute("browser");
		att.value = strbrowser;
		elm.attributes.setNamedItem(att);
		  
		window.status = "Autenticando...";
		  
		xmlDoc.documentElement.appendChild(elm);
		
		//alert(xmlDoc.documentElement.childNodes[0].getAttribute("user") + " - "  + xmlDoc.documentElement.childNodes[0].getAttribute("pwd"));
		  
		/*try { xmlHTTPLogin = new ActiveXObject("msxml2.xmlHTTP"); }
		catch (e) { xmlHTTPLogin = new ActiveXObject("Microsoft.xmlHTTP"); }*/
	    xmlHTTPLoginFF = W3CNewObject('xmlhttp'); 
		xmlHTTPLoginFF.onreadystatechange = eval(method);
		//xmlHTTPLogin.onreadystatechange = finishLogin;
		xmlHTTPLoginFF.open("POST", "/crm/_code/xhtCRMLogin.asp", true);
		xmlHTTPLoginFF.send(W3CXMLList(xmlDoc));
	}
}
/******************************************************************************
'Function: createPopupLogin()
'Purpose:  This function opens a modal dialog with the popuplogin.
'Inputs:   winParent - parent window;
'          strTitle - title of the message;
'          strMessage - message;
'          intWidth - width size of the modal window;
'          intHeight - height size of the modal window.
'Returns:  anything - it can be a boolean, string, object, integer, etc.
'******************************************************************************
' Author   Date       Comments
' -------- ---------- ---------------------------------------------------------
' JGP/V    2002.09.26 Creation of the function.
******************************************************************************/
function createPopupLogin (winParent, strTitle, strMessage, strUser,
                           intWidth, intHeight, Caso)
{	
  // Declaration of the local variables.
  var objDialogParameters = new Object();
  var result;
  
  // Validation of the parameters.
  if (strTitle == '') strTitle = 'econstroi.com';
  if (strMessage == '') strMessage = '...';
  if (isNaN(intWidth) || intWidth <= 0) intWidth = 200;
  if (isNaN(intHeight) || intHeight <= 0) intHeight = 200;
  
  // Initialize the dialog parameters.
  objDialogParameters.winParent = winParent;
  objDialogParameters.title = strTitle;
  objDialogParameters.text = strMessage;
  objDialogParameters.user = strUser;
  
  // Open the modal dialog.
  if (Caso==3){
  result = winParent.showModalDialog("/crm/_inc/crmPopupLogin.asp?Caso=" + Caso, 
            objDialogParameters, "dialogWidth: " + intWidth
            + "px; dialogHeight: " + intHeight 
            + "px; center: Yes; help: No; status: No; scroll: No;");
            }else{
    result = winParent.showModalDialog("/crm/_inc/crmPopupLogin.asp?Caso=" + Caso, 
            objDialogParameters, "dialogWidth: " + intWidth
            + "px; dialogHeight: " + intHeight 
            + "px; center: Yes; help: No; status: No; scroll: No;");}
            
 
  return result;
}

/******************************************************************************
'Function: createPopupQuiz()
'Purpose:  This function opens a modal dialog with the popup of the quiz.
'Inputs:   winParent - parent window;
'          strTitle - title of the message;
'          strMessage - message;
'          intWidth - width size of the modal window;
'          intHeight - height size of the modal window.
'Returns:  anything - it can be a boolean, string, object, integer, etc.
'******************************************************************************
' Author   Date       Comments
' -------- ---------- ---------------------------------------------------------
' JGP/V    2002.09.26 Creation of the function.
******************************************************************************/
function createPopupQuiz (winParent, xmlDoc, strTitle, strMessage, strUser,
                           intWidth, intHeight, Caso)
{	
  // Declaration of the local variables.
  var objDialogParameters = new Object();
  var result;
  
  // Validation of the parameters.
  if (strTitle == '') strTitle = 'Vortal.biz';
  if (strMessage == '') strMessage = '...';
  if (isNaN(intWidth) || intWidth <= 0) intWidth = 200;
  if (isNaN(intHeight) || intHeight <= 0) intHeight = 200;
  
  // Initialize the dialog parameters.
  objDialogParameters.winParent = winParent;
  objDialogParameters.title = strTitle;
  objDialogParameters.text = strMessage;
  objDialogParameters.userEC = xmlDoc.selectSingleNode("xml/diagnet/loginEC").text;
  objDialogParameters.userdiagnet = xmlDoc.selectSingleNode("xml/diagnet/logindiagnet").text;
  objDialogParameters.pwddiagnet = xmlDoc.selectSingleNode("xml/diagnet/pwddiagnet").text;
  
  
  // Open the modal dialog.
  //if (Caso==3){
  result = winParent.showModalDialog("/crm/_inc/crmPopupQuiz.asp", 
            objDialogParameters, "dialogWidth: " + intWidth
            + "px; dialogHeight: " + intHeight 
            + "px; center: Yes; help: No; status: No; scroll: No;");
        /*    }else{
    result = winParent.showModalDialog("/crm/_inc/crmPopupLogin.asp?Caso=" + Caso, 
            objDialogParameters, "dialogWidth: " + intWidth
            + "px; dialogHeight: " + intHeight 
            + "px; center: Yes; help: No; status: No; scroll: No;");}
          */  
 
  return result;
}