<!--Pohan's Cookie Code-->
<!--V2.0 Revised By Mark Farahani on 4-22-2004-->
<!--V3.0 Revised By Mark Farahani on 7-26-2004-->
<!--V3.2 Revised By Mark Farahani on 8-11-2004-->
<!--Updated Version Includes:-->
<!--	Multiple TrackID cookie	[MTIDC]	-	Saves up to 7 IDs, ID-1 being permanent while others cycle out	-->
<!--	Original TrackID cookie	[STIDS]									-->



var YouWrote			=	-1;		//store STIDC value if found
var found_TID			=	0;		//boolean (0/1) if STIDC is found
var cookie_name			=	"WebExCookie";	//STDIC cookie name
var TrackIDStr			=	-1;		//TID from query string
var cookie_nameNew		=	"WebExMTID";	//MTDIC cookie name
var YouEntered			=	123;
var today				=	new Date();
var expires			=	new Date(today.getTime() + (365 * 86400000));	//STDIC exp
var ExpYear			=	new Date(today.getTime() + (365 *86400000));		//MTDIC exp


//OrigTrackID	=	T0
//TrackID	=	T6/TrackIDStr

function DelThanks(){
	
	Delete_Cookie(cookie_nameNew);
	
}

function start_off(){
	//alert(Export());
	//alert(Get_Cookie(cookie_name));
	//Get TrackID from query string / URL
	TrackIDStr = getQueryVariable("TrackID");

	
	//alert("loading at "+new Date() +"\n\nTrack ID = "+TrackIDStr);

	if(! TrackIDStr){
		if(window.gotrackid){
			TrackIDStr = gotrackid;
		}
	}

	//Add to MTDIC if URL TID found
	if(TrackIDStr){
		if(TrackIDStr.length<1)
		{ 
			found_TID = 0;
		} 
		if(isNaN(TrackIDStr))
		{
			found_TID = 0;
		}
		else{
			
			if(document._mktf){			//added error check to avoid JS warning 8.11.04
			
				if(document._mktf.TrackID){			//added error check to avoid JS warning 8.11.04
					document._mktf.TrackID.value = TrackIDStr;
				}
			}
			AddID(eval(TrackIDStr));
			found_TID = 1;
		}
	}

	//Get original cookie information
	YouWrote=getName();
	
	//if no URL TID, ADD defualt value or value of original cookie
	if(found_TID == 0)
	{
		AddID("000000");
	}

	//if we're on a submit page, fill in the appropiate field
	if(document._mktf){			//added error check to avoid JS warning 8.11.04			
		if(document._mktf.TrackID2){
			document._mktf.TrackID2.value = Export();
			/*if(document._mktf.TrackID2.value == "null"){
				document._mktf.TrackID2.value = "000000";
			}*/
		}
	}
}


function testIsValidObject(objToTest) {
	if (null == objToTest) {
		return false;
	}
	if ("undefined" == typeof(objToTest) ) {
		return false;
	}
	return true;
}


//ON LOAD FUNCTION
//MUST BE CALLED using BODY TAG
//OR with JS INCLUDE code
function OnLoadFn()
{
	
	if (getQueryVariable("TrackID")) {
	

			//self.status = document._mktf.Thanks;
		if(document._mktf){			//added error check to avoid JS warning 8.11.04
			if(document._mktf.Thanks){			//Broke into 2 if statements to avoid JS warning 8.11.04
				if((document._mktf.Thanks.value=="DEL")){
					DelThanks();
				}
			}
			else{
				DoTheRest();
			}
		}
		else
		{
			DoTheRest();

		}
	}
}


function DoTheRest()
{


		start_off();
	
		//alert("I got :"+YouWrote+"\nName="+cookie_name);
		
		if(YouWrote != -1) 
		{
			//alert("Cookie found, use cookie value");
			//document._mktf.TrackID.value = YouWrote;
		}
		else 
		{
			//alert("No cookie or cookie expired, use URL or default value");
		
			if(TrackIDStr == "") {
				TrackIDStr = 2;
			}
			if(isNaN(TrackIDStr)) {
				//alert("NOT DIGIT IN TID");
				TrackIDStr = 2;
			}
			Set_Cookie(cookie_name, TrackIDStr,expires,"/");
			//alert(cookie_name+"++"+TrackIDStr+"++"+expires+"++"+"/");
		}


}


function getName() 
{
	var index = -1;

	if(document.cookie)
	{
		index = Get_Cookie(cookie_name);
		//alert(index+"\n"+document.cookie);
		if (index)
		{
			if(index.length<1)
			{
			//	alert("Cookie value not found"); 
				return -1;
			} 
			if(isNaN(index))
			{
			//	alert("NOT DIGIT IN COOKIE");
				return -1;
			}
			else
			{
			//	alert("DIGIT IN COOKIE");
				return index;
			}
		}	
	}
	return -1;
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  //alert('Query Variable ' + variable + ' not found');
}


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));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
	
var dom_array=top.location.host.split(".");
dom_array.splice(0,1);
var domStr = dom_array.join(".");
if (domStr.length == 0)	{
	domain = '.webex.com';

}
else {
	domain = domStr;	
}
path = '/';
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
	
var dom_array=top.location.host.split(".");
dom_array.splice(0,1);
var domStr = dom_array.join(".");
if (domStr.length == 0)	{
	domain = '.webex.com';
}
else {
	domain = domStr;
}
path = '/';
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function AddID(tID)
{
	//alert("adding : " +tID);
	var cookie_tID			=	new Array();
	var cookie_times		=	new Array();
	var cookie_timesLCK		=	new Array();
	var cookie_Count		=	new Array();
	var cookie_val;
	var cookie_exist;
	var i,j;
	var thisCookie, tempStr, TIDdate;
	var maxLength			=	7;
	var startLp			=	0;
	var cookie_date			=	today;
	var thisDate, oldestDate, lastDate;
	var oldestJ;
	var existsAt			=	-1;
	
	cookie_exist = Get_Cookie(cookie_nameNew);

	//Set master cookie tID if no cookie found
	if (cookie_exist == null) {
		cookie_val = "T0="+tID+"|"+cookie_date+"|"+cookie_date+"|"+"1"+"";
		Set_Cookie(cookie_nameNew, cookie_val,expires,"/");
	}
	//Get old cookie info and add new tID
	else
	{
		//cookie_val = cookie_exist + "&"+"T0="+tID+"|"+cookie_date+"";
		cookie_olds = cookie_exist.split("&");

		//Get the Track IDs into an array
		for (i = 0;i<cookie_olds.length;i++){
			tempStr = new String(cookie_olds[i]);
			thisCookie = tempStr.split("=");
			TIDdate = thisCookie[1].split("|");
			cookie_tID[i]=TIDdate[0];
			cookie_timesLCK[i] = TIDdate[1];
			cookie_times[i]=TIDdate[2];
			cookie_Count[i]=eval(TIDdate[3]);
		}
		
		//see if track ID exists and update date_stamp
		for(j=0;j<i;j++){
			if(cookie_tID[j]==tID){
				existsAt=j;
			}
		}
		if(existsAt!=-1){
			cookie_tID[existsAt] = tID;
			cookie_times[existsAt] = today;
			cookie_Count[existsAt] += 1;
		}
		else{
			//If limit is reached find oldest value and replace
			if (i >= maxLength){
				for(j=1;j<=i;j++){
					thisDate = new Date(cookie_times[j]);
					if(j==1){
						oldestDate = new Date(cookie_times[j]);
						oldestJ = j;
					}
					else{
						if(thisDate.getTime() < oldestDate.getTime()){
							oldestDate = thisDate;
							oldestJ = j;
						}
					}
				}
				cookie_tID[oldestJ] = tID;
				cookie_times[oldestJ] = today;
				cookie_timesLCK[oldestJ] = today;
				cookie_Count[oldestJ] = 1;
			}
			//Else add the new id at the end
			else{
				cookie_tID[i] = tID;
				cookie_times[i] = today;
				cookie_timesLCK[i] = today;
				cookie_Count[i] = 1;
				i += 1;
			}
		}
		//Set cookie again
		cookie_val = "";
		for(j=0;j<i;j++){
			if(j==0){
				cookie_val += "T"+j+"="+cookie_tID[j]+"|"+cookie_timesLCK[j]+"|"+cookie_times[j]+"|"+cookie_Count[j]+"";
			}
			else{
				cookie_val += "&"+"T"+j+"="+cookie_tID[j]+"|"+cookie_timesLCK[j]+"|"+cookie_times[j]+"|"+cookie_Count[j]+"";
			}
		}
		Set_Cookie(cookie_nameNew, cookie_val,expires,"/");	

	}



}


function Export()
{
	var cookie_tID			=	new Array();
	var cookie_times			=	new Array();
	var cookie_timesLCK		=	new Array();
	var cookie_Count			=	new Array();
	var cookie_val			=	"";
	var cookie_exist;
	var i,j;
	var thisCookie, tempStr, TIDdate;
	var maxLength			=	7;
	var startLp			=	0;
	var cookie_date			=	today;
	var thisDate, oldestDate, lastDate;
	var oldestJ;
	var existsAt			=	-1;
	
	cookie_exist = Get_Cookie(cookie_nameNew);

	//return 0 if no cookie
	if (cookie_exist == null) {
		return 0;
	}
	//Get old cookie info and re-format
	else
	{
		cookie_olds = cookie_exist.split("&");

		//Get the Track IDs into an array
		for (i = 0;i<cookie_olds.length;i++){
			tempStr = new String(cookie_olds[i]);
			thisCookie = tempStr.split("=");
			TIDdate = thisCookie[1].split("|");
			cookie_tID[i]=TIDdate[0];
			cookie_timesLCK[i] = TIDdate[1];
			cookie_times[i]=TIDdate[2];
			cookie_Count[i]=eval(TIDdate[3]);
		}
		
		//update date format
		for(j=0;j<i;j++){
			cookie_timesLCK[j] = ToDate(cookie_timesLCK[j]);
		}	

		
		//update string and return
		for(j=0;j<i;j++){
			if(j==0){
				cookie_val += cookie_tID[j]+"="+cookie_timesLCK[j]+"|";
			}
			else{
				cookie_val += cookie_tID[j]+"="+cookie_timesLCK[j]+"|";
			}
		}
		//cookie_val += "|";		//Close with pipe as requested 8.11.04
	}

	//if we're on a submit page, fill an additional export field
	if(document._mktf){			//added error check to avoid JS warning 8.11.04
		if(document._mktf.OrigTrackID){
			document._mktf.OrigTrackID.value = cookie_tID[0];
		}
	}

	if(cookie_val == null || cookie_val == "null"){
		var todayIs = new Date();
		var expDD  = ToDate(todayIs);
		cookie_val = "000000="+expDD+"|";
	}

	var the_length=cookie_val.length;
	var last_char=cookie_val.charAt(the_length-1);
	if (last_char!="|"){
		cookie_val +="|";
	}

	//self.status = cookie_val;

	return cookie_val;

}


function ToDate(date){
	var TheDate = "";
	var ThisDate = new Date(date);
	var tempItem = "0";

	TheDate +=(ThisDate.getYear())+"-";

	tempItem = (ThisDate.getMonth()+1);
	tempItem +="";
	if(tempItem.length == 1) {
		tempItem = "0"+tempItem;
	}
	TheDate +=(tempItem)+"-";
	tempItem = 0;

	tempItem = (ThisDate.getDate());
	tempItem +="";
	if(tempItem.length == 1) {
		tempItem = "0"+tempItem;
	}
	TheDate +=(tempItem)+" ";
	tempItem = 0;


	tempItem = (ThisDate.getHours());
	tempItem +="";
	if(tempItem.length == 1) {
		tempItem = "0"+tempItem;
	}
	TheDate +=(tempItem)+":";
	tempItem = 0;

	tempItem = (ThisDate.getMinutes());
	tempItem +="";
	if(tempItem.length == 1) {
		tempItem = "0"+tempItem;
	}
	TheDate +=(tempItem)+":";
	tempItem = 0;


	tempItem = (ThisDate.getSeconds());
	tempItem +="";
	if(tempItem.length == 1) {
		tempItem = "0"+tempItem;
	}
	TheDate +=(tempItem);
	tempItem = 0;

	TheDate +=".001"
	
	
	return TheDate;
}


OnLoadFn();

