﻿var xmldoc;

function tracking() {
    try {
        var is404 = $("#is404").val();

        if (is404 != "true") {
            var pipc = $("#ipc").val();
            var pref = $("#referer").val();
            var pidiocodi = $("#idiocodi").val();
            var ppartcodi = $("#partcodi").val();
            var psesicodi = $("#sesicodi").val();
            var pguid = $("#guid").val();
            var phash = $("#hash").val();
            
            var RUURI = "http://www.iberostar.com/asp/tracker.asp";
            xmldoc = jsAJAXCargaXML(RUURI + "?sesicodi=" + psesicodi + "&partcodi=" + ppartcodi + "&ipc=" + pipc + "&ref=" + pref + "&idiocodi=" + pidiocodi + "&guid=" + pguid + "&hash=" + phash + "", RUTracking);


        }
    }
    catch (err) {
        //Handle errors here
        //        alert(err);
    }

}


function RUTracking() {
    var sesicodi_node;
    var sesicodi = "";

    try {/*Chrome and safari*/
        sesicodi_node = xmldoc.responseXML.childNodes[0].childNodes[1];
        //sesicodi_node = xmldoc.documentElement.getfirstElementChild.childNodes[0];

        sesicodi = sesicodi_node.textContent;
    } catch (e) {
        try {/*Explorer*/
            sesicodi_node = xmldoc.documentElement.childNodes[0].firstChild;
            //sesicodi_node = xmldoc.documentElement.getfirstElementChild.childNodes[0];

            sesicodi = sesicodi_node.nodeValue;
        } catch (e) {
            try {/*Firefox and Opera*/
                //sesicodi_node = xmldoc.getElementsByTagName('sesicodi')[0];
                sesicodi_node = xmldoc.documentElement.childNodes[1].firstChild;

                sesicodi = sesicodi_node.nodeValue;
            } catch (e) { }
        }
    }

    if (sesicodi.length > 0) {
        $("#sesicodi").val(sesicodi);
    }

}


function jsAJAXCargaXML(url, functionAJAX) {
    //Vaciamos el objeto global de respuesta para evitar posibles datos corruptos
    xmldoc = null;
    var conDat;
    var navegador = "";
    //Averiguamos el navegador y creamos objeto XML
    if (window.ActiveXObject) {
        conDat = new ActiveXObject("Microsoft.XmlDom");
        navegador = "IE";
    } else {
        if (document.implementation && document.implementation.createDocument) {
            conDat = document.implementation.createDocument("", "", null);
            navegador = "NIE";
        } else {
            navegador = "";
        }
    }


    if (navigator.userAgent.indexOf("Safari") != -1) {
        conDat = new XMLHttpRequest();
    }


    if (navegador != "") {
        conDat.async = true;

        if (navegador == "IE") {
            conDat.onreadystatechange = function() {
                if (conDat.readyState == 4) {
                    functionAJAX();
                }
            };
        } else {
            conDat.onload = function() {
                functionAJAX();
            }
        }


        if (navigator.userAgent.indexOf("Safari") != -1) {
            conDat.open("GET", url, true);
            conDat.send("");
            return conDat;
        } else {
            conDat.load(url);
        }

        return conDat;
    } else {
        return null;
    }
}
function onError(XMLHttpRequest, textStatus, errorThrown) {

}

function trackingVariables() {
    var ipc = $("#ipc").val();
    var ref = $("#referer").val();
    var idiocodi = $("#idiocodi").val();
    var partcodi = $("#partcodi").val();
    var sesicodi = $("#sesicodi").val();
    var guid = $("#guid").val();
    var hash = $("#hash").val();

    ref = escape(ref);

    var variables = "ipc=" + ipc + "&ref=" + ref + "&idiocodi=" + idiocodi + "&cp=" + partcodi +
    "&sesicodi=" + sesicodi + "&guid=" + guid + "&hash=" + hash;

    return variables;
}

function path(basedir) {
    var variables = trackingVariables();

    var x = "";
    if (basedir.indexOf("?") != -1) {
        x = "&";
    }
    else {
        x = "?";
    }

    return basedir + x + variables;
}

function doTrackLinks() {
    $("a").each(function() {
        var url = $(this).attr("href");

        if (url.indexOf("iberostar.com") != -1) {
          //  $(this).attr("href", path(url));
        }
    })
}
