/* ------------------------------------------------------------------------------- +

	File:		forms.js
	Author:		Ben Nadel
	Date:		February 02, 2005
	Desc:		This has functions for form-related actions.

+ ------------------------------------------------------------------------------- */


// Forwards browser window to the given location string. If the form
// object was passed as a second argument, then the form will be disabled
function Cancel(strUrl, objForm){
	// Check to see if the form was passed
	if (objForm){
		DisableForm(objForm);
	}
	
	// Redirect browser
	window.location.href = strUrl;
}


// Deletes the selected options from the select box
function DeleteSelectedOptions(objSelect){
	var arrOptions = objSelect.options;
	
	// First, move all the selected options to the bottom
	MoveSelectedOptionsToBottom(objSelect);
	
	// Now, loop over options and convert selected to null
	for (var i = (arrOptions.length - 1) ; i >= 0 ; i--){
		// Check to see if the option is selected
		if (arrOptions[i].selected){
			// Make this option null 
			arrOptions[i] = null;
		}
	}
}


// This is called when submitting a form. It will go through the 
// form elements and disable all the buttons, submits and otherwise.
function DisableForm(objForm){
	for (var i = 0 ; i < objForm.elements.length ; i++){
		if (objForm.elements[i].tagName.toLowerCase() == "input"){
			if ((objForm.elements[i].type.toLowerCase() == "button") ||
				(objForm.elements[i].type.toLowerCase() == "submit") ||
				(objForm.elements[i].type.toLowerCase() == "reset") ||
				(objForm.elements[i].type.toLowerCase() == "image")
				){
				// Disable the input
				objForm.elements[i].disabled = true;
				
				// Only change the class if it's not an image
				if (objForm.elements[i].type.toLowerCase() != "image"){
					objForm.elements[i].className = "button-disabled";
				}
			}		
		}
		
		// Disable any events that might trigger a form-submit
		objForm.elements[i].onclick = null;
		objForm.elements[i].onchange = null;
		objForm.elements[i].onfocus = null;
		objForm.elements[i].onblur = null;
		objForm.elements[i].onkeydown = null;
		objForm.elements[i].onkeyup = null;
	}
	
	// Also, since the form has already been submitted, let's have it return false
	// if it tries to submit again. I am worried though that this might stop the
	//previous form submission.
	objForm.onsubmit = new function(){
		return(false);
	}
}


// This creates a deliniated list of values of all the options
// in the given select box. It uses both the selected and the 
// non-selected options when building the list.
function GetSelectOptionsValueList(objSelect, strDelimiter){
	var arrOptions = objSelect.options;
	var strValueList = "";
	strDelimiter = ParamArgument(strDelimiter, ",");
	
	// Loop over the options and get the values
	for (var i = 0 ; i < arrOptions.length ; i++){
		// Check to see if we need to append the delimiter
		if (i > 0){
			strValueList += strDelimiter;
		}
		
		// Append the next value
		strValueList += arrOptions[i].value;
	}
	
	// Return the value list
	return(strValueList);
}


// This creates a deliniated list of values of only the options
// in the given select box that are currently selected.
function GetSelectedOptionsValueList(objSelect, strDelimiter){
	var arrOptions = objSelect.options;
	var strValueList = "";
	strDelimiter = ParamArgument(strDelimiter, ",");
	
	// Loop over the options and get the values
	for (var i = 0 ; i < arrOptions.length ; i++){
		// Check to see if the option is selected
		if (arrOptions[i].selected){
			// Check to see if we need to append the delimiter
			if (strValueList.length > 0){
				strValueList += strDelimiter;
			}
			
			// Append the next value
			strValueList += arrOptions[i].value;
		}
	}
	
	// Return the value list
	return(strValueList);
}


// Moves the selected options in a select box down
function MoveSelectedOptionsDown(objSelect){
	var arrOptions = objSelect.options;
	
	// Check to make sure the select has more than one size
	if (objSelect.size > 1){
	
		// Loop over the options and swap out
		for (var i = (arrOptions.length - 2) ; i >= 0 ; i--){
			// Check to see that the current option is highlighted and
			// that the one above it is NOT highlighted
			if (arrOptions[i].selected && !arrOptions[i+1].selected){
				// Swap options
				SwapSelectOptions(arrOptions[i], arrOptions[i+1]);
			}
		}
		
	}
}


// Moves the selected options to the bottom of the select box
function MoveSelectedOptionsToBottom(objSelect){
	var arrOptions = objSelect.options;
	
	// Check to make sure the select has more than one size
	if (objSelect.size > 1){
	
		// Loop over the array and swap options
		for (var i = 0 ; i <= (arrOptions.length - 2) ; i++){
			// Do a double loop to make sure the selected one always moves as low
			// as it can possibly go
			for (var j = (arrOptions.length - 2) ; j >= i ; j--){
				// Check to see if the current option is selected and 
				// that the next option is NOT selected
				if (arrOptions[j].selected && !arrOptions[j+1].selected){
					// Swap these two options
					SwapSelectOptions(arrOptions[j], arrOptions[j+1]);
				}
			}
		}
		
	}
}


// Moves the selected options to the top of the select box
function MoveSelectedOptionsToTop(objSelect){
	var arrOptions = objSelect.options;
	
	// Check to make sure the select has more than one size
	if (objSelect.size > 1){
		
		// Loop over the array and swap options
		for (var i = arrOptions.length ; i >= 0 ; i--){
			// Do a double loop to make sure the selected one always moves as high
			// as it can possibly go
			for (var j = 1 ; j < i ; j++){
				// Check to see if the current option is selected and 
				// that the previous option is NOT selected
				if (arrOptions[j].selected && !arrOptions[j-1].selected){
					// Swap these two options
					SwapSelectOptions(arrOptions[j], arrOptions[j-1]);
				}
			}
		}
		
	}
}


// Moves the selected options in a select box up
function MoveSelectedOptionsUp(objSelect){
	var arrOptions = objSelect.options;
	
	// Check to make sure the select has more than one size
	if (objSelect.size > 1){
	
		// Loop over the options and swap out
		for (var i = 1 ; i < arrOptions.length ; i++){
			// Check to see that the current option is highlighted and
			// that the one above it is NOT highlighted
			if (arrOptions[i].selected && !arrOptions[i-1].selected){
				// Swap options
				SwapSelectOptions(arrOptions[i], arrOptions[i-1]);
			}
		}
		
	}
}


// This assumes that there is an attribute in an input box called "defaultvalue". 
// This removes the value of a text field if it is equal to the default value.
function RemoveInputDefaultValue(objInput){
	var strDefaultValue = ParamArgument(objInput.getAttribute("defaultvalue"), "");
	
	// Check to see if the current value is the default value
	if (objInput.value == strDefaultValue){
		// It is the default value, so remove it
		objInput.value = "";
	} 
}


// This assumes that there is an attribute in an input box called "defaultvalue". 
// This puts the default value into the text field on blur if the text field is empty.
function SetInputDefaultValue(objInput){
	var strDefaultValue = ParamArgument(objInput.getAttribute("defaultvalue"), "");
	
	// Check to see if the current value is empty
	if (objInput.value == ""){
		// It is empty, so put the default value back in
		objInput.value = strDefaultValue;
	} 
}


// This merely submits the data form and then disables it.
function SubmitForm(objForm){
	objForm = ParamArgument(objForm, document.forms["data_form"]);
	
	// Submit the form
	objForm.submit();
	
	// Disable the current form to remove duplicate submissions
	DisableForm(objForm);
}


// This submits the form directly to a new action page, no going back 
// to the same page to process form information first. This should be
// used when going from a list page to detail action page. Especially
// useful when you are passing around "wizzard" type data.
function SubmitFormWithAction(strAction, strActionID){
	var objForm = document.forms["data_form"];
	var objAction = objForm.elements["action"];
	var objUniqueID = objForm.elements["unique_id"];
	var objSubmitted = objForm.elements["submitted"];
	var objDataBaseID = null;
	strActionID = ParamArgument(strActionID, "0");
	
	// The value of the unique id field should hold the name of the 
	// unique id value that we are using from the database. 
	objDataBaseID = objForm.elements[objUniqueID.value];
	
	// Make sure that submitted is zero since we are going to a new page
	objSubmitted.value = "0";
	
	// Set the action for the new page
	objAction.value = strAction;
	
	// Set the unique id for the new page if we have a unique id
	if (objDataBaseID){
		objDataBaseID.value = strActionID;
	}
	
	// Submit the form 
	SubmitForm(objForm);
}


// This is used with the data_form form pages. It submits the form back 
// to the same page after setting the new_action input value.
function SubmitFormWithNewAction(strNewAction){
	var objForm = document.forms["data_form"];
	var objNewAction = objForm.elements["new_action"];
	
	// Set the new action
	objNewAction.value = strNewAction;
	
	// Submit the form
	SubmitForm(objForm);
}


// This is used with the data_form form pages. It submits the form back 
// to the same page after setting the a new input value.
function SubmitFormWithNewValue(strInputName, strInputValue){
	var objForm = document.forms["data_form"];
	var objInput = objForm.elements[strInputName];
	
	// Set the new value
	objInput.value = strInputValue;
	
	// Submit the form
	SubmitForm(objForm);
}


// This is used with the data_form form pages. It submits the form back
// to the same page after it sets up the sub action and the sub action
// id input values.
function SubmitFormWithSubAction(strSubAction, strSubActionID){
	var objForm = document.forms["data_form"];
	var objSubAction = objForm.elements["sub_action"];
	var objSubActionID = objForm.elements["sub_action_id"];
	
	// Set the sub action and action id
	objSubAction.value = strSubAction;
	objSubActionID.value = strSubActionID;
	
	// Submit the form
	SubmitForm(objForm);
}


// This basically submits the form with a sub action but no sub action ID.
// It is a bit of a hack, but it basically gets the form to submit back 
// to itself for "Update Data" type links.
function SubmitFormWithUpdate(){
	// Submit the form with sub action
	SubmitFormWithSubAction("update", "0");
}


// This swaps two options in a select box
function SwapSelectOptions(objOption1, objOption2){
	var objTempOption = new Option("", "");
	var arrProperties = new Array("text","value","className","selected");
	
	// Move all the properties of option 2 into the temp option
	for (var i = 0 ; i < arrProperties.length ; i++){
		objTempOption[ arrProperties[i] ] = objOption2[ arrProperties[i] ];
	}
	
	// Move all the properties of option 1 into option 2
	for (var i = 0 ; i < arrProperties.length ; i++){
		objOption2[ arrProperties[i] ] = objOption1[ arrProperties[i] ];
	}
	
	// Move all the properties of the temp option back to option 1
	for (var i = 0 ; i < arrProperties.length ; i++){
		objOption1[ arrProperties[i] ] = objTempOption[ arrProperties[i] ];
	}
}