var addLoadEvent;
 
if(window.addEventListener) {
addLoadEvent = function(func) {
window.addEventListener("load", func, false);
}
} else {
addLoadEvent = function(func) {
window.attachEvent("onload", func);
}
}



/* FOR PAGE TITLE CUSTOMIZATION */
function updatePreview() {
	var preview = document.getElementById('pageTitlePreviewer');
	var inputtxt = document.getElementById('inputPageTitle');
	preview.firstChild.nodeValue=inputtxt.value;
}

function setPageTitle() {
	if (!document.getElementById("inputPageTitle")) return false;
	var inputtxt = document.getElementById("inputPageTitle");
	if(document.getElementById('inputPageTitle').value!=document.getElementById('pageTitlePreviewer').firstChild.nodeValue) {
		updatePreview();
	}
}
addLoadEvent(setPageTitle);

function updateTitleColor(txtcolor) {
	var txt = document.getElementById('pageTitlePreviewer');
	txt.style.color = txtcolor;
	return false;
}

function clearColorSelections() {
	var colors = document.getElementById("colorselections");
	var choices = colors.getElementsByTagName("a");
	for (var i = 0; i < choices.length; i++) {
		choices[i].className="";
	}
}

function setTitleColor() {
	if (!document.getElementById("pageTitleColor")) return false;
	var titlecolor = document.getElementById("pageTitleColor");
	var colorselection = document.getElementById("colorselections");
	var links = colorselection.getElementsByTagName("a");
	for ( var i = 0; i < links.length; i++) {
		if (links[i].className == "on") {
			updateTitleColor(links[i].style.backgroundColor)
		}
		links[i].onclick = function() {
			clearColorSelections();
			updateTitleColor(this.style.backgroundColor);
			this.className = "on";
			titlecolor.setAttribute("value",this.title);
			return false;
		}
	}
}
addLoadEvent(setTitleColor);



/* For Template Choice */
/* ------------------- */
function clearChoices() {
	var list = document.getElementById("choicesList");
	var choices = list.getElementsByTagName("div");
	for (var i = 0; i < choices.length; i++) {
		choices[i].className="";
	}
}

function highlightChoice(template) {
	var business = document.getElementById("li-business");
	var wedding = document.getElementById("li-wedding");
	var leisure = document.getElementById("li-leisure");
	if (template == "li-business") {
		clearChoices();
		business.className = "on";
	}
	else if (template == "li-wedding") {
		clearChoices();
		wedding.className = "on";
	}
	else if (template == "li-leisure") {
		clearChoices();
		leisure.className = "on";
	}
}

function imageClick() {
	if (!document.getElementById("choicesList")) return false;
	var business = document.getElementById("li-business");
	var wedding = document.getElementById("li-wedding");
	var leisure = document.getElementById("li-leisure");
	var imgbusiness = business.getElementsByTagName("img");
	var imgwedding = wedding.getElementsByTagName("img");
	var imgleisure = leisure.getElementsByTagName("img");
	var inputbusiness = document.getElementById("radiobusiness");
	var inputwedding = document.getElementById("radiowedding");
	var inputleisure = document.getElementById("radioleisure");
	imgbusiness[0].onclick = function() {
		inputbusiness.checked = true;
		highlightChoice("li-business");
	}
	imgwedding[0].onclick = function() {
		inputwedding.checked = true;
		highlightChoice("li-wedding");
	}
	imgleisure[0].onclick = function() {
		inputleisure.checked = true;
		highlightChoice("li-leisure");
	}
}
addLoadEvent(imageClick);

/* so that if someone clicks on the actual radio and not the image, the effect still happens */
function prepareChoices() {
	if (!document.getElementById("choicesList")) return false;
	var business = document.getElementById("radiobusiness");
	var wedding = document.getElementById("radiowedding");
	var leisure = document.getElementById("radioleisure");
	business.onclick = function () {
		highlightChoice("li-business");
	}
	wedding.onclick = function () {
		highlightChoice("li-wedding");
	}
	leisure.onclick = function () {
		highlightChoice("li-leisure");
	}
}
addLoadEvent(prepareChoices);


/* FOR MASTHEAD SELECTION */
/* ---------------------- */
function showPic(whichpic) {
	var selectedMast = whichpic.getAttribute("value");
	var chosenbanner = document.getElementById("chosenbanner");
	selectedMast = "/en/groups/personalized/media/images/mastheads/custom/" + selectedMast;
	chosenbanner.setAttribute("src",selectedMast);
	return true;
}

function prepareMastheadPreview() {
	if (!document.getElementById("chosenbanner")) return false;
	if (!document.getElementById("mastheadoptions")) return false;
	var mastheads = document.getElementById("mastheadoptions");
	var inputs = mastheads.getElementsByTagName("input");
	for ( var i = 0; i < inputs.length; i++) {
		inputs[i].onclick = function() {
			return showPic(this);
		}
	}
}
addLoadEvent(prepareMastheadPreview);

function selectedMasthead() {
	if (!document.getElementById("mastheadoptions")) return false;
	var options = document.getElementById("mastheadoptions");
	var imgs = options.getElementsByTagName("img");
	for ( var i = 0; i < imgs.length; i++) {
		imgs[i].onclick = function() {
			var alts = this.getAttribute("alt");
			var desiredInput = document.getElementById(alts);
			desiredInput.checked = true;
			showPic(desiredInput);
		}
	}
}
addLoadEvent(selectedMasthead);

/* popup the search box */
function popUpSearchWidget(winURL) {
	window.open(winURL,"popup","resizable=1,location=0,toolbar=0,scrollbars=1,width=460,height=500");
}

function popUpSample(winURL) {
	window.open(winURL,"popup","resizable=1,location=0,toolbar=0,scrollbars=1,width=510,height=650");
}

function popUpHelp(winURL) {
	window.open(winURL,"popup","resizable=1,location=0,toolbar=0,scrollbars=1,width=400,height=400");
}

function prepareLinks() {
	if (!document.getElementsByTagName) return false;
	var lnks = document.getElementsByTagName("a");
	for (var i = 0; i < lnks.length; i++) {
		if (lnks[i].className == "popupSearch") {
			lnks[i].onclick = function() {
				popUpSearchWidget(this.getAttribute("href"));
				return false;
			}
		}
		if (lnks[i].className.indexOf("popupSample") != -1) {
			lnks[i].onclick = function() {
				popUpSample(this.getAttribute("href"));
				return false;
			}
		}
		if (lnks[i].className.indexOf("pagehelpa") != -1) {
			lnks[i].onclick = function() {
				popUpHelp(this.getAttribute("href"));
				return false;
			}
		}
	}
}
addLoadEvent(prepareLinks);

/* Highlights input boxes and sets focus to first input */
function initHighlight() {
    if (!document.getElementsByTagName) { 
    	return false;
    }
    var pogs = document.getElementById("pogc");
    var allfields = pogs.getElementsByTagName("input");
	var focused = false;
    // loop through all input tags and add events
	for (var i = 0; i < allfields.length; i++){
        var field = allfields[i];
		if ((field.getAttribute("type") == "text") || (field.getAttribute("type") == "password") ) {
            field.onfocus = function () {this.style.backgroundColor = "#f9f9d7";}
            field.onblur = function () {this.style.backgroundColor = "#ffffff";}
            // put focus on first field that is empty
            if ( !focused && field.value == "" ) {
            	field.focus();
				focused = true;
			}
        }
    }
    var allselects = pogs.getElementsByTagName("select");
    for (var j = 0; j < allselects.length; j++) {
        var field = allselects[j];
        field.onfocus = function () {this.style.backgroundColor = "#f9f9d7";}
        field.onblur = function () {this.style.backgroundColor = "#ffffff";}
    }
}
addLoadEvent(initHighlight);


/*
 * Used in the Event Description page to setup "eventDescText"
 * and track number of characters left.
 */
function prepareEventDescText() {
	if ( !document.getElementById("eventDescText") ) return false;
	var textarea = document.getElementById("eventDescText");
	trackCount(textarea, 2000);
	textarea.onkeypress = function () {
		trackCount(this, 2000);
	}
	textarea.onkeyup = function () {
		trackCount(this, 2000);
	}
	textarea.onkeydown = function () {
		trackCount(this, 2000);
	}
}
addLoadEvent(prepareEventDescText);

function setGuideLink() {
	if ( !document.getElementById("guideLink") || !document.getElementById("closeGuideText") || !document.getElementById("guideText") ) return false;
	var guidelink = document.getElementById("guideLink");
	var closeguidetext = document.getElementById("closeGuideText");
	var guidetext = document.getElementById("guideText");
	guideLink.onclick = function() {
		guidetext.style.display = "block";
	}
	closeguidetext.onclick = function() {
		guidetext.style.display = "none";
	}
       //add return false
	return false;
}
addLoadEvent(setGuideLink);

/*
 * Used in the Comments page to setup "commentsText"
 * and track number of characters left.
 */
function prepareCommentsText() {
	if ( !document.getElementById("commentsText") ) return false;
	var textarea = document.getElementById("commentsText");
	trackCount(textarea, 256);
	textarea.onkeypress = function () {
		trackCount(this, 256);
	}
}
addLoadEvent(prepareCommentsText);


function trackCount(textarea, maxChars) {
	// Need to check & enforce limit here also in case user pastes data
	if ( textarea.value.length > maxChars ) {
		textarea.value = textarea.value.substring(0,maxChars);
	} else {
		var element = document.getElementById("countdown");
		if( element != null ) {
			element.innerHTML = maxChars - textarea.value.length;
		}
	}
}


function decodeFunc()
{
	if(document.getElementById("CreateGrpNameBtn")!=null && typeof(document.getElementById("CreateGrpNameBtn"))!='undefined')
	{
		document.getElementById("CreateGrpNameBtn").disabled = true;
	}
	if(document.getElementById("CreateEventSubmitBtn")!=null && typeof(document.getElementById("CreateEventSubmitBtn"))!='undefined')
	{
		document.getElementById("CreateEventSubmitBtn").disabled = true;
	}
	if(typeof(document.forms.pogform)!='undefined')
	{
		if(typeof(document.forms.pogform.pageTitle)!='undefined')
		{
			var str = document.forms.pogform.pageTitle.value;
			if(str.indexOf("&#")<0)
			{
				str = unescape(str);
				document.forms.pogform.pageTitle.value = str;
				document.getElementById('pageTitlePreviewer').firstChild.nodeValue = str;
			}
			
		}
		if(typeof(document.forms.pogform.eventDescText)!='undefined')
		{
			var str = document.forms.pogform.eventDescText.value;
			str = unescape(str);
			str = changeSpecialChar(str);
			document.forms.pogform.eventDescText.value = str;
		}
	}
	if(document.getElementById("CreateGrpNameBtn")!=null && typeof(document.getElementById("CreateGrpNameBtn"))!='undefined')
	{
		document.getElementById("CreateGrpNameBtn").disabled = false;
	}
	if(document.getElementById("CreateEventSubmitBtn")!=null && typeof(document.getElementById("CreateEventSubmitBtn"))!='undefined')
	{
		document.getElementById("CreateEventSubmitBtn").disabled = false;
	}
}
addLoadEvent(decodeFunc);

function changeSpecialChar(pogString)
{
	pogString = pogString.replace("&reg;","%u00AE");
	pogString = pogString.replace("&trade;","%u2122");
	pogString = unescape(pogString);
	return pogString;
}

function checkAllowedCharacters(pogString)
{
	//var pogString = document.forms.pogform.pageTitle.value;
	var allowedChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
	var flag = false;
	for (var i = 0; i < pogString.length; i++) {
	if (allowedChars.indexOf(pogString.charAt(i)) != -1) 
	     {
		     flag=true;
	     }
	else
	     {	
		flag=false;     
		alert("Containts special characters. \n These are not allowed.\n Please remove them and try again.");
		return false;
	     }
	}
	if(flag)
	{
	     pogString.value = escape(pogString);
	     document.forms.pogform.submit();
	}
}


function checkAllowedCharactersToSubmitForm(pogStringHidden,pogString)
{					
     pogStringHidden.value = escape(pogString.value);
		
     var allowedChars = "!\"#$%&'()*+,-.\\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~“”™¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþ ";
     var flag = false;
     for (var i = 0; i < pogStringHidden.value.length; i++) {
	    if (allowedChars.indexOf(pogStringHidden.value.charAt(i)) != -1) 
	     {
		     flag=true;
	     }
	     else
	     {	
		flag=false;
		alert("Containts special characters. \n These are not allowed.\n Please remove them and try again.");
		return flag;	
	     }
     }
     if(flag)
     {
	  document.forms.pogform.submit();
     }
}
