﻿var expDays = 1; // number of days the cookie should last
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
ThaiPups={
    GetCookie:function(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 this.getCookieVal(j);
            i = document.cookie.indexOf(" ", i) + 1;
            if (i == 0) break;
        }
        return null;
    },
    SetCookie:function(name, value) {
        var argv = this.SetCookie.arguments;
        var argc = this.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" : "");
    },
    DeleteCookie:function(name) {
        var exp = new Date();
        exp.setTime (exp.getTime() - 1);
        var cval = this.GetCookie (name);
        document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    },
    amt:function(){
        var count = this.GetCookie('count')
        if(count == null) {
            this.SetCookie('count','1')
            return 1
        } else {
            var newcount = parseInt(count) + 1;
            DeleteCookie('count')
            this.SetCookie('count',newcount,exp)
            return count
        }
    },
    getCookieVal:function(offset) {
        var endstr = document.cookie.indexOf (";", offset);
        if (endstr == -1)
            endstr = document.cookie.length;
        return unescape(document.cookie.substring(offset, endstr));
    },
    GetRequestObject:function(){
        req = false;
	    // branch for native XMLHttpRequest object
	    if(window.XMLHttpRequest) {
	        try {
	            req = new XMLHttpRequest();
            } catch(e) {
	            req = false;
            }
	        // branch for IE/Windows ActiveX version
        } else if(window.ActiveXObject) {
	        try {
	            req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
	            try {
	                req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
	                req = false;
                }
            
	        }
	    }
        return req;
    },
    hide:function(){
        document.getElementById('thaipopups').style.display='none';
        return false;
    },
    checkCount:function() {
        var count = ThaiPups.GetCookie('count');  
        if (count == null) {
            count=1;
            ThaiPups.SetCookie('count', count, exp);
            var url = 'currencyPopup.axd';
            var req =ThaiPups.GetRequestObject();
            if(req) {
                req.onreadystatechange = function(){
	                if(req.readyState == 4){
	                    var response = req.responseText;
	                    if(response!=null&&response!=''&&response.indexOf('FAILED')==-1){
	                        document.getElementById('lblText').innerHTML = response.split('|')[0];
	                        document.getElementById('ctl00_hidCurrency').value = response.split('|')[1]
	                        document.getElementById('thaipopups').style.display='block';
	                    }
	                }
                }
                req.open("get", url, true);
                req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            	req.send(null);
            } 
        } else {
            count++;
            ThaiPups.SetCookie('count', count, exp);
        }
    },
    addLoadEvent: function (func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        }
        else {
            window.onload = function () {
                oldonload();
                func();
            }
        }
     }
}
ThaiPups.addLoadEvent(ThaiPups.checkCount);
