﻿var secs
var timerID = null
var timerRunning = false
var delay = 1000
// default cookie expiration date to fifty years
var date = new Date();
var expYears = 50;
date.setTime(date.getTime() + (expYears * 365 * 24 * 60 * 60 * 1000));
// default score increment value in case of high value activity
var defaultScoreIncrementValue = 100
// Maximum score value for cookie
var maxScoreValue = 999
// Index of cookie score parameter to be updated in case of high value activity
var scoreCount = 5
// Cookie name
var cookieName = 'USPS_Visitor'
var addPage;
var addOffSecs;
var addONSecs = 30;



var secs1
var timerID1 = null
var timerRunning1 = false
var delay1 = 1000
// default cookie expiration date to fifty years
var date1 = new Date();
var expYears1 = 50;
date1.setTime(date.getTime() + (expYears1 * 365 * 24 * 60 * 60 * 1000));
// default score increment value in case of high value activity
var defaultScoreIncrementValue1 = 100
// Maximum score value for cookie

// Index of cookie score parameter to be updated in case of high value activity
var scoreCount1 = 5
// Cookie name

var addOffSecs1;
var addONSecs1 = 30;

//This fuction initializes the timer to check high value activity
function InitializeTimer() {
    // Set the length of the timer, in seconds
    secs = 60
    //StopTheClock()
    StartTheTimer()
}
function InitializeTimer1() {
    // Set the length of the timer, in seconds
    secs1 = 30
    //StopTheClock()
    StartTheTimer1()
}


//This fuction starts the timer to check high value activity
function StartTheTimer() {
    if (secs == 0) {
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        var c_value = myCookieValue(cookieName, scoreCount)
        //checking the value of score not to exceed 999
        if (parseInt(c_value) < maxScoreValue) {
            if (!(String(c_value) == null || String(c_value) == NaN || String(c_value) == "")) {
                //starting the timer for checking high value activity
                InitializeTimer();


                if (c_value != null) {
                    c_value = parseInt(c_value) + parseInt(defaultScoreIncrementValue);
                }
                else
                    c_value = parseInt(defaultScoreIncrementValue);
                //checking the value of score not to exceed 999
                if (parseInt(c_value) < maxScoreValue) {
                    myCookieHandler(cookieName, scoreCount, c_value)
                    //alert('new score is = ' + myCookieValue(cookieName, scoreCount))
                    //START 
                    //Added for Last Mile on page for calling the Ajax  (08_03_10)
                    // debugger;
                    // START OF LAST MILE ON PAGE
                    if (addPage != null && addPage != false) {
                        if (myCookieValue('USPS_Visitor', scoreCount) > 400) {

                            show_data(contentTopic, myCookieValue('USPS_Visitor', 0), myCookieValue('USPS_Visitor', scoreCount), 'Add');
                        }
                    }
                    //END
                }
                else {
                    myCookieHandler(cookieName, scoreCount, maxScoreValue)
                    //alert('new score is = ' + myCookieValue(cookieName, scoreCount))
                }
            }
        }
    }


    else {
        //self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }

}





function StartTheTimer1() {
    //debugger;

    if (secs1 == 0)

        InitializeTimer1()
    else {
        //self.status = secs1
        secs1 = secs1 - 1
        if (addONSecs1 > 0) {
            addONSecs1 = addONSecs1 - 1
        }
        else
            if (addONSecs1 == 0) {
                //debugger;    
                if (addPage != null && addPage != false) {

                    if (myCookieValue('USPS_Visitor', 3) == "0" || myCookieValue('USPS_Visitor', 3) == "Yes") {
                        show_popup();
                    }
                }
                addOffSecs1 = 20;
                addONSecs1 = -1;

            }


        timerRunning1 = true;
        timerID1 = self.setTimeout("StartTheTimer1()", delay);
    }
}

	
function UpdateCookie_FromUser() 
{
        get_radio_value();
        if(rad_val !=null && rad_val!="")
        {
            UpdateCookie_UserInput('USPS_Visitor',rad_val, rad_val_Sublevel,'Confirmed');
            show_data(contentTopic, rad_val,myCookieValue('USPS_Visitor', scoreCount),'RelatedLinks');
        }

        //document.getElementById("AddPopup").style.display="none";

         //show_data('Test','Federal123','500'); 
         //show_HowtoBuy('Federal','D.c');
        }
  

//This fuction will increase the score value by 100 in case of high value activity
function myCookieHandler(name, count, value) {
    //debugger;        
    if (document.cookie.length > 0) {
        var cookieValue = readCookie(name);
        var cookieValueArr = cookieValue.split(',');
        cookieValueArr[count] = value;
        cookieValue = cookieValueArr.toString();
        var expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + cookieValue + "; expires=" + expires + "; path=/";
    }
}

// This fuction returns score value from the USPS_Visitor cookie
function myCookieValue(name, count) {
    if (document.cookie.length > 0) {
        var cookieValue = readCookie(name);
        var cookieValueArr = cookieValue.split(',')
        return cookieValueArr[count];
    }
    else
        return null;
}

// This function is used to updated cookie value

function UpdateCookie(name, level, subLevel, campaignvalue) {
    //   debugger;
    if (document.cookie.length > 0) {
        var cookieValue = readCookie(name);
        var cookieValueArr = cookieValue.split(',');

        var temp = cookieValueArr[0];
        var levelArr = temp.toString().split('=');



        if (level != null) {
            cookieValueArr[0] = level.substring(0, 1);
        }

        if (subLevel == null) {


            cookieValueArr[1] = '0';

        }
        else {
            cookieValueArr[1] = subLevel;
        }
       
        if(campaignvalue != null)
        {
        	if(campaignvalue == 0)
        	{
	        	if(parseInt(cookieValueArr[5]) < 900)
	        	{
	        		cookieValueArr[5]= parseInt(cookieValueArr[5]) + 100;
	        	}
	        	else
	        	{
	        		cookieValueArr[5]= 999;
	        	}
	        }
	        else
	        {
	        	if(parseInt(cookieValueArr[5]) < 850)
	        	{
		        	cookieValueArr[5]=parseInt(cookieValueArr[5]) + 150;		
		        }
		        else
		        {
		        	cookieValueArr[5]= 999;
		        }
	        }	
      		cookieValueArr[2]=campaignvalue;
        }	
		
		
		
        cookieValue = cookieValueArr.toString();
        var expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + cookieValue + "; expires=" + expires + "; path=/";
    }
    //webtrackingradiobutton1(level);


}



// This Function is used to update the score entered by User
function UpdateCookie_UserInput(name, level, subLevel, askedSegment) {
    //   debugger;
    if (document.cookie.length > 0) {
        var cookieValue = readCookie(name);
        var cookieValueArr = cookieValue.split(',');

        var temp = cookieValueArr[0];
        var levelArr = temp.toString().split('=');



        if (level != null) {
            cookieValueArr[0] = level.substring(0, 1);
        }

        if (subLevel == null) {


            cookieValueArr[1] = '0';

        }
        else {
            cookieValueArr[1] = subLevel;
        }


        if (askedSegment != null) {

            cookieValueArr[3] = askedSegment;

        }
        else {
            cookieValueArr[3] = 'Confirmed';

        }


        cookieValue = cookieValueArr.toString();
        var expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + cookieValue + "; expires=" + expires + "; path=/";
    }
    //webtrackingradiobutton1(level);


}
function UpdateCookie_UserInputGovIT(name, level, subLevel, askedSegment) {
    //   debugger;
    if (document.cookie.length > 0) {
        var cookieValue = readCookie(name);
        var cookieValueArr = cookieValue.split(',');

        var temp = cookieValueArr[0];
        var levelArr = temp.toString().split('=');



        if (level != null) {
            cookieValueArr[0] = level.substring(0, 1);
        }

        if (subLevel == null) {


            cookieValueArr[1] = '0';

        }
        else {
            cookieValueArr[1] = subLevel;
        }


        if (askedSegment != null) {

            cookieValueArr[2] = askedSegment;

        }
        else {
            cookieValueArr[2] = 'Confirmed';

        }


        cookieValue = cookieValueArr.toString();
        var expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + cookieValue + "; expires=" + expires + "; path=/";
    }
   // webtrackingradiobutton1(level);


}

function webtrackingradiobutton1(tag) {
    //debugger;
    dcsMultiTrack('WT.seg_1', tag);
}





// This function is used to create a cookie
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = ";";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

// This fuctions reads the cookie value
function readCookie(name) {
    var nameEQ = name + "=";
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        while (cookie.charAt(0) == ' ') {
            cookie = cookie.substring(1, cookie.length);
        }
        if (cookie.indexOf(nameEQ) == 0) {
            return cookie.substring(nameEQ.length, cookie.length);
        }
    }
    return null;
}

// This fuction is used to remove the existing cookie
function eraseCookie(name) {
    createCookie(name, "", -1);
}  


