/*

    glogal javascript file
    
    functions and declarations placed here will
    be defined for every page
    
*/

//Cross-Browser,multi-frame get element
function getElement(id,scope) {

    if (scope == null)
        scope = "document";
    else
        scope = scope + ".document";


    if (document.all) {
        return eval(scope).all[id];
    }
    else if (document.getElementById) {
        return eval(scope).getElementById(id);
    }

}

// Cross-browser function to create XML HttpRequest object
function getXMLHttp() {
    var objXMLHttp=null

    if (window.XMLHttpRequest)
        objXMLHttp=new XMLHttpRequest();
    else if (window.ActiveXObject)
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");

    return objXMLHttp;
}


// Passthrough call to retrieve text from whatever editor is used in JS Module EDIT_CONTROL
function getEditorValue( instanceName ) { return tinyMCE.getInstanceById(instanceName).getBody().innerHTML }

// Passthrough call to set text for whatever editor is used in JS Module EDIT_CONTROL
function setEditorValue( instanceName, newContent ) { tinyMCE.getInstanceById(instanceName).getBody().innerHTML = newContent; }

// Takes an XML body and returns the data for the provided elementName
function getXMLData(response,elementName) { return response.getElementsByTagName(elementName)[0].firstChild.data }

function getXMLNodeValue(xmlBody,tagName) {
	
	// Check if node has data, if not, return empty string
	var hasChild = xmlBody.getElementsByTagName(tagName)[0].childNodes.length;
	if (hasChild == 0) return '';
	
	var node = xmlBody.getElementsByTagName(tagName)[0].childNodes[0];		
	return node.nodeValue.trim();
}

// Adds an onload event to a page
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// Adds an onUnload event to a page
function addunLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = func;
	}
	else {
		window.onunload = function() {
			oldonunload();
			func();
		}
	}
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

String.prototype.swapBR = function() {

	var brReplace = new RegExp("<br />", "g");
	return this.replace(brReplace,'\r\n');	
	
}

//Advanced Email Check credit-
//By JavaScript Kit (http://www.javascriptkit.com)
//Over 200+ free scripts here!
// filter test by them, I modified their logic to make the code much
// less cludgy
function validEmail(str) {

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str)) return true;
	
	return false;			
}

document.createNamedElement = function(type, name) {
  var element;
  try {
    element = document.createElement('<'+type+' name="'+name+'">');
  } catch (e) { }
  if (!element || !element.name) { // Not in IE, then
    element = document.createElement(type)
    element.name = name;
  }
  return element;
}


function c_showCover() {

	// toggle cover display
	getElement('cover').style.display = 'block';
	
}

function c_hideCover() {

	// toggle cover display
	getElement('cover').style.display = 'none';
	
}

function c_showProcessing() {

	// toggle cover display
	getElement('processingGraphic').style.display = 'block';
	
}

function c_hideProcessing() {

	// toggle cover display
	getElement('processingGraphic').style.display = 'none';
	
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(formID,radioName) {

    radioObj = document.forms[formID].elements[radioName];

	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function deleteChildren(el) {			
			
	// Delete all child nodes of el
	if (el.hasChildNodes()) {
	    while ( el.childNodes.length >= 1 )
	        el.removeChild(el.firstChild);    
	}			
	
}