var kSettingsCookieName		= "Settings";
var kUserStateCookieName	= "State";
var kMessageListStateName	= "MLState"; 
var kNewUserCookie			= "NU";
var kRegCookieName			= "reginfo";

var kUserStateCookieVersion = "3";

window.defaultStatus = document.title;

var hex = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function stdToHex(code) { return code.toString(16); }
function safariToHex(code) {
	var d1 = code>>8;
	code -= d1<<8;
	var d2 = code>>4;
	code -= d2<<4;
	d1 = d1 == 0 ? "" : hex[d1];
	return d1+hex[d2]+hex[code];
}


function MyEncodeURIComponent(s) {
	var result = "";
	for (var i=0; i<s.length; i++) {
		var si = s.substr(i,1);
		var code = s.charCodeAt(i);
		if (si.search(/[0-9a-z-_.!~*'()]/i) != -1)
			result += si;
		else if (code == 32)
			result += "+";
		else
			result += (code <= 15 ? "%0" : "%")+(navigator.userAgent.indexOf("Safari") != -1 ? safariToHex : stdToHex)(code);
	}
	return result;
}

function MyDecodeURIComponent(s) {
	var result = "";
	for (var i=0; i<s.length; i++) {
		var si = s.substr(i,1);
		if (si != "%")
			result += si == "+" ? " " : si;
		else {
			result += String.fromCharCode(parseInt(s.substr(i+1,2),16));
			i += 2;
		}
	}
	return result;
}

if (eval("typeof(encodeURIComponent)") == "undefined") {
	encodeURIComponent = MyEncodeURIComponent;
	decodeURIComponent = MyDecodeURIComponent;
}

function GetServiceDomain()
{
	
	
	
	
	var hostName = document.location.hostname;
	if (hostName.indexOf("192.168.") == 0)
		return null;
	if (hostName.search(/localhost/) != -1)
		return null;
	
	
	if ((matchArray = hostName.match(/\.([^.]+\.[^.]+)$/)) != null)
		return matchArray[1];
	
	
	return null;
}


function GetCookie(sName)
{
  
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return aCrumb[1];
  }

  
  return null;
}






function GetCookieValue(cookie, setting, defaultValue)
{
	if (cookie == null)
		return null;
		
	
	var pattern = new RegExp(setting + ":([^&]+)");
	var match = pattern.exec(cookie);
	
	
	if (match != null)
		return match[1];
	else
		return ((defaultValue != null) ? defaultValue : null);

}


function GetCookieValueInt(cookie, setting, defaultValue)
{
	var valString = GetCookieValue(cookie, setting);
	if ((valString == null) || (valString == ""))
		return defaultValue;
		
	return parseInt(valString, 10);
}



function SetCookieValue(cookie, setting, value)
{
	if (cookie == null) {
		
		cookie = setting + ":" + value;
	} else {
		
		
		var pattern = new RegExp(setting + ":([^&]+)&");
		cookie = cookie.replace(pattern,"");
		
		cookie = cookie + setting + ":" + value;	
	}
	cookie += "&";
	return cookie;
}



function SetCookie(sName, sValue, dExpiration, path, domain)
{
	var cookieString = sName + "=" + sValue;
	if (dExpiration != null)
		cookieString += "; expires=" + dExpiration.toGMTString();
	if ((path != null) && (path != ""))
		cookieString += "; path=" + path;
	if ((domain != null) && (domain != ""))
		cookieString += "; domain=" + domain;
		
	document.cookie = cookieString;
}


function DeleteCookie(sName)
{
	var date = new Date(1980, 1, 1, 0, 0, 0, 0);
	SetCookie(sName, "", date, "/");
	SetCookie(sName, "", date);
}

var pageHRef = document.location.href;

function GetPageLeaf() {
	var index = pageHRef.lastIndexOf("/");
	if (++index == 0)
		return;
	
	var leaf = pageHRef.substr(index).toLowerCase();
	if (leaf == "" || leaf.substr(0,1) == "?")
		leaf = "."+leaf;
	return escape(leaf)+"&";
}

function CheckPageDirty() {
	var dirty = GetCookie("dp");
	if (dirty == null || dirty == "")
		return;
	
	var leaf = GetPageLeaf();
	if ((index = dirty.indexOf(leaf)) != -1) {
		dirty = dirty.substr(0,index)+dirty.substr(index+leaf.length);
		SetCookie("dp", dirty);
		document.location.reload();
	}
}

CheckPageDirty();
		
function SetPageDirty() {
	var leaf = GetPageLeaf();
	var dirty = GetCookie("dp");
	if (dirty == null)
		dirty = "";
	if ((index = dirty.indexOf(leaf)) == -1)
		SetCookie("dp", dirty+leaf);
}

function GetEntryPage() {
	var entryCookie = GetCookie("Entry");
	return decodeURIComponent(entryCookie);
}

function SetEntryPage() {
	if (GetCookie("Entry") != null)
		return;
	
	var domain = "";
	var hostName = document.location.hostname;
	if (hostName.search(/localhost/) != -1)
		domain = "";
	else if ((matchArray = hostName.match(/\.([^.]+\.[^.]+)$/)) != null)
		domain = matchArray[1];
		
	var page = document.location.href;
	var ref = document.referrer;
	
	var entry = page;
	if ((ref != null) && (ref != ""))
		entry += " (" + ref + ")";
		
	var cookieString = "Entry" + "=" + encodeURIComponent(entry);
	if (domain != "")
		cookieString += "; domain=" + domain;
	
	document.cookie = cookieString;
}

function IsLoggedIn(redirect) {
	if (GetCookie("Settings") == null)
	{
		if (redirect == true) {
			alert("You are no longer logged into Mailblocks.  Click OK, Log in, then retry the action");
			window.open("login.aspx","","",true);
			}
		return false;
	}
	else
		return true;
}
		
function CheckSettings() {
	
}
	
function GetUserName() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return "";
	var matchArray = settings.match(/un:([^&]+)/);
	if (matchArray == null)
		return "";
	return matchArray[1];
}
function GetUserDomain() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return "";
	var matchArray = settings.match(/ud:([^&]+)/);
	if (matchArray == null)
		return "";
	return matchArray[1];
}
function GetUserAddressInInputForm() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return "";
	var matchArray = settings.match(/ai:([^&]+)/);
	if (matchArray == null)
		return "";
	return matchArray[1];
}

function GetUserUID() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return "";
	var matchArray = settings.match(/uuid:([^&]+)/);
	if (matchArray == null)
		return "";
	return matchArray[1];
}
	
function GetMessagesPerPage() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return 25;
	var matchArray = settings.match(/mp:([^&]+)/);
	if (matchArray == null)
		return 25;
	return matchArray[1];
}


function GetStorageQuota() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return 12;
	var quota = GetCookieValue(settings,"msq");
	quota = (quota==null) ? 12:quota;	
	return parseInt(quota);
}


function GetStorageUsed() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return 0;
	var used = GetCookieValue(settings,"msu");
	used = (used==null) ? 0:used;
	return parseInt(used);
}

function GetComposeAddress() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return "";
	var matchArray = settings.match(/ca:([^&]+)/);
	if (matchArray == null)
		return "";
	return unescape(matchArray[1]);
}

function GetBaseDomain() {
	
	var matchArray;
	var ai = GetUserAddressInInputForm();
	if (ai != null && (matchArray = ai.match(/[^&@]+@([^&@]+)/)) != null)
		return matchArray[1];

	var hostName = document.location.hostname;
	var matchArray = hostName.match(/\.([^.]*\.[^.]*)/);
	if (matchArray != null)
		hostName = matchArray[1];
	return hostName;
}

function GetHomeServer() {
	var settings = GetCookie("Settings");
	if (settings == null)
		return "";
	var matchArray = settings.match(/hs:([^&]+)/);
	if (matchArray == null)
		return "";
	return matchArray[1];
}

function ShowAdsToUser()
{
	
	
	
	var settings = GetCookie("Settings");
	if ((settings != null) && (settings != ""))
		return (GetCookieValueInt(settings, "ssa", 0) == 1);
		
	
	
	
	
	var state = GetCookie(kUserStateCookieName);
	if ((state != null) && (state != ""))
		return (GetCookieValueInt(state, "ssa", 0) == 1);
	
	
	return false;
}

function createCookie(name, value, days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

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 c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}


