﻿// Contains helper functions for managing data in HTML forms.

// Sets the inner html content for a the  main content iframe.
function setHtml(element, path) {
    element.src = path;
}

// Sets the inner text content for a control. This is different between IE and FireFox.
function setText(element, text) {
    if (browser != 'moz') {
        element.innerText = text;
    }
    else {
		element.value = text;
        //element.textContent = text;
    }
}


// Gets the inner text for a control. This is different between IE and FireFox
function getText(element) {
    if (document.all) {
        return element.innerText;
    }
    else {
        return element.textContent;
    }
}


// Leftover code from the previous version. This was mostly used to dynamically generate form elements.
// I'm leaving it in for now in case someone wants to try and re-use it.
// Gets the value of an HTML form control
//function getFormValue(control, delimiter, prependRule, appendRule) {
//    
//    // If the control is null, return a blank;
//    if (control == null) return "";
//    
//    // Get the value
//    var val = null;
//    switch(control.tagName) {
//        case "INPUT":
//			switch(control.type) {
//				case "radio":
//				case "checkbox":
//					// If we're getting the value for a checkbox group, we need to concatenate all the checked values into a delimited string.
//					if (control.name != null && control.name.length > 0) {
//						var checkboxGroup = document.getElementsByName(control.name);
//						if (checkboxGroup.length > 0)
//							return this.getCheckboxGroupValue(checkboxGroup, delimiter, prependRule, appendRule);
//						else
//							val = control.value;
//		            }
//		            else {
//						val = control.value;
//					}
//					break;
//				default:
//					val = control.value;
//					break;
//			}
//        case "SELECT":
//            val = control.value;
//            break;
//        case "TEXTAREA":
//            val = this.getText(control);
//            break;
//    }
//    
//    // Check for syntax rules for the current source code language
//    if (prependRule != null) val = prependRule.childNodes[0].nodeValue + val;
//	if (appendRule != null) val = val + appendRule.childNodes[0].nodeValue;
//	
//	// Return the value
//	return val;
//    
//}

// Sets the value of an HTML form control
//function setFormValue(control, value) {

//    // Set the value into the control
//    // Keep in mind the settingControlID might point to a checkbox or radio
//    // button group.                            
//    switch(control.tagName) {
//        case "INPUT": 
//            switch(control.type) {
//                case "radio":
//                    this.setRadioGroupValue(control.name, value);
//                    break;
//                case "checkbox":
//                    // See if more than one checkbox has the same name. If so, the value is probably a comma-delimited list of values that should be checked.
//                    var checkboxGroup = (control.name != null && control.name.length > 0) ? document.getElementsByName(control.name) : null;
//                    if (checkboxGroup != null && checkboxGroup.length > 0) {
//                        this.setCheckboxGroupValue(checkboxGroup, value);
//                    }
//                    else {
//                        // Only one checkbox - the value should be true / false
//                        var checked = Boolean(((value == 1 || value == "true") ? true : false));
//                        if (control.checked != checked) control.click();
//                    }
//                    break;
//                default:
//                    control.value = value;
//                    break;
//            }
//            break;
//        case "SELECT":
//            control.value = value;
//            if (control.onchange) control.onchange();
//            break;
//        case "TEXTAREA":
//            this.setText(control, value);
//            break;
//    }
//    
//}


// Sets the selected value of a radio button group
//function setRadioGroupValue(groupName, selectedValue) {
//    
//    // Get the radio buttons in the group
//    var buttons = document.getElementsByName(groupName);
//    
//    // Loop through each button until we find one whose value matches
//    for(var i = 0; i < buttons.length; i++) {
//        if (buttons[i].value == selectedValue) {
//            buttons[i].checked = true;
//            buttons[i].click();
//            break;
//        }
//    }
//    
//}


// Sets which checkboxes are checked in a group of checkboxes with the same name (given a comma-delimited list of checked values)
//function setCheckboxGroupValue(checkboxGroup, checkedValues) {
//    // Pad the start and end of the checkedValues with an additional comma to make searching for values easier.
//    checkedValues = "," + checkedValues + ",";
//    
//    // Now loop through each checkbox in the group. If ",<checkboxvalue>," exists in the checkedValues string, check the box.
//    for(var i = 0; i < checkboxGroup.length; i++) {
//        var checked = Boolean(checkedValues.indexOf("," + checkboxGroup[i].value + ",") >= 0);
//        if (checkboxGroup[i].checked != checked) checkboxGroup[i].click();            
//    }
//}


// Gets the values of a group of checkboxes and concatenates them together into a delimited string.
//function getCheckboxGroupValue(checkboxGroup, delimiter, prependRule, appendRule) {
//	
//	// Make a delimeted list of the values
//	var sb = "";
//	for(var i = 0; i < checkboxGroup.length; i++) {
//		if (checkboxGroup[i].checked) {
//			if (sb.length > 0) sb += delimiter;
//			var val = checkboxGroup[i].value.toString();
//			if (prependRule != null) val = prependRule.childNodes[0].nodeValue + val;
//			if (appendRule != null) val = val + appendRule.childNodes[0].nodeValue;
//			sb += val;
//        }   
//    }
//  
//	// Return the value
//    return sb;
//}

// Validates the value of a textbox is a whole number value.
// If min or max is -1, the setting is ignored.
//function validateInteger(control, min, max, required) {
//	// Get the error control (which shows the error message). If one doesn't exist, there is no need validating.
//	var errorControl = document.getElementById(control.name + "Error");
//	if (errorControl != null) {
//	
//		// Get the value of the textbox being validated
//		var val = control.value.toString();
//		
//		// Determine if the value is valid
//		var error = false;
//		if (val.length == 0) {
//			error = required;
//		}
//		else if (val.length > 0) {
//			var exp = /(^-?\d\d*$)/;
//			error = !(exp.test(val));
//		}
//		
//		// Make sure its between the min and max
//		if (error == false) {
//			if (min != "NaN" && val < min) control.value = min;
//			if (max != "NaN" && val > max) control.value = max;
//		}
//		
//		// If an error was found, show the error message
//		if (error == true && (errorControl.style.display == "none" || errorControl.style.display == "")) {
//			errorControl.style.display = "inline";
//			errorControl.style.visibility = "visible";
//			this.errorCount++;
//		}
//		else if (error == false && errorControl.style.display != "none") {
//			errorControl.style.display = "none";
//			errorControl.style.visibility = "hidden";
//			this.errorCount--;
//		}
//		
//		// If 1 or more errors exist, disable search
//		if (this.errorCount > 0) 
//			this.disableSearch();
//		else
//			this.enableSearch();
//		
//	}
//}


// Validates the value of a textbox is a whole number value.
// If min or max is -1, the setting is ignored.
//function validateDecimal(control, min, max, decimalPlaces, required) {
//	// Get the error control (which shows the error message). If one doesn't exist, there is no need validating.
//	var errorControl = document.getElementById(control.name + "Error");
//	if (errorControl != null) {
//	
//		// Get the value of the textbox being validated
//		var val = control.value.toString();
//		
//		// Determine if the value is valid
//		var error = false;
//		if (val.length == 0) {
//			error = required;
//		}
//		else if (val.length > 0) {
//			var exp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
//			error = !(exp.test(val));
//		}
//		
//		// Make sure its between the min and max and round the decimal places
//		if (error == false) {
//			if (min != "NaN" && val < min) val = min;
//			if (max != "NaN" && val > max) val = max;
//			control.value = (+val).toFixed(decimalPlaces);
//		}
//		
//		// If an error was found, show the error message
//		if (error == true && (errorControl.style.display == "none" || errorControl.style.display == "")) {
//			errorControl.style.display = "inline";
//			errorControl.style.visibility = "visible";
//			this.errorCount++;
//		}
//		else if (error == false && errorControl.style.display != "none") {
//			errorControl.style.display = "none";
//			errorControl.style.visibility = "hidden";
//			this.errorCount--;
//		}
//		
//		// If 1 or more errors exist, disable search
//		if (this.errorCount > 0) 
//			this.disableSearch();
//		else
//			this.enableSearch();
//		
//	}
//}