// Infor Confidential
// © Copyright Infor
// 2008 Licensed Material - Program
// Property of Infor

//
// These functions are classed as common functions and can be used any page. 
// 
// <revision date="2008-08-15" author="Peter Eskarous" release="03.03" drn="">Initial development.</revision>
// <revision date="2008-09-22" author="Ross Stephens" release="03.04" drn="23019">Added SetElementText() GetElementText and GetEventTrigger.</revision>

function formatCurrency(num) 
{
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '$' + num + '.' + cents);
}

//************************************************** **
// ResetValidators(validationSummaryId, validationControlsPrefix)
// Purpose: Resets ASP.NET validation control text display
// AND validation summary control display to empty.
// Inputs: validationSummaryID - string - id of ValidationSummary
// control
// validationControlsPrefix - string - a prefix to use
// to identify validation controls when looping through all
// span Ids. (Validation controls are represented by spans).
// Outputs: none
//************************************************** **

function ResetValidators(validationSummaryId, validationControlsPrefix)
{
    var spans;
    var divValSummary;

    if (document.all) {
        spans = document.all.tags('span');
        divValSummary = document.all(validationSummaryId);
    }
    else if (document.getElementsByTagName) {
            spans = document.getElementsByTagName('span');
            divValSummary = document.getElementById(validationSummaryId);
    }
    if (spans) {
        for (var i = 0; i < spans.length; i++) {
            var prefixLength = "" + validationControlsPrefix.length;
            var currID = "" + spans[i].id
            if ((currID != '') && (prefixLength != '')) {
                if (currID.substring(0,prefixLength) == validationControlsPrefix) {
                    //note - set visibility to hidden, not display=none.
                    // otherwise validator text will never show up again after reset.
                    spans[i].style.display='none';
                }
            }
        }
    }
    if (divValSummary) {
        // note set display=none, NOT visibility=hidden. Exact opposite of
        // spans above!! Otherwise, validation summary is hidden permanently.
        divValSummary.style.display='none';
    }
}

// Fixes .NET 2.0 bug with defaultbutton
// DefaultButton should not kick in if the source element is another button
if (typeof(WebForm_FireDefaultButton) != 'undefined' ) 
	var origFireDefaultButton = WebForm_FireDefaultButton; 

WebForm_FireDefaultButton = function(event, target) { 
	if (event.keyCode == 13) { 
		var src = event.srcElement || event.target; 
		// Don't call original function if focus is on a button 
		if (src && (src.tagName.toLowerCase() == "input") && 
			(src.type.toLowerCase() == "submit" || src.type.toLowerCase() == "button")) { 
			return true; 
		} 
	} 

	return origFireDefaultButton(event, target); 
}

function SetFocus() {

     // loop through all forms on the page
    for(var f=0; f < document.forms.length; f++){
        // set frm variable so we don't have to call 'document.forms[f]' everywhere
        var frm = document.forms[f]

        // loop through all fields in the form
        for(var x=0; x < frm.length; x++){
            // set fld variable so we don't have to call 'document.forms[f][x]' everywhere
            var fld = frm[x]

            // use a try/catch block so that if anything fails we just go to the next field
            try{
                if(!fld.isDisabled && !fld.readOnly){
                    // I want to handle different field types differently, so switch on the type
                    switch(fld.type){
                        // for text boxes, set the focus AND select the text, just like a Windows app
                        case 'textarea':
                        case 'text':
                        case 'password':
                            fld.select();
                            fld.focus();
                            return;
                        // for check boxes and radio buttons, set the focus to the one that is already
                        // checked in the group.
                        case 'checkbox':
                        case 'radio':
                            // if it's not an array, there's only one, so focus on it
                            if(frm[fld.name].length == undefined)
                                fld.focus();
                            else
                            {
                                // find which one is checked and focus on it
                                for(var x=0; x < frm[fld.name].length; x++){
                                    fld = frm[fld.name][x];
                                    if(fld.checked){
                                        fld.focus();
                                        break;
                                    }
                                }
                                // if we didn't find the checked one, just focus on the first one
                                if(x == frm[fld.name].length)
                                    fld = frm[fld.name][0].focus();
                            }
                            return
                        // select boxes and buttons can just have a simple focus() call
                        case 'select-one':
                        case 'select-multiple':
                        case 'button':
                            fld.focus();
                            return;
                    }
                }
            }
            // if anything fails for any reason (hidden div, etc.), execution jumps to catch
            // then goes through the loop again
            catch(e){}
        }
    }
}

// Use this function to change the text in an element not .innerText as it only works in IE, not Firefox

function SetElementText(element, value)
{
    var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
	if(!hasInnerText){
	   element.textContent = value;
	} else{
	   element.innerText = value;
	}
}

function GetElementText(element)
{
    var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;

    return hasInnerText  ? element.innerText : element.textContent;
}

// Return Event Target for IE or Others
function GetEventTrigger(e)
{
    if (!e)
        e = window.event;
      
    return e.target || e.srcElement;
}
