/* -----------------------------------------------------
GeoMgr
------------------------------------------------------ */

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function GeoCookie() {
    this.domain = getPrimaryDomain();
    this.secure = "";
    this.pathname = "/";
    this.cookieName = "SNGC";
    this.expirationDate = new Date(new Date().getTime() + (30 * 1000 * 60 * 60 * 24));
    this.currentVersion = "1";
    this.info = new Array();
    this.updateCookie = updateCookie;
    this.getInfoValue = getInfoValue;
    this.setInfoValue = setInfoValue;
    this.writeCookie = writeCookie;
    this.loadCookies = loadCookies;
    this.parseCookie = parseCookie;
    this.getCookieVersion = function() {return this.getInfoValue('v');};
    this.getCountryCode = function() {return this.getInfoValue('c');};
    this.isValid = function() {return (this.getCookieVersion() != undefined);};
    function loadCookies() {
        var cookies = new Array();
        if (document.cookie != '') {
            var cookieArray = document.cookie.split(';');
            for (var i = 0; i < cookieArray.length; i++) {
                var cookiesValues = cookieArray[i].split('=');
                cookies[cookiesValues[0].trim()] = cookiesValues[1];
            }
        }

        return cookies;
    }

    function getInfoValue(key) {
        return ((this.info != null) ? this.info[key] : undefined);
    }

    function setInfoValue(key, value) {
        this.info[key] = value;
    }

    function updateCookie(c) {
        this.setInfoValue("v", this.currentVersion);
        this.setInfoValue("c", c);
        this.writeCookie();
    }

    function parseCookie(cookies) {
        if (cookies[this.cookieName] != undefined) {
            var cookie = unescape(cookies[this.cookieName]);
            var cookieArray = cookie.split("|");
            for (var i = 0; i < cookieArray.length; i++) {
                chips = cookieArray[i].split(":");
                this.info[chips[0]] = chips[1];
            }
        }
    }

    function writeCookie() {
        var cookieValue = "v:" + this.getCookieVersion() + "|" + "c:" + this.getCountryCode() + "|";

        document.cookie = this.cookieName + "=" + escape(cookieValue) +
                          ( (this.expirationDate) ? ";expires=" + this.expirationDate.toGMTString() : "") +
                          ( (this.pathname) ? ";path=" + this.pathname : "" ) +
                          ( (this.domain ) ? ";domain=" + this.domain : "" ) +
                          ( (this.secure ) ? ";secure=" + this.secure : "" );

    }

    function getPrimaryDomain() {

        var theUrl = document.domain;
        if(theUrl == 'localhost')
            return '';
        var urlLength = theUrl.length;
        var firstDot = theUrl.lastIndexOf(".");
        var secondDot = theUrl.lastIndexOf(".", firstDot - 1);
        var primaryDomain = theUrl.substr(secondDot);
        return primaryDomain;
    }
}

function GeoMgr(exclusiveRestriction) {
    this.geoCookie = new GeoCookie();
    this.exclusiveRestriction = exclusiveRestriction;
    this.init = init;
    this.initGEO = initGEO;
    this.getCountryCode = getCountryCode ;

    this.restrictedCountry = restrictedCountry;

    function init() {
       this.geoCookie.parseCookie(this.geoCookie.loadCookies());
        if (!this.geoCookie.isValid()) {
            dwrClientChecker.getCountryCode(geoCallback);
        } else {}
    }

   function initGEO(data) {
        this.geoCookie.updateCookie(data);
   }

   function getCountryCode() {
         this.geoCookie.getCountryCode()
   }

   function restrictedCountry() {
      if(this.exclusiveRestriction != null) {
         if(this.geoCookie.getCountryCode() != this.exclusiveRestriction.country)
            return true;
      }
      return false;
    }
}

function GeoMgrExclusiveRestriction(country, redirect) {
      this.country = country;
      this.redirect = redirect;
}



