// Retrieves particular cookie
function getCookie(cookieName)
{
   var cookieFoundAt;
   var cookieValue;
 
   // find start position in cookie string
   cookieFoundAt = document.cookie.indexOf(cookieName + "=");
   
   if (cookieFoundAt < 0)
      {
         cookieValue = "";
      }
   else
      {
         // move to actual start of cookie's data
         cookieFoundAt = document.cookie.indexOf("=",cookieFoundAt);
         cookieFoundAt++;
         
         // find end position of cookie's data
         cookieEnd = document.cookie.indexOf(";", cookieFoundAt);
         if (cookieEnd == -1)
            {
             cookieEnd = document.cookie.length - 1;
            }
         cookieValue =document.cookie.substring(cookieFoundAt,cookieEnd  );
      }
   return cookieValue;
}

/**
 * adds trim method to strings
 *
 * @param {String} delim A list of characters to remove from beginning and end
 *				         of the current string; default is whitespace
 */
String.prototype.trim = function(delim) {
	return this.replace(new RegExp("^[\\s" + delim + "]+"),'').replace(new RegExp("[\\s" + delim + "]+$"), '');
}

/***
 * Sets textbox input state to blur/focus styles
 *
 * @param	o		DOMObject	The input object to style
 * @param	action	string		The action to style
 */
function formatInput(o, action) {
	switch(action) {
		case "blur":
			o.className = "blur";
			break;
		case "focus":
			o.className = "focus";
			break;
		default:
			break;
	}
}

/**
 * Provides generic form validation.
 *
 * Usage:
 * 1. Add to form: onsubmit="return(fmValidate(this))"
 * 2. Add required="true" attribute to desired inputs
 * 2a. Add validate="email" to input that needs email format verification
 *
 */
function fmValidate(theForm) {
	var hlColor = "#fc3";
	var hlMark = "&raquo;"
    var isValid = true;
	
	var els = theForm.elements;
    var emailRegex = /^[A-Za-z0-9\_\-\.]+@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,}$/;
	var firstBad = false;
    for(var i=0; i < els.length; i++) {
		var el = els[i];
		if(el.getAttribute("required") == "true") {
	        if(el.value == "") {
	            el.style.background = hlColor;
	            isValid = false;
				fmToggleMark(el.id, true);
				if(!firstBad) {
					el.focus();
					firstBad = true;
				}
	        } else {
	            el.style.background = "none";
				fmToggleMark(el.id, false);
	        }				
		}
		if(el.getAttribute("validate") == "email" && !emailRegex.test(el.value)) {
            el.style.background = hlColor;
            isValid = false;
			fmToggleMark(el.id, true);
		}
    }
    
    if(!isValid) alert("You need to fill in the required fields,\nwhich are marked in yellow.");
    return isValid;
}

/**
 * Toggles the error chevron on form inputs.
 *
 */
function fmToggleMark(inputId, turnOn) {
	var mk = document.getElementById(inputId + "_Err");
	if(!turnOn && mk != null) {
		mk.parentNode.removeChild(mk);
	} else if(turnOn && mk == null){
		mk = document.createElement("span");
		mk.id = inputId + "_Err";
		mk.className = "mark";
		mk.appendChild(document.createTextNode("»"));
		var inp = document.getElementById(inputId);
		inp.parentNode.insertBefore(mk, inp);
	}
}


function getElementsByClassName(node, classname)
{
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function flipPanel(caller, panelId) {
	$("." + caller.className).removeClass('selected');
	$(caller).addClass('selected');
	
	$("." + $("#" + panelId).attr('class')).hide();
	$("#" + panelId).css('display','block');

}

var gRotatePanelsPid = 0;
function rotatePanels(panelClass, panelContainer, selectedIndex, timeout) {

	var ACTIVE_CLASS_NAME = 'activePanel';

    // set defaults
    if(!selectedIndex) selectedIndex = 0;
    if(!timeout) timeout = 5000;
    
    // get DOMs
    var domContainer = $(panelContainer);
	var domPanels = getElementsByClassName(domContainer, panelClass);
	var panelCount = domPanels.length;
    
    // determine next panel to show
    var nextIndex = (selectedIndex + 1) % panelCount;
    var activeIndex = 0;
	
	// hide all panels
	for(var i=0; i<panelCount; i++) {
	    if(Element.hasClassName(domPanels[i], ACTIVE_CLASS_NAME)) {
	        activeIndex = i;        
	        break;
	    }
	}

	Element.addClassName(domPanels[selectedIndex], ACTIVE_CLASS_NAME);

	
	return true;
    
}

function showhide(layer_ref) {
	
	$('#' + layer_ref).slideToggle();
	
	return false;
}

var heroModuleTimerOffset = 0;

var pauseAllHeros = 0;

