// Startup Functions
function init() {
	changeFontSize(getSelectedFontSizeFromCookie()); // set font size	
}
window.onload = init;

/* parse the entered query String */
function parseSearchQuery(theForm, message) {
	var queryValue = theForm.elements["query2"].value;
	var testValue = queryValue.replace(/ /g, "");
	if (testValue.length < 3) {
		alert(message);
		return (false);
	}
	theForm.elements["query"].value = queryValue;
	return (true);
}

/* open the print version popup window */
function openPrintVersion() {
	var params = "print=true";
	if (document.location.search != "") {
		params = document.location.search + "&" + params;		
	} else {
		params = "?" + params;
	}

        if(document.location.pathname.substring(0, 5) == "/app/") {
		window.open(document.location.pathname + params, "print", "width=807,height=750,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes");
	} else {
		window.open("/app"+document.location.pathname + params, "print", "width=807,height=750,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes");
	}
}

/* open the imprint popup window */
function openImprint(imprintUri, pageUri, theLocale, site) {
	pageUri = encodeURIComponent(pageUri); 
	pageUri = "?__locale=" + theLocale + "&uri=" + pageUri + "&site=" + site;
	window.open(imprintUri + pageUri, "imprint", "width=670,height=550,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes");
}

/* open the recommend page form popup window */
function openRecommendForm(recommendUri, pageUri, theLocale, site) {	
	if (window.location.search != "") {
		pageUri += window.location.search;		
	}
	pageUri = encodeURIComponent(pageUri); 
	pageUri = "?__locale=" + theLocale + "&uri=" + pageUri + "&site=" + site;
	window.open(recommendUri + pageUri, "recommend", "width=670,height=700,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes");
}

/* open the recommend page form popup window */
function openLetterForm(letterUri, pageUri, theLocale, site) {	
	if (window.location.search != "") {
		pageUri += window.location.search;		
	}
	pageUri = encodeURIComponent(pageUri); 
	pageUri = "?__locale=" + theLocale + "&uri=" + pageUri + "&site=" + site;
	window.open(letterUri + pageUri, "contact", "width=670,height=700,dependent=yes,status=no,toolbar=no,location=no,scrollbars=yes,resizable=yes");
}

 
// Create Cookie
function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ";path=/"; 
}

// Get Cookie Value
function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

// Updates font size selection menu
function updateFontSizeMenu() {
	
	var currentFontSize = getSelectedFontSizeFromCookie(); // get current font size from cookie		
	var menu = "";	
		
	// Create Menu
	menu += '<a href="javascript:setDefaultFontSize(\'small\');" title="Small/Petit"><img id="small_textIcon" src="/app/system/modules/ca.greenridge.focalpoint.electionsmb/resources/images/smText_'+ ((currentFontSize == "small") ? "on" : "off") + '.gif" class="textSizeIcon" alt="Small/Petit"></a>';
	menu += '<a href="javascript:setDefaultFontSize(\'medium\');" title="Medium/Moyen"><img id="medium_textIcon" src="/app/system/modules/ca.greenridge.focalpoint.electionsmb/resources/images/medText_'+ ((currentFontSize == "medium") ? "on" : "off") +'.gif" class="textSizeIcon" alt="Medium/Moyen"></a>';
	menu += '<a href="javascript:setDefaultFontSize(\'large\');" title="Large/Grand"><img id="large_textIcon" src="/app/system/modules/ca.greenridge.focalpoint.electionsmb/resources/images/bgText_'+ ((currentFontSize == "large") ? "on" : "off") +'.gif" class="textSizeIcon" alt="Large/Grand"></a>';	

	// Output Menu to Screen
	document.getElementById('fontSizeMenu').innerHTML = menu;

}

// Get currently selected font size
function getSelectedFontSizeFromCookie() {

	// Retrieve current font size from cookie
	var fontSizeFromCookie = getCookie('fontSize');
		
	// If cookie doesn't exist, current fonsize is Small (default)
	if (fontSizeFromCookie!=null && fontSizeFromCookie!="") {
		return fontSizeFromCookie;
	}
	else {
		return "small";
	}
	
}

// Sets font size for page
function setDefaultFontSize(size) {
	setCookie('fontSize',size,365); // update cookie with selected font size
	changeFontSize(size); // override CSS with select font size
	updateFontSizeMenu(); // update menu
}

// Override CSS with global font size selected by user
function changeFontSize(size) {
   
	var oldClasses = eval("/large|medium|small/"); //sets key words to be eliminated
	var currentClass = document.body.className; //gets the current class names
	document.body.className = currentClass.replace(oldClasses, "") + " " + size; //eliminates key words from string, then adds new size
	
		var elementId="";
		if (window.frames.length > 0){	
		elementId = document.getElementById("app_iframe_no_javascript");
		var $jelementId = $('#app_iframe_no_javascript');
		if (size == "small")
	    	 {			 
			 	
			//var iframeElement = (elementId.contentWindow || elementId.contentDocument); 
	        	//if(iframeElement.document) iframeElement = iframeElement.document;
			    $jelementId.contents().find('body').css("font-size", "11px");
				$jelementId.contents().find('table').css("font-size", "11px");
						
			 }
			 
		else if(size =="medium")
	    	 {			 
			//var iframeElement = (elementId.contentWindow || elementId.contentDocument); 
	        	//if(iframeElement.document) iframeElement = iframeElement.document;
			    $jelementId.contents().find('body').css("font-size", "13px");	
				$jelementId.contents().find('table').css("font-size", "13px");
							 			
			 }
				
		else {			 
			 //var iframeElement = (elementId.contentWindow || elementId.contentDocument); 
	        	//if(iframeElement.document) iframeElement = iframeElement.document;			
				$jelementId.contents().find('body').css("font-size", "16px");
				$jelementId.contents().find('table').css("font-size", "16px");
				//window.frames[1].document.getElementById('table1').style.fontSize="24px";											
			 }}
   
}



