
//-------------------------------------------------------------------------------------
//
// Description:	Contains JavaScript functions specific to user interface "fields" which
//              are elements/objects that display data or accept user input.
//
//------------------------------------------------------------------------------------- 

//Default and hilite colors for input fields and action fields/links.
var msFieldBorderDefaultColor = null;          
var msFieldBorderHiliteColor  = "orange";          
var msActionDefaultColor      = "blue";          
var msActionHiliteColor       = "orange"; 

//Function object for field-level messages.
function UIFieldMsg(sFieldID, bShow, oRef, sLocation, sMsgType, sMsgText) {
    
    this.FieldID    = sFieldID;
    this.Show       = bShow;
    this.Ref        = oRef;
    this.Location   = sLocation;
    this.MsgType    = sMsgType;
    this.MsgText    = sMsgText;

}         

//-------------------------------------------------------------------------------------
// Unchecks all checkboxes in the specified parent element. 
//-------------------------------------------------------------------------------------
function ClearAllCheckboxes(oParent) {
    
    for (var i=0; i<oParent.children.length; i++) {
        var oChild = oParent.children(i);
        if ((oChild.type == "checkbox") && (oChild.checked)) {
            oChild.checked = false; 
        } 
    }
}

//-------------------------------------------------------------------------------------
// Returns the number of checkboxes checked for the specified group/box of checkboxes. 
//-------------------------------------------------------------------------------------
function GetCheckedCount(oParent) {

    var iCnt = 0;
    
    for (var i=0; i<oParent.children.length; i++) {
        var oChild = oParent.children(i);
        if ((oChild.type == "checkbox") && (oChild.checked)) iCnt++;  
    }
    
    return iCnt;
}


//-------------------------------------------------------------------------------------
// Allows only date-oriented characters (numbers and forward slash "/"). 
//-------------------------------------------------------------------------------------
function KeyDateOnly() {
        
    //alert("keycode = " + event.keyCode );

    if (event.keyCode >= 48 && event.keyCode <= 57) {
        //Allow numbers
    }
    else if (event.keyCode == 47) {
        //Allow forward slash ("/")
    }
    else {
        event.keyCode = "";
    } 
}

//-------------------------------------------------------------------------------------
// Allows only money-oriented characters (numbers and decimal "."). 
//-------------------------------------------------------------------------------------
function KeyMoneyOnly() {
        
    //alert("keycode = " + event.keyCode );

    if (event.keyCode >= 48 && event.keyCode <= 57) {
        //Allow numbers
    }
    else if (event.keyCode == 46) {
        //Allow decimal/period (".")
    }
    else {
        event.keyCode = "";
    } 
}

//-------------------------------------------------------------------------------------
// Allows only numeric characters and commas (",") for thousands separator.
//-------------------------------------------------------------------------------------
function KeyNumericOnly() {

    if (event.keyCode >= 48 && event.keyCode <= 57) {
        //Allow numbers
    }
    else if (event.keyCode == 44) {
        //Allow comma
    }
    else {
        event.keyCode = "";
    } 
}

//-------------------------------------------------------------------------------------
// Checks the length of an input field (text or textarea) to determine if the maximum
// length has been exceeded. Optionally displays current count if requested via 
// "showcount=true" custom attribute.
//-------------------------------------------------------------------------------------
function UserInputCheckLength(e) {

	var evt = window.event || e;
	var oSrc = evt.srcElement || e.target;
	var sType = evt.type || e.type;
    
    try 
    {
		var iMax = parseInt(oSrc.getAttribute("maxlength"));
		var iLen = oSrc.value.length;
		var bShowCount = (oSrc.getAttribute("showcount")) ? true : false;
		
		if (iLen >= iMax)
		{
		    evt.keyCode = "";
		    if (iLen > iMax) oSrc.value = oSrc.value.substring(0, iMax-1);
		}
		else 
		{
			if (bShowCount) {
				var sCountLabel = oSrc.getAttribute("showcount");
				document.getElementById(sCountLabel).innerHTML = (iLen+1) + "/" + iMax;
			}
		}
	}
    catch(e) {}
         
}

//-------------------------------------------------------------------------------------
// Adds or removes highlighting for an input field element.
//-------------------------------------------------------------------------------------
function SetInputFieldHilite(e) {

	var evt = window.event || e;
	var oSrc = evt.srcElement || e.target;
	var sType = evt.type || e.type;

    //var oSrc = event.srcElement;
        
    if (sType == "focus") {
        if (msFieldBorderDefaultColor == null) msFieldBorderDefaultColor = oSrc.style.borderColor; 
        oSrc.style.borderColor = msFieldBorderHiliteColor;
    }
    else {
        try {
            oSrc.style.borderColor = msFieldBorderDefaultColor;
        }
        catch(e) {
            //ignore error
        }
    }      
}

//-------------------------------------------------------------------------------------
// Cancel user update action.
//-------------------------------------------------------------------------------------
function UserInputCancel() {
	
	//Cancel keystroke.
	event.returnValue = false;
}

//-------------------------------------------------------------------------------------
// Disable editting for the specified input element.
//-------------------------------------------------------------------------------------
function UserInputDisable(oThat) {

	//Get the current height of the object so we can reset it later. Changing the 
	//className property will cause the height attribute to be lost.  
	var iHeight = oThat.offsetHeight;
	
    oThat.attachEvent('onkeydown', UserInputCancel);
    oThat.style.height = "18px";
    oThat.style.cursor = "default";
    oThat.style.color = "black";
    oThat.tabIndex = -1;
    oThat.hideFocus = true;
        
    oThat.detachEvent('onmouseover', UserInputHilite);
    oThat.detachEvent('onmouseout',  UserInputHilite);
    oThat.detachEvent('onclick',     UserInputShow);
	
    var oThatInput = $(oThat.id + "Input");
    oThatInput.detachEvent("onblur", UserInputSave); 
}

//-------------------------------------------------------------------------------------
// Enables user editting for the specified object.
//-------------------------------------------------------------------------------------
function UserInputEnable(oThat, sCustomHandler) {
	
	if (oThat.getAttribute("InputEnabled") == "Y") return; 
	
	oThat.attachEvent('onmouseover', UserInputHilite);
	oThat.attachEvent('onmouseout',  UserInputHilite); 
	oThat.attachEvent('onclick',     UserInputShow); 
	
	if (sCustomHandler) oThat.setAttribute("CustomHandler", sCustomHandler);
	
    var oThatInput = $(oThat.id + "Input");
    
    //Attach event handler to save/store the changed value and make any needed UI change(s).
    if (oThatInput.tagName == "SELECT") {
        oThatInput.attachEvent("onchange", UserInputSave); 
        oThatInput.attachEvent("onblur", UserInputHide);
    }
    else { 
        oThatInput.attachEvent("onblur", UserInputSave); 
    }
    
    oThat.setAttribute("InputEnabled","Y");
}

//-------------------------------------------------------------------------------------
// Hides a dynamic user input field.
//-------------------------------------------------------------------------------------
function UserInputHide() {
    
	//Hide the shadow object.
    with (event.srcElement) {	
	    style.visibility = "hidden";
	    style.display = "none";
    }
}

//-------------------------------------------------------------------------------------
// Highlights a dynamic user input field.
//-------------------------------------------------------------------------------------
function UserInputHilite() {

	//Cancel user action?
	//if (!mbAllowUserActions) return;

	with (event.srcElement) {
		if (event.type == "mouseover") {
		    style.color = msActionHiliteColor;
		}    
		else if (event.type == "mouseout") {
		    style.color = msActionDefaultColor;
		}    
    }		    
}

//-------------------------------------------------------------------------------------
// Transfers user input from the "shadow" object to the "display" object.
//-------------------------------------------------------------------------------------
function UserInputSave() {

	//Get the name of the object that triggered the event.
	var oSrc = event.srcElement;	
	
	//Get the name of the source object. This assumes the name of the source object is 
	//the name of the shadow object with the "Input" suffix stripped off.
	var sName = oSrc.id;
	var i = sName.indexOf("Input");
	if (i >= 0) sName = sName.substr(0,i);
	
	//Get the new value.
	oDisplayed = document.all(sName);
	var sNewValue = "";
	if (oSrc.tagName == "SELECT") {
		if (oSrc.selectedIndex >= 0) {
		    sNewValue = oSrc.item(oSrc.selectedIndex).text;
		    var sNewID = oSrc.value;
		}
	}
	else {
		sNewValue = oSrc.value;
		var sNewID = "";
	}

	//Determine if the user input is different from the original value.
	if (oDisplayed.innerText != sNewValue) {
	    if (oDisplayed.getAttribute("CustomHandler")) {
	        eval(oDisplayed.getAttribute("CustomHandler"));
	    }
	    else {
	        oDisplayed.innerText = sNewValue;
		}
		if (oDisplayed.innerText == "") oDisplayed.innerText = "(enter value)";	
	}

	//Hide the shadow object.
	oSrc.style.visibility = "hidden";
	oSrc.style.display = "none";
}

//-------------------------------------------------------------------------------------
// Displays an object for user input.
//-------------------------------------------------------------------------------------
function UserInputShow() {

	//Get the name of the object that triggered the event.
	var oSrc = event.srcElement;
	
	//Determine the user input object. This assumes the name of the user input object 
	//is the name of the source object with the text "Input" appended to it.
	var oInput = document.all(oSrc.id + "Input");
	
	//Transfer value of the source object to the user input object and allow the user 
	//to see it.
	if (oInput.tagName == "SELECT") {
        SetSelectText(oInput, oSrc.innerText);
		oInput.style.left = oSrc.offsetLeft;
		oInput.style.top  = oSrc.offsetTop;	
		oInput.style.width = oSrc.offsetWidth + 45;
		oInput.style.visibility = "visible";
		oInput.style.display = "inline";
		oInput.focus(); 
	}
	else {
		oInput.value = oSrc.innerText;
		oInput.style.left = oSrc.offsetLeft;
		oInput.style.top  = oSrc.offsetTop;	
		//oInput.style.height = oSrc.offsetHeight;
		oInput.style.width = oSrc.offsetWidth;
		oInput.style.visibility = "visible";
		oInput.style.display = "inline";
		oInput.focus(); 
		oInput.select(); 
	}
}
