
function getBase64Code(str, pos)
{
   var code = str.charCodeAt(pos);
   if ((code >= 65) && (code <= 90))  return code - 65;
   if ((code >= 97) && (code <= 122)) return code - 71;
   if ((code >= 48) && (code <= 57))  return code +  4;
   if (code == 43) return 62;
   if (code == 47) return 63;
   return 0;
}

function decodeBase64(str)
{
	var txt = "";
	for (var i = 0; i < str.length; i+=4)
	{
		var chs = [getBase64Code(str,i+0),getBase64Code(str,i+1),getBase64Code(str,i+2),getBase64Code(str,i+3)];
	   var tot0 = ((chs[0] << 2) + (chs[1] >> 4)) % 256;
	   var tot1 = ((chs[1] << 4) + (chs[2] >> 2)) % 256;
	   var tot2 = ((chs[2] << 6) + (chs[3] >> 0)) % 256;
	   txt += (tot0>0 ? String.fromCharCode(tot0):"") + (tot1>0 ? String.fromCharCode(tot1):"") + (tot2>0 ? String.fromCharCode(tot2):"");
	}
   return txt;
}

function getBase64Encode(code)
{
   if (code < 26) return String.fromCharCode(code + 65);
   if (code < 52) return String.fromCharCode(code + 71);
   if (code < 62) return String.fromCharCode(code - 4);
   if (code == 62) return String.fromCharCode(43);
   if (code == 63) return String.fromCharCode(47);
   return 0;
}

function encodeBase64(str)
{
	var txt = "";
	for (var i = 0; i < str.length; i+=3)
	{
	   var chs = [str.charCodeAt(i), (str.length > i+1) ? str.charCodeAt(i+1) : -1,  (str.length > i+2) ? str.charCodeAt(i+2) : -1];
	   var tot0 = (chs[0] >> 2) % 64;
	   var tot1 = (((chs[0] << 6) >> 2) + (chs[1] > 0 ? (chs[1] >> 4) : 0)) % 64;
	   var tot2 = (((chs[1] << 6) >> 4) + (chs[2] > 0 ? (chs[2] >> 6) : 0)) % 64;
	   var tot3 = ((chs[2] << 2) >> 2) % 64;

	   txt += getBase64Encode(tot0);
	   txt += getBase64Encode(tot1);
	   txt += (chs[1] > 0) ? getBase64Encode(tot2) : "=";
	   txt += (chs[2] > 0) ? getBase64Encode(tot3) : "=";
	}
   return txt;
}

function createCookie(name,value,days)
{
   if (!days) days = 30;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
   var thecookie = name + "=" + encodeBase64(value.toString()) + expires + "; path=/; domain=" + window.location.hostname;
	document.cookie = thecookie;
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return decodeBase64(c.substring(nameEQ.length,c.length));
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}
