/****************************************************************/
/***  SUPPORTING FUNCTIONS									  ***/
/****************************************************************/


// ---------------------------------------------------------------------
// string.trim (our own language addition)
// ---------------------------------------------------------------------
if (String.prototype.trim == null) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/, '');
	};
}

//NEW FORM
function displayFromSelectedValue(selectField, valueSelected,idItem, idField) {

	if(selectField.value == valueSelected) {
		document.getElementById(idItem).style.display = "block";
	} else {
		document.getElementById(idItem).style.display = "none";
		document.getElementById(idField).value = "";
	}
}
//RADIO FORM
function displayItem(idItem) {
	if(document.getElementById(idItem).style.display == "none") {
		document.getElementById(idItem).style.display = "block";
	}
}
function hideItem(idItem) {
	if(document.getElementById(idItem).style.display == "block") {
		document.getElementById(idItem).style.display = "none";
	}
}


// REMOVE ERROR CLASS
function RemoveErrorClass (strNodeName, strClassName)
{
	RemoveClass(document.getElementById(strNodeName), strClassName);
}


function AddClass (theNode, theClass) {
	if (theNode.className == '') {
		theNode.className = theClass;
	} else {
		theNode.className += ' ' + theClass;
	}
}

function RemoveClass (theNode, theClass) {
	var oldClass = theNode.className;
	var regExp = new RegExp('\\s?\\b'+theClass+'\\b');
	if (oldClass.match(regExp) != null) {
		theNode.className = oldClass.replace(regExp,'');
	}
}

// Show/Hide a DIV using the classes ".show" & ".hide"
function ShowHide(sDIVID)
{
	var sClass = "";
	
	switch (document.getElementById(sDIVID).className)
	{
		case "hide":
			sClass = "show";
			break;
		default:
			sClass = "hide";
			break;
	}
	document.getElementById(sDIVID).className = sClass;
}



/****************************************************************/
/***  LIBRARY FUNCTIONS										  ***/
/****************************************************************/


var maxLen,errMsg;invChars="" ///\W/;  //global default settings
function initCount(ident,maxChars,displayId,chkStr){ // init settings
  taObj=document.getElementById(ident);maxLen=maxChars;
  errMsg="A maximum of " + maxLen + " characters are allowed for School description.  Any text above this has been deleted.";
  if(chkStr!==""){invChars=chkStr;}
  if(chkStr.toLowerCase()==="nocheck"){invChars="";}
  if(displayId.toLowerCase()==="noshow"){return;} // suppress display
  dispObj=document.getElementById(displayId);
  dispObj.innerHTML=maxLen-taObj.value.length;
}
function taCount(ident,displayId){
  taObj=document.getElementById(ident);
  taLength=taObj.value.length; // look at current length
  if(taLength>maxLen){ // clip characters
    taObj.value=taObj.value.substring(0,maxLen);alert(errMsg);}
  taLength=taObj.value.length;oldLength=0;
  dispObj=document.getElementById(displayId);
  dispObj.innerHTML=(maxLen-taObj.value.length);
}



// Open new window in full-screen mode
function OpenFullScreenWindow(url, options) {
  var winOptions = '';
  switch (options)
  {
    case 'scroll':
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no, scrollbars=yes';
      break;
      
    default:
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no';
  }
  
  var newWin = window.open(url, 'newWindow', winOptions + ", height=" + screen.height + ", width=" + screen.width);
  newWin.moveTo(0,0);
  newWin.focus();
  return false;
}

function OpenPopupWindow(sURL, sWidth, sHeight)
{
    // Open Popup window
    var objNewWin = window.open(sURL, "_blank", "directories=no, menubar=no, status=no, toolbar=no, scrollbars=yes" + ", height=" + sHeight + ", width=" + sWidth);
    // Centre Popup window - watch the security issue if sURL is on a seperate domain!
    try
    {
        objNewWin.moveTo(Math.round((screen.width - parseInt(sWidth)) / 2), Math.round((screen.height - parseInt(sHeight)) / 2));
    }
    catch(e){}
    
    objNewWin.focus();
    return false;
}


// HELP hide/show code
function ShowHelp (sHelpID, iXPos, iYPos)
{
	document.getElementById(sHelpID).style.marginTop = iXPos + 'px';
	document.getElementById(sHelpID).style.marginLeft = iYPos + 'px';
	document.getElementById(sHelpID).style.display = 'block';
}

function HideHelp (sHelpID)
{
	document.getElementById(sHelpID).style.display = 'none';
}

// Print current page
function PrintPage()
{
	window.print();
}

// Highlight field background (for forms)
function PlaceFocus(sItemID, bHighlight)
{
	var oItem = document.getElementById(sItemID);
	var sHighlightClass = "highlightItem";

	if (bHighlight)
	{
		//alert("before className - " + oItem.className);
		oItem.className += " " + sHighlightClass;
		//alert("after className - " + oItem.className);
	}
	else
	{
		var iHighlighClassPos = oItem.className.indexOf(sHighlightClass);
		//oItem.className = oItem.className.substring(0, iHighlighClassPos - 2) + oItem.className.substr(iHighlighClassPos + iHighlighClassPos.length + 1);
		oItem.className = oItem.className.substring(0, iHighlighClassPos - 1);
		//alert("className - " + oItem.className);
	}
}


// ---------------------------------------------------------------------
// Cookies
// ---------------------------------------------------------------------

 /**
 * @name cc.cookie.set
 * @desc Set a cookie
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Hash options A set of key/value pairs for optional cookie parameters.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 * If set to null or omitted, the cookie will be a session cookie and will not be retained
 * when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 * require a secure protocol (like HTTPS).
 */
 function SetCookie (name, value, options) {
	options = options || {};
	var expires = '';

	options.expires = options.expires || 356;

	var date = new Date();
	date.setTime(date.getTime()+(options.expires*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();

	var path = options.path ? '; path=' + options.path : '';
	var domain = options.domain ? '; domain=' + options.domain : '';
	var secure = options.secure ? '; secure' : '';
	document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
 }

 /**
 * @name cc.cookie.get
 * @desc Get the value of a cookie
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 */
 function GetCookie (name) {
	var cookieValue = null;
	if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = cookies[i].trim();
			// Does this cookie string begin with the name we want?
			if (cookie.substring(0, name.length + 1) == (name + '=')) {
				cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
				break;
			}
		}
	}
	return cookieValue;
 }

 /**
 * @name cc.cookie.remove
 * @desc Delete a cookie
 * @param String name The name of the cookie.
 */
 function DeleteCookie (name) {
	SetCookie(name, "", {expires: -1});
 }




// ---------------------------------------------------------------------
// text resizing controls
// ---------------------------------------------------------------------
$(document).ready(function(){

initFontSizeControls();
function initFontSizeControls() {
 var maxFontSize = 160; // %
 var minFontSize = 80; // %
 function incrementFontSize(sizeDelta) {
 var newSize = parseFloat(GetCookie("font-size")) + sizeDelta;
 if (newSize > minFontSize && newSize < maxFontSize) {
 setBodyFontSize(newSize);
 SetCookie("font-size", newSize, {path: "/"});
 }
 }

 function setBodyFontSize(size) {
 $("#content").css("font-size", size + "%");
 }

 var fontSize = GetCookie("font-size");
 var fontIncrement = 8.33; // %

 if (!fontSize) {
 SetCookie("font-size", "100", {path: "/"}); // set the default if there' no value
 fontSize = GetCookie("font-size"); // read it back to make sure cookies are supported
 }

 // check we can support this functionality
 if (window.print && fontSize) {
	setBodyFontSize(fontSize);
 
 // attach font down handler
 $("#fontSizeDownControl").click(function() {
 incrementFontSize(-fontIncrement);
 });

 // attach font up handler
 $("#fontSizeUpControl").click(function() {
 incrementFontSize(fontIncrement);
 });

 // attach hover handler so we can style cursor with a hand on mouseover
 $("#font_size_button img").hover(
 function() { AddClass(this, "hover"); },
 function() { RemoveClass(this, "hover"); }
 );
 }
}
});
