﻿// JScript File

var _cookieName		= "TheTimes";
var _cookieDomain	= "thetimes.co.za";


function getUserID()
{
	return document.getElementById("ctl00_hdnUserID").value;
}

function getUserName()
{
	return document.getElementById("ctl00_hdnUserName").value;
}

// =============================================================================
// Open popup window
// =============================================================================
function PopTastic(url, name, height, width)
{
	newwindow=window.open(url, 'name', 'height=' + height + ',width=' + width + ',left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,status=yes');
	
	if (window.focus) 
	{
		newwindow.focus();
	}
}
	
// =============================================================================
// get cookie value by key
// =============================================================================
function getCookieValue(key)
{
	var cookieValue = getCookieData(_cookieName);	
	
	if (cookieValue == null)
	{
		return "";
	}
	
	// to lower case
	key = key.toLowerCase();
	
	// check if key exists
	var iStart = cookieValue.indexOf("{" + key + "|");
	var iEnd = 0;
	
	if (iStart == -1) // key doesn't exist
	{
		return "";
	}
	
	
	// get value
	var tempValue = cookieValue.substring(iStart + key.length + 2, cookieValue.length);
	iEnd = tempValue.indexOf("}");
	return tempValue.substring(0, iEnd);
}

// =============================================================================
// set cookie value
// Storing format {key|value}{key|value}
// =============================================================================
function setCookieValue(key, val)
{
	var cookieValue = getCookieData(_cookieName);
	
	if (cookieValue == null)
	{
		cookieValue = "";
	}
	else
	{
		// check if old type cookies?
		if (cookieValue.indexOf("{") == -1)
		{
			cookieValue = "";
		}
	}
	
	// to lower case
	key = key.toLowerCase();
	
	// check if key exists
	var iStart = cookieValue.indexOf("{" + key + "|");
	var iEnd = 0;
	
	if (iStart == -1) // key doesn't exist
	{
		cookieValue += "{" + key + "|" + val + "}";
	}
	else
	{
		// get values before key
		var tempFront = cookieValue.substring(0, iStart);
		
		// get values after key	
		var tempBack = cookieValue.substring(iStart, cookieValue.length);				
		iEnd = tempBack.indexOf("}");
		tempBack = tempBack.substring(iEnd + 1, tempBack.length);
		
		// add updated key
		cookieValue = tempFront + tempBack + "{" + key + "|" + val + "}";
		
		// test
		//alert(cookieValue);
	}
	
	setCookieData(_cookieName, cookieValue, 30);	
}



// ==========================================
// Retrieve Cookie Data
// ==========================================
function getCookieData(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));
}	


// ================================
// Set cookie Data
// ================================
function setCookieData(name, value, days)
{
	if (days == null)
	{
		days = 30;
	}
	
	var exp = new Date();
	var oneYear = exp.getTime() + (days * 12 * 60 * 60 * 1000); // 12 hours from now
	exp.setTime(oneYear);
	
	document.cookie = name + "=" + escape(value) + "; expires=" + exp.toGMTString() + ";";
	
	//alert('cookie data set');
}

// =============================================================================
// Get query string value
// =============================================================================
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].toLowerCase() == variable.toLowerCase())
		{
			return pair[1];
		}
	}
}