function textSize(id)
{
	if (-1 < id && id < textSize.sizes.length)
	{
		document.cookie = "fontSize=" + id + "; domain=friendsofquinn.com; path=/;";
		location.reload();
	}
}
textSize.sizes = ["1em", "1.1em", "1.2em"];


window.document["cookies"] = (function()
{
	var items = [];

	if (/\;/.test(document.cookie))
	{
		items = document.cookie.replace(/^(.*?)\;?$/, "$1").split(/\;\s*/g);
		for (var i = 0, item = null; i < items.length; ++i)
		{
			try
			{
				item = items[i].split("=");
				if (item[0])
				{
					items[i] = {name:item[0], value:unescape(item[1])};
					items[item[0]] = items[i].value;
				}
			}
			catch (error)
			{
			}
		}
	}
	items.enabled = (function()
	{
		var cookie = toParam("ENABLED" + Math.random(), "ENABLED");
		var test = (function()
		{
			document.cookie = cookie;
		})();
		var result = -1 < document.cookie.indexOf(cookie);

		document.cookie = cookie + toParam("expires", toDate(-1));
		return (result);
	})();
	items.add = function(name, value, expiry, path, domain, secure)
	{
		document.cookie = toParam(name, escape(value)) + toParam("expires", toDate(expiry)) + toParam("path", path) + toParam("domain", domain) + (secure ? "secure;" : "");
		items[items.length] = {name:name, value:value};
		items[name] = value;
	}
	items.clear = function()
	{
		for (var i = 0, expiry = toParam("expires", toDate(-1)); i < items.length; ++i)
		{
			delete items[items[i].name];
			document.cookie = toParam(items[i].name, items[i].name) + expiry;
		}
		items.length = 0;
	}
	items.remove = function(name, path, domain)
	{
		var idx = 0;

		for ( ; idx < items.length; ++idx)
			if (items[idx].name == name)
				break;
		if (idx < items.length)
		{
			delete items[name];
			items = items.slice(0, idx).concat(items.slice(idx + 1));
			document.cookie = toParam(name, name) + toParam("path", path) + toParam("domain", domain) + toParam("expires", toDate(-1));
		}
	}
	items.toString = function()
	{
		return (document.cookie);
	}
	function toDate(value)
	{
		var date = new Date();

		if (!value)
			;
		else if (value.constructor == Date)
			date = value;
		else if (!isNaN(value))
			date.setDate(date.getDate() + parseInt(value, 10));
		return (date);
	}
	function toParam(name, value)
	{
		return (value ? name + "=" + value.toString() + ";" : "");
	}
	return (items);
})();

if (document.cookies["fontSize"])
{
	var id = parseInt(document.cookies["fontSize"], 10);

	if (isNaN(id) || id < 0 || textSize.sizes.length <= id)
		;
	else if (document.body)
		document.body.style.fontSize = textSize.sizes[id];
	else
	{
		var sheets = document.styleSheets;
		var sheet = null;
		var rules = -1;
		
		if (sheets)
			sheet = sheets[sheets.length ? sheets.length - 1 : 0];
		if (sheet)
			rules = sheet.cssRules ? sheet.cssRules.length : sheet.rules.length;
		if (rules < 0)
			;
		else if (typeof (sheet["insertRule"]) == "function")
			sheet.insertRule("body{font-size:" + textSize.sizes[id] + ";}", rules);
		else
			sheet.addRule("body", "font-size:" + textSize.sizes[id]);
	}
}

toggleMiniHome = function()
{
	if (document.cookies["membervert"] != "share")
	{
		document.getElementById('login_user_panel').style.display = 'block';
		document.getElementById('signed_in_user_panel').style.display = 'none';
	}
	else
	{
		addHandler (window, 'onload', function() {
			new Ajax.Updater('signed_in_user_panel', '/share/c/weblog/mini_home', {asynchronous:true, evalScripts:true});
		});
		
		document.getElementById('login_user_panel').style.display = 'none';
		document.getElementById('signed_in_user_panel').style.display = 'block';
	}	
}

function validateAndSubmitNewsletterSignup(email, confirmEmail, vertical, hcnupdates)
{
	if(email != '' && ValidMailAccount(email) && email == confirmEmail)
	{
		if(hcnupdates == true) { var hcnupdates_value = 'y'; } else { var hcnupdates_value = 'n'; }
		document.getElementById('foq_newsletter_module_thank_you').style.display = 'block';
		document.getElementById('foq_newsletter_module_form').style.display = 'none';
		
		// Do AJAX call
		var xhr = new XHR();
		try
		{
			xhr.request.open("get", "http://communities.friendsofquinn.com/share/c/newsletter_subscription/silent_subscribe/?email="+email+"&nls="+vertical+"&site="+vertical+"&hcnupdates="+hcnupdates_value, true);
			xhr.request.send();
		}
		catch (error)
		{
			alert(error);
		}
	}
	else
	{
		document.getElementById('foq_newsletter_module_error').style.display = 'block';
	}
	
	return false;
}

/*
    Function name   : ValidMailAccount
    Description     : To check the correctance of the email address written. 
    Input Variables : String Expression presenting the email address.
    Output Variables: Boolean flag ( True | False )
	Source			: /common/js/forms.js
*/

function ValidMailAccount(accname){
	acc = trim(accname);
	if (acc==''){return false}

	pos = acc.indexOf(' ');
	if (pos>=0){return false}

	pos = acc.indexOf('@');
	if (pos<0){return false}

	pos1 = acc.lastIndexOf('.');
	if (pos<0){return false}
	if (pos>pos1){return false}
	return true;

    var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9]{1,}[a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i;
    return objRegExp.test(strValue);

}

/*
    Function name   : trim
    Description     : To remove the left and right spaces
                      from a an input string.      
    Input Variables : String Expression
    Output Variables: String Expression
	Source			: /common/js/forms.js
*/
function trim(ss){
	if (ss.length<=0) {return('');}
	pos = ss.indexOf(' ');
	while (pos==0) {
		ss = ss.substr(1, ss.length-1);
		pos = ss.indexOf(' ');
	}
	pos = ss.charCodeAt(ss.length-1);
	while (pos==32) {
		ss = ss.substr(0, ss.length-1);
		pos = ss.charCodeAt(ss.length-1);
	}
	return(ss);
}


// This function is use by the Printer Friendly links in the right rail.
function getFoqPrinterFriendlyLink()
{
	var showLink = true;
	var pfURL = '';
	
	if (location.href.match(/.*\/c\/.*/))
	{
		// Check for SharePost Entry pages. Example: /breast-cancer/c/6863/3171/cancer
		if (location.href.match(/(.*\/c\/\d{1,10}\/\d{1,10}\/[^/]+)/))
		{
			pfURL = location.href.replace(/(.*\/c\/\d{1,10}\/\d{1,10}\/[^/?]+)\/?(.*)\/?/,'$1/pf/');
		}
		// Check for SharePost Profile pages. Example: /breast-cancer/c/6863/profiles/
		else if (location.href.match(/(.*\/c\/\d{1,10}\/profile[s]*)(.*)/))
		{
			pfURL = location.href.replace(/(.*\/c\/\d{1,10}\/profile[s]*)(.*)/,'$1/pf');
		}
		else if (location.href.match(/(.*\/c\/diaries\/\d{1,10})(.*)/))
		{
			pfURL = location.href.replace(/(.*\/c\/diaries\/\d{1,10})(.*)/,'$1/pf');
		}
		else
		{
			showLink = false;
		}
	}
	
	// It's a "regular"/non-SharePost page, so remove any _page#s.
	else
	{
		pfURL = location.href.replace(/(.*)[_][2-7]*\.html.*/, '$1');
		pfURL = pfURL.replace(/(.*).html.*/, '$1')+'_pf.html';
	}
	
	if (showLink)
	{
		var link = '<a href="' + pfURL + '" onclick="javascript:window.open(\''+pfURL+'\'); return false;" title="Printer friendly">Print</a>';
		document.write(link);
	}
}

