﻿

var _countDowncontainer = 0;
var _currentSeconds = 0;
var _flag = 0;

window.onload = WindowLoad;
function WindowLoad(event) {
    //alert('yes');
    testFunction();
}
function getCookie(name) {
    var sPos = document.cookie.indexOf(name + "=");
    var len = sPos + name.length + 1;
    if ((!sPos) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (sPos == -1) {
        return null;
    }
    var ePos = document.cookie.indexOf(';', len);
    if (ePos == -1) ePos = document.cookie.length;
    return unescape(document.cookie.substring(len, ePos));
}
function setCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    if (expires) {
        expires = expires * 1000 * 3600 * 24;
    }
    document.cookie = name + '=' + escape(value) + ((expires) ? ';expires=' + new Date(today.getTime()
            + expires).toGMTString() : '') + ((path) ? ';path=' + path : '') + ((domain) ? ';domain=' + domain : '') +
           ((secure) ? ';secure' : '');
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        setCookie(name, '', -30, path, domain);
    }
}
function testFunction() {
    //alert('No initial'); 
    var c = parseInt(getCookie('Value'));
    if (!c) {
        // alert('No initial Value\nSetting New Cookie\nValue : 0');
       // setCookie('Value', 310);
        var d = document.getElementById("ctl00_ContentPlaceHolder1_myval").value; //value is got from hidden value myval
       // alert(d);
       ActivateCountDown("timedisp", parseInt(d, 10)); //parseInt(roiCategory));
    }
    else {
        // alert('Stored Cookie\nValue : ' + c);
        // alert('Setting New Cookie\nValue : ' + (c + 1));
         setCookie('Value', c - 2); 
        ActivateCountDown("timedisp", c - 1);
    }
}



function ActivateCountDown(strContainerID,initialValue)
 {
    _countDowncontainer = document.getElementById(strContainerID);
    if (!_countDowncontainer)
     {
        alert("count down error: container does not exist: " + strContainerID +"\nmake sure html element with this ID exists"); return;
     }
     if (_flag == 0)
     {
        _flag = 1;
        SetCountdownText(initialValue);
        window.setTimeout("CountDownTick()", 1000);
     }
     else 
    {
        //SetCountdownText(_currentSeconds-1);
        //window.setTimeout("CountDownTick()", 1000);
    }
}
function CountDownTick()
{
    setCookie('Value', _currentSeconds);
    
//    if (_currentSeconds == 10)
//     {
//      //alert("Only 1 Minutes Remaining!");
//     }
     if (_currentSeconds == 0)
      {
          // alert("Transaction Time out!")
          deleteCookie('Value', path, domain);
          document.getElementById("timedisp").value = 0
          document.getElementById("ctl00_ContentPlaceHolder1_myval").value = 0
          var divHidePayment = document.getElementById("ctl00_ContentPlaceHolder1_lnkCompletePurchase");
          //document.getElementById("ctl00_ContentPlaceHolder1_trhidetimer").style.display = 'None';
          if (divHidePayment != null) {
              divHidePayment.style.display = 'None';
              document.getElementById("ctl00_ContentPlaceHolder1_divclose").className = "csHide";
              document.getElementById("ctl00_ContentPlaceHolder1_lblErrorMessage").innerHTML = "Sorry, your transaction has timed out. Please <a href='http://www.kyazoonga.com' class='start'>click here</a> to restart your buying process.";
              // document.getElementById("spanTimeOutP").innerHTML = "Sorry, your transaction has timed out. Please <a href='http://www.kyazoonga.com' class='start'>click here</a> to restart your buying process.";

              //            EmptyMe();
          }
          
          
         return(_currentSeconds);
    }
    SetCountdownText(_currentSeconds - 1);
    window.setTimeout("CountDownTick()", 1000);
}
function SetCountdownText(seconds)
{
    //store:
    _currentSeconds = seconds;
    //get minutes:
    var minutes = parseInt(seconds / 60); //shrink:
    seconds = (seconds % 60);
    //get hours:
    // var hours = parseInt(minutes / 60); //shrink:
   // minutes = (minutes % 60);
    //build text:
    var strText =  AddZero(minutes) + ":" + AddZero(seconds)+" MIN ";
    //var strText = AddZero(hours) + ":" + AddZero(minutes) + ":" + AddZero(seconds);
    //apply:
    _countDowncontainer.innerHTML = strText;
}
function AddZero(num)
{
    return ((num >= 0) && (num < 10)) ? "0" + num : num + "";
}




 
 
 

