// Some browsers (namely IE) do not send the submit
// button if the user clicked enter and only one text
// box is in the form. In any form where this is a
// problem, this method should be used as the onKeyPress
// handler. i.e:
// <input type="text" onKeyPress="return 
//      eventHandler(defaultSubmitButton, event);" />
// Works in IE4+, NS4+. If browser does not support JS,
// form will behave as normal. If user is in IE4+ and
// does not support JS, enter will have undesirable results.
function enterHandler(btn, e) {
  var keycode;
  if (window.event) {
    keycode = window.event.keyCode;
  } else if (e) {
    keycode = e.which;
  } else {
    return true;
  }
  if (keycode == 13) { 
    btn.click();
    return false;
  }
  return true;
}

function displayResource(location) {

	// ideally, this will be 924 wide, but we need to scale back on 800x600
	var width = 924;
	
	if (width > screen.width) {
		width = screen.width - 100;
		if (width < 700) {
			width = 700;
		}
	}
	
    var resourceWindow = window.open(location, 'resourcePopup', "location=no, scrollbars=yes, resizable=yes, width=" + width + ", height=525, left=50, top=50");
    resourceWindow.focus();
}

// Opens a new popup window
// location - URL of popup window
// winName - name of popup window (for control purposes, should be unique)
function open_window(location,winName) {
    NewWindow = window.open(location, winName,"location=no, scrollbars=yes, resizable=yes, width=550, height=525, left=235, top=50");
    NewWindow.focus();
}

function open_help_topic(label) {
	var location = getContextPath() + "helpTopic.action?topic=" + label; 
	var winName = "help";
    NewWindow = window.open(location, winName,"location=no, scrollbars=yes, resizable=yes, width=725, height=575, left=20, top=20");
    NewWindow.focus();
}


function open_help() {
	var location = getContextPath() + "helpContents.action";
	var winName = "help";
    NewWindow = window.open(location, winName,"location=no, scrollbars=yes, resizable=yes, width=725, height=575, left=20, top=20");
    NewWindow.focus();
}

function getContextPath() {
	return g_contextPath;
}

// Opens a new popup window for help - is substantially wider than normal popup, and a little shorter. 
// Still pinned in top right corner on 800x600
// location - URL of popup window
// winName - name of popup window (for control purposes, should be unique)
function open_window_wide(location,winName) {
    NewWindow = window.open(location, winName,"location=no, scrollbars=yes, resizable=yes, width=555, height=400, left=230, top=123");
    NewWindow.focus();
}

// Opens a new popup window that is approximately the width of the screen, but about half the height.
// location - URL of popup window
// winName - name of popup window (for control purposes, should be unique)
function open_window_skinnywide(location,winName) {
    NewWindow = window.open(location, winName,"location=no, scrollbars=yes, resizable=yes, width=790, height=350, left=5, top=30");
    NewWindow.focus();
}


// Opens a new popup window
// location - URL of popup window
// winName - name of popup window (for control purposes, should be unique)
function open_window_small(location,winName) {
    NewWindow = window.open(location, winName,"location=no, scrollbars=yes, resizable=yes, width=440, height=250, left=380, top=123");
    NewWindow.focus();
}

// Trim a string from both left and right
function Trim( p_string ) {
	if (typeof p_string != "string") {
		return p_string;
	}

	var t_string = p_string;
	var t_ch = '';

	// Trim beginning spaces

	t_ch = t_string.substring( 0, 1 );
	while ( t_ch == " " ) {
		t_string = t_string.substring( 1, t_string.length );
		t_ch = t_string.substring( 0, 1 );
	}

	// Trim trailing spaces

	t_ch = t_string.substring( t_string.length-1, t_string.length );
	while ( t_ch == " " ) {
		t_string = t_string.substring( 0, t_string.length-1 );
		t_ch = t_string.substring( t_string.length-1, t_string.length );
	}

	return t_string;
}

// Sets a pair of divs as open/closed, based on the last known state
// toggle value corresponding to the div name prefix
function SetDivPair(p_div, closed) {

	if (closed) {
		document.getElementById(p_div + "_open").style.display = "none";
		document.getElementById(p_div + "_closed").style.display = "";
	} else {
		document.getElementById(p_div + "_open").style.display = "";
		document.getElementById(p_div + "_closed").style.display = "none";	
	}
	
}

 
// Toggles a div pair between open/closed. Used for shading
// and unshading a section of the site. Expects DIVs to be
// named with a common prefix, followed by _open and _closed.
// This will work with all modern browsers. It relies on a
// cookie to simulate stickiness. 
// To have the state be remembered between page loads, the
// following JS should be added to the end of the page:
// 
//  SetDiv('divName');
//
// It is recommended to default shadable divs to unshaded.
// This code defaults a div to closed, so calling SetDiv
// will force the div to its default state without updating
// the toggle cookie. This way, if the browser does not
// support JS, he will see shader in its opened state.
function ToggleDivPair(p_div) {
	
    if (document.getElementById(p_div + "_open").style.display == "none") {
        SetDivPair(p_div, false);
    } else {
        SetDivPair(p_div, true);
    }
}

// Toggles a div between open and closed
function ToggleDiv(p_div) {
    if (document.getElementById(p_div).style.display == "none") {
        SetDiv(p_div, false);
    } else {
        SetDiv(p_div, true);
    }
}

// Sets a div as opened or closed
function SetDiv(p_div, closed) {
    if (closed) {
        document.getElementById(p_div).style.display = "none";
    } else {
        document.getElementById(p_div).style.display = "";
    }
}

function SetDivsWithPrefix(prefix, closed) {

	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; ++i) {
	    if(divs[i].id != "menuDiv" && divs[i].id.indexOf(prefix) >= 0) {
		    SetDiv(divs[i].id, closed);
		}
	}
}

function ValidateMaxCheckboxes(frm, cbox, maxVal) {

    if (!cbox.checked) {
        return false;
    }
    
    var checkCount = 0;
    for (i=0; i < frm.length; i++) {
    
        if (frm.elements[i].type == "checkbox" && frm.elements[i].checked) {
            checkCount++;
        }
    }
    if (checkCount > maxVal) {
        return false;
    }
    
    return true;
}

function escQuotes(text) {
    var rx = new RegExp("\"", "g"); 
    return text.replace(rx,"&#34;");
}
 
function escEntities(text) {
    var rx = new RegExp("&", "g");
    text = text.replace(rx,"&amp;");
    for(var i=161; i<=255; i++) {
        rx = new RegExp(String.fromCharCode(i), "g");
        text = text.replace(rx, "&#"+i+";");
    }
    return text;
}

//window.onload=activateMenu;

function handleErrorFade(propertyName) {
	var errorObjName = 'error_'+propertyName;
	var obj = document.getElementById(errorObjName).previousSibling;
	
	while (obj != null && obj.nodeName != 'TEXTAREA' && obj.nodeName != 'INPUT' && obj.nodeName != 'SELECT') {
		obj = obj.previousSibling;
	}

	if ((obj == null) || (obj.nodeName != 'TEXTAREA' && obj.nodeName != 'INPUT' && obj.nodeName != 'SELECT')) {
		obj = document.getElementById(errorObjName).nextSibling;
	
		while (obj != null && obj.nodeName != 'TEXTAREA' && obj.nodeName != 'INPUT' && obj.nodeName != 'SELECT') {
			obj = obj.nextSibling;
		}
	}
//	alert(obj.nodeName);	
	addErrorFade(obj);
	window.location = '#' + errorObjName;
	obj.focus();
	dojo.event.connect(obj, 'onblur', 'removeErrorFade');
}

function addErrorFade(obj) {
	dojo.graphics.htmlEffects.colorFadeTo(obj, '#e6e6b4', 100, 0)
}

function removeErrorFade(evt) {
	dojo.graphics.htmlEffects.colorFadeTo(evt.target, '#FFF', 100, 0)
	dojo.event.disconnect(evt.target.id, "onblur", "removeErrorFade");
}

function showPanel(button, panelName) { 
	var container = button.findParentWithClass('div', 'panelContainer');
    $A(container.getElementsByTagName('div')).each( function(elem) {
        if (elem.id.indexOf("Content") > -1) {
            elem.style.display = 'none'; 
		}
	});

    $A(container.getElementsByTagName('li')).each( function(elem) {
        if (elem.id.indexOf("Button") > -1) { 
        	elem.removeClassName('panelSelected');
		}
	});
	
    $(panelName + 'Content').style.display = 'block'; 
    $(panelName + 'Button').addClassName('panelSelected');
}

function textCounter(field,maxlimit) {
	if (field.value.length > maxlimit)
	field.value = field.value.substring(0, maxlimit);
}

Element.addMethods( {
	findParentWithClass: function(element, strTagName, strClassName) {
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    
	    var oElement = element;
	    while (oElement.parentNode != document) {
	    	oElement = oElement.parentNode;
	        if(oRegExp.test(oElement.className)){
	        	return $(oElement);
	        }   
	    }
	    return null;
	}
});

	function ajaxSubmit(form, responseDiv, callback) {
	
		new Ajax.Request($(form).action,
			{ parameters: Form.serialize(form),
				onSuccess: function(request) {
				Element.update(responseDiv, request.responseText);
				if (callback) {
					eval(callback);
				}
			} 
		});
		return false;
	}
	
	function ajaxLoad(url, responseDiv, callback) {
	
		new Ajax.Request(url,
			{ onSuccess: function(request) {
				Element.update(responseDiv, request.responseText);
				if (callback) {
					eval(callback);
				}
			} 
		});
		return false;
	}
	
	function wwFormID(formName, fieldName) {
		return formName + "_" + fieldName.replace("\.", "_");
	}
	