// requires prototype.js
// Get element by id
function el(id) { return $(id); }
// value of element by id
function elval(id) { return $(id).value; }
// get first element with this name property
function elByName(name) { els = document.getElementsByName(name); if (els.length>0) {return els[0];} return null; }
function stopEvent(e)
{
alert("here");
    e = e || (window.event || {});
    if(e.stopPropagation) {e.stopPropagation();}
    e.cancelBubble = true;
    alert(e + " stop event");
    return true;
}
function emob(id, one, two){ $(id).innerHTML = '<'+'a hre'+'f="mai'+'lto:' + one + '@' + two + '">' + one + '@' + two + '</a>'; }
function preLoadImage(url){ img = new Image(); img.src = url; return img;}
/*
http://www.ejball.com/EdAtWork/2005/03/30/JavaScriptForEach.aspx
Let me explain:

* Calculating the array length outside the loop can be slightly faster.
* I pass the item index as the second argument to the called function, since that can often be useful. (The called function doesn’t need to declare it as an explicit argument if it doesn’t need it.)
* If the called function returns anything but undefined, the loop is terminated. This allows us to simulate what would be a break statement in a normal for loop. The ForEach method returns the value returned by the called function.
* If the called function returns undefined, the loop continues. (A function returns undefined if it gets to the end without hitting a return statement, if it hits a return statement without an expression, or if it hits a return undefined (naturally).) An explicit return statement without an expression thus simulates what would be a continue statement in a normal for loop.
* Using the call method allows us to specify this for the called function, which can be useful when calling ForEach from an object method.
* The objThis argument is optional, but I make sure that it is never undefined, because under Internet Explorer, the call method is slightly faster when the first argument is a valid object (passing undefined forces it to determine the “global” object itself).

e.g.
function SumArray(array)
{
  var nSum = 0;
  ForEach(array, function (n) { nSum += n; });
  return nSum;
}
*/
function ForEach(array, fn, objThis)
{
  objThis = objThis || this;
  var len = array.length;
  for (var n = 0; n < len; n++)
  {
    var r = fn.call(objThis, array[n], n);
    if (r !== undefined)
      return r;
  }
}


function FindInArray(array, find)
{
  var index = ForEach(array,
    function (item, n)
    {
      if (item == find)
        return n;
      return null;
    });
  return index === undefined ? -1 : index;
}
/* CollapsablePanel */
function toggleBox(blockId, toggleImageId, stateId) {
	var block = document.getElementById(blockId);
	var ctlImg = document.getElementById(toggleImageId);
	var stateInput = document.getElementById(stateId);
	stateInput.value = (block.style.display == "block");
	if (stateInput.value=='true') {
		block.style.display = "none";
		ctlImg.src = AddApplicationPath("/ui/formats/general/images/status_indicators/button_plus.gif");
		stateInput.value = 'false';
	}else{
		block.style.display = "block";
		ctlImg.src = AddApplicationPath("/ui/formats/general/images/status_indicators/button_minus.gif");
		stateInput.value = 'true';
	}
}

function textLimit(field, maxlen) { if (field.value.length > maxlen + 1) field.value = field.value.substring(0, maxlen); }

function purge(d) {
    var a = d.attributes, i, l, n;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n] = null;
            }
        }
    }
    a = d.childNodes;
    if (a) {
        l = a.length;
        for (i = 0; i < l; i += 1) {
            purge(d.childNodes[i]);
        }
    }
}
function removeChild(child){ // get around the IE DOM memory leak: http://javascript.crockford.com/memory/leak.html
	purge(child);
	child.parentNode.removeChild(child);
}
var FileUtils = {
    "BaseName" : function(fullpath) {
        var parts = fullpath.split("\\");
        parts = parts[parts.length-1].split("/");
        return parts[parts.length-1];
    }
}
// Various utilities for cross browser DOM manipulation
var DOMUtils = {
	"getEventTarget" : function(e){
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		return targ;
	},
	"toggleLayer" : function(_id) {
		el = document.getElementById(_id);
		el.style.display = el.style.display=='block' ? 'none' : 'block';
	},
	// DHTML DOM Functions
	"insertNodeAfter" : function(node, target){
		var aft = target.nextSibling;
		if (aft==null)
			target.parentNode.appendChild(node);
		else
			target.parentNode.insertBefore(node, aft);
	},
	"moveNodeUp" : function(node){
		if (!node) return;
		var prv = node.previousSibling;
		if (!prv || prv.previousSibling==null) return; // first node
		node.parentNode.insertBefore(node, prv);
	},
	"moveNodeDown" : function(node){
		if (node==null) return;
		var nxt = node.nextSibling;
		if (nxt==null) return; // last node
		DOMUtils.insertNodeAfter(node, nxt);
	},
	// create a node of the given tag within the parent
	"createNode" : function (tag, parent) {
		node = document.createElement(tag);
		parent.appendChild(node);
		return node;
	},
	// create a node of the given tag within the parent before the given child
	"createNodeBefore" : function (tag, parent, afterChild) {
		node = document.createElement(tag);
		parent.insertBefore(node, afterChild);
		return node;
	},
	// recolor row backgrounds by setting alternating css classes, ignoring rows with headerClass set
	"recolorRows" : function(tableNode, headerClass, contentClass, altContentClass){
		if (tableNode==null) return;
		var node = tableNode.firstChild;
		var cls = contentClass;
		while (node != null){
			if (node.className != headerClass && node.nodeName && node.nodeName == 'TR'){
				node.className = cls;
				cls = (cls==contentClass) ? altContentClass : contentClass;
			}
			node = node.nextSibling;
		}
	},
	"removeTableRowFor" : function(cellNode) {
	    if (cellNode==null) return;
	    var tr=cellNode;
	    while(tr!=null && tr.nodeName!="tr" && tr.nodeName!="TR")
	        tr = tr.parentNode;
	    this.removeChild(tr);
	},
	"purge" : function(d) {
        var a = d.attributes, i, l, n;
        if (a) {
            l = a.length;
            for (i = 0; i < l; i += 1) {
                n = a[i].name;
                if (typeof d[n] === 'function') {
                    d[n] = null;
                }
            }
        }
        a = d.childNodes;
        if (a) {
            l = a.length;
            for (i = 0; i < l; i += 1) {
                purge(d.childNodes[i]);
            }
        }
    },
    "removeChild" : function(child){ // get around the IE DOM memory leak: http://javascript.crockford.com/memory/leak.html
	    this.purge(child);
	    child.parentNode.removeChild(child);
    },
    "insertNodeCopy" : function(beforeNode, templateNodeParent) {
        //dbg(templateNodeParent.nodeName+":"+templateNodeParent.nodeType);
        var templateNode = templateNodeParent.firstChild;
        while (templateNode !=null && templateNode.nodeName=="#text"){
        //dbg(templateNode.nodeName+":"+templateNode.nodeType);
         templateNode = templateNode.nextSibling;
         }
        //dbg(templateNode.nodeName+":"+templateNode.nodeType);
        var newNode = DOMUtils.createNodeBefore("tr", $('menuItemContainer'), beforeNode);
        newNode.appendChild(templateNode.cloneNode(true));
        //dbg(templateNode.nodeName);
        //dbg(newNode.innerHTML);
        return newNode;
    },
    "firstChild" : function(node){
        c = node.childNodes;
        if (c) {
            l = c.length;
            for (i = 0; i < l; i += 1) {
                if (c[i].nodeName!="#text"){dbg("first child of "+node.innerHTML+" found "+ c[i].nodeName);
                  return c[i];
                }
            }
        }//dbg("first child of "+node.innerHTML+" not found");
        return null;
    }
}
/* write a debug message into a DOM element with the id 'debug' if debug=true is in the query parameters */
function dbg(msg){
  var regexp = /[\?&]debug=true/gi;
  if ($('debug')!=null
   && regexp.test(window.location)){
  	$('debug').innerHTML = $('debug').innerHTML + msg + "<br />";
	$('debug').style.display = 'block'; /* remove display:none, so use a css class for other styling */
  }
}
function debug(msg){ dbg(msg); }

function hilite_row(row){
    row.className = row.className+" hiliterow";
  }
function unhilite_row(row){
    row.className = row.className.replace(/ hiliterow$/, '');
}
var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function invi(flag)
{
	if (!DHTML) return;
	var x = new getObj('text');
	x.style.visibility = (flag) ? 'hidden' : 'visible'
}

var texttop = 400;

function move(amount)
{
	if (!DHTML) return;
	var x = new getObj('text');
	texttop += amount;
	x.style.top = texttop;
}


function changeCol(col)
{
	if (!DHTML) return;
	var x = new getObj('text');
	x.style.color = col;
}

function Validate(formToValidate) {
  var isFormValid = false;
  ResetFormValidation();  
  isFormValid = validateForm(formToValidate, false, true, false, true, 4);  
  if (! isFormValid) {  
      $('errorSummary').style.visibility = "";  
      $('errorSummary').style.display = "";  
  }
  return isFormValid;  
}  
  
function ResetFormValidation() {      
    $('errors').innerHTML = "";  
    $('errorSummary').style.visibility = "hidden";  
    $('errorSummary').style.display = "none";  
}  