/* --------------------------------------------------- */
/* name:     j5admin.js                                */
/* author:   j5 development                            */
/* purpose:  javascript administrative functions       */
/* --------------------------------------------------- */

/* --------------------------------------------------- */
/* common "add" function                               */
/* --------------------------------------------------- */
function addIt(theForm,actionPage) {    
  thisForm = eval("document." + theForm);
  thisForm.recid.value = "";
  thisForm.mode.value = "add";
  thisForm.operation.value = "load";
  thisForm.action = actionPage;
	thisForm.method = "post";
  thisForm.submit();
}

/* --------------------------------------------------- */
/* common "edit" function                              */
/* --------------------------------------------------- */
function editIt(theForm,theID,actionPage) {
  thisForm = eval("document." + theForm);
  thisForm.recid.value = theID;
  thisForm.mode.value = "edit";
  thisForm.operation.value = "load";
  thisForm.action = actionPage;
	thisForm.method = "post";
  thisForm.submit();
}

/* --------------------------------------------------- */
/* common "delete" function                            */
/* --------------------------------------------------- */
function deleteIt(theForm,theID,actionPage,label) {    
  if (!label) { label = "record"; }
  var msg = "Are you sure you want to delete this " + label + "?";
  if (confirm(msg)) {
    thisForm = eval("document." + theForm);
    thisForm.recid.value = theID;
    thisForm.mode.value = "delete";
    thisForm.operation.value = "submit";
    thisForm.action = actionPage;
  	thisForm.method = "post";
    thisForm.submit();
  }
}

/* --------------------------------------------------- */
/* common "upload" function                            */
/* --------------------------------------------------- */
function uploadIt(theForm,actionPage) {
  thisForm = eval("document." + theForm);
  thisForm.action = actionPage;
	thisForm.encoding = "multipart/form-data";
	thisForm.method = "post";
  thisForm.submit();
}

/* --------------------------------------------------- */
/* generic "post" function                             */
/* --------------------------------------------------- */
function postIt(theForm,theID,theMode,theOp,actionPage) {
  thisForm = eval("document." + theForm);
  thisForm.recid.value = theID;
  thisForm.mode.value = theMode;
  thisForm.operation.value = theOp;
  thisForm.action = actionPage;
	thisForm.method = "post";
  thisForm.submit();
}

/* --------------------------------------------------- */
/* "sort order" functions                              */
/* --------------------------------------------------- */
function orderUp(theForm,theBox) {
	chosen = eval("document." + theForm + "." + theBox);
	/* check if the selected item is already at the top */
	if (chosen.selectedIndex==0 || chosen.selectedIndex==-1) {return;}
	/* swap the selected item with the one above it */
	var theItem = chosen.selectedIndex-1;
	var moveOut = new Option(chosen.options[chosen.selectedIndex-1].text,chosen.options[chosen.selectedIndex-1].value);
	var moveIn  = new Option(chosen.options[chosen.selectedIndex].text,chosen.options[chosen.selectedIndex].value);
	chosen.options[chosen.selectedIndex-1] = moveIn;
	chosen.options[chosen.selectedIndex]   = moveOut;
	chosen.options[theItem].selected = true;
}
function orderDown(theForm,theBox) {
	chosen = eval("document." + theForm + "." + theBox);
	/* check if the selected item is already at the bottom */
	if (chosen.selectedIndex==chosen.length-1 || chosen.selectedIndex==-1) {return;}
	/* swap the selected item with the one below it */
	var theItem = chosen.selectedIndex+1;
	var moveOut = new Option(chosen.options[chosen.selectedIndex+1].text,chosen.options[chosen.selectedIndex+1].value);
	var moveIn  = new Option(chosen.options[chosen.selectedIndex].text,chosen.options[chosen.selectedIndex].value);
	chosen.options[chosen.selectedIndex+1] = moveIn;
	chosen.options[chosen.selectedIndex]   = moveOut;
	chosen.options[theItem].selected = true;
}
function orderSubmit(theForm,theBox,theHidden) {
  thisForm = eval("document." + theForm);
	chosen = eval("document." + theForm + "." + theBox);
	sortOrder = eval("document." + theForm + "." + theHidden);
	sortOrder.value = "";
	for (var i=0; i < chosen.length; i++) {
		sortOrder.value += chosen.options[i].value;
		if (i != chosen.length-1) { sortOrder.value += ","; }
	}
	thisForm.submit();
}

/* --------------------------------------------------- */
/* checkbox all functions                              */
/* --------------------------------------------------- */
function checkboxAll(theForm,theArray,cbo) {
  thisForm = eval("document." + theForm);
	cbArray = eval("document." + theForm + "." + theArray);
	for (var i=0 ; i < cbArray.length ; i++) {
		cbArray[i].checked = cbo.checked;
	}
}
