﻿// JScript File
var it = null;
var flagVisualizer=0;

if (!Get_Cookie('strTimeVisualizer')==null)
{
    flagVisualizer = parseInt(Get_Cookie('strTimeVisualizer'));
}

var t;
var timer_is_on=0;
var xmlHttp;

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
  	{
  	// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e)
  	{
  	// Internet Explorer
  		try
    	{
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

function closeit(strSesId) 
{
    //alert("it"+it);
    if (it != null)
    {
        if (!it.closed) it.close();
      	var strTime=Get_Cookie('strTimeVisualizer');
        clear();
      	xmlHttp=GetXmlHttpObject();
	    if (xmlHttp==null)
  	    {
  		    alert ("Your browser does not support AJAX!");
  		    return;
  	    } 
	    var url="ajaxproc_processtime.aspx?strSesId="+strSesId+"&strTime="+strTime;
	    url=url+"&sid="+Math.random();
	    xmlHttp.open("GET",url,true);
	    xmlHttp.send(null);
	    
	}
}

function openit(strSesId)
{
    //it = window.open('vztest_call.aspx');
    it = window.open('frmSndVis.aspx');
    doTimer(strSesId);
}

function timedCount(strSesId)
{
    if (!it.closed)
    {
        flagVisualizer=flagVisualizer+1;
        Set_Cookie('strTimeVisualizer',flagVisualizer.toString(),365);
        
        t=setTimeout("timedCount('"+strSesId+"')",1000);
    }
    else
    {
        stopCount(strSesId);
    }
}

function doTimer(strSesId)
{
    if (!timer_is_on)
    {
      timer_is_on=1;
      timedCount(strSesId);
    }
}

function stopCount(strSesId)
{
  clearTimeout(t);
  var strTime=Get_Cookie('strTimeVisualizer');
  clear();
  	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="ajaxproc_processtime.aspx?strSesId="+strSesId+"&strTime="+strTime;
	url=url+"&sid="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

//function getCookie(c_name)
//{
//if (document.cookie.length>0)
//  {
//  c_start=document.cookie.indexOf(c_name + "=");
//  //alert("c_start"+c_start);
//  if (c_start!=-1)
//    {
//    c_start=c_start + c_name.length+1;
//    c_end=document.cookie.indexOf(";",c_start);
//    if (c_end==-1) c_end=document.cookie.length;
//    return unescape(document.cookie.substring(c_start,c_end));
//    }
//    /*else
//    {
//    return "";
//    }*/
//  }
//return "";
//}

//function setCookie(c_name,value,expiredays)
//{
//var exdate=new Date();
//exdate.setDate(exdate.getDate()+expiredays);
//document.cookie=c_name+ "=" +escape(value)+
//((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
//}

/*function checkCookie()
{
username=getCookie('strTime');
if (username!=null && username!="")
  {
  alert('Welcome again '+username+'!');
  }
else
  {
  username=prompt('Please enter your name:',"");
  if (username!=null && username!="")
    {
    setCookie('username',username,365);
    }
  }
}*/

function clear()
{
	Set_Cookie('strTimeVisualizer',"0",365);
    timer_is_on=0;
    flagVisualizer=0;
    it = null;
}

//window.onunload=closeit();

function Get_Cookie(name) 
{ 
   var start = document.cookie.indexOf(name+"="); 
   var len = start+name.length+1; 
   if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
   if (start == -1) return null; 
   var end = document.cookie.indexOf(";",len); 
   if (end == -1) end = document.cookie.length; 
   return unescape(document.cookie.substring(len,end)); 
} 

// This function has been slightly modified
function Set_Cookie(name,value,expires,path,domain,secure) 
{ 
	expires = expires * 60*60*24*1000;
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
    document.cookie = cookieString; 
}
