// top of page
function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}

//Suckerfish JavaScript menu
sfHover = function() {
	if (document.getElementById("nav")) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) 			
			{
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}	
			
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover); 


// Load Calendar
if (window.addEventListener) //DOM method for binding an event
	{window.addEventListener("load", initCalendar, false)}
else if (window.attachEvent) //IE exclusive method for binding an event
	{window.attachEvent("onload", initCalendar)}
else if (document.getElementById) //support older modern browsers
	{window.onload=initCalendar;}


// Open print page using _print extension.
function showPrint() {
	var strPage = window.location.pathname;
	
	strPage = strPage.substring(0, strPage.indexOf("_1.aspx"));
	
	if(strPage.toLowerCase().indexOf("news") != -1) {
		strPage = "chalet_search";	
	}	
	else if(strPage.toLowerCase().indexOf("search") != -1) {
		strPage = "search";	
	}
	else {
		// set document as default
		strPage = "document";
	}
	/*window.location = strPage + "_print_1.aspx" + window.location.search;*/
	window.open(strPage + "_print_1.aspx" + window.location.search);
	
}

// Print page dialog
function printpage() {
  window.print();
}

// Search site
function checkSearchFields(objForm)
{	

		if(trim(objForm.keywords.value) == "" || objForm.keywords.value == " Search the site...") {
			alert("Please enter your keyword(s)");
			objForm.keywords.value='';
			objForm.keywords.focus();
			return false;
		}
		else {
			return true;
		}
}


// Show & Hide
function show(c) {
	if (document.getElementById && document.getElementById(c)!= null) 
		node = document.getElementById(c).style.display='block'; 
	else if (document.layers && document.layers[c]!= null) 
		document.layers[c].display = 'block'; 
	else if (document.all && document.all(c)!= null) 
		document.all[c].style.display = 'block';
} 

function hide(c) {	
	if (document.getElementById && document.getElementById(c)!= null)
		node = document.getElementById(c).style.display='none';
	else if (document.layers && document.layers[c]!= null) 
		document.layers[c].display = 'none';
	else if (document.all && document.all(c)!= null) 
		document.all[c].style.display = 'none';
}

function toggle(obj) {
	var el = document.getElementById(obj);
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
}

//Trim string
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
//End Function

//Right Trim string
function rtrim(str)
{
	return str.replace(/\s*$/, ""); 
}
//End Function

//Left Trim string
function ltrim(str)
{
	return str.replace(/^\s*/, ""); 
}
//End Function


// Macromedia
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}

/*** cricket bath toggles ***/
function showHideCourseDetails(oAnchor) {
	var oDetails = oAnchor.parentNode.nextSibling.nextSibling.nextSibling;
	var oSwitch = oAnchor.firstChild;
	var fVisible = oDetails.style.display=='block';
	
	if(!fVisible) {
		oDetails.style.display = "block";
		oSwitch.src = "/assets/images/toggle_up.jpg";
	} else {
		oDetails.style.display = "none";
		oSwitch.src = "/assets/images/toggle_down.jpg";
	}
	return false;
}

/*** keep alive ***/
var goAjax = null;
function keepAlive() {
	var strUrl = "";

	strUrl = window.location.protocol + "//" + window.location.host + "/webservices/keep_alive.asmx/Ping";
	goAjax = new EdeptiveAjax();
	goAjax.postRequest(strUrl, "", onKeepAliveResponse);
}
function onKeepAliveResponse() {
	if(goAjax.checkReadyState("keepAlive", "ping.", "ping..", "ping...") == "OK") {
		if(document.getElementById("keepAlive")) {
			var response = goAjax.request.responseXML.documentElement;
			document.getElementById("keepAlive").innerText = "sid=" + getText(response.firstChild);
		}
		setTimeout("keepAlive()", 1000*60);
	}
}
setTimeout("keepAlive()", 1000*2);
