﻿/***************************************************************************/
/********************************************************************
* Helper
*
* Authors:
*  Gerald de Bueger (gerald@ldvproduction.be)
* 
* Copyright LDV Production 2010-2011
* http://www.ldvproduction.com
*
* DO NOT COPY OR USE WITHOUT PRIOR AGREEMENT FROM LDV Production
* 
*
********************************************************************/
/***************************************************************************/
var FrmMessage = "Please wait while we save your data";
var FrmTimeOut = 4000;
var iTimeoutId;


/**********************************************/
function ShowMessage(__Message) {
	var sMessage = __Message + '<br>';
	var _Btns = {};
	_Btns[GetVoc('sys_ok')] = function() { $(this).dialog("destroy"); };
	$("#AMGModal").html(sMessage);
	$("#AMGModal").dialog({
		autoOpen: true, modal: true, title: '', resizable: false,
		buttons: _Btns
	});
}
/**********************************************/
function DisplayError(__Message, __MessageType, __Prefix, __TimeOut) {
	__Message = __Message || '';
	__MessageType = __MessageType || '';
	__Prefix = __Prefix || '';
	__Message = __Message.replace(/(\r\n|[\r\n])/g, "<br />");
	var sBuf = '<DIV style="PADDING-BOTTOM: 0px; PADDING-LEFT: 0.7em; PADDING-RIGHT: 0.7em; PADDING-TOP: 0px" class="ui-state-error ui-corner-all">';
	sBuf += '<P><SPAN style="FLOAT: left; MARGIN-RIGHT: 0.3em" class="ui-icon ui-icon-alert"></SPAN><STRONG>' + __MessageType + '</STRONG> ' + __Message + '</P></DIV>';
	$('#ErrorMessage' + __Prefix).html(sBuf);
	$('#ErrorMessageRow' + __Prefix).show();
	if (__TimeOut) {
		setTimeout('RemoveError("'+__Prefix+'")', __TimeOut);
	}
}
function RemoveError(__Prefix) {
	$('#ErrorMessage' + __Prefix).html('');
	$('#ErrorMessageRow' + __Prefix).hide();
}
/**********************************************/
function DisplayAlert(__Message, __MessageType, __Prefix) {
	__Message = __Message || '';
	__MessageType = __MessageType || '';
	__Prefix = __Prefix || '';
	__Message = __Message.replace(/(\r\n|[\r\n])/g, "<br />");
	var sBuf = '<DIV style="PADDING-BOTTOM: 0px; PADDING-LEFT: 0.7em; PADDING-RIGHT: 0.7em; PADDING-TOP: 0px" class="ui-state-highlight ui-corner-all">';
	sBuf += '<P><SPAN style="FLOAT: left; MARGIN-RIGHT: 0.3em" class="ui-icon ui-icon-info"></SPAN><STRONG>' + __MessageType + '</STRONG> ' + __Message + '</P></DIV>';
	$('#AlertMessage' + __Prefix).html(sBuf);
	$('#AlertMessageRow' + __Prefix).show();
}
/**********************************************/
function MessageReplace(__Message, __LookFor, __ReplaceWith) {
	return __Message.replace(__LookFor, __ReplaceWith);
}
/**********************************************/
function GoToBack(__PageName) {
	var sUrl = __PageName || AMGPageContext.PageName + '';
	GoToUrl(sUrl);
}
/**********************************************/
function HighlightSet(__This) {
	$(__This).addClass('highlightedcolor');
}
function HighlightRemove(__This) {
	$(__This).removeClass('highlightedcolor');
}

/**********************************************/
function GetRegId() {
	try {
		//alert(ReadCookie('RegId') + ' - ' + GetRQ('RegId'));
		if (iRegId && iRegId > -1) {
			return iRegId
		}
		else {
			return GetRQ('RegId');
		}
	}
	catch (e) {
		return -1;
	}

}
function FixHexColor(__Color, __NeedHex) {
	if (__Color) {
		if (!__NeedHex) {
			__Color.replace('#', '');
		}
		else if (__Color.indexOf('#') != 0) {
			__Color = '#' + __Color;
		}
	}
	return __Color
}
/**********************************************/
/**************** VOC *************************/
/**********************************************/
function GetVoc(__Code) {
	try {
		return AryVocabulary[__Code] || '--';

	} catch (e) {
		return '--';
	}
}
function GetSysVoc(__Code) {
	try {
		return AryVocabulary[__Code] || '--';

	} catch (e) {
		return '--';
	}
}

/**********************************************/
/************ QUERYSTRING *************************/
/**********************************************/
var g_QueryString = {};
$(function() {
	var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&;=]+)=?([^&;]*)/g,
        d = function(s) { return decodeURIComponent(s.replace(a, " ")); },
        q = window.location.search.substring(1);
	while (e = r.exec(q))
		g_QueryString[d(e[1]).toLowerCase()] = d(e[2]);

	CheckDebug();
}
);
function GetJsonToQueryString(__Object, __Sep) {
	var sBuffer = '';
	for (property in __Object) {
		sBuffer += property + '=' + __Object[property] + __Sep
	}
	return sBuffer;
}
/**********************************************/
function GetQS(__Key) {
	if ((__Key + '').length > 0) {
		__Key = __Key.toLowerCase();
		return g_QueryString[__Key];
	}
	else {
		return '';
	}
}
/**********************************************/
function AddQueryString(__Key, __Value, __Query) {
	__Query = __Query || window.location.search;
	var q = __Query + "&";
	var re = new RegExp("[?|&]" + __Key + "=.*?&");
	if (!re.test(q))
		q += __Key + "=" + __Value;
	else
		q = q.replace(re, "&" + __Key + "=" + __Value + "&");
	q = q.trimStart("&").trimEnd("&");
	return (q.indexOf("?") == 0) ? q : q = "?" + q;
}
/**********************************************/
/************ CAPTURE KEYPRESS *************************/
/**********************************************/
$(function() {
	try {
		$('input').keypress(function(e) {
			try {
				var _Code = (e.keyCode ? e.keyCode : e.which);
				if (_Code == 13) {
					return false;
				}
			}
			catch (e) {
			}
		});
	}
	catch (e) {

	}
});
function GetKeyCode(__Key) {
	if (__Key == null) {
		keycode = event.keyCode;
		// To Mozilla
	} else {
		keycode = __Key.keyCode;
	}
	return keycode;
}
/**********************************************/
function GetKeyStroke(__Key) {
	return String.fromCharCode(GetKeyCode(__Key)).toLowerCase();
}
/**********************************************/
//$(document).keypress(function(__Key) {
//	LogDebug(1, "Keypress: The key is: " + GetKeyStroke(__Key) + ' with KeyCode : ' + GetKeyCode(__Key));
//	alert($(document).keypress)
//});
/**********************************************/
function ExtendKeyCapture(__ExtendedMethod) {
	$(document).keypress(__ExtendedMethod);
}
/**********************************************/
/**********************************************/
String.prototype.trimEnd = function(c) {
	if (c)
		return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
	return this.replace(/\s+$/, '');
}
String.prototype.trimStart = function(c) {
	if (c)
		return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
	return this.replace(/^\s+/, '');
}
String.prototype.escapeRegExp = function() {
	return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};
function CopyClipboard(__Text) {
	//alert(__Text)
	try {
		window.clipboardData.setData('Text', __Text);
	}
	catch (e) {
		alert(e.description)
	}

}
/***************************************************************************/
function ValidateFormClear() {
	$("#AMGModal").dialog("destroy");
	clearTimeout(iTimeoutId);
}
/***************************************************************************/
function ModalResult(__Response, __Message, __TimeOutDelay) {
	__Message = __Message || '';
	__TimeOutDelay = __TimeOutDelay || 1500;
	if (__Response) {
		$("#AMGModal").html(__Message + '<br><br><center><img src="/Pix/Icon/Success.png"></center>');
	}
	else {
		$("#AMGModal").html(__Message + '<br><br><center><img src="/Pix/Icon/Failed.png"></center>');
	}
	setTimeout('ValidateFormClear()', __TimeOutDelay);
}
/***************************************************************************/
function ClearModal() {
	$("#AMGModal").dialog("destroy");
}
function InitSaving(__Message) {
	$("#AMGModal").html(__Message + '<br><br><center><img src="/Pix/waiting.gif"></center>');
	$("#AMGModal").dialog({
		autoOpen: true, modal: true, title: 'Saving', resizable: false
		//,buttons: _Btns
	});
}
function IsEmail(__Val) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
	return emailPattern.test(__Val);

}
/***************************************************************************/
/***************************************************************************/
function GoToUrl(__Url) {
	try {
		__Url = __Url || document.location.href;
		//alert('__Url : ' + __Url)
		document.location.href = __Url;
	}
	catch (e) {
		alert(e.description)
	}
}
function Reload() {
	document.location.href = document.location.href;
}
/***************************************************************************/
/***************************************************************************/
function OpenLdv(__Options) {
	try {
		var oWin = window.open('http://www.ldvproduction.com', 'LDV');
		oWin.focus();
	}
	catch (e) {

	}
}
/***************************************************************************/
/***************************************************************************/
function Popup(__Options) {
	try {
		var Options = {};
		Options = $.extend(Options, __Options, { menubar: 'no', resizable: 'yes', status: 'n', scrollbar: 'yes' });
		var oWin = window.open(Options.url, Options.name, "resizable=" + Options.resizable + ",menubar=" + Options.menubar + ", status=" + Options.status + ",scrollbar=" + Options.scrollbars + ", width=" + Options.width + ", height=" + Options.height + "");
		oWin.focus();
	}
	catch (e) {

	}
}
/***************************************************************************/
/***************************************************************************/
function OpenDownload(__File) {
	try {
		var oWin = window.open(__File, 'download', "resizable=yes,menubar=0, status=0,scrollbar=0, width=100, height=100");
		oWin.focus();
	}
	catch (e) {

	}
}
function MaximizeWindow() {
	window.moveTo(0, 0);
	if (document.all) {
		top.window.resizeTo(screen.availWidth, screen.availHeight);
	}
	else if (document.layers || document.getElementById) {
		if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = screen.availWidth;
		}
	}
}
/***************************************************************************/
/**********************************************/
function LogOut() {
	//alert(444)
	AjaxManager.CallJson({
		url: '/WS/Broadcast.asmx/LogOut',
		data: "{'Name':'Name'}",
		success: LogOutSuccess
	});
}
/**********************************************/
function LogOutSuccess(__Response) {
	if (__Response.StatusMessage == 'success') {
		document.location.href = document.location.href;
	}
	else {
		alert('We could not log you out')
	}
}
/***************************************************************************/
function ShowList() {
	$('#ContDetails').hide()
	$('#ContList').show()
}
function ShowDetails() {
	$('#ContDetails').show()
	$('#ContList').hide()
}

/******************************************************************************/
function GetFormName(__Name) {
	var oForm = document.forms[0];
	var iLength = __Name.length + 1;
	for (var i = 0; i < oForm.length; i++) {
		try {
			var iIndex = oForm.elements[i].name.indexOf('$' + __Name);
			var iIndex2 = oForm.elements[i].getAttribute('id').indexOf('_' + __Name);
			if ((iIndex > -1 && iIndex == (oForm.elements[i].name.length - iLength)) || (iIndex2 > -1 && iIndex2 == (oForm.elements[i].getAttribute('id').length - iLength))) {
				//LogDebug(1, "i : " + oForm.elements[i].getAttribute('id'))
				return ReplaceAll(oForm.elements[i].name, '\\$', '_', true)
			}
		}
		catch (e) { }
	}
}
/******************************************************************************/
function GetIdName(__Name) {
	var oForm = document.forms[0];
	var iLength = __Name.length + 1;
	//LogDebug(1, __Name + " - " + iLength)
	for (var i = 0; i < oForm.length; i++) {
		//var iIndex = oForm.elements[i].name.indexOf('$' + __Name);
		var iIndex2 = oForm.elements[i].getAttribute('id').indexOf('_' + __Name);
		//LogDebug(1, iIndex2 + " - " + oForm.elements[i].getAttribute('id').length  + " - " + oForm.elements[i].getAttribute('id'))
		if (iIndex2 > -1 && iIndex2 == (oForm.elements[i].getAttribute('id').length - iLength)) {
			//LogDebug(1, oForm.elements[i].getAttribute('id'))
			return ReplaceAll(oForm.elements[i].getAttribute('id'), '\\$', '_', true)
		}
	}
}
//********************************************************
function ReplaceAll(__sText, __sToReplace, __sToReplaceWith, __bCaseSensitive) {
	if (__sToReplace)
		return __sText.replace(new RegExp(__sToReplace, (__bCaseSensitive == true) ? 'g' : 'ig'), __sToReplaceWith);
	return __sText
}

/******************************************************************************/
function two(x) { return ((x > 9) ? "" : "0") + x }
function three(x) {
	return (
		(x > 99) ? "" : (x > 9) ? "0" : "00") + x
}
/******************************************************************************/
function time(__Seconds) {
	var sBuffer = '';
	sBuffer = two(__Seconds % 60);

	if (__Seconds >= 60) {
		var Minute = Math.floor(__Seconds / 60);
		if (Minute >= 60) {
			sBuffer = Math.floor(Minute / 60) % 60 + ":" + two(Minute % 60) + ":" + sBuffer;
		}
		else {
			sBuffer = (Minute % 60) + ':' + sBuffer;
		}
	}
	return sBuffer
}
/******************************************************************************/
function GetTimeNiceFromSeconds(__Seconds, __ShowMilliSeconds) {
	var _LocalSec = Math.abs(__Seconds);
	if (!isNaN(_LocalSec)) {
		_LocalSec = parseInt(_LocalSec);
		var iSec = (_LocalSec % 60);
		_LocalSec = _LocalSec - iSec;
		var iMin = (_LocalSec / 60) % 60;
		_LocalSec = _LocalSec - (iMin * 60);
		var iHours = parseInt(_LocalSec / (3600));
		var _Ms = (__ShowMilliSeconds) ? ":000" : "";
		if (__Seconds < 0) {
			return "-" + two(iHours) + ":" + two(iMin) + ":" + two(iSec) + _Ms
		}
		else {
			return two(iHours) + ":" + two(iMin) + ":" + two(iSec) + _Ms
		}
	}
	else {
		var _Ms = (__ShowMilliSeconds) ? ":000" : "";
		return "00:00:00" + _Ms;
	}

	//return sBuffer
}
/******************************************************************************/
function GetTimeNiceFromMilliSeconds(__MilliSeconds, __ShowMilliSeconds) {
	if (__MilliSeconds) {
		var __Seconds = (__MilliSeconds - (__MilliSeconds % 1000)) / 1000;
		if (__ShowMilliSeconds) {
			return GetTimeNiceFromSeconds(__Seconds, false) + ":" + three(__MilliSeconds % 1000);
		}
		else {
			return GetTimeNiceFromSeconds(__Seconds, false);
		}
	}
	else {
		var _Ms = (__ShowMilliSeconds) ? ":000" : "";
		return "00:00:00" + _Ms;
	}

	//return sBuffer
}
function GetMilliSecondsFromTimeNice(__TimeNice, __HasMilliSeconds) {
	//LogDebug(1, __TimeNice)
	var AryValues = __TimeNice.split(':');
	var iLen = AryValues.length;
	var iTemp;
	if (iLen == 1) {
		return AryValues[0] * 1
	}
	else if (iLen == 2) {
		return AryValues[0] * 1000 + AryValues[1] * 1;
	}
	else if (iLen == 3) {
		return (AryValues[0] * 60 * 1000) + (AryValues[1] * 1000) + AryValues[2] * 1;
	}
	else if (iLen == 4) {
		return (AryValues[0] * 60 * 60 * 1000) + (AryValues[1] * 60 * 1000) + (AryValues[2] * 1000) + AryValues[3] * 1;
	}
	else {
		return 0;
	}
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
function DateFromString(__DateString, __Format) {
	return new Date(__DateString);
}
function DateFormat(format, date) {
	if (date == undefined) {
		date = new Date();
	}
	if (typeof date == 'number') {
		time = new Date();
		time.setTime(date);
		date = time;
	} else if (typeof date == 'string') {
		date = new Date(date);
		alert(date)
	}
	var fullYear = date.getYear();
	if (fullYear < 1000) {
		fullYear = fullYear + 1900;
	}
	var hour = date.getHours();
	var day = date.getDate();
	var month = date.getMonth() + 1;
	var minute = date.getMinutes();
	var seconde = date.getSeconds();
	var milliSeconde = date.getMilliseconds();
	var reg = new RegExp('(d|m|Y|H|i|s)', 'g');
	var replacement = new Array();
	replacement['d'] = day < 10 ? '0' + day : day;
	replacement['m'] = month < 10 ? '0' + month : month;
	replacement['Y'] = fullYear;
	replacement['Y'] = fullYear;
	replacement['H'] = hour < 10 ? '0' + hour : hour;
	replacement['i'] = minute < 10 ? '0' + minute : minute;
	replacement['s'] = seconde < 10 ? '0' + seconde : seconde;
	return format.replace(reg, function($0) {
		return ($0 in replacement) ? replacement[$0] : $0.slice(1,
				$0.length - 1);
	});
}
function dateFormat(format, date) {
	return DateFormat(format, date)
}
/******************************************************************************/
/******************************************************************************/
/******************************************************************************/
function GetObjectProperties(__Object, __ShowValue, __Return) {
	if (DebugLevel > 0) {
		var sBuffer = '';
		var sSep = (__Return) ? '<br>' : '\r\n';
		for (property in __Object) {
			sBuffer += property
			if (__ShowValue)
				sBuffer += ' : ' + __Object[property]
			sBuffer += sSep;
		}
		if (__Return) {
			return sBuffer
		}
		else {
			alert(sBuffer);
		}
	}
}
function DateDeserialize(dateStr) {
	return eval('new' + dateStr.replace(/\//g, ' '));
}

/******************************************************************************/
//0 The playback state is undefined.
//1 Playback is stopped.
//2 Playback is paused.
//3 The player is playing a stream.
//4 The player is scanning a stream forward.
//5 The player is scanning a stream in reverse.
//6 The player is buffering media.
//7 The player is waiting for streaming data.
//8 The player has reached the end of the media.
//9 The player is preparing new media.
//10 The player is ready to begin playback.
/*********************************/


function noclick(e) {
	if (navigator.appName == "Netscape" && e.which > 1) {
		return false
	}
	else if (navigator.appName == "Microsoft Internet Explorer" && (event.button > 1)) {
		return false;
	}
}
function InitNoClick() {
	if (DebugLevel == 0) {
		document.onmousedown = noclick;
		document.oncontextmenu = new Function("return false")
		if (document.layers) {
			window.captureEvents(Event.MOUSEDOWN);
		}
	}
}
/***************************************************************/
function CheckDebug() {
	if (GetQS('DebugLevel')) {
		DebugLevel = parseInt(GetQS('DebugLevel'));
	}
	//LogDebug(1, 'DebugLevel set to : ' + DebugLevel);
}
/***************************************************************/
function LogDebugObjectProperties(__Object, __ShowValue) {
	var sBuffer = '';
	for (property in __Object) {
		sBuffer += property
		if (__ShowValue)
			sBuffer += ' : ' + __Object[property]
		sBuffer += '<br>';
	}
	LogDebug(1, sBuffer)
}
/***************************************************************/
var DebugLevel = 10
var g_WinHandle = { Window: null, CurrentPage: null, ErrorLevel: DebugLevel };
function LogDebug(__Level, __Message, __Key) {
	try {
		//alert(g_WinHandle.Window + ' - ' + __Level)
		//alert(g_WinHandle.ErrorLevel + ' - ' + __Level)
		if (__Level <= DebugLevel) {
			if (!g_WinHandle.Window) { //we need to open a debug window
				//alert("Mess : " + window.open)
				var s = window.open("", "DebugWindow", 'scrollbars=1, resizable=1, width=450,height=' + screen.height);
				//window.open('Index.aspx?eId=1', "viewer");
				//alert(s)
				g_WinHandle.Window = s
				g_WinHandle.Window.moveTo((screen.width - 450), 0);
			}
			//alert(g_WinHandle.Window)

			//g_WinHandle.Window.resizeTo(650, screen.height);
			if (g_WinHandle.CurrentPage != GetPageName()) {
				g_WinHandle.Window.document.write('<hr sryle="border:2px;Color:#000000;">');
				g_WinHandle.Window.document.write('<font face="Arial" size="2pt" color="maroon"><b>' + GetPageName() + '</b><hr></font>');
				g_WinHandle.CurrentPage = GetPageName();
			}
			//alert(new Date())
			var bShowAsIs = (typeof asIs != "undefined") ? asIs : false;
			var str = "<font face='Arial' size='2pt' color='Navy'>";
			if (__Key) {
				str += "<b>" + __Key + "</b> : ";
			}
			str += __Message + "</font><hr>";
			if (bShowAsIs) {
				str = "<font face='Arial' size='2pt' color='Navy'>";
				str += (__Message) + "<br></font>";
			}
			$('#InlineDebug').html(str + $('#InlineDebug').html());
			g_WinHandle.Window.document.write(str);
			var winWidth = g_WinHandle.Window.document.body.clientWidth;
			var winHeight = g_WinHandle.Window.document.body.clientHeight;
			//g_WinHandle.Window.window.scrollTo(0, winHeight * 10000);
		}
	}
	catch (e) {
		//alert('LogDebug : ' + e.descripiton)
	}
}
var g_PressedKey = '';
var g_PressedKeyTimeout = null;
function ClearKeyCombination() {
	g_PressedKey = '';
	g_PressedKeyTimeout = null;
}
$.ctrl = function(key, callback, args) {
	$(document).keydown(function(e) {
		if (!args) args = [];
		var _Code = (e.keyCode ? e.keyCode : e.which);
		g_PressedKey += GetKeyStroke(e);
		if (g_PressedKey == key) {
			callback.apply(this, args);
			return false;
		}
		if (!g_PressedKeyTimeout) {
			g_PressedKeyTimeout = setTimeout('ClearKeyCombination()', 1000);
		}
	})
};



/***************************************************************/
function GetPageName() {
	var sPath = window.location.pathname;
	if (sPath.indexOf('/') >= 0) {
		return sPath.substring(sPath.lastIndexOf('/') + 1);
	}
	else {
		return sPath.substring(sPath.lastIndexOf('\\') + 1);
	}
}



/*******************************************************
** ******************************************************
** ******************************************************
COOKIE RELATED FUNCTIONS
** ******************************************************
** *****************************************************/

/*******************************************************
This escape the cookie value for compatibility with server side
*******************************************************/
function EscapeCookie(vValue) {
	return vValue;
	var bGoOn = true;
	while (bGoOn == true) { if (vValue.indexOf('+') != -1) { vValue = vValue.substring(0, vValue.indexOf('+')) + '~~' + vValue.substring(vValue.indexOf('+') + 1, vValue.length) } else { bGoOn = false; } } bGoOn = true;
	while (bGoOn == true) { if (vValue.indexOf('&') != -1) { vValue = vValue.substring(0, vValue.indexOf('&')) + '``' + vValue.substring(vValue.indexOf('&') + 1, vValue.length) } else { bGoOn = false; } } bGoOn = true;
	while (bGoOn == true) { if (vValue.indexOf('=') != -1) { vValue = vValue.substring(0, vValue.indexOf('=')) + '**' + vValue.substring(vValue.indexOf('=') + 1, vValue.length) } else { bGoOn = false; } } bGoOn = true;
	while (bGoOn == true) { if (vValue.indexOf(' ') != -1) { vValue = vValue.substring(0, vValue.indexOf(' ')) + '+' + vValue.substring(vValue.indexOf(' ') + 1, vValue.length) } else { bGoOn = false; } }
	return vValue
}
/*******************************************************
This read a cookie
*******************************************************/
function ReadCookie(sKey, sSubKey, bUnescape) {
	try {
		var sReturnValue = '';
		var sCookie = document.cookie.toLowerCase();
		var sSearchFor = "; " + sKey.toLowerCase() + "=";
		if (sCookie.length > 0) { // if there are any cookies
			var iOffset = (sCookie.indexOf(sSearchFor) != -1) ? (sCookie.indexOf(sSearchFor)) + sSearchFor.length : -1;
			if (iOffset == -1) {
				sSearchFor = sKey.toLowerCase() + "=";
				iOffset = (sCookie.indexOf(sSearchFor) != -1) ? (sCookie.indexOf(sSearchFor)) + sSearchFor.length : -1;
			}
			if (iOffset != -1) {
				var iEnd = (sCookie.indexOf(";", iOffset) != -1) ? sCookie.indexOf(";", iOffset) : sCookie.length;
				sReturnValue = unescape(sCookie.substring(iOffset, iEnd));
				if (sSubKey) {//if subkey, search for the value
					sSearchFor = sSubKey.toLowerCase() + "="
					var iKeyOffset = (sReturnValue.indexOf(sSearchFor) != -1) ? (sReturnValue.indexOf(sSearchFor)) + sSearchFor.length : -1;
					if (iKeyOffset != -1) {
						var iKeyEnd = (sReturnValue.indexOf("&", iKeyOffset) != -1) ? sReturnValue.indexOf("&", iKeyOffset) : sReturnValue.length;
						sReturnValue = unescape(sReturnValue.substring(iKeyOffset, iKeyEnd))
					}
					else {
						sReturnValue = '';
					}
				}
			}
		}
		if (bUnescape != 'no') {
			var bGoOn = true; while (bGoOn == true) { if (sReturnValue.indexOf('+') != -1) { sReturnValue = sReturnValue.substring(0, sReturnValue.indexOf('+')) + ' ' + sReturnValue.substring(sReturnValue.indexOf('+') + 1, sReturnValue.length) } else { bGoOn = false; } }
			bGoOn = true; while (bGoOn == true) { if (sReturnValue.indexOf('~~') != -1) { sReturnValue = sReturnValue.substring(0, sReturnValue.indexOf('~~')) + '+' + sReturnValue.substring(sReturnValue.indexOf('~~') + 2, sReturnValue.length); } else { bGoOn = false; } }
			bGoOn = true; while (bGoOn == true) { if (sReturnValue.indexOf('``') != -1) { sReturnValue = sReturnValue.substring(0, sReturnValue.indexOf('``')) + '&' + sReturnValue.substring(sReturnValue.indexOf('``') + 2, sReturnValue.length) } else { bGoOn = false; } }
			bGoOn = true; while (bGoOn == true) { if (sReturnValue.indexOf('**') != -1) { sReturnValue = sReturnValue.substring(0, sReturnValue.indexOf('**')) + '=' + sReturnValue.substring(sReturnValue.indexOf('**') + 2, sReturnValue.length) } else { bGoOn = false; } }
		}
		return sReturnValue;
	}
	catch (e) {
		alert(e.description);
	}
}
/*
jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
var expires = '';
if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
var date;
if (typeof options.expires == 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
}
// CAUTION: Needed to parenthesize options.path and options.domain
// in the following expressions, otherwise they evaluate to undefined
// in the packed version for some reason...
var path = options.path ? '; path=' + (options.path) : '';
var domain = options.domain ? '; domain=' + (options.domain) : '';
var secure = options.secure ? '; secure' : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
};
*/
/*******************************************************
Delete cookie*******************************************************/
function DeleteCookie(sKey, sSubKey) {
	SaveCookie(sKey, "", sSubKey, -1)
}
/*******************************************************
This put a cookie on the client machine
*******************************************************/
function SaveCookie(sKey, vValue, sSubKey, sExpire, sDomain, sPath, bSecure) {
	if (!sKey || vValue == 'undefined' || vValue == null) {
		alert('Missing information'); return;
	}
	var sCookieValue = ReadCookie(sKey, null, 'no');
	var sNewValue = EscapeCookie(vValue);
	var sNewCookie;
	if (sSubKey) {
		if (sCookieValue.indexOf('=') != -1) {
			if (ReadCookie(sKey, sSubKey, 'no')) {
				var sSearchFor = sSubKey.toLowerCase() + "="
				var iKeyOffset = sCookieValue.indexOf(sSearchFor) + sSearchFor.length - 1;
				var iKeyEnd = (sCookieValue.indexOf("&", iKeyOffset) != -1) ? sCookieValue.indexOf("&", iKeyOffset) : sCookieValue.length;
				sNewValue = sCookieValue.substring(0, iKeyOffset + 1) + sNewValue + sCookieValue.substring(iKeyEnd, sCookieValue.length)
			}
			else {
				sNewValue = sCookieValue + "&" + sSubKey.toLowerCase() + "=" + sNewValue;
			}
		}
		else {
			sNewValue = sSubKey.toLowerCase() + "=" + sNewValue;
		}
	}
	sNewCookie = sKey.toLowerCase() + "=" + sNewValue + ";"
	var iDayExpires = (sExpire) || 30;
	var dDate = new Date();
	dDate.setTime(dDate.getTime() + (iDayExpires * 24 * 60 * 60 * 1000))
	sNewCookie += "expires=" + dDate.toGMTString() + ";";
	if (sDomain) {
		if (sDomain.indexOf(".") == sDomain.lastIndexOf(".")) {
			sDomain = "." + sDomain
		}
		sNewCookie += "domain=" + sDomain + ";";
	}
	sPath = sPath || '/';
	if (sPath)
		sNewCookie += "path=" + sPath + ";";
	if (bSecure)
		sNewCookie += "secure=" + bSecure + ";";
	document.cookie = sNewCookie
}
