
////////////////////////////////////////////////////////////////////////////////
// Ensure that all Node types are set 
//////////////////////////////////////////////////////////////////////////////// 

if (!window['Node']) {
    window.Node = new Object();
    Node.ELEMENT_NODE = 1;
    Node.ATTRIBUTE_NODE = 2;
    Node.TEXT_NODE = 3;
    Node.CDATA_SECTION_NODE = 4;
    Node.ENTITY_REFERENCE_NODE = 5;
    Node.ENTITY_NODE = 6;
    Node.PROCESSING_INSTRUCTION_NODE = 7;
    Node.COMMENT_NODE = 8;
    Node.DOCUMENT_NODE = 9;
    Node.DOCUMENT_TYPE_NODE = 10;
    Node.DOCUMENT_FRAGMENT_NODE = 11;
    Node.NOTATION_NODE = 12;
}

////////////////////////////////////////////////////////////////////////////////
// Images used by the showDiv() function
////////////////////////////////////////////////////////////////////////////////  

r_arrow = 'menuclosed.png'; 
d_arrow = 'menuopen.png'; 

////////////////////////////////////////////////////////////////////////////////
// Controls the display some element
//////////////////////////////////////////////////////////////////////////////// 

showDiv = function(id,tid) { 
	var img = document.getElementById(tid);
	var src = img.src; 
	var div = document.getElementById(id);
	if(div.style.display == 'block') { 
		div.style.display = 'none';
		var path = img.src.substring(0, (img.src.lastIndexOf('/'))+1);
		img.src = path+(r_arrow); 
		img.alt = '(show)';
	} 
	else { 
		div.style.display = 'block'; 
		var path = img.src.substring(0, (img.src.lastIndexOf('/'))+1);
		img.src = path+(d_arrow); 
		img.alt = '(hide)';
	} 
}


////////////////////////////////////////////////////////////////////////////////
// Add event listener
//////////////////////////////////////////////////////////////////////////////// 

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

////////////////////////////////////////////////////////////////////////////////
// Something do do with caching events...I don't know
////////////////////////////////////////////////////////////////////////////////

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);


////////////////////////////////////////////////////////////////////////////////
// Checks all input with the given name for duplicate values.  Duplicates are 
// highlighted in red.  If the second argument is true, also checks for empty 
// values and highlights them in yellow.  Returns true if there are no duplicate 
// or missing values; otherwise, returns false.
////////////////////////////////////////////////////////////////////////////////

function checkValues(name, req) {
	var check = true

	// Get all fields with this name
	var fields = document.getElementsByName(name)
	for(i=0;i<fields.length;i++) {
		var unique = true
		if (fields[i].value == '' && req) {
			// If the field is empty and is required, turn this field yellow
			fields[i].style.background = '#EEEE22'
			check = false
		} else {
			// Compare this field to all other fields with the same name
			for(j=0;j<fields.length;j++) {
				// Don't compare this field to itself, skip empty fields
				if (i == j) { continue }
				if (fields[i].value == '') { continue }

				// If a match is found, turn this field and the matching field red
				if (fields[i].value == fields[j].value) {
					fields[i].style.background = '#FF6644'
					fields[j].style.background = '#FF6644'
					// Note that this field's value is not unique
					unique = false
					check = false
				}
			}
			// If unique flag is true, turn this field white
			if (unique) { fields[i].style.background = '#FFFFFF' }
		}
	}
	
	return check
}


////////////////////////////////////////////////////////////////////////////////
// Functions for adding and removing elements from a page.  A namespace and an
// undo array must be created on the calling page to use the undo function.
////////////////////////////////////////////////////////////////////////////////

 function cloneElement(template_id, parent_id, name, display) {
	var ns = YAHOO.namespace(name);
	var tmp = document.getElementById(template_id); 
	var par = document.getElementById(parent_id); 
	var ins = tmp.cloneNode(true);
		if (display) { ins.style.display = display; }
		else { ins.style.display = 'block'; }
		ins.id = '';
	par.appendChild(ins);
	ns.undoArray.push(['a']); 
		
} 

function deleteElement(parent_id, name) {
	var ns = YAHOO.namespace(name);
	var par = document.getElementById(parent_id);
	var lc = par.lastChild; 
	if(lc) { 
		var un = par.removeChild(lc); 
		ns.undoArray.push(['r', un]);
	}
} 
 
function undo(parent_id, name) {
	var ns = YAHOO.namespace(name);
	if(ns.undoArray.length) { 
		par = document.getElementById(parent_id); 
		bac = ns.undoArray.pop();
		if(bac[0] == 'r') { 
			par.appendChild(bac[1]); 
		}
		else { 
			par.removeChild(par.lastChild); 
		}
	}
}
