﻿
var _send = 0;
var xmlHttp;

function trackingMails(sessionId){
    if(typeof(sessionId) != 'undefined'){
        if(sessionId != '' && window.location.href != ''){
            var pattern = '//'
            var temp = window.location.href.substring(window.location.href.indexOf(pattern) + pattern.length,window.location.href.length);

            var host = temp.substring(temp.pattern3,temp.indexOf('/')); 

            if((document.referrer == '') || (document.referrer.indexOf(host) == -1)){
                var objURL = new Object();
                var emailId;
                var userId;
                var Id;
                var nombre;
                var link;

                // get queryString

                window.location.search.replace(new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
                                            function( $0, $1, $2, $3 )
                                            {   objURL[ $1 ] = $3;});
                emailId = objURL['et'];
                userId = objURL['ui'];
                Id = objURL['gi'];

                link = window.location.href.substring(7,window.location.href.indexOf('?'));

                if (typeof(emailId) != 'undefined' && typeof(userId) != 'undefined' && typeof(Id) != 'undefined'){
                    if (emailId != '' && userId != '' && Id != ''){
                        var UrlGetData = '/WS/OLAP.asmx/ReturnMails';
                        var postbody = 'HTTP_HOST=' + host + '&SessionId=' + sessionId + '&emailId=' + emailId + '&userId=' + userId + '&generatorId=' + Id + '&link=' + link + '\n'; 

                        if (_send == 0) {
                            sendRemoteQuery(UrlGetData,postbody);
                        }
                    }
                }
            }
        }
    }
}
    
 //Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} 
}


function sendRemoteQuery (queryString,params)
{
    var contentType = 'application/x-www-form-urlencoded';
    var encoding = 'UTF-8';
    
    searching = true;

 // si hay una peticion activa me la cargo y lanzo otra
  if(xmlHttp && xmlHttp.readyState != 4) {
    xmlHttp.abort()
  }
  
 // xmlHttp= getXMLHTTP();
  xmlHttp = getXmlHttpRequestObject();
   if(xmlHttp){
   
        xmlHttp.open("POST", queryString, true);
        xmlHttp.setRequestHeader('Content-Type', contentType + '; charset=' + encoding);
    
    // What do we do when the response comes back?
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4 && xmlHttp.responseText && searching) {
            
            if (xmlHttp.responseXML.childNodes.length > 0)
            {
                result = xmlHttp.responseXML.getElementsByTagName('boolean')[0].firstChild;
                if (result.textContent == 'true') _send = 1; 
            }
              
            searching = false;
        }
    };
         xmlHttp.send(params);
  }
  
};
