// Constants for identifying classes to use for input types.
var mandatoryInFocusClassName = "mandatoryInFocus";
var mandatoryOutFocusClassName = "mandatoryOutFocus";
var inFocusClassName = "inFocus";
var outFocusClassName = "outFocus";
var currencySymbol="£";

//Set field display for mandatory input
function Mandatory(objGen)
{
    if(objGen.Mandatory == "true")
    {
        if(objGen.value.length == 0)
        {
            objGen.className = mandatoryOutFocusClassName;
        }
        else
        {
            objGen.className = outFocusClassName;
        }
    }
    else
    {
        objGen.className = outFocusClassName;
    }
}

//Code required on releasing key such as rounding of decimals
function GenBoxKeyUp()
{
    var objGen = window.event.srcElement;
    Mandatory(objGen);
    switch(objGen.Type)
    {
        case "Currency":
            if((objGen.value.charAt(objGen.value.length - 4) == '.'))
            {
                if((objGen.value.charAt(objGen.value.length - 1) > 4))
                {
                    var i = 0;
                    i = parseFloat(objGen.value.charAt(objGen.value.length - 2));
                    objGen.value = Math.round(parseFloat(objGen.value.substring(0, objGen.value.length - 2)) + ((i + 1) / 100));
                }
                else
                {
                    objGen.value = objGen.value.substring(0, objGen.value.length - 2);
                }
            }
            break;
        case "Postcode":
        case "String":
        case "Date":
        case "Decimal":
        case "Numeric":			
        case "AccountNo":
        case "Calendar":
    }
    
    if ( objGen.autoCloseField != null )
		{
		// Exception for TAB and BACKTAB
		if (window.event.keyCode != 9 && window.event.keyCode != 16 )
			{
			if (objGen.value.length == objGen.maxLength )
				{
				objGen.autoCloseField.focus();
				}
			}	
		}
	
}

//Validate input character against valid input for this control i.e. don't allow alpha's in a numeric input
function GenBoxKeyPress(Capitalise)
{
    var i = 0;
    var objGen = window.event.srcElement;

    // Allow Escape / Return etc.
    if(window.event.keyCode == 27 || window.event.keyCode == 13)
    {
        return(true);
    }

    switch(objGen.Type)
    {
        case "AccountNo":
            validateAccountNumber(objGen);
            break;
        case "Numeric":
            if(window.event.keyCode < 48 || window.event.keyCode > 57)
            {
                window.event.keyCode = 0;
            }
            break;
        case "Decimal":
        case "Currency":
            validateCurrency(objGen);
            break;
        case "Postcode":
        case "String":
            if(objGen.Capitalise == 1)
            {
                if(window.event.keyCode > 96 && window.event.keyCode < 123)
                {
                    window.event.keyCode = (window.event.keyCode - 32);
                }
            }

            if(objGen.Capitalise == 2)
            {
                if(objGen.value.length == 0)
                {
                    if(window.event.keyCode > 96 && window.event.keyCode < 123)
                    {
                        window.event.keyCode = (window.event.keyCode - 32);
                    }
                }
                else
                {
                    if(objGen.value.substring(objGen.value.length - 1, objGen.value.length) == " ")
                    {
                        if(window.event.keyCode > 96 && window.event.keyCode < 123)
                        {
                            window.event.keyCode = (window.event.keyCode - 32);
                        }
                    }
                }
            }
            break;
        case "Calendar":
        	keyPressed(objGen.name);
        	break;
    }
}

//Not implemented because of keypress and keyup routines
function GenBoxChange()
{
    var objGen = window.event.srcElement;
    switch(objGen.Type)
    {
        case "Currency":
        case "Postcode":
        case "String":
        case "Date":
        case "Decimal":
        case "Numeric":
        case "AccountNo":
        case "Calendar":
    }
}

//Validation on exit from input (validate whole field)
function GenBoxBlur()
{
    var objGen = window.event.srcElement;
    var blnInvalid = false;
    var ErrorMessage = "";
    if(document.activeElement != null)
    {
        if(document.activeElement.style.validate == "false")
        {
            return;
        }
    }

    switch(objGen.Type)
    {
        case "Currency":
            if(objGen.NoDecimals == true)
            {
                objGen.value = FormatCurrencyEx(objGen.value, objGen.CurrencySymbol, true);
            }
            else
            {
                objGen.value = FormatCurrencyEx(objGen.value, objGen.CurrencySymbol, false);
            }

            // Right Justify Field
            objGen.style.textAlign = "right";
            break;
        case "Decimal":
            objGen.value = FormatCurrency(objGen.value, "");
            // Right Justify Field
            objGen.style.textAlign = "right";
            break;
        case "Date":
            if(isValidDate(objGen) == false)
            {
                ErrorMessage = "Date must be in the format of dd/mm/yy, dd/mm/yyyy.";
                blnInvalid = true;
            }
            break;
        case "Postcode":
            objGen.value = (formatPostcode(objGen.value)).toUpperCase();
            break;
        case "AccountNo":
            if(CheckDigit(objGen.value) == false)
            {
                ErrorMessage = "Invalid Account Number ";
                blnInvalid = true;
            }
            break;
        case "String":
            if(objGen.pad == true && objGen.MaxLength > 0)
            {
                objGen.value = PadSpaces(objGen.value, objGen.MaxLength);
            }
            break;
        case "Calendar":
            if (!checkDate(this))
            	blnInvalid = true;
            break;
    }

    if(blnInvalid == true && document.activeElement != objGen)
    {
        if(ErrorMessage.length > 0)
        {
            alert(ErrorMessage);
        }

        //objGen.focus();
    }
    else
    {
	    if(objGen.Mandatory == "true")
	    {
		if(objGen.value.length == 0)
		{
		    objGen.className = mandatoryOutFocusClassName;
		}
		else
		{
		    objGen.className = outFocusClassName;
		}
	    }
	    else
	    {
		objGen.className=outFocusClassName;
	    }
    }
}

//Styling when input is selected
function GenBoxFocus()
{
    var objGen = window.event.srcElement;
    objGen.className = inFocusClassName;
    switch(objGen.Type)
    {
        case "AccountNo":
            break;
        case "Postcode":
            if(objGen.value != "")
            {
                objGen.value = stripCharString(objGen.value, " ");
            }
            break;
        case "Currency":
        case "Decimal":
            objGen.style.textAlign = "right";
            objGen.value = UnformatCurrency(objGen.value);
            break;
        case "Calendar":
            break;
    }
}

//Make currency box, will also read inital value from XML is present.
function MakeCurrencyBox(objBox, blnNoDecimals, XMLPath)
{
    objBox.NoDecimals = blnNoDecimals;
    MakeGenBox(objBox, ReadXMLValue(XMLPath), "Currency", false, 1, 10);
}

//Make input box with styling and validation as appropriate
function MakeGenBox(objGen, Text, Type, Mandatory, Capitalise, MaxLength)
{
    objGen.className = outFocusClassName;
    objGen.Mandatory = Mandatory;
    objGen.Type = Type;
    objGen.Capitalise = Capitalise;
    if(MaxLength > 0)
    {
        objGen.maxLength = MaxLength;
    }

    objGen.onfocus = GenBoxFocus;
    objGen.onblur = GenBoxBlur;
	objGen.onchange = GenBoxBlur;
    objGen.onkeyup = GenBoxKeyUp;
    objGen.onkeypress = GenBoxKeyPress;

    //	objGen.onchange = GenBoxChange;
    // Use current value if no text to set !!
    if(Text.length == 0)
    {
        Text = objGen.value;
    }

    if(objGen.Capitalise == 1)
    {
        objGen.value = Text.toUpperCase();
    }

    if(objGen.Capitalise == 2)
    {
        objGen.value = toTitleCase(Text);
    }

    if(objGen.Capitalise == 3)
    {
        objGen.value = Text.toLowerCase();
    }

    if(objGen.Type == "Currency")
    {
        objGen.CurrencySymbol = currencySymbol;
        if(objGen.NoDecimals == true)
        {
            objGen.size = 10;
            objGen.maxLength = 7;
            if (Text!="")
	            objGen.value = FormatCurrencyEx(Text, objGen.CurrencySymbol, true);
        }
        else
        {
            objGen.size = 12;
            objGen.maxLength = 11;
            if (Text!="")
	            objGen.value = FormatCurrencyEx(Text, objGen.CurrencySymbol, false);
        }
    }


    if (objGen.Type == "Calendar")
    {
    	//Already Created
    }
    
    if(objGen.Type == "Date")
           if (Text!="")
 	   	objGen.value = formatDate(Text);
	
    if(objGen.Mandatory == "true")
    {
        if(objGen.value.length == 0)
        {

            objGen.className = mandatoryOutFocusClassName;
        }
        else
        {
            objGen.className = outFocusClassName;
        }
    }
    else
    {
        objGen.className=outFocusClassName;
    }
}

//No different to GenBox at present
function MakeGenCombo(objGen)
{
    objGen.className = outFocusClassName;
    objGen.onfocus = GenBoxFocus;
    objGen.onblur = GenBoxBlur;
}

//Function to auto click default buttons on keypress
function CheckDefaultKeys()
{
    switch(window.event.keyCode)
    {
        case 27:
            cmdCancel_OnClick();
            break;
        case 13:
            cmdOK_OnClick();
            break;
    }
}