
// prototype-like functions
function $(myid) {
  return document.getElementById(myid);
}

function changeClass(myid, myclass) {
  if (myclass == undefined) myClass = '';
  if ($(myid)) $(myid).className = myclass;
}

function in_array(myArr, myVal) {
	var len = myArr.length;
	for (var x = 0 ; x < len; x++) {
	//alert('testing: '+myArr[x] + ' vs '+myVal);
		if ( myArr[x] == myVal ) return true;
	}
	return false;
}

// check form input, return false if value is in arguments
// checkInputValue(inputObj, value1, value2, ...)
function checkInputValue() {
  var args = checkInputValue.arguments;
  var inputObj = args[0];
  for (var j = 1; j < args.length; j++) {
    if (args[j] == inputObj.value) return false;
  }
  return true;
}

// check form input, clear default value from input
// checkInputValue(inputObj, defaultValue1, defaultValue2, ...)
function clearDefaultValue() {
  var args = clearDefaultValue.arguments;
  var inputObj = args[0];
  for (var j = 1; j < args.length; j++) {
    if (args[j] == inputObj.value) inputObj.value = '';
  }
}

function clearSelectBox(selectObj) {
  while (selectObj.options.length > 0) {
    selectObj.options[0] = null;
  }
}

function insertAfter(parent, node, referenceNode) {
  parent.insertBefore(node, referenceNode.nextSibling);
}

// nifty function to find {x,y} coords of any object in the window
function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    curleft = obj.offsetLeft
    curtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      curleft += obj.offsetLeft
      curtop += obj.offsetTop
    }
  }
  return [curleft,curtop];
}

// open new browser window to print image.
var win_printImage;
function printImg(fileId, displaymeta) {
  if (displaymeta == null) displaymeta = false;
  var m = (displaymeta) ? '1' : '0';
  win_printImage = window.open('print_image.php?fid='+fileId+'&m='+m, win_printImage, 'print_window');
  win_printImage.focus();
}

// clean up
function returnGet(parentObj) {
  var inputs = parentObj.getElementsByTagName("SELECT");
  for (var a = 0; a < inputs.length; a++) {
    alert(inputs[a].id);
  }
}

// simple date validation
function isValidDate(year, month, day) {
  if (year == '' && month == '' && day == '') return true;
  if (month < 1 || month > 12) return false;
  if (day < 1 || day > 31) return false;
  if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
  if (month == 2) {
    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
    if (day > 29 || (day==29 && !isleap)) return false;
  }
  return true;
}

function confirmFileDelete(name) {
	return confirm('Are you sure you want to delete: '+ name +'?');
}

function confirmRemove(file_name, goto_url) {
  if (confirm('Are you sure you wish to remove "'+file_name+'" from this collection?'+"\n"+'(Note: this will not delete the item from the overall archive.)')) {
    document.location.href=goto_url;
  }
}

function confirmDelComment(goto_url) {
  if (confirm('Are you sure you wish to remove this comment?')) {
    document.location.href=goto_url;
  }
}

// display "warnDeceased" div box, move to correct location, and fill with info
function warnDeceased(tdObj, goToHref) {

  var warnDiv = $('warnDeceased');
  var people =  $('warnDeceasedPeople');

  warnDiv.style.display='block';
  // note: reposition div _after_ displaying (otherwise Safari will crash)
  // create a very very small jump sometimes, but ....
  var coors = findPos(tdObj);
  var x = coors[0] - (parseFloat(warnDiv.offsetWidth) / 2) + 50; // TODO: change magic number to 1/2 width of cell
  if (x <= 10) x = 10;
  var y = coors[1];
  warnDiv.style.left = x + 'px';
  warnDiv.style.top  = y + 'px';

  // list people
  people.innerHTML = '';
  var myDiv, myBr;
  for (var a = 2; a < warnDeceased.arguments.length; a++) {
      myDiv = document.createElement("SPAN");
      myDiv.innerHTML = warnDeceased.arguments[a];
      myBr = document.createElement("BR");
      people.appendChild(myDiv);
      people.appendChild(myBr);
  }

  // create "Yes" button onclick value
  var myInputs = warnDiv.getElementsByTagName("INPUT");
  for (a = 0; a < myInputs.length; a++) {
  	if (myInputs[a].value.indexOf("Yes") != -1) {
  	  myInputs[a].onclick = function() { document.location.href = goToHref; }
  	}
  }

}

// from http://www.robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};
