//	Manning School for Girls | default.js

function include(script_filename)  
{
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

include("/jscript/ajax.js"); 

function get_int_attribute(vElement, vAttribute, vDefault)
{
	var vVal = 	parseInt(vElement.getAttribute(vAttribute));
	if (isNaN(vVal))
		return vDefault;
	return vVal;
}

function htmlEntities(str) 
{// Added MSF 2011-02-14, used in XML Parser
    return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
}

function fnChangeClass(elementId, newClass)
{
	document.getElementById(elementId).className = newClass;
}

function fnHideClass(classId, element, strShowText, strHidetext)
{
	var valSet = changecss(classId, 'display', 'none', 'block');
	if (valSet == "none")
		element.innerHTML = strShowText;
	else
		element.innerHTML = strHidetext;
}

function changecss(theClass, element, value, flipValue)
{
//Last Updated on June 23, 2009
//documentation for this script at
//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
// Returns the Value Set, but is not sophisticated enough to cope with multiple flips (ie some on and some off)

	var cssRules;
	var added = false;
	var valueSet = value;
	for (var S = 0; S < document.styleSheets.length; S++)
	{
//		alert(document.styleSheets.href);
		if (document.styleSheets[S]['rules']) 
		{
			cssRules = 'rules';
		} 
		else if (document.styleSheets[S]['cssRules']) 
		{
			cssRules = 'cssRules';
		} 
		else 
		{
			alert("no rules found... browser unknown");
		}
		
		for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) 
		{
			if (document.styleSheets[S][cssRules][R].selectorText == theClass) 
			{
				if(document.styleSheets[S][cssRules][R].style[element])
				{
//					alert("found theElement - " + theClass + " - " + element);
					if (document.styleSheets[S][cssRules][R].style[element] == value)
						document.styleSheets[S][cssRules][R].style[element] = flipValue;
					else
						document.styleSheets[S][cssRules][R].style[element] = value;
					added=true;
					valueSet = document.styleSheets[S][cssRules][R].style[element];
					break;
				}
			}
		}
		if(!added)
		{
			newRuleIndex = document.styleSheets[S][cssRules].length;
			if(document.styleSheets[S].insertRule)
			{
				document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',newRuleIndex);
			} 
			else if (document.styleSheets[S].addRule) 
			{
				styleSheet = document.styleSheets[S];
//				alert(theClass);
//				styleSheet.addRule("this", element+': '+value+';');
				styleSheet.addRule(theClass,element+': '+value+';', newRuleIndex);
				styleSheet.removeRule(newRuleIndex);
			}
//			else
//			{
//				alert("failed to add class");			
//			}
		}
	}
	return valueSet;
}

function appendCssRule(theClass, element, value)
{
	var cssRules;
	var added = false;
	S = document.styleSheets.length -1;
	styleSheet = document.styleSheets[S];

	if (styleSheet['rules']) 
	{
		cssRules = 'rules';
	} 
	else if (styleSheet['cssRules']) 
	{
		cssRules = 'cssRules';
	} 
	
	newRuleIndex = styleSheet[cssRules].length;

	if(styleSheet.insertRule)
	{
		styleSheet.insertRule(theClass+' { '+element+': '+value+'; }',newRuleIndex);
	} 
	else if (styleSheet.addRule) 
	{
		styleSheet.addRule(theClass,element+': '+value+';', newRuleIndex);
	}
	return newRuleIndex;
}

function popCssRule()
{// remove the last css rule to be appended.

	var cssRules;
	var added = false;
	S = document.styleSheets.length -1;
	styleSheet = document.styleSheets[S];

	if (styleSheet['rules']) 
	{
		cssRules = 'rules';
	} 
	else if (styleSheet['cssRules']) 
	{
		cssRules = 'cssRules';
	} 
	
	newRuleIndex = styleSheet[cssRules].length;

	if(styleSheet.deleteRule)
	{
		styleSheet.deleteRule(newRuleIndex-1);
	} 
	else if (styleSheet.removeRule) 
	{
//		styleSheet.addRule(theClass,element+': '+value+';', newRuleIndex);
		styleSheet.removeRule(newRuleIndex -1);
	}
	return newRuleIndex;
}

var showHelp = {
	toggle : function () {
		if (!this.state) {
			this.state = 1;	
		} else {
			this.state = 0;
		}
		alert(this.state);
	}
}

var Screen = {
	fade : function ( element, from, to, increment, callback ) {
		var ele 	 = this.getElement(element);
		var ie 		 = this.isIE();
		var opa_sta	 = from
		var opa_end  = to
		var inc 	 = (!increment) ? 10 : increment;
		var callback = callback
		var fad_typ  = null;
		var that 	 = this
		if (opa_sta < opa_end ) {
			var opa_inc = inc;
			fad_typ = "in";
			if (this.getStyle(ele, "display")==="none") {
				ele.style.display = "block";
			}
		} else {
			var opa_inc = -inc;
			fad_typ = "out";
		}
		function anim() {
			that.setOpacity(ele, opa_sta);	
			opa_sta+=opa_inc;
			if ((fad_typ==="in" && opa_sta < opa_end)||(fad_typ==="out" && opa_sta > opa_end)) {
				setTimeout(anim, 20);
			} else if (fad_typ==="out") {
				that.setOpacity(ele, opa_end);
				ele.style.display = "none";
				if (callback) {	
					callback.call(that); 
				}
			} else if (fad_typ==="in") {
				that.setOpacity(ele, opa_end);
				if (callback) {	
					callback.call(that); 
				}
			}
		}
		anim();
	},
	getElement : function (element) {
		if(typeof(element)==="string"){
			return document.getElementById(element);
		} 
		return element;
	},
	getStyle : function (element, style) {
		var ele = this.getElement(element);
		if (ele.currentStyle) {
			return ele.currentStyle[style];	
		} else if (window.getComputedStyle) {
			return document.defaultView.getComputedStyle(ele, null).getPropertyValue(style);	
		} 
		return false;	
	},
	setOpacity	: function (element, opa) {
		var ele = this.getElement(element);
		if (this.isIE()) { 	
			ele.style.filter = "alpha(opacity=" + opa + ")";
		} else {	
			ele.style.opacity  = (opa/100);
		}
	},
	isIE : function () {
		return (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
	},
	on : function (element) {
		this.getElement(element).style.display = "block";
	},
	off : function (element) {
		this.getElement(element).style.display = "none";		
	},
	showPopup : function (element) {
	// where element is a <div> inside the popup element
		if(element){
			// if element doesn't exist, exit the program
			if(!this.getElement(element)) {
				return false;
			}
			this.popupElement = element;
		}
		this.setBlackoutSize();
		this.fade('blackout', 0, 80, 20, this.showPopupMember)
		
	},
	setBlackoutSize : function () {
		var bo = document.getElementById("blackout");
		bo.style.height = Dom.currentPageSize().pageY + "px";
	},
	showPopupMember : function () {
		// display parent popup-wrap
		if (this.popupElement) {
			// this following line ensures the popups appear central to the viewer
			document.getElementById("popup-wrap").style.top = (100 + window.scrollY) +"px";
			this.on("popup-wrap");
			this.fade(this.popupElement, 0, 100, 20);
			var that = this;
			this.getElement("blackout").onclick = function () {
				that.hidePopup();
			}
		}
	},
	imagePopup : function (source) {
		this.showPopup("image-lightbox");
		var boxWrap = document.getElementById("image-lightbox");
		var boxImg  = document.getElementById("lightbox");
		boxImg.src = source;
		boxWrap.style.width  = boxImg.width + "px";
		boxWrap.style.height = boxImg.height + "px";
		boxWrap.style.marginLeft = -(boxImg.width/2) +"px";
		
	},
	hidePopup : function (element) {
		if (element) {
			this.popupElement = this.getElement(element);	
		}
		
		this.getElement("blackout").onclick = null;
		this.fade(this.popupElement, 100, 0, 20, this.hideBlackout);
	},
	hideBlackout : function () {
		this.fade("blackout", 90, 0, 20);	
	}
}

/*********************************************************************
	
	Microsoft IE 5.5 - 6 *.png alpha loader.
		
	Parses the DOM and reloads and replaces *.png images
	through an IE proprietary css filter

**********************************************************************/
/*
var fnLoadPngs = function() {
	var isIE = navigator.appVersion.match(/MSIE (\d+\.\d+)/, ''),
		pngFixIE = (isIE != null && Number(isIE[1]) >= 5.5);
	
	if (isIE!=null) { 
		for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) 			{
			//if (pngFixIE && img.src.match(/\site-logo.png$/i) != null) {
			if (pngFixIE && img.src.match("site-logo.png") != null) {
			var src = img.src,
				id  = img.id,
				cls = img.className,
				div = document.createElement("DIV");
			div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
			div.style.width = img.width + "px";
			div.style.height = img.height + "px";
			div.id = id;
			div.className = cls;
			img.replaceNode(div);
		}
		img.style.visibility = "visible";

	}
}
}
*/

var fnLoadPngs = function () {
	return false;
}



/*********************************************************************
	
	Block {}	Called from the block div (none IE6)
	Usage:		Block.toggleDisplay ( block number ) : show/hide block
				Block.toggleGroup()  : switches the active display state for each block 
				Block.showGroup() : shows all block elements
				Block.hideGroup() : hides all block elements
				
**********************************************************************/

var Block = {
	blockEle : null,
	clickEle : null,
	blockGrp : null,
	toggleDisplay : function (blockNum) {
		this.setElements(blockNum);
		this.setDisplayState();
	},
	setElements : function (blockNum) {
		var blockId = "block_" + blockNum, 
			clickId = "click_" + blockNum;
		this.blockEle = document.getElementById(blockId);
		this.clickEle = document.getElementById(clickId);		
		this.clickEle.onselectstart = function () { return false; }
	},
	setDisplayState : function () {
		if (this.blockEle.className.match("div-hide")) {	
			this.showEle();
		} else if (this.blockEle.className.match("div-show")) {
			this.hideEle();
		} else {
			this.blockEle.className += " div-hide";
			this.hideEle();
		}		
	}, 
	getBlockNum : function (blockId) {
		return 	blockId.slice(6);
	},
	showEle : function () {
		this.blockEle.className = this.blockEle.className.replace("div-hide", "div-show");
		this.clickEle.innerHTML = "-";
	},
	hideEle : function () {
		this.blockEle.className = this.blockEle.className.replace("div-show", "div-hide");
		this.clickEle.innerHTML = "+";
	},
	setGroup : function (groupName) {
		var groupName = groupName;
		if (!groupName) {
			groupName = "clickable";
		}
		this.blockGrp  = document.getElementsByName(groupName);
	},
	toggleGroup : function () {		
		this.setGroup();
		for (x=this.blockGrp.length-1; x>=0; x--) {
			this.toggleDisplay(this.blockGrp[x].id.slice(6));
		}
	},
	showGroup : function () {
		this.setGroup();
		for (x=this.blockGrp.length-1; x>=0; x--) {
			var blockNum = this.getBlockNum(this.blockGrp[x].id);
			this.setElements(blockNum);
			this.checkClass();
			this.showEle();
		}
	},
	hideGroup : function () {
		this.setGroup();
		for (x=this.blockGrp.length-1; x>=0; x--) {
			var blockNum = this.getBlockNum(this.blockGrp[x].id);
			this.setElements(blockNum);
			this.checkClass();
			this.hideEle();
		}
	},
	checkClass : function () {
		var cls = this.blockEle.className;
		if (cls.match("div-hide")===null && cls.match("div-show")===null) {
			this.blockEle.className += " div-show";		
		}
	}
}

/*********************************************************************

	ShowHide {}
	
	Usage: ShowHide.toggle("{element id you want to show or hide}")
	Optional: Use the css classes 'div-show' or 'div-hide' to manipulate 
	the elements display property. If no class is found, object will treat
	the element as being visible and assign it the div-show class.
	
**********************************************************************/
function fnMinMaxBlock(elementId)
{
//	alert(elementId);
	var blockElement = document.getElementById(elementId);
	var strClass = blockElement.className
//	alert(strClass);
	var bIsMax = (-1 != strClass.search(/maximised/i));
	var bIsMin = (-1 != strClass.search(/minimised/i));
	if (bIsMax)
	{
		if (bIsMin)
			strClass = 	strClass.replace(/minimised/i, ""); // Just to make sure that min and max are not both set
		strClass = 	strClass.replace(/maximised/i, "minimised");
	}
	else
	{
		if (bIsMin)
			strClass = 	strClass.replace(/minimised/i, "maximised");
		else
			strClass += " minimised";
	}
//	alert(strClass);
	blockElement.className = strClass;
}

var ShowHide = {
	bankstate  : [],
	element    : null,
	toggle : function (elementId) {
		var cls = ""
		this.element = document.getElementById(elementId);
		if (this.bankstate[this.element.id]===undefined) {
			this.initBankState(this.element.id);
		} 
		if(!this.bankstate[this.element.id]) {
			this.bankstate[this.element.id] = 1;		
			this.showElement();
		} else {
			this.bankstate[this.element.id] = 0;
			this.hideElement();
		}
	},
	showElement : function () {
		cls = this.element.className
		this.element.className = cls.replace("div-hide", "div-show");
		return;
	},
	hideElement : function () {
		cls = this.element.className
		this.element.className = cls.replace("div-show", "div-hide");
		return;
	},
	initBankState : function () {
		if (this.element.className.match("div-hide")) {	
				this.bankstate[this.element.id] = 0;
		} else if (this.element.className.match("div-show")) {
				this.bankstate[this.element.id] = 1;
		} else {
			// no class defined, set the default to show 
			this.element.className += " div-show";
			this.bankstate[this.element.id] = 1;
		}
	}
}

/*********************************************************************
	
	IE only getElementsByName function ()
	replaces the getElementsByName function
	for IE only
	
		
**********************************************************************/

if(typeof(window.external) != 'undefined'){
	document.getElementsByName = function(name, tag){
		if(!tag){ tag = '*';}
		var elems = document.getElementsByTagName(tag);
		var res = []
		for(var i=0;i<elems.length;i++){
			att = elems[i].getAttribute('name');
			if(att == name) {
				res.push(elems[i]);
			}
		}
    	return res;
	}
}

var Dom = {
	currentPageSize : function () {
		var xScroll, 
			yScroll, 
			windowWidth, 
			windowHeight, 
			pageWidth,
			pageHeight;
		
		if (window.innerHeight && window.scrollMaxY) {
			// firefox 3.5+
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) { 
			// all but explorer mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			// explorer mac
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		if (self.innerHeight) { // all except Explorer
			if (document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // older internet explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
		}
		// for small pages with total height less then height of the viewport
		if (yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if (xScroll < windowWidth) {
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		// return the results as an object literal.
		return {"pageX" : pageWidth, "pageY" : pageHeight, "windowX" : windowWidth, "windowY" : windowHeight, "scrollX" : xScroll, "scrollY" : yScroll};
	}
}


