// Swap text style when treating submit button as a link [for styling]
function button_as_link(div, change) {
	
	var ele = document.getElementById(div);
	
	if(change == true){
		ele.style.color = BUTTON_AS_LINK_HOVER_COLOR;
	} else{
		ele.style.color = BUTTON_AS_LINK_DEFAULT_COLOR;
	}
}

// Prompt user to make sure submit button was clicked correctly.
function confirmSubmit(){
	var agree=confirm("Are you sure you wish to continue? \n\n This cannot be undone.");
	if (agree)
		return true ;
	else
		return false ;
}

// Determing what form a given element is a part of/within.
function GetFormName(form_element){
	
	// Loop through all forms on a page/document.
	for(i=0; i<document.forms.length; i++){
		
		// Make sure current form elemnt is not NULL
		if(document.forms[i].getAttribute("name") != null){
			
			// Loop through all elements within a form within a document
			for(j=0; j<document.forms[i].elements.length; j++){
				
				// Check to see if current element name is the 'form_element' we are looking for
				if(form_element == document.forms[i].elements[j].getAttribute("name")){
					
					// If check passes, return the current form [object HtmlFormElement]
					return document.forms[i];
					
				}
			}
		}	
	}
}

// Submit Form. Determine which form to submit by getting the name of the form associated with the element calling the function.
function submit_form(element_name){
	
	// Determine name of the form that element_name is associated with
	var form_name = GetFormName(element_name);
	
	// Submit form
	form_name.submit();
	
}

// Function to clear all values on a form. This is done through DOM instead of the standard HTML 'reset' input button to allow for cases of clearing preloaded values.
function clear_form(element_name){
	
	// Get Name of Form from element_name
	var temp = GetFormName(element_name.name)
	
	// Loop through all form elements on page and 'reset'/'clear' value based on type
	for(i=0; i<temp.length; i++){
		
		// Clear <Checkbox>
		if(temp.elements[i].type == "checkbox"){
			temp.elements[i].checked = false;
		}
		// Clear <radio>
		else if(temp.elements[i].type == "radio"){
			temp.elements[i].checked = false;
		}
		// Clear <file>
		else if(temp.elements[i].type == "file"){
			remove(temp.elements[i].name);
		}
		// Clear <password>
		else if(temp.elements[i].type == "password"){
			temp.elements[i].value = "";
		}
		// Clear <text>
		else if(temp.elements[i].type == "text"){
			temp.elements[i].value = "";
		}
		// Clear <textarea>
		else if(temp.elements[i].type == "textarea"){
			temp.elements[i].value = "";
		}
		// Clear <select-one>
		else if(temp.elements[i].type == "select-one"){
			for(j=0; j<temp.elements[i].options.length; j++){
				temp.elements[i].options[j].selected = null;//);// = 0;
			}
		}
		// Clear <select-one>
		else if(temp.elements[i].type == "select-multiple"){
			for(j=0; j<temp.elements[i].options.length; j++){
				temp.elements[i].options[j].selected = null;//);// = 0;
			}
		}		
	}
}

// Removes control on file for file upload field CLEARING [see: clear_form(...)]
function remove(control){
    var who=document.getElementsByName(control)[0];
    var who2= who.cloneNode(false);
    who2.onchange= who.onchange;
    who.parentNode.replaceChild(who2,who);
}


