﻿var rdCookieName = 'rdcookie';
var rdCountry;
var rdDomain;

function getRedirection() {
    var v = getRDCookie();
    var fromRedirect = getQuerystring('rdf');
    if ((v != window.location.hostname) && ((fromRedirect == '') || (fromRedirect == null))) {
        ret = AdvantechWebServiceLocal.CheckURLRedirection(OnRedirectComplete, OnRedirectTimeOut, OnRedirectError);
    } else {
        setRDCookie(window.location.hostname, 365);
    }
    return (true);
}

function OnRedirectComplete(args) {
    //alert(args);
    if (args != '') {
        var c = args.split(",")[0];
        var d = args.split(",")[1];
        if (d != document.domain) {
            addRedirectElement(c, d);
        }
    }
}

function OnRedirectTimeOut(args) {
    //alert('time out');
}

function OnRedirectError(args) {
    //alert('error: ' + args);
}

function addRedirectElement(country, newDomain) {
    var currentDomain = 'http://' + window.location.hostname;
    if ((country != '') && (country != 'XX') && (newDomain != currentDomain)) {
        rdCountry = country;
        rdDomain = newDomain;

        var newdiv = document.createElement('div');

        var divIdName = 'redirectDiv';

        newdiv.setAttribute('id', divIdName);
        newdiv.setAttribute('class', 'mainGradient text');
        var searchString = window.location.search;
        if ((searchString != '') && (searchString != null)) {
            searchString = searchString + '&rdf=' + window.location.hostname;
        } else {
            searchString = '?rdf=' + window.location.hostname;
        }
        newdiv.innerHTML = '<img src=\'http://wfcache.advantech.com/www/images/fileclose.png\' alt=\'Close\' width=\'11\' onClick=\'closeRDPanel();\' style=\'margin-top:1px;\' />Welcome to Advantech. <a href=\'' + newDomain + window.location.pathname + searchString + '\'>Going to Advantech ' + country + '?</a>';
        var con = document.getElementById('container');
        con.insertBefore(newdiv, con.firstChild);
    }
}

function closeRDPanel() {
    var d = document.getElementById('redirectDiv');
    if (d) {
        d.style.display = 'None';
        setRDCookie(window.location.hostname, 365);
    }

}

function setRDCookie(value, exdays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
    document.cookie = rdCookieName + "=" + c_value;
}

function getRDCookie() {
    var i, x, y, ARRcookies = document.cookie.split(";");
    for (i = 0; i < ARRcookies.length; i++) {
        x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
        y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
        x = x.replace(/^\s+|\s+$/g, "");
        if (x == rdCookieName) {
            return unescape(y);
        }
    }
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
