
// 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, '');
  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; }
  	}
  }

}
