﻿function GetControl(columnName) {
    var control = document.getElementById(columnName);
    if (!control) {
        var columnNameCleaned = columnName.replace(/\$/g, "_");
        control = document.getElementById(columnNameCleaned);
        if (control == null) {
            alert("Cannot find control: " + columnName);
        }
        else { return control; }
    } else {
        return control;
    }
}
function ShowHide(controlName, action) {
    var ctl = GetControl(controlName);
    if (action == '0')
    { ctl.style.visibility = 'hidden'; }
    else{ctl.style.visibility = 'visible';} 
}
function ShowHideV2(controlName, action) {
    var ctl = GetControl(controlName);
    if (action == '0')
    { ctl.style.display = 'none'; }
    else { ctl.style.display = 'block'; }
}

function GetSelectValue(selectObj) {
    return selectObj.options[selectObj.selectedIndex].value;
}
function GetSelectText(selectObj) {
    return selectObj.options[selectObj.selectedIndex].text;
}
function ControlExists(columnName) {
    var isExist = false;
    try {
       var control = $find(columnName); 
        if(control)
            {  
                isExist = true; 
            }   
       }
     catch (e) 
        {
        var msg = e.message; 
        isExist = false;
       }  
return isExist;}
function SetFieldValue(fieldObj, newValue) {
	if (fieldObj.type == "select-one") {
	    SetSelectValue(fieldObj, newValue);
	} else if (fieldObj.type == "checkbox") {
	SetCheckboxValue(fieldObj, newValue);
	} else if (fieldObj.useHiddenValue) {
	fieldObj.hiddenValue = newValue;
}
    //As to not break existing functionality
    else if (fieldObj.tagName == "SPAN" && fieldObj.className == "SER_cost") {
    SetInnerHTML(fieldObj, newValue);
    }
	else if (fieldObj.tagName == "SPAN") {
	SetCheckboxValue(fieldObj.firstChild, newValue);
	} else {
		fieldObj.value = newValue;
	}
}
function GetFieldValue(fieldObj) {
    if (fieldObj.type == "select-one") {
        return (fieldObj.selectedIndex == -1 ? "" : fieldObj.options[fieldObj.selectedIndex].value);
    } else if (fieldObj.type == "checkbox") {
        return (fieldObj.checked ? 1 : 0);
    } else if (fieldObj.useHiddenValue) {
        return fieldObj.hiddenValue;
    } else if (fieldObj.type == "radio") {
        return (fieldObj.checked ? 1 : 0);
    }
    else if (fieldObj.tagName == "span") {
        return (fieldObj.innerHTML);
    }
    else {
        return fieldObj.value;
    }
}
function SetFocus(controlName) {
    var ctl = GetControl(controlName);
    if (ctl.focus)
        { ctl.focus();
        return;
       }  
	if (ctl.type == "select-one")
		{ctl.select();}
}
function AddDropDownOption(controlName, optionValue, optionText) {
	var dropdown = this.GetControl(controlName);
	var option = document.createElement("option");
	dropdown.options.add(option);
	option.innerText = optionText;
	option.Value = optionValue;
}
function RemoveDropDownOption(controlName, optionValue, optionText) {
	var dropDown = this.GetControl(controlName);
    for (currentOption=0; currentOption<dropDown.length;currentOption++) {
        if(dropDown.options[currentOption].value == optionValue) {
            dropDown.options[currentOption] = null;
        }  
    } 
}
function SetSelectValue(selectObj, newValue) {
    var max = selectObj.length;
    var i;
    for (i = 0; i < max; i++) {
        if (selectObj.options[i].value == newValue) {
            selectObj.selectedIndex = i;
            return true;
        }
    }
	return false
}
function SetSelectText(selectObj, newValue) {
    var max = selectObj.length;
    var i;
    for (i = 0; i < max; i++) {
        if (selectObj.options[i].text == newValue) {
            selectObj.selectedIndex = i;
            return true;
        } 
    }return false;
}
function SetCheckboxValue(fieldObj, newValue) {
    var strValue = newValue.toString().toLowerCase();
	if (strValue == "yes" || strValue == "true" || strValue == "1")
	{ fieldObj.checked = true; }
	else { fieldObj.checked = false; }
}
function SetInnerText(fieldObj, newValue) {
    var ctl = GetControl(fieldObj);
    ctl.innerHTML = newValue;
}

function SetInnerHTML(fieldObj, newValue) {
    fieldObj.innerHTML = newValue;
}

function SetInnerTextInput(fieldObj) {
    var allItems = document.getElementsByTagName("body")[0].getElementsByTagName("*");
    for( i = 0; i < allItems ; i++) {
        s = allItems[i].id;
       if (fieldObj.test(s)) 
        { alert(fieldObj);} 
    }
}
function RuleProcessor() {
	// PROPERTIES
    this.rules = [];
    this.wasValid = true;
	
	// PUBLIC METHODS
    this.ProcessRules = function() {
        this.wasValid = true;
        for (var i = 0; i < this.rules.length; i++) {
            strRule = this.rules[i];
            eval("this." + strRule);
        }
    }
	// RULE ACTIONS
	this.ACK = function() {
	    this.wasValid = true;
	}
	this.RunScript = function(scriptCommand) {
	    // Use this sparingly, really should have only defined commands
	    eval(scriptCommand);
	}
	this.DisplayError = function(msg) {
	    // display error message to user
	    alert(msg);
	    this.wasValid = false;
	}
	this.SetControlValue = function(columnName, newValue) {
	    var control = this.GetControl(columnName)
	    if (control)
	    {SetFieldValue(control, newValue);}
	}
	this.SetReadOnlyValue = function(columnName, value, text) {
	    var control = this.GetControl(columnName)
	    if (control) {
	        //SetFieldValue(control, text)
	        control.hiddenValue = value;
	        control.value = text;
	    }
	}
	this.SetDisplayValue = function(columnName, code) {
	    var control = this.GetControl(columnName)
	    if (control)
	    { SetDisplayValue(control, value);}
	}
	this.DisableControl = function(columnName) {
	    this.GetControl(columnName).disabled = true;
	}
	this.EnableControl = function(columnName) {
	    this.GetControl(columnName).disabled = false;
	}
	this.ClearDropDown = function(columnName) {
	    var dropdown = this.GetControl(columnName);
	    dropdown.options.length = 0;
	}
	this.AddDropDownOption = function(columnName, text, value) {
	    var dropdown = this.GetControl(columnName);
	    var option = document.createElement("option");
	    option.text = text;
	    option.value = value;
	    dropdown.options.add(option);
	}
	this.SetFocus = function(columnName) { 
		this.GetControl(columnName).focus()
	}
	this.SetFilter = function(filterWhere, filterDescription) {
	    //alert("TODO - reload the record with the following filter: " + filterWhere)
	    parent.ReplaceFilter(filterWhere, filterDescription);
	}
	this.ReloadPage = function() { 
		//location.reload(true)
		window.location.href = window.location.href;
	}
	this.NavigateTo = function(sUrl) {
	    window.location.href = sUrl;
	}
	this.SaveRecord = function(saveMethod) {
       SaveRecord(saveMethod);
    }	
    this.DisplayPreference = function(preferenceID) {
        AddPreferenceSummaryItem(preferenceID); 
   }
   this.DisplayPreferenceOnLoad = function(preferenceType, preferenceText, preferenceID) {
       AddPreferenceSummaryOnLoad(preferenceType, preferenceText, preferenceID);
   }
   this.RemovePreference = function(preferenceID) {
        RemovePreferenceSummaryItem(preferenceID);
   } 
   this.RemovePreferenceSummary = function(level, preferenceText) {
        RemoveCriteriaSummary(level,preferenceText);
   }   
   this.DisplayCriteria = function(level,headingLabel, displayValue) {
        AddCriteriaSummary(level,headingLabel,displayValue);
   }
   this.UncheckPreference = function(columnName) {
       SetInnerTextInput(columnName);
   }
   this.ShowHideControl = function(controlID, visible) {
        ShowHide(controlID, visible);
   }
   this.RemoveDropDownSelect = function(controlName, optionValue, optionText){
    RemoveDropDownOption(controlName, optionValue, optionText);
   }
   this.SetDivValue = function(columnName, fieldValue) {
        SetInnerText(columnName, fieldValue);
   }
	// PRIVATE METHODS
	this.GetControl = function(columnName) {
	    //GetControl(columnName);
       var control = document.getElementById(columnName);
       if (!control) {
            control = GetControlBySpan(columnName);
       } 
        if (!control){
            // Many aspnet controls have $ for name and underscore for id so try replacing that
            var columnNameCleaned = columnName.replace(/\$/g, "_");
            control = document.getElementById(columnNameCleaned); 
            if (control == null) {  
	            //alert("Cannot find control: " + columnName); 
	       }
	       else { return control;}
	     } else {
        return control;
       } 
	}
}
function GetControlBySpan(columnName) {
   var controls = document.getElementsByTagName("SPAN");
   for (i = 0; i < controls.length; i++) {
        if (controls[i].name ==columnName) {
            return controls[i];
        } 
    }
}
