/*********************************************************************
COMMON FORM FUNCTIONS
**********************************************************************/

function SubmitAction(sInput)
{
    oInput = document.getElementById(sInput);
    if (oInput != null)
    {
        oInput.disabled = false;
    }
	
	if (document.forms[0].submit)
	{
	    document.forms[0].submit();
	}
	    
	return false;
}

function Submit()
{
	if (document.forms[0].submit)
	    document.forms[0].submit();
	    
	return false;
}

function SetScroll(id)
{       
    if (document.getElementById(id) != null)
    {            
        var y = 0;
        if(document.documentElement.scrollTop != null)
        {
             y = document.documentElement.scrollTop;             
        }
        document.getElementById(id).value = y;
    }
}

function ScrollPage(id) 
{   
    if (document.getElementById(id) != null) 
    {   
        if(document.documentElement.scrollTop != null)
        {
            document.documentElement.scrollTop = document.getElementById(id).value;            
            document.getElementById(id).value = 0;
        }
    }
}

function SetScrollElement(inputId, elementId)
{       
	if (document.getElementById(inputId) != null)
	{            
		var y = 0;
		if(document.getElementById(elementId).scrollTop != null)
		{
			 y = document.getElementById(elementId).scrollTop;             
		}
		document.getElementById(inputId).value = y;
	}
}

function ScrollElement(inputId, elementId) 
{   
	if (document.getElementById(inputId) != null) 
	{   
		if(document.getElementById(elementId).scrollTop != null)
		{
			document.getElementById(elementId).scrollTop = document.getElementById(inputId).value;            
			document.getElementById(inputId).value = 0;
		}
	}
}

function SubmitSelection(sInput, sField, sValue)
{
    document.getElementById(sField).value = sValue;
    SubmitAction(sInput);
}

function ChangeVisibility(AllRadioButton, Fields)
{
    if(document.getElementById(AllRadioButton).checked == true)
    {
        document.getElementById(Fields).className = 'hide'
    }
    else
    {
        document.getElementById(Fields).className = 'show'
    }
}

function UpdateFieldValue(fieldId, value) 
{
    elem = document.getElementById(fieldId);
    elem.value = value
}

function ChangeCharCount(iMax, Text, CharCountId, fieldId) 
{
    var CharCount = document.getElementById(CharCountId);
    var Field = document.getElementById(fieldId);

    // determine how many characters are left
    CharCount.innerHTML = iMax - Field.value.length;
}

function ChangeHTMLCharCount(iMax, hiddenFieldID, charCountID, htmlFieldID)
{
	var CharCount = document.getElementById(charCountID);
	var Text = document.getElementById(hiddenFieldID);

	// determine how many characters are left
	CharCount.innerHTML = iMax - Text.value.length;
	
	if (CharCount.innerHTML < 0) // check innerText as we don't count HTML tags
	{ 
		Text.value = Text.value.substr(0, iMax);
		var Text_Edit = document.getElementById('Text_Edit_'+ htmlFieldID);
		if (Text_Edit)
			Text_Edit.innerHTML = Text.value;
		CharCount.innerHTML = 0;
	}
}

function CheckTextAreaLength(e, textArea, maxLength)
{
    keyNum = GetKeyNum(e);

    //Allow backspace, delete, and the cursor keys
    if(keyNum == 46 || keyNum == 37 || keyNum == 38 || keyNum == 39 || keyNum == 40)
        return true;

    if (keyNum != 8)
    {
        if(textArea.value.length >= maxLength)
        {    
            e.returnValue = false;
            return false;
        }
    }
    
    //check whether it can be dirtied
    if(document.getElementById(ControlName + '.Dirty') != null)
    {
        // WARNING THIS IS A HACK
        // need to handle a special case for the customer claim history form
        // there are multiple sections on this page that require fading
        // each section is defined by the claimlineid
        // need to try catch here as the bCustomerClaimHistoryPage var wont
        // be set otherwise
        
        try
        {
            if (bCustomerClaimHistoryPage)
            {
                // get the claimlineid from the text area id
                iStart = textArea.id.lastIndexOf('.');
                iLength = textArea.id.length;
                iClaimLineID = textArea.id.substring(iStart + 1, iLength)
                return CheckThisForm(e, iClaimLineID);
            }
        }
        catch (e)
        {
        }

        return CheckForm(e, ControlName);
    }
}

/*********************************************************************
KEY PRESS EVENTS
**********************************************************************/

function GetKeyNum(e)
{
    if(!e) var e = window.event;
    if(typeof(e.which) == 'number') //NS 4, NS 6+, Mozilla 0.9+, Opera
        keyNum = e.which;
    else if(typeof(e.keyCode) == 'number') //IE, NS 6+, Mozilla 0.9+
        keyNum = e.keyCode;
    else if(typeof(e.charCode) == 'number') //also NS 6+, Mozilla 0.9+
        keyNum = e.charCode;
    else keyNum = -1;
    return keyNum;
}

function GetKeyChar(e)
{
    keyNum = GetKeyNum(e);
    if (keyNum == -1) return '';
    return String.fromCharCode(keyNum);
}

function FieldInt(e)
{
    keyChar = GetKeyChar(e);
    //return ((parseInt(keyChar) >= 0 && parseInt(keyChar) <= 9) || (IsKeySpecial(e)));
    reg = /[^0-9]/;
	return !reg.test(keyChar) || IsSpecialKey(e);
}

function FieldFloat(e)
{
    keyChar = GetKeyChar(e);
    //return (((parseInt(keyChar) >= 0 && parseInt(keyChar) <= 9) || (keyChar == ".")) || (IsKeySpecial(e)));
    reg = /[^0-9.]/;
	return !reg.test(keyChar) || IsSpecialKey(e);
}

function FieldAlpha(e)
{
    keyChar = GetKeyChar(e);
    reg = /[^A-Za-z\' \-]/;
	return !reg.test(keyChar) || IsSpecialKey(e);
}

function OnEnter(e, button)
{ 
    if(GetKeyNum(e) == 13)
    {
        oButton = document.getElementById(button);
        if (oButton != null)
        {
            oButton.click();
        }
    }
    return true;
}

function IsKeyEnter(e)
{
    return (GetKeyNum(e) == 13);
}

//tests whether the string given is an integer
function IsInteger(n) // 3425234
{
    var anum=/(^(\d)+$)/;
    return anum.test(n);
}

//tests whether the string given is a decimal
// 435 or 32.3242
function IsDecimal(n)
{
    var anum=/(^\d+(\.\d+)?$)/; //  34534.234, .234 is optional
    return anum.test(n);
}

// keys that can be used within fields on the form
function IsSpecialKey(e)
{
    keyNum = GetKeyNum(e);
    // is the key pressed a special/function key
    return (keyNum == 0 || keyNum == 8 || keyNum == 9 || keyNum == 13 || keyNum == 16  || keyNum == 17  || keyNum == 18
     || keyNum == 19 || keyNum == 20 || keyNum == 27 || (keyNum >= 33 && keyNum <= 40) || keyNum == 45);
}

// keys that will not cause the form to be dirtied
function IsNonDirtyKey(e)
{
    keyNum = GetKeyNum(e);
    // is the key pressed a special/function key
    return (keyNum == 0 || keyNum == 9 || keyNum == 13 || keyNum == 16  || keyNum == 17  || keyNum == 18
     || keyNum == 19 || keyNum == 20 || keyNum == 27 || (keyNum >= 33 && keyNum <= 40) || keyNum == 45);
}

function CancelEvent(e)
{
    if (e.returnValue != null && e.returnValue != 'undefined') e.returnValue = false;
    if (e.cancelBubble != null && e.cancelBubble != 'undefined') e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

/*********************************************************************
SPECIFIC FORM FUNCTIONS
**********************************************************************/

function DisplayBasicCustomerSearch() 
{
    document.getElementById('CustomerDisplay').className = "hide";
    document.getElementById('CustomerSearch').className = "show";
    
    //oRes = document.getElementsByTagName(ControlName + '.Resolution');alert (oRes);
    //if (oRes != null)
    //    oRes[0].disabled = true;

    oRslv = document.getElementById(ControlName + '.Action.Resolve');
    if (oRslv != null)
        oRslv.disabled = true;
        
    oCus = document.getElementById(ControlName + '.CustomerID');
    if (oCus != null)
        oCus.value = 0;
    
}

function ShowExtraFields()
{
    oDiv = document.getElementById('ExtraFields');
    oLink = document.getElementById('ExtraFieldsLink');
    oHidden = document.getElementById(ControlName + '.AdvancedSearch');

    if(oDiv.className == "hide")
    {
        oDiv.className = "show";
        oLink.innerHTML = 'Simple search';
        if (oHidden != null)
            oHidden.value = true;
    }
    else
    {
	    oDiv.className = "hide";
	    oLink.innerHTML = 'Give me more search fields';
	    if (oHidden != null)
            oHidden.value = false;
    }
}

function CheckForm(e, sForm, sType)
{
    if (typeof(sType) == 'undefined')
        sType = '';
        
    if (IsKeyEnter(e))
    {
        SubmitAction(sForm + '.Action.Save');
    }
    else if (!IsNonDirtyKey(e))
    {
        if ((sType == '') ||
            (sType == 'int' && FieldInt(e)) ||
            (sType == 'float' && FieldFloat(e)))
        DirtyForm(sForm);
    }
    return true;
}

function DirtyForm(sForm)
{   
    if (document.getElementById(sForm + '.Dirty') != null)
    {
        if (document.getElementById(sForm + '.Dirty').value != "True")
        {
            document.getElementById(sForm + '.Dirty').value = "True";
        
            FadeBackground('admincontent', "#ffffff", "#faf8a8", 250);
            
            oElements = document.getElementsByTagName('input');
            for (i = 0; i < oElements.length; i++)
            {
                if (oElements[i].id == sForm + '.Action.Save')
                {
                    oElements[i].disabled = false;
                    oElements[i].className = "active";
                }
            }
            
            oElements = document.getElementsByTagName('span');
            for (i = 0; i < oElements.length; i++)
            {
                if (oElements[i].id == 'LinkInactive')
                {
                    oElements[i].className = "inactive";
                }
            }
            
            oElements = document.getElementsByTagName('a');
            for (i = 0; i < oElements.length; i++)
            {
                if (oElements[i].id == 'LinkActive')
                {
                    oElements[i].className = "active";
                }
            }
        }
    }
}

function RemoveFormat(obj, bRemoveCents)
{
	if (obj.value != "")
	{
		if (bRemoveCents)
			obj.value = obj.value.toString().replace(/\$|\,|\.00/g, '');
		else
			obj.value = obj.value.toString().replace(/\$|\,/g, '');
	}
}
	
function AddFormat(obj, bAddCents)
{
	if (obj.value != "")
	{
		if (bAddCents)	
			obj.value = FormatCurrency(obj.value, '/\$|\,|\.00/g');
		else
			obj.value = FormatCurrency(obj.value, '/\$|\,/g');
	}
}

function GetComboSelectedValue(combo)
{
    var comboValue;
	var selectedIndex = document.getElementById(combo).selectedIndex;				
	comboValue = document.getElementById(combo).options[selectedIndex].text;				
	
	return comboValue;
}

function GetComboSelectedHiddenValue(combo)
{
    var comboValue;
	var selectedIndex = document.getElementById(combo).selectedIndex;				
	comboValue = document.getElementById(combo).options[selectedIndex].value;				
	
	return comboValue;
}



function FormatCurrency(sValue, sRegexFormat)
{
	dValue = parseFloat(sValue.toString().replace(sRegexFormat, ''));
	bSign = (dValue == (dValue = Math.abs(dValue)));
	dValue = Math.floor(dValue*100+0.50000000001);
	iCents = dValue%100;
	sCents = iCents.toString();
	dValue = Math.floor(dValue/100).toString();
	if(iCents<10)
		sCents = "0" + sCents;
	for (var i = 0; i < Math.floor((dValue.length-(1+i))/3); i++)
	{
		dValue = dValue.substring(0,dValue.length-(4*i+3))+','+ dValue.substring(dValue.length-(4*i+3));
	}
	return (((bSign)?'':'-') + '$' + dValue + '.' + sCents);
}

function ToggleEnabledField( id )
{
    elem = document.getElementById( id );
    if(elem != null)
    {
        elem.disabled = !elem.disabled;
    }
}

function EnableField( id, flag)
{
    elem = document.getElementById( id );
    if(elem != null)
    {
        elem.disabled = flag;
    }
}

function ChangeEnabled(AllRadioButton, Fields)
{
    if(document.getElementById(AllRadioButton).checked)
    {
        document.getElementById(Fields).className = "hide";
    }
    else
    {
        document.getElementById(Fields).className = "show";
    }
}