// copies an object or array (rather than creating a reference). Handles recursion.
function copy(a){
	var r;
	if (typeof a === 'object' && typeof a.length === 'number' && !(a.propertyIsEnumerable('length'))){
		r = [];
		for (var i = 0, il = a.length; i < il; i++)
			r[i] = copy(a[i]);
	}
	else if (typeof a === 'object'){
		r = {};
		for (i in a)
			r[i] = copy(a[i]);
	}
	else
		r = a;
	return r;
};
/*
// Ensure that all checkboxes on forms are submitted - whether they are checked or not...
addEvent(window,'load',function(){
	var eleForms = document.getElementsByTagName("form");
	for (var x = 0; x < eleForms.length; x++) { 
		var f = eleForms[x];
		addEvent(f, 'submit', function () { submitAll(f) }); // save state on unload
	}
	
	var submitAll = function(fm){
		for (var i=0;i<fm.elements.length;i++) {
			var e = fm.elements[i];
			if (e.type=='checkbox' && !e.disabled && !e.checked) {
				var hiddenEl = document.createElement("input");
				hiddenEl.type = 'hidden';
				hiddenEl.name = e.name;
				hiddenEl.value = '';
				fm.appendChild(hiddenEl);
				hiddenEl = null;
			}
		}
	}	
});
*/
// see http://knowledge-base.server/making-nice-link-lists/

// applies onclick events to the first parent of a link, and mouseover styles
// el can be a reference to an element, or the id of said element
// parentTag is the type of parent tag we're looking for- for example, li, or tr. If not set, the first parent found will be used
// overClass is the class that will be added onmouseover
// applyClass is the class of the links to be processed- others will be ignored if this is set
function fixLinks(container, parentTag, overClass, applyClass){
	if (typeof container == 'string')
		container = $(container);
	else
		base2.DOM.bind(container);
	//console.log(container);
	
	// loop through all the links within the element and apply events. 
	var links = container.querySelectorAll('a'), link, el, att;
	for (var i = 0, j = links.length; i < j; i++){
		link = links.item(i);
		
		if (applyClass && !link.classList.has(applyClass))
			continue;
		
		// find appropriate parent
		el = link.parentNode;
		while (parentTag && parentTag.toUpperCase() != el.tagName && el.parentNode)
			el = el.parentNode;
		
		base2.DOM.bind(el);
		
		
		// apply onclick events to parent
		if (att = link.getAttribute('href')){
			att = att.replace(/\"/,'\"');
			if (att.match(/^javascript\:/))
				addEvent(el, 'click', new Function(att.replace(/^javascript\:/,'')));
			else
				addEvent(el, 'click', (function(location){
					return function(){
						window.location = location;
					};
				})(att));
		}
		else if(att = link.getAttribute('onclick')){
			addEvent(el, 'click', new Function(att));
		}
		else continue; 
		

		
		// kill click event if link is clicked, so we don't get the same event firing twice
		addEvent(link, 'click', function(e){
			return killEvent(e);
		});		
		
		el.style.cursor = 'pointer';
		if (overClass){
			(function(el){
				addEvent(el, 'mouseover', function(){
					el.classList.add(overClass);
				});
				addEvent(el, 'mouseout', function(){
					el.classList.remove(overClass);
				});
			})(el);
		}
	}
};


// sets a select box's selected index according to value passed
function setSelect(s, v){
	for (var i = 0, il = s.options.length; i < il; i++){
		if (s.options[i].value == v)
			s.selectedIndex = i;
	}
};

var popupCal = {
 calRegistered : Array(),
 
 init : function(field, _align){ 
  
  if (!field.id)
   field.setAttribute('id', field.getAttribute('name'));
 
  if (this.inArray(field.id, this.calRegistered))
   return;
   
  this.calRegistered[this.calRegistered.length] = field.id;
  if (!_align){
		_align = "Tr";
  }
  Calendar.setup({
   inputField     :    field.id,       // id of the input field
   ifFormat       :    "%d-%m-%Y",       // format of the input field
   singleClick    :    true,           // double-click mode
   step           :    1,                // show all years in drop-down boxes (instead of every other year as default)
   align     :    _align,
   eventName      :    "focus",
   cache     : 1
  });
  
 },
 
 inArray : function(needle,haystack){
  var retVal = false;
  for (z in haystack){
   if (needle == haystack[z])
    retVal = true;
  }
  return retVal
 }
};

// finds current link and adds 'current' class to it
function fixNavMenu(){
	var nav = document.getElementsByTagName('a'),
	loc = (window.location + '').replace(/\/{0,1}$/, '');
	
	for (var i = 0; i < nav.length; i++){
		if ((nav[i].href).replace(/\/{0,1}$/, '') == loc) {
			nav[i].className += ' current';
		}
	}
};

addEvent(window, 'load', function() {
	fixNavMenu();
});


// on checking/unchecking checkbox of object 't' all checkboxes of class_name are checked/unchecked
function select_all(t,class_name){
	chbxs = document.getElementsByTagName("input");
	state = false;
	if(t.checked == true){	
		state =true;
	}
	
	for (var i=0; i<chbxs.length; i++) {
		chbx = chbxs[i];
		if(chbx.className.indexOf(class_name) != -1){
			chbx.checked = state;
		}
	}	
}
// used to uncheck checkbox with id
function uncheck_selector(id){
	document.getElementById(id).checked = false;
}

// clears form of object oForm
function clearForm(oForm) {
	var elements = oForm.elements;
	oForm.reset();
	
	for(i=0; i<elements.length; i++) {
		field_type = elements[i].type.toLowerCase();
		
		switch(field_type) {
			case "text": 
			case "password": 
			case "textarea":
			case "hidden":	
				elements[i].value = ""; 
				break;
			case "radio":
			case "checkbox":
				if (elements[i].checked) {
					elements[i].checked = false; 
				}
				break;
			case "select-one":
			case "select-multi":
				elements[i].selectedIndex = -1;
				break;
			default: 
				break;
		}
	}
}