
var AHUtils = {
    
    // External survey cookie handler
    imcexpDays: 0,
    imcpage: "",
    ximcpage: "",
    imcwindowprops: "",
    exp : new Date(),
    pageCntLim : 0,
    xpageCntLim : 0,
    
	init : function() {
        this.imcexpDays = 90; // number of days the cookie should last

        this.imcpage = "http://www2.integratedmediaconcepts.com/amerihealth";
        this.ximcpage = "http://www2.integratedmediaconcepts.com/amerihealthexpress";
        
        //var imcpage = "default.asp"
        //var imcwindowprops = "width=360,height=480,location=no,toolbar=no,menubar=no,scrollbars=yes,resizable=no";
        this.imcwindowprops = "height=580,width=560";

        //exp = new Date(); 
        this.exp.setTime(this.exp.getTime() + (this.imcexpDays*24*60*60*1000));
        
        this.pageCntLim = 2;
        this.xpageCntLim = 4;
    
	},
	
	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 = AHUtils.SetCookie.arguments;  
	    var argc = AHUtils.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=Thu, 01-Jan-70 00:00:01 GMT";
	    document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT";
    },
    
    amt : function(){
        var count = this.GetCookie('scount')
	    if(count == null) {
		    //this.SetCookie('scount','1')
		    this.SetCookie('scount','1',this.exp, '/')
		    return 1
	    }else {
		    var newcount = parseInt(count) + 1;
		    this.DeleteCookie('scount')
		    //SetCookie('count',newcount,exp)
		    this.SetCookie('scount',newcount,this.exp,'/')
		    return count
	    }
    },
    
    xamt : function(){
        var count = this.GetCookie('xscount')
	    if(count == null) {
		    //this.SetCookie('scount','1')
		    this.SetCookie('xscount','1',this.exp, '/')
		    return 1
	    }else {
		    var newcount = parseInt(count) + 1;
		    this.DeleteCookie('xscount')
		    //SetCookie('count',newcount,exp)
		    this.SetCookie('xscount',newcount,this.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));
    },
    
    checkCount : function() {
	    var count = this.GetCookie('scount');
	    //alert(count);
	    if (count == null) {
		    count=1;
		    //this.SetCookie('count', count, exp);
		    this.SetCookie('scount', count, this.exp, '/');
		    //this.showSurvey();
	    } else {
		    count++;
		    //alert("in:" + this.exp.getTime());
		    this.DeleteCookie('scount');
		    //this.SetCookie('count', count, exp);
		    this.SetCookie('scount', count, this.exp, '/');
		    //alert(this.GetCookie('count'));
	    }
	    //alert(count + ":" + this.pageCntLim);
	    if (count > this.pageCntLim) {
	    //	this.showSurvey();
	    //	alert(count);
		    this.checkComplete() ;
	    }
    },
    
    xcheckCount : function() {
	    var count = this.GetCookie('xscount');
	    //alert(count);
	    if (count == null) {
		    count=1;
		    //this.SetCookie('xcount', count, exp);
		    this.SetCookie('xscount', count, this.exp, '/');
		    //this.showSurvey();
	    } else {
		    count++;
		    //alert("in:" + this.exp.getTime());
		    this.DeleteCookie('xscount');
		    //this.SetCookie('xcount', count, exp);
		    this.SetCookie('xscount', count, this.exp, '/');
		    //alert(this.GetCookie('count'));
	    }
	    //alert(count + ":" + this.xpageCntLim);
	    if (count > this.xpageCntLim) {
	    //	this.xshowSurvey();
	    //	alert(count);
		    this.xcheckComplete() ;
	    }
    },
    
    checkComplete : function() {
	    //alert("take the survey");
	    var sTaken = this.GetCookie('sTaken');
	    var sDay = this.GetCookie('sDay');
	    var d,s;
	    d = new Date();
	    s = (d.getMonth() + 1) + "_";
	    s += d.getDate() + "_";
	    s += d.getYear();
	    //alert(s);
    	
	    if (sTaken == null) {
		    //Is it the same day
		    if (sDay == null) {
		        //this.SetCookie('sDay', s, this.exp);
			    this.SetCookie('sDay', s, this.exp, '/');
			    this.showSurvey();
			    //this.SetCookie('sTaken', 0, this.exp);
		        this.SetCookie('sTaken', 0, this.exp, '/');
		    } else {
			    if (sDay == s) {
			    //	alert("this is the same day");
			    } else {
				    //this.SetCookie('sDay', s, this.exp);
				    this.SetCookie('sDay', s, this.exp, '/');
				    this.showSurvey();
				    //this.SetCookie('sTaken', 0, this.exp);
				    this.SetCookie('sTaken', 0, this.exp, '/');
			    }
		    }
	    } else {
		    if (sTaken == 0) {
			    if (sDay == s) {
			    //	alert("this is the same day2");
			    } else {
			        //this.SetCookie('sDay', s, this.exp);
				    this.SetCookie('sDay', s, this.exp, '/');
				    this.showSurvey();
			    }			
		    }
	    }
    },
    
    xcheckComplete : function() {
	    //alert("take the survey");
	    var sTaken = this.GetCookie('xsTaken');
	    var sDay = this.GetCookie('xsDay');
	    var d,s;
	    d = new Date();
	    s = (d.getMonth() + 1) + "_";
	    s += d.getDate() + "_";
	    s += d.getYear();
	    //alert(s);
    	
	    if (sTaken == null) {
		    //Is it the same day
		    if (sDay == null) {
		        //this.SetCookie('xsDay', s, this.exp);
			    this.SetCookie('xsDay', s, this.exp, '/');
			    this.xshowSurvey();
			    //this.SetCookie('xsTaken', 0, this.exp);
		        this.SetCookie('xsTaken', 0, this.exp, '/');
		    } else {
			    if (sDay == s) {
			    //	alert("this is the same day");
			    } else {
				    //this.SetCookie('xsDay', s, this.exp);
				    this.SetCookie('xsDay', s, this.exp, '/');
				    this.xshowSurvey();
				    //this.SetCookie('xsTaken', 0, this.exp);
				    this.SetCookie('xsTaken', 0, this.exp, '/');
			    }
		    }
	    } else {
		    if (sTaken == 0) {
			    if (sDay == s) {
			    //	alert("this is the same day2");
			    } else {
			        //this.SetCookie('xsDay', s, this.exp);
				    this.SetCookie('xsDay', s, this.exp, '/');
				    this.xshowSurvey();
			    }			
		    }
	    }
    },
    
    
    surveyComplete : function() {
	    //the survey is complete
	    //alert("the survey is complete");
	    var sTaken = this.GetCookie('sTaken');
	    //alert(sTaken);
	    if (sTaken == null) {
		    sTaken=1;
		    //this.SetCookie('sTaken', 1, this.exp);
		    this.SetCookie('sTaken', 1, this.exp, '/');
	    } else {
		    this.DeleteCookie('sTaken');
		    //this.SetCookie('sTaken', 1, this.exp);
		    this.SetCookie('sTaken', 1, this.exp, '/');
	    }
    },
    
    xsurveyComplete : function() {
	    //the survey is complete
	    //alert("the survey is complete");
	    var sTaken = this.GetCookie('xsTaken');
	    //alert(sTaken);
	    if (sTaken == null) {
		    sTaken=1;
		    //this.SetCookie('xsTaken', 1, this.exp);
		    this.SetCookie('xsTaken', 1, this.exp, '/');
	    } else {
		    this.DeleteCookie('xsTaken');
		    //this.SetCookie('xsTaken', 1, this.exp);
		    this.SetCookie('xsTaken', 1, this.exp, '/');
	    }
    },
    
    showSurvey : function() {
	    var imcsurveyWin = window.open(this.imcpage, "", this.imcwindowprops);
        if (imcsurveyWin != null) {
            imcsurveyWin.focus();
        }
    },
    
    xshowSurvey : function() {
	    var imcsurveyWin = window.open(this.ximcpage, "", this.imcwindowprops);
        if (imcsurveyWin != null) {
            imcsurveyWin.focus();
        }
    }

};


AHUtils.init();

window.onunload= function (evt) {
    //AHUtils.checkCount();
}