// -------------------------------------------------------- //
// ----- General DHTML functions for main page dislay ----- //
// -------------------------------------------------------- //
// Determine browser.
var isMinNS4 = document.layers;
var ie=document.all&&navigator.userAgent.indexOf("Opera")==-1;
var dom=document.getElementById&&!ie;
var opera=navigator.userAgent.indexOf("Opera");
// Get version of IE
function getIEVersionNumber() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}
// Block use of enter key
function stopKey(evt) {
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type!="textarea") && (node.type=="text"))  {return false;}
	//document.loginForm.username.value = evt.keyCode;
}
// -----------------------------------------------------------------------------------------------------------------
// Preload common images
// if(document.images) {
//	var buttons = new Object();
	/* Add Button
	buttons[0] 	   = new Image(22,44);
	buttons[0].src = "images/addUp.jpg";
	buttons[1] 	   = new Image(22,44);
	buttons[1].src = "images/addDown.jpg";
	*/
//}
// -----------------------------------------------------------------------------------------------------------------
// Import Style sheets based on Browser
if(getIEVersionNumber()) {
	if(getIEVersionNumber( )>= 6) var goFade = true;
	var siteCSS = "css/siteStylesIe.css"; 
}
else if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
	var goFade = false;
	var siteCSS = "css/siteStylesChrome.css"; 		
}
else {
	var goFade = false;
	var siteCSS = "css/siteStyles.css";
}
document.write('<link href="' + siteCSS + '" rel="stylesheet" type="text/css">');
// -----------------------------------------------------------------------------------------------------------------
// Numbers only in text field
function numOnly(fld) {
    var re = /[0-99999 ]/;
	if(fld.value != "" && !re.test(fld.value)) {
		// Field must only contain numbers
		messageBox("alert","This field will only accept a numerical value!",250,100);
        fld.value = "";
		fld.focus();
		return(false);
	  }
	return(true);
}
// -----------------------------------------------------------------------------------------------------------------
// Function to return a string to the equivilent of PHP ucwords()
// use this as the onChange function - onChange="this.value=ucwords(this.value)"
function ucwords(str) {
	return (str + '').replace(/^([a-z])|\s+([a-z])/g, function ($1) {
		return $1.toUpperCase();     }); 
}  
function strtolower(str) {
	return (str+'').toLowerCase(); 
} 
function strtoupper(str) {
	return (str+'').toUpperCase(); 
} 
// -----------------------------------------------------------------------------------------------------------------
// Create a fixed div for displaying alert and confirm messages
function messageBox(type,message,boxWidth,boxHeight,confirmOK,confirmValue) {
	var html = "";
	var messageDiv = getLayer("alertBox");
	// Get the width & height of the window
	var winHeight = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	var winWidth = (window.innerWidth) ? window.innerWidth : document.body.scrollWidth;
	// Get current scroll position
  	var db = (document.body) ? 1 : 0;
  	// Set vars
  	var xOffset = (db) ? document.body.scrollLeft : pageXOffset;
  	var yOffset = (db) ? document.body.scrollTop : pageYOffset;
	// Set Left value for Div
	var divXPos = ((winWidth / 2) - (boxWidth / 2) + xOffset);
	// Set Top value for Div
	var divYPos = (50 + yOffset);
	// Set position
	messageDiv.style.width = boxWidth;
	messageDiv.style.height = 'auto';
	messageDiv.style.left = divXPos;
	messageDiv.style.top = divYPos;
	// Replace all \n's with BR's
	message = message.replace(/\n/gi,"<br>");
	// Build HTML
	html += '<table width="' + boxWidth + 'px" cellspacing="0" cellpadding="0" align="center" class="AlertBox">';
	html += '<tr><td align="center"><b>AGILITY CLUB WEBSITE MESSAGE</b><br><br></td></tr>';
	html += '<tr><td align="center"><b>' + message + '</b><br><br></td></tr>';
	html += '<tr><td align="center"><img src="images/ok.png" name="ok" id="ok" align="center" onClick="displayBlockOff(document.getElementById(\'alertBox\'));displayBlockOff(document.getElementById(\'opaque\'));';
	if(type == "confirm") html += ';document.getElementById(\'' + confirmOK + '\').value=\'' + confirmValue + '\';document.userForm.submit()';
	html += '">';
	if(type == "confirm") html += '&nbsp;&nbsp;&nbsp;<input type="image" src="images/cancel.png" name="cancel" id="cancel" align="center" onClick="displayBlockOff(document.getElementById(\'alertBox\'));displayBlockOff(document.getElementById(\'opaque\'));return false"></td></tr></table>';
	messageDiv.innerHTML = html;
	// Set grey BG
	// Set height
	document.getElementById("opaque").style.height = winHeight + yOffset;
	document.getElementById("opaque").style.width = winWidth + xOffset;
	displayBlockOn(document.getElementById("opaque"));
	// Set visibility
	displayBlockOn(messageDiv)
	return false;
}
// -----------------------------------------------------------------------------------------------------------------
// OnLoad ALERT messages
function alertCheck(Form) {
    if(Form.onLoadAlert.value) {
		return messageBox("alert",Form.onLoadAlert.value,250,100);
     }
}
// -----------------------------------------------------------------------------------------------------------------
// OnLoad CONFIRM message (will create a confirm message and send back a value passed into onLoadConfirmValue hidden var)
function confirmCheck(Form) {
    if(Form.onLoadConfirm.value) {
		return messageBox("confirm",Form.onLoadConfirm.value,250,100,"confirmOK",document.getElementById("onLoadConfirmValue").value);
     }
}
// -----------------------------------------------------------------------------------------------------------------
// Confirm DELETE
function confirmDelete(item,value) {
	var returnValue = (value) ? value : 1;
	if(item) {
    	var message = "You are about to DELETE this " + item + "\nClick OK to continue";
    	return messageBox("confirm",message,250,100,"deleteOK",returnValue);		
	}
}
// -----------------------------------------------------------------------------------------------------------------
// Function to save current scroll position of window
function getDivPos(Form,GET) {
  // Check browser version
  var db = (document.body) ? 1 : 0;
  // Set vars
  var xOffset = (db) ? document.body.scrollLeft : pageXOffset;
  var yOffset = (db) ? document.body.scrollTop  : pageYOffset;
  Form.wPx.value = xOffset;
  Form.wPy.value = yOffset;
  if(GET) {
  	var returnUrl = "";
  	returnUrl =  '&wPx=' + xOffset;
 	returnUrl += '&wPy=' + yOffset;
	return returnUrl;
  }
}
// -----------------------------------------------------------------------------------------------------------------
// Set todays date (short)
  var today = new Date();
  // Set Month, Year & Day
  var thisMonth1 = today.getMonth()+1;
  var thisYear1 = today.getFullYear();
  var thisDay1 = today.getDate();
  var todaysDate = thisDay1 + "/" + thisMonth1 + "/" + thisYear1;

// -----------------------------------------------------------------------------------------------------------------
// Get Cookie function
// Will search for the passed cookie name and return its value
function getCookieData(labelName) {
    var labelLen = labelName.length;
    // read cookie property only once for speed
    var cookieData = document.cookie;
    var cLen = cookieData.length;
    var i = 0;
    var cEnd;
    while (i < cLen) {
        var j = i + labelLen;
        if (cookieData.substring(i,j) == labelName) {
            cEnd = cookieData.indexOf(";",j);
            if (cEnd == -1) {
                cEnd = cookieData.length;
            }
        return unescape(cookieData.substring(j+1, cEnd));
        }
        i++;
    }
    return "";
}
// -----------------------------------------------------------------------------------------------------------------
// Functions to save cookie for currState
function saveCookie(cookieVar,state) {
	var exp = new Date();
	var oneYear = exp.getTime() + (365 * 24 * 60 * 60 *1000);
	exp.setTime(oneYear);
	document.cookie = cookieVar + " = " + state + "; expires = " + exp.toGMTString();
}
// -----------------------------------------------------------------------------------------------------------------
// function to swap class for button
function itemState(evt,styleRef) {
    evt = (evt) ? evt : ((window.event) ? window.event : false);
    if (evt) {
        var elem = (evt.srcElement) ? evt.srcElement : evt.target;
        elem.className = styleRef;
    }
}
// -----------------------------------------------------------------------------------------------------------------
// function to swap image
function swapImg(elem,swapFor) {
    if (elem) {
        elem.src = swapFor;
    }
}
// -----------------------------------------------------------------------------------------------------------------
// Function to get layer
function getLayer(name) {
  if (isMinNS4)
    return findLayer(name, document);
  if (ie)
    return eval('document.all.' + name);
  if (dom)
    return document.getElementById(name);
  return null;
}
// -----------------------------------------------------------------------------------------------------------------
// Function to get width of div
function getWidth(layer) {
  if (isMinNS4) {
    if (layer.document.width) return(layer.document.width);
    else return(layer.clip.right - layer.clip.left);
  }
    if (layer.style.width) return(layer.style.width);
    else return(layer.offsetWidth);
}
// -----------------------------------------------------------------------------------------------------------------
// Function to get height of div
function getHeight(layer) {
  if (isMinNS4) {
    if (layer.document.height) return(layer.document.height);
    else return(layer.clip.bottom - layer.clip.top);
  }
  else {
    if (layer.style.height) return(layer.style.height);
    else return(layer.offsetHeight);
  }
  return(-1);
}
// -----------------------------------------------------------------------------------------------------------------
function getVisibility(layer) {
  if (isMinNS4) {
    if (layer.visibility == "show")
      return "visible";
    if (layer.visibility == "hide")
      return "hidden";
    return layer.visibility;
  }
  if (ie||dom)
    return layer.style.visibility;
  return "";
}
// -----------------------------------------------------------------------------------------------------------------
// Turn layer ON
function layerOn(obj) {
        obj.style.visibility = "visible";
}
// Turn layer OFF
function layerOff(obj) {
        obj.style.visibility = "hidden";
}
// -----------------------------------------------------------------------------------------------------------------
// Turn Block level display ON
function displayBlockOn(obj) {
        obj.style.display = "block";
}
// Turn Block level display OFF
function displayBlockOff(obj) {
        obj.style.display = "none";
}
// -----------------------------------------------------------------------------------------------------------------
function setDivPosition(showHide,div,width,height,grey) {
	var divToSet = getLayer(div);
	// get width of this div
	var divWidth = width;
	divToSet.style.width = width;
	// get height of this div
	var divHeight = height;
	divToSet.style.height = height;
	// Get the width & height of the window
	var winHeight = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	var winWidth = (window.innerWidth) ? window.innerWidth : document.body.scrollWidth;
	// Get current scroll position
  	var db = (document.body) ? 1 : 0;
  	// Set vars
  	var xOffset = (db) ? document.body.scrollLeft : pageXOffset;
  	var yOffset = (db) ? document.body.scrollTop : pageYOffset;
	// Set Left value for Div
	var divXPos = ((winWidth / 2) - (divWidth / 2) + xOffset);
	// Set Top value for Div
	//var divYPos = ((winHeight / 2) - (divHeight / 2) + yOffset);
	var divYPos = "30px";
	// Set position
	divToSet.style.left = divXPos;
	divToSet.style.top = divYPos;
	// If grey out requested, do
	if(grey) {
		document.getElementById("opaque").style.height = winHeight + yOffset;
		document.getElementById("opaque").style.width = winWidth + xOffset;
		displayBlockOn(document.getElementById("opaque"));
	}
	// Set visibility
	divToSet.style.display = showHide;
	return true;
}
// -------------------------------------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------------------------------
// AJAX functions
// -----------------------------------------------------------------------------------------------------------------
// URL = page to call, elemId = element to change, elemType = Type of element "form" or "html"
// display is optional. Pass a div id and it will set that div to visible if some response text is received
// subType = GET or POST
function ajaxCall(url,elemId,elemType,subType,display) { 
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null) {
  		alert ("Browser does not support AJAX!");
  		return;
  	}
  	xmlHttp.elemId = elemId; 
  	if(display) xmlHttp.display = document.getElementById(display);
	xmlHttp.onreadystatechange = (elemType == "form") ? formElemChanged : innerHTMLChanged;
	xmlHttp.open(subType,url,true);
	xmlHttp.send(null);
}
function formElemChanged()  { 
	if (xmlHttp.readyState==4) { 
		document.getElementById(xmlHttp.elemId).value = xmlHttp.responseText;
		// debug
		// alert(xmlHttp.responseText);
	}
}
function innerHTMLChanged()  { 
	//document.write("infunc");
	if (xmlHttp.readyState==4) { 
		document.getElementById(xmlHttp.elemId).innerHTML = xmlHttp.responseText;
		if(xmlHttp.display && xmlHttp.responseText) xmlHttp.display.style.display = "block";
		// debug
		// alert(xmlHttp.responseText);
	}
}
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e) {
  		// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e) {
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}
// -----------------------------------------------------------------------------------------------------------------
