/* Subversion: $Id: content.js 35 2005-03-18 17:34:36Z rhorton $ */

var MULTIPLE_EDIT = 0;

function showCalendar(calendar, link, format, inputId) {

	var i = document.getElementById(inputId);
	calendar.select(i, link, format, i.value);

	return false;
}


// invoked when a user clicks the checkbox to either expire or delete a record
function SetExpireFlag(b) {
  var f = document.forms['confirmDelete'];
  if (b) {
	  f.expiryFlag.value = 1;
  } else {
  	f.expiryFlag.value = 0;
  }
//	f.submit();
}



function clickContentBox(box, trueValue, falseValue) {

	var pattern = box.id.replace(/checkbox_/, "");
	var checkbox = get(pattern);

	if (! checkbox) {
		alert('System error - this field may not be updated in the database');
	}

	if (box.checked) {
		checkbox.value = trueValue;
	} else {
		checkbox.value = falseValue;
	}
}

function clickMultipleEdit(checked) {

	if (checked) {
		MULTIPLE_EDIT++;
	} else {
		MULTIPLE_EDIT--;
	}

	var button = document.getElementById('submitMultipleEdit');
	if (MULTIPLE_EDIT) {
		button.removeAttribute('disabled');
	} else {
		button.setAttribute('disabled', 'disabled');
	}
}

function SubmitSearch() {
	document.claeroForm.action.value = "search";
	document.claeroForm.submit();
}

function CancelSearch() {
	document.claeroForm.action.value = "cancel_search";
	document.claeroForm.submit();
}

function SubmitAdd() {
	document.claeroForm.action.value = "add";
	document.claeroForm.submit();
}

function RecordDetail(id) {;
  var f = document.forms['claeroForm'];
	f.action.value = "detail";
	f.id.value = id;
	f.submit();
}

function RecordEdit(id) {
  var f = document.forms['claeroForm'];
  f.elements['action'].value = "edit";
	f.id.value = id;
	f.submit();
}

function RecordDelete(id) {
	if (confirmDelete(id)) {
	  var f = document.forms['claeroForm'];
		f.action.value = "delete";
		f.id.value = id;
		f.submit();
	}
}

function RecordCustom(id,action,target) {
	if (confirmDelete(id)) {
	  var f = document.forms['claeroForm'];
		f.action.value = action;
		f.id.value = id;

		if (target != null){
		    f.target = target;
		}

		f.submit();
	}
}

function confirmDelete(name) {
//	input_box=confirm("Are you sure you want to remove '" + name + "' ?");
//	if (input_box==true) {
		return true;
//	}
}

function EditRecords() {

	var f = document.forms['claeroForm'];

	if (countCheckedBoxes('claeroForm', 'ids[]')) {
		f.action.value = "edit_multiple";
		f.submit();
	} else {
			alert('Please select atleast one record to edit.');
	}
}

function RecordAdd(theRecord) {
	var f = document.forms['claeroForm'];
	f.action.value = "add";
	f.id.value = theRecord;
	f.submit();
}

function CancelForm(theForm, url) {

    // no url was passed, so submit the form
    if (arguments.length == 1) {
	    theForm.action.value = "cancel";
	    theForm.submit();

    // a url was passed, so use that to redirect the browser
    } else {
        document.location = url;
    }
}

function Download(url) {

//	var win = window.open(url, "download");
	document.location = url;
}

function EditRecord(theForm) {
	theForm.action.value = "edit";
	theForm.submit();
}

// used by CreateSearchResults' "checkAllBoxes" option
function checkAllCheckBoxes(f, name, checked) {
	claeroFormBoxes = ! claeroFormBoxes;
	if (arguments.length == 2) { checked = true; }
	var form = getElement(f);
	var children = getByTagName(f, "input");
	for (var i = 0; i < children.length; i++) {
		if (children[i].type == "checkbox") {
			if (!name || (name && children[i].name == name)) {
				children[i].checked = claeroFormBoxes;
			}
		}
	}
	return false;
}

// get a reference to an element by id
function getElement(id) {
	if (typeof id == "string") {
		return document.getElementById(id);
	} else {
		return id;
	}
}
