var expDays = 365; // number of days the cookie should last

function SetCookie (name, value){
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function DeleteCookie (name){
    var exp = new Date();
    exp.setTime (exp.getTime() - 1);
    var cval = GetCookie (name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function amt(){
    var count = GetCookie('count')
    if(count == null){
        SetCookie('count','1')
        return 1
    }
    else{
        var newcount = parseInt(count) + 1;
        DeleteCookie('count')
        SetCookie('count',newcount,exp)
        return count
    }
}

function getCookieVal(offset){
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name){
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return getCookieVal(j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

function Querystring(qs){ // optionally pass a querystring to parse
    this.params = new Object()
    this.get=Querystring_get
    if (qs == null)
        qs=location.search.substring(1,location.search.length)
    if (qs.length == 0) return
// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ')
    var args = qs.split('&') // parse out name/value pairs separated via &
// split out each name=value pair
    for (var i=0;i<args.length;i++){
        var value;
        var pair = args[i].split('=')
        var name = unescape(pair[0])
        if (pair.length == 2)
            value = unescape(pair[1])
        else
            value = name
        this.params[name] = value
    }
}

function Querystring_get(key, default_){
    // This silly looking line changes UNDEFINED to NULL
    if (default_ == null) default_ = null;
    var value=this.params[key]
    if (value==null) value=default_;
    return value
}

function getRootDomain(url){
    var baseURL = (url == null) ? location.href : url;
    var rootURL = "";
    if (baseURL.indexOf('/', 7) < 1) {
        rootURL = baseURL;
    }
    else {
        if ((baseURL.indexOf('https://')) > -1) {
            rootURL = baseURL.substring(0, baseURL.indexOf('/', 8));
        }
        else
        {
            rootURL = baseURL.substring(0, baseURL.indexOf('/', 7));
        }
    }
    if (baseURL.indexOf('localhost') == -1) {
        if (rootURL.indexOf("http://") == 0) {
            rootURL = rootURL.substring(7);
        }
        if (rootURL.indexOf("https://") == 0) {
            rootURL = rootURL.substring(8);
        }
        while (rootURL.split(".").length > 2) {
            var index = rootURL.indexOf('.', 1) + 1;
            rootURL = rootURL.substring(index);
        }
        return rootURL;
    }
    else {
        return rootURL + baseURL.substring(baseURL.indexOf('/', 8), baseURL.indexOf('/', baseURL.indexOf('/', 8) + 1)) + "/";
    }
}

function detectGeo(addr) {
    var geo = GetCookie('geo');
    var qs = new Querystring();
    var redir = qs.get('redir');

    if (redir == '0') {
        SetCookie('redir', '0');
    }
    if (GetCookie('redir') != '0') {
        if (GetCookie('wsEntity') == "NA") {
            return;
        }
        if (GetCookie('wsEntity') == "EU") {
            document.location = 'http://www.forex.com/uk/' + addr;
        }
        if (GetCookie('wsEntity') == "JP") {
            document.location = 'http://jp.forex.com/jp/';
        }
        switch (geo) {
            case 'GB': document.location = 'http://www.forex.com/uk/' + addr; break;
        }
    }
}

function detectGeoDemo(addr) {
    var geo = GetCookie('geo');
    var qs = new Querystring();
    var version = (qs.get("v") == null) ? "" : "?v=" + qs.get("v");
    if (GetCookie('wsEntity') == "NA") {return;}
    if (GetCookie('wsEntity') == "EU") {document.location = 'http://www.forex.com/uk/' + addr + version;}
    if (GetCookie('wsEntity') == "JP") { document.location = 'http://jp.forex.com/jp/services/forextrader_open_demo.shtml' }
    switch (true) {
        case /AL|AD|AG|AN|AR|AM|AW|AU|AT|AZ|BS|BD|BB|BE|BZ|BJ|BM|BT|BO|BA|BW|BR|VG|BN|BG|BF|KH|CM|CA|CV|KY|CF|TD|CL|CO|CK|CR|HR|CY|CZ|DK|DJ|DM|DO|TL|EC|SV|GQ|EE|ET|FO|FJ|FI|FR|GF|GB|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GT|GW|GY|HT|HN|HU|IS|IN|ID|IE|IL|IT|JM|KZ|KE|KG|LA|LV|LS|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MQ|MR|MU|MX|MD|MC|MN|ME|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NO|PK|PA|PG|PY|PE|PH|PL|PT|ZA|RE|RO|RU|RW|WS|SM|SN|RS|SC|SL|SK|SI|SB|SO|ZA|ES|LK|KN|LC|PM|VC|SR|SZ|SE|CH|TJ|TA|TZ|TH|TG|TO|TT|TN|TR|UG|UA|UY|UZ|VU|VA|VE|N|ZM|ZW/.test(geo):
            document.location = 'http://www.forex.com/uk/' + addr + version;
        break;
    }
}

function grabGeo(){
    var geo = GetCookie('geo');
    if (geo == null){
        document.write('<s'+'cript type="text/javascript" src="https://secure.efxnow.com/zipproxy/keyfield.asp"></s'+'cript>');
    }
}

function assignGeoDemo(addr)
{
    var geo = GetCookie('geo');
    if (typeof(cc) != "undefined")
    {
        if ((cc != '') && (geo == null))
        {
            var rURL = getRootDomain();
            if (rURL.indexOf(".") > -1) {
                SetCookie('geo', cc, exp, "/", rURL);
            }
            else {
                SetCookie('geo', cc, exp, "/");
            }
        }
    }
    detectGeoDemo(addr);
}

function assignGeo(addr) {
    var geo = GetCookie('geo');
    if (typeof (cc) != "undefined") {
        if ((cc != '') && (geo == null)) {
            var rURL = getRootDomain();
            if (rURL.indexOf(".") > -1) {
                SetCookie('geo', cc, exp, "/", rURL);
            }
            else {
                SetCookie('geo', cc, exp, "/");
            }
        }
    }
    detectGeo(addr);
}

function assignGeoDemoBeta() {
    var geo = GetCookie('geo');
    if (typeof (cc) != "undefined") {
        if ((cc != '') && (geo == null)) {
            var rURL = getRootDomain();
            if (rURL.indexOf(".") > -1) {
                SetCookie('geo', cc, exp, "/", rURL);
            }
            else {
                SetCookie('geo', cc, exp, "/");
            }
        }
    }
    detectGeoDemoBeta();
}

function detectGeoDemoBeta(){
    var geo = GetCookie('geo');
    var pathString = window.location.pathname;
    var pageString = pathString.substring(pathString.lastIndexOf('/') + 1) + location.search;
    if (GetCookie('wsEntity') == "NA") {
        return;
    }
    if (GetCookie('wsEntity') == "EU") {
        document.location = "http://www.forex.com/uk/" + pageString;
    }
    if (GetCookie('wsEntity') == "JP") {
        document.location = 'http://jp.forex.com/jp/services/forextrader_open_demo.shtml'
    }
    switch (true) {
        case /AL|AD|AG|AN|AR|AM|AW|AU|AT|AZ|BS|BD|BB|BE|BZ|BJ|BM|BT|BO|BA|BW|BR|VG|BN|BG|BF|KH|CM|CA|CV|KY|CF|TD|CL|CO|CK|CR|HR|CY|CZ|DK|DJ|DM|DO|TL|EC|SV|GQ|EE|ET|FO|FJ|FI|FR|GF|GB|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GT|GW|GY|HT|HN|HU|IS|IN|ID|IE|IL|IT|JM|KZ|KE|KG|LA|LV|LS|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MQ|MR|MU|MX|MD|MC|MN|ME|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NO|PK|PA|PG|PY|PE|PH|PL|PT|ZA|RE|RO|RU|RW|WS|SM|SN|RS|SC|SL|SK|SI|SB|SO|ZA|ES|LK|KN|LC|PM|VC|SR|SZ|SE|CH|TJ|TA|TZ|TH|TG|TO|TT|TN|TR|UG|UA|UY|UZ|VU|VA|VE|N|ZM|ZW/.test(geo):
            document.location = 'http://www.forex.com/uk/' + pageString;
            break;
    }
}
