// setMenu - set nav to current section
// 2008.11.14 - commented out footer bg change for future date
// 2008.11.23 - added sermons submenu highlight functionality
function setMenu(suburl, subnav) {
	var menuLink = document.getElementById("nav_"+suburl);
	var banner = document.getElementById("banner");
	//var footer = document.getElementById("footer");
	if(menuLink != null) {
		menuLink.setAttribute("class", "hover");
		menuLink.setAttribute("className", "hover");
		banner.setAttribute("class", suburl);
		banner.setAttribute("className", suburl);
		//footer.setAttribute("class", suburl);
		//footer.setAttribute("className", suburl);
		if(subnav.length > 0) {
			var subnavLink = document.getElementById("subnav_"+subnav);
			subnavLink.setAttribute("style", "color: #8BCDDF;");
			subnavLink.style.setAttribute('cssText', 'color: #8BCDDF;', 0);
		}
	}
	// sermon submenu highlights
	if(suburl == "sermons") {
		if(stripos(subnav, "player") !== false ||  stripos(subnav, "archive") !== false) {
			var divSubmenu = document.getElementById("listen");
		} else {
			var divSubmenu = document.getElementById("podcast");
		}
		divSubmenu.setAttribute("class", "hover");
		divSubmenu.setAttribute("className", "hover");
	}
}

// formSubmit - submit form
function formSubmit(formid) {
	document.forms[formid].submit();
}

// formDisable - disable a form element
function formDisable(element)
{
	document.getElementById(element).disabled = true;
}

// sortOptionSort - sort a list of values (e.g. option list)
function sortOptionSort(a, b) {
	A = a.text.toLowerCase();
	B = b.text.toLowerCase();
	if (A < B) { return -1; }
	if (A > B) { return 1; }
	return 0;
}

// strToTitle - converts str to title format
function strToTitle(str) {
    var regex = new RegExp(/^(a|about|after|an|and|at|by|for|from|in|into|nor|of|on|onto|over|the|to|up|with|within)$/);
    return str.toLowerCase().replace(/\b([a-z])(\w*)\b/g, evalUpper);
    function evalUpper() {
        if (regex.test(arguments[0]) && arguments[arguments.length-2])
            return arguments[0];
        else
            return arguments[1].toUpperCase() + arguments[2];
    }
}

/* Add new option to parent window list (NOT USED RIGHT NOW)
function addOptToList(strList, strOptID, strOptText) {
  	var winOpener = window.opener;
  	var frmParentDevList = winOpener.document.getElementById(strList);
	var optNewOption = winOpener.document.createElement("option");
  	var strNewText = winOpener.document.createTextNode(strOptText);

  	optNewOption.setAttribute("value",strOptID);
	optNewOption.appendChild(strNewText);
	frmParentDevList.appendChild(optNewOption);
	
	sortParentList(strList, strOptID, 1);
}*/

/* Sort drop-down list */
function sortParentList(strList, optSelected, optStartOption) {
  	var frmParentList = document.getElementById(strList);
  	var bCurrent = false;
  	var nSelected = 0;
  	
  	// make copy of current list
  	var optArray = new Array();
  	for(x=optStartOption; x<frmParentList.options.length; x++) {
  	  	if(x != frmParentList.options.length-2 && bCurrent) {
	    	optArray[x-optStartOption-1] = frmParentList.options[x];
	    } else if(x != frmParentList.options.length-2 && !bCurrent) {
	    	optArray[x-optStartOption] = frmParentList.options[x];
	    } else
	    	bCurrent = true;
	}
	
	// sort temporary list
	optArray = optArray.sort(sortOptionSort);
	optArray[optArray.length] = frmParentList.options[frmParentList.options.length-2];
	
	// repopulate parent list with sorted data
	for(y=0; y<optArray.length; y++) {
	  	frmParentList.options[y+optStartOption] = optArray[y];
	  	if(optArray[y].value == optSelected)
	  		nSelected = y+optStartOption;
	}
	
	// make sure new user is selected
	frmParentList.options[nSelected].selected = true;
}

/*---------------------------
// VALIDATION FUNCTIONS
-----------------------------*/
// stripos - Find position of first occurrence of a case-insensitive string
//	return: numeric position of the first occurrence of needle in the haystack string
function stripos( f_haystack, f_needle, f_offset ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Martijn Wieringa
    // +     revised by: Onno Marsman
    var haystack = (f_haystack+'').toLowerCase();
    var needle = (f_needle+'').toLowerCase();
    var index = 0;
    if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
        return index;
    }
    return false;
}
// trim - same as VB trim()
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
// ucfirst - like php ucfirst(); capitalizes the first char of str
function ucfirst(str) {
	return str = str.substr(0, 1).toUpperCase() + str.substr(1);
}
// checkAlpha - check string to make sure it only has alpha-numeric characters
// Return: true = no alpha-numerics, false = alpha-numeric chars present
function checkAlpha(str)
{
	var theString = str;
	for(var j = 0; j < theString.length; j++) {
		var currChar = theString.charAt(j);
		var currCharCode = currChar.charCodeAt(0);
		if((currCharCode > 47 && currCharCode < 59) || 
			(currCharCode > 64 && currCharCode < 91) || 
			(currCharCode > 96 && currCharCode < 123))
		{
		} else {
			return false;
		}
	}
	return true;
}
// checkAlphaOnly - check string to make sure it only has alphabetic characters (no numbers)
// Return: true = no alpha-numerics, false = alpha-numeric chars present
function checkAlphaOnly(str)
{
	var theString = str;
	for(var j = 0; j < theString.length; j++) {
		var currChar = theString.charAt(j);
		if((currChar >= "a" && currChar <= "z") || 
			(currChar >= "A" && currChar <= "Z"))
		{
		} else {
			return false;
		}
	}
	return true;
}
// checkAlphaPlus - check string for alpha-numerics and special, allowable characters
// Return: true = only allowable chars, false = non-allowable chars found
// NOTE: allowed chars (!@#$%&?:)
function checkAlphaPlus(str)
{
	var theString = str;
	for(var j = 0; j < theString.length; j++) {
		var currChar = theString.charAt(j);
		var currCharCode = currChar.charCodeAt(0);
		if((currCharCode > 47 && currCharCode < 59) || 
			(currCharCode > 64 && currCharCode < 91) || 
			(currCharCode > 96 && currCharCode < 123) ||
			(currCharCode > 32 && currCharCode < 38) || 
			currCharCode == 39 || 
			currCharCode == 58 || 
			currCharCode == 63)
		{
		} else {
			return false;
		}
	}
	return true;
}