/*
    View the common.readme file for detailed explanation of each function.
    
    Author:		JP Jackson
    Create date: October 12,2005
    Update date: June 16, 2009
*/

function IsNumeric(Value)
{
    if(Value !=9 & Value != 8 & Value != 46)
    {
        if(Value >= 48 & Value <= 57 & event.shiftKey == false | Value == 190)
        {
            event.returnValue = true;
        }
        else
        {
            if(Value >= 96 & Value <= 105)
            {
                event.returnValue = true;
            }
            else
            {
                event.returnValue = false;
            }
        }
    }
}

function IsAlpha(Value,AllowSpace)
{
    if(Value != 9 & Value != 8 & Value != 46 & Value != 222)
    {
        if(Value >= 65 & Value <= 90 | Value == 190)
        {
            if(Value == 32)
            {
                if(!AllowSpace)
                {
                    event.returnValue = false;
                }
            }
        }
        else
        {
            event.returnValue = false;
        }
    }
}

function IsAlphaNumeric(Value,AllowSpace)
{
    if(Value != 8 & Value != 9 & Value != 46)
    {
        if((Value >= 48 & Value<= 57) | (Value >= 65 & Value<= 90) | (Value >= 96 & Value<= 105) | Value == 190)
        {
            if(Value == 32)
            {
                if(!AllowSpace)
                {
                    event.returnValue = false;
                }
            }
        }
        else
        {
            event.returnValue = false;
        }
    }
}

function IsNotSpace(Value)
{
    if(Value !=9 & Value != 8 & Value != 46)
    {
        if(Value != 32)
        {
            event.returnValue = true;
        }
        else
        {
            event.returnValue = false;
        }
    }
}

function IsDate(AllowFutureDates,Value)
{

    if(Value.indexOf('/')>-1)
    { 
        slash1 = Value.indexOf('/');
        slash2 = Value.indexOf('/',slash1 + 1);
        
        //Note Len position character is not inclusive
        Month = Value.substring(0,slash1);
        Day = Value.substring(slash1 + 1,slash2);
        Year = Value.substring(slash2 + 1, Value.length);
    }
    else
    {
        Month = parseInt(Value.substring(0,2));
        Day = parseInt(Value.substring(2,4));
        Year = parseInt(Value.substring(4,9));
    }
    
    ErrorMsg = '';
    Word = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    
    if(Month<1 | Month>12)
    {
	    ErrorMsg='Invalid Month';
    }
    else
    {

	    if(Day<1 | Day>31)
	    {
		    ErrorMsg='Invalid Day';
	    }
	    else
	    {
		    if((Month==4 | Month==6 | Month==9 | Month==11) & Day >30)
		    {
			    ErrorMsg=Word[Month - 1] + ' can only have 30 days';
		    }
		    else
		    {
			    if(Month==2)
				{
			 	    if ((Math.floor(Year/4) == (Year/4)) && ((Math.floor(Year/100) != (Year/100)) || (Math.floor(Year/400) == (Year/400))))
				    {//leap year
					    if(Day>29)
					    {
						    ErrorMsg= Word[Month - 1] + ' only has 29 days in ' + Year;
					    }
				    }
				    else
				    {//non Leap year
					    if(Day>28)
					    {
							    ErrorMsg= Word[Month - 1] + ' only has 28 days in ' + Year;
					    }
				    }					
			    }
			    else
			    {
				    if(parseInt(Year)<1900 | parseInt(Year)>2099)
				    {
					    ErrorMsg='Invalid year range: 1900 - 2099';
				    }
				    else
				    {
				        if(AllowFutureDates == false)
				        {
				            UserDate = new Date(Value.substring(4,8),Value.substring(0,2) - 1, Value.substring(2,4));
				            
				            SystemDate = new Date();

				            if(UserDate>SystemDate)
				            {
				                ErrorMsg = 'I cannot accept dates into the future. I must have a date that is Today or in the past.';
				            }
				        }
				    }
			    }
		    }
	    }
    }
    if(ErrorMsg == '')
    {
        return true;
    }
    else
    {
        return false;
    }
}

function CompareDates(AllowFutureDates,Date1, Date2)
{
    if(Trim(Date1)!='' & Trim(Date2)!='')
    {
        if(IsDate(AllowFutureDates,Date1) & IsDate(AllowFutureDates,Date2))
        {
            DateA = new Date(GetDatePart('year',Date1),GetDatePart('month',Date1) -1,GetDatePart('day',Date1));
            DateB = new Date(GetDatePart('year',Date2),GetDatePart('month',Date2) -1,GetDatePart('day',Date2));
            if(DateA < DateB)
            {
                return -1;   
            }
            else
            {
                if(DateA > DateB)
                {
                    return 1;
                }
                else
                {
                    if(DateA.toLocaleDateString() == DateB.toLocaleDateString())
                    {
                        return 0;
                    }
                }
            }
        }
    }
}

function MaskValue(Type,inString,fieldName)
{
    if(event.keyCode!=8)
    {
        switch(Type)
        {
            case 'Date':
                do
                {
                     inString = inString.replace('/',''); 
                }while(inString.indexOf('/')>-1)
                
                switch(inString.length)
                {
                    case 1:
                        inString = inString + '/';
                    break;
                    
                    case 2:              
                        if(parseInt(inString)<13)
                        {
                            inString = inString + '/';
                        }
                        else
                        {
                            inString = inString.substring(0,1) + '/' + inString.substring(1,2);
                        }
                    break;
                    
                    case 3:
                        inString = inString.substring(0,2) + '/' + inString.substring(2,3) + '/';
                    break;
                    
                    case 4:
                        inString = inString.substring(0,2) + '/' + inString.substring(2,4) + '/';
                    break;
                    
                    case 5:
                        inString = inString.substring(0,2) + '/' + inString.substring(2,4) + '/' + inString.substring(4,5);
                    break;
                    
                    case 6:
                        inString = inString.substring(0,1) + '/' + inString.substring(1,2) + '/' + inString.substring(2,6);
                    break;
                    
                    case 7:
                        if(parseInt(inString.substring(1,3))>9)
                        {
                            inString = inString.substring(0,1) + '/' + inString.substring(1,3) + '/' + inString.substring(3,7);
                        }
                        else
                        {
                            inString = inString.substring(0,2) + '/' + inString.substring(2,3) + '/' + inString.substring(3,7);
                        }
                    break;
                    
                    case 8:
                        inString = inString.substring(0,2) + '/' + inString.substring(2,4) + '/' + inString.substring(4,8);
                    break;
                }
                
            break;
            
            case 'SSN':
            break;
            
        }
       document.getElementById(fieldName).value = inString;
   }
}

function IsPhoneNumber(IsRequired,Value,MsgControl)
{
    Value = Trim(Value);
    if(IsRequired == true)
    {
        if(Value !='')
        {
            try
            {
                Phone = parseInt(Value);
                if(Value.length < 10)
                {
                    document.getElementById(MsgControl).style.display = 'inline';
                    return false;
                }
                else
                {                    
                    document.getElementById(MsgControl).style.display = 'none';
                    return true;
                }
            }
            catch(e)
            {
                document.getElementById(MsgControl).style.display = 'inline';
                return false; 
            }
        }
        else
        {
            document.getElementById(MsgControl).style.display = 'inline';
            return false;
        }    
    }
    else
    {
        try
        {
            if(Value!= '')
            {
                Phone = parseInt(Value);
                if(Value.length < 10)
                {
                    document.getElementById(MsgControl).style.display = 'inline';
                    return false;
                }
                else
                {
                    document.getElementById(MsgControl).style.display = 'none';
                    return true;
                }
            }
            else
            {
                document.getElementById(MsgControl).style.display = 'none';
                return true;
            }
        }
        catch(e)
        {
            document.getElementById(MsgControl).style.display = 'inline';
            return false; 
        }
    }
}

function IsEmailAddress(IsRequired,Value,MsgControl) 
{
    strValue = '';
    
    if(Value.value != undefined)
    {
        CreateAttribute(Value,'Required',IsRequired.toString());
        CreateEvent(Value, 'onkeyup', 'OverrideError(this)');
        CreateEvent(Value, 'onblur', 'OverrideError(this)');
        
        strValue = Trim(Value.value);  
    }
    else
    {
        strValue = Trim(Value);
    }
    
    if(IsRequired == true)
    {
        if(strValue !='')
        {
            try
            {
                if(strValue.indexOf('.') < 2 | strValue.indexOf('@')< 1)
                {
                    document.getElementById(MsgControl).style.display = 'inline';
                    if(Value != null)
                    {
                        Value.className='inputError';
                    }
                    return false;
                }
                else
                {                    
                    document.getElementById(MsgControl).style.display = 'none';
                    if(Value != null)
                    {
                        Value.className = '';
                        Value.onkeyup = '';
                        Value.onblur = '';
                    }
                    return true;
                }
            }
            catch(e)
            {
                document.getElementById(MsgControl).style.display = 'inline';
                if(Value != null)
                {
                    Value.className='inputError';
                }
                return false; 
            }
        }
        else
        {
            document.getElementById(MsgControl).style.display = 'inline';
            if(Value != null)
            {
                Value.className='inputError';
            }
            return false;
        }    
    }
    else
    {
        try
        {
            if(strValue.indexOf('.') < 2 | strValue.indexOf('@')< 1)
            {
                document.getElementById(MsgControl).style.display = 'inline';
                if(Value != null)
                {
                    Value.className='inputError';
                }
                return false;
            }
            else
            {
                document.getElementById(MsgControl).style.display = 'none';
                if(Value != null)
                {
                    Value.className = '';
                    Value.onkeyup = '';
                    Value.onblur = '';
                }
                return true;
            }
        }
        catch(e)
        {
            document.getElementById(MsgControl).style.display = 'inline';
            if(Value != null)
            {
                Value.className='inputError';
            }
            return false; 
        }
    }
}

function SetSelectedIndexByValue(Control, Value)
{
    Status = false;  //Indicates weather or not the value was found in the list of options
   
    if(document.getElementById(Control).length == undefined)
    {
        //RadioButtonList
        radioChoices = ReturnListControlArray(Control);
        for (var Count = 0; Count <= radioChoices.length - 1; Count++)
        {
            if(radioChoices[Count].value == Value)
            {
                document.getElementById(radioChoices[Count].id).checked = true;
                Status = true;
            }
        }
    }
    else
    {
        //DropDownList
        for (var Count = 0; Count <= document.getElementById(Control).length - 1; Count++)
        {   
            if(document.getElementById(Control).options[Count].value == Value)
            {
                document.getElementById(Control).selectedIndex = Count;
                Status = true;
            }
        }
    }
    return Status
}

function SetSelectedIndexByText(Control, Value)
{
    Status = false;  //Indicates weather or not the value was found in the list of options
   
    if(document.getElementById(Control).length == undefined)
    {
        //RadioButtonList
        radioChoices = ReturnListControlArray(Control);
        for (var Count = 0; Count <= radioChoices.length - 1; Count++)
        {
            if(radioChoices[Count].innerText == Value)
            {
                document.getElementById(radioChoices[Count].id).checked = true;
                Status = true;
            }
        }
    }
    else
    {
        //DropDownList
        for (var Count = 0; Count <= document.getElementById(Control).length - 1; Count++)
        {   
            if(document.getElementById(Control).options[Count].innerText == Value)
            {
                document.getElementById(Control).selectedIndex = Count;
                Status = true;
            }
        }
    }

    return Status
}

function MoveCursor(Value,MoveLength,Control)
{
    if(Value.length == MoveLength)
    {
        document.getElementById(Control).focus();
    }
}

function ResetMaxLength(Control,MaxLength)
{
    document.getElementById(Control).maxlength = MaxLength
}

function CheckRange(IsRequired,Value,Low,High,MsgControl)
{
    strValue = '';
    
    if(Value.value != undefined)
    {
        CreateAttribute(Value,'Required',IsRequired.toString());
        CreateAttribute(Value,'Low',Low.toString());
        CreateAttribute(Value,'High',High.toString());
        CreateEvent(Value, 'onkeyup', 'OverrideError(this)');
        CreateEvent(Value, 'onblur', 'OverrideError(this)'); 
        if(MsgControl!= null)
        {
          CreateAttribute(Value,'MsgControl',MsgControl);  
        }
        
        strValue = Trim(Value.value);   
    }
    else
    {
        strValue = Trim(Value)
    }
    
    try
    {
        if(parseInt(strValue)){}
    }
    catch(e)
    {
        return false;
    }
    
    if(strValue == '' & IsRequired == true)
    {
        if(Value != null)
        {
            Value.className='inputError';
        }
        
        if(MsgControl != '')
        {
            document.getElementById(MsgControl).style.display = 'inline';
        }
        return false;
    }
    else
    {
        if(strValue > High)
        {
             if(MsgControl != '')
             {
                document.getElementById(MsgControl).innerText = 'Maximum value: ' + High.toString();
             }
            
            Value.className='inputError';
            return false;
        }
        else if(strValue < Low)
        {
            if(MsgControl != '')
            {
                document.getElementById(MsgControl).innerText = 'Minimum value: ' + Low.toString();
            }
            Value.className='inputError';
            return false;
        }
    }
    return true;
}

function CheckLength(IsRequired,Value,Min,Max,MsgControl)
{
    strValue = '';
    if(Value.value != undefined)
    {
        CreateAttribute(Value,'Required',IsRequired.toString());
        CreateAttribute(Value,'MinLength',Min.toString());
        if(Value.type=='textarea')
        {
            //TEXTAREA input types do not support the 'maxLength' attribute, 
            //so we must create a custom attribute to hold it
            CreateAttribute(Value,'MaxArea',Max.toString());
        }
        else
        {
            CreateAttribute(Value,'MaxLength',Max.toString());
        }
        CreateEvent(Value, 'onkeyup', 'OverrideError(this)');
        CreateEvent(Value, 'onblur', 'OverrideError(this)');
        strValue = Trim(Value.value);
    }
    else
    {
        strValue = Value;
    }
    
    if(strValue.length == 0 & IsRequired == true)
    {
        if(MsgControl != null)
        {
            document.getElementById(MsgControl).style.display = 'inline';
        }
        
        if(Value != null)
        {
            Value.className='inputError';
        }
        return false;
    }
    else
    {
        if(strValue == '')
        {
            if(MsgControl != null)
            {
                document.getElementById(MsgControl).style.display = 'none';
            }
            
            if(Value != null)
            {
                Value.className = '';
                Value.onkeyup = '';
                Value.onblur = '';
            }
            return true;
        }
        else
        {
            if(strValue.length < Min | strValue.length > Max)
            {
                if(MsgControl != null)
                {
                    document.getElementById(MsgControl).style.display = 'inline';
                }
                
                if(Value != null)
                {
                    Value.className='inputError';
                }
                return false;
            }
            else
            {
                if(MsgControl != null)
                {
                    document.getElementById(MsgControl).style.display = 'none';
                }
                
                if(Value != null)
                {
                    Value.className = '';
                    Value.onkeyup = '';
                    Value.onblur = '';
                }
                return true;
            }
        }
    }
}

function CheckLength2(IsRequired,Value,Min,Max,Padding, MsgControl)
{
    strValue = '';
    if(Value.value != undefined)
    {
        CreateAttribute(Value,'Required',IsRequired.toString());
        CreateAttribute(Value,'MinLength',Min.toString());
        CreateAttribute(Value,'MaxLength',Max.toString());
        CreateEvent(Value, 'onkeyup', 'OverrideError(this)');
        CreateEvent(Value, 'onblur', 'OverrideError(this)');
        if(Padding!= '')
        {
            CreateEvent(Value, 'onblur', "PadInput(this.id, \'" + Padding.substring(0,1) + "\', \'" + Padding.substring(1,2) + "\', " + Max.toString() + ", this.Value)");
        }
        
        strValue = Trim(Value.value);
    }
    else
    {
        strValue = Value;
    }
    
    if(strValue.length == 0 & IsRequired == true)
    {
        if(MsgControl != null)
        {
            document.getElementById(MsgControl).style.display = 'inline';
        }
        
        if(Value != null)
        {
            Value.className='inputError';
        }
        return false;
    }
    else
    {
        if(strValue == '')
        {
            if(MsgControl != null)
            {
                document.getElementById(MsgControl).style.display = 'none';
            }
            
            if(Value != null)
            {
                Value.className = '';
                Value.onkeyup = '';
                Value.onblur = '';
            }
            return true;
        }
        else
        {
            if(strValue.length < Min | strValue.length > Max)
            {
                if(MsgControl != null)
                {
                    document.getElementById(MsgControl).style.display = 'inline';
                }
                
                if(Value != null)
                {
                    Value.className='inputError';
                }
                return false;
            }
            else
            {
                if(MsgControl != null)
                {
                    document.getElementById(MsgControl).style.display = 'none';
                }
                
                if(Value != null)
                {
                    Value.className = '';
                    Value.onkeyup = '';
                    Value.onblur = '';
                }
                return true;
            }
        }
    }
}



function CreateEvent(Control, PropertyName, EventName)
{
    if(Control == null || (Control.value == undefined & Control.selectedIndex == undefined & Control.frameElement == undefined))
    {
        alert('Common JS: Control parameter must be passed as an object.');
    }
    else
    {
        var n= Control.id;
        if(PropertyName != '' )
        {
            if(Control[PropertyName] == undefined || Control[PropertyName] == '')
            {
                Control[PropertyName] = new Function(EventName);
            }
            else
            {
                if(Control[PropertyName].toString().toLowerCase().indexOf(EventName.toLowerCase()) == -1)
                {
//                    if(Control.ControlID = "30")
//                    {
//                        var a= "";
//                    }
//                    var brace = Control[PropertyName].toString().lastIndexOf('}');
//                    Control[PropertyName] = Control[PropertyName].toString().substring(0,brace-1) + ';' + EventName.toLowerCase() + '}'; 
                    
                    
                    
                    
                    
                   // Control[PropertyName] = Control[PropertyName] + ';' + EventName ;
                    //Control[PropertyName] = new Function(EventName) + Control[PropertyName];
                }
            }
        }
        else
        {
            alert('Common JS: Cannot find Attribute name');
        }
    }
}

function CreateAttribute(Control, PropertyName, AttributeValue)
{
    if(Control.value == undefined & Control.selectedIndex == undefined & Control.frameElement == undefined)
    {
        alert('Common JS: Control parameter must be passed as an object or the object being passed is not a recognized type.');
    }
    else
    {
        if(PropertyName != '')
        {
            Control[PropertyName] = AttributeValue;
        }
        else
        {
            alert('Common JS: Cannot find Attribute name');
        }
    }
}

function RemoveAttribute(Control,PropertyName)
{
    if(Control.value == undefined & Control.selectedIndex == undefined)
    {
        alert('Common JS: Control parameter must be passed as an object.');
    }
    else
    {
        if(PropertyName != '')
        {
            Control[PropertyName] = null;
        }
        else
        {
            alert('Common JS: Cannot find Attribute name');
        }
    }
}

function OverrideError(ControlObject)
{
    //requires the common.css elements to display properly
    if(ControlObject.className == 'inputError' || ControlObject.className == 'inputCorrected' || 
       ControlObject.className == 'selectError' || ControlObject.className == 'selectCorrected')
    {
        switch(ControlObject.type)
        {
            case 'text':
                if(ControlObject.MinLength != null)
                {//Text length check
                    if(ControlObject.Required.toUpperCase() == 'TRUE' & (ControlObject.value.length >= ControlObject.MinLength & ControlObject.value.length <= ControlObject.maxLength))
                    {
                        ControlObject.className = 'inputCorrected';
                    }
                    else
                    {
                        ControlObject.className = 'inputError';
                    }
                }
                else
                {//Integer check
                    if(ControlObject.Required.toUpperCase() == 'TRUE' && (ControlObject.value != '' && (parseInt(ControlObject.value) >= parseInt(ControlObject.Low) & parseInt(ControlObject.value) <= parseInt(ControlObject.High))))
                    {
                        ControlObject.className = 'inputCorrected';
                        if(ControlObject.MsgControl != '')
                        {
                            document.getElementById(ControlObject.MsgControl).style.display = 'none';
                        }
                    }
                    else
                    {
                        ControlObject.className = 'inputError';
                    }
                }
                break;
                
            case 'select-one':
                if(ControlObject.Required.toUpperCase() == 'TRUE')
                {
                    if(ControlObject.selectedIndex > 0)
                    {
                        ControlObject.className = 'selectCorrected';
                    }
                    else
                    {
                        ControlObject.className = 'selectError';
                    }
                }
                break;
                
            case 'radio':
                document.getElementById(Control[0].id).className = '';
                break;
                
            case 'textarea':
                if(ControlObject.MinLength != null)
                {//This input type does not support 'maxLength' attribute. You must create a custom atribute to hold maxlength
                    if(ControlObject.Required.toUpperCase() == 'TRUE' & (ControlObject.value.length >= ControlObject.MinLength & ControlObject.value.length <= ControlObject.MaxArea))
                    {
                        ControlObject.className = 'inputCorrected';
                    }
                    else
                    {
                        ControlObject.className = 'inputError';
                    }
                }
                else
                {//Integer check
                    if(ControlObject.Required.toUpperCase() == 'TRUE' && (ControlObject.value != '' && (parseInt(ControlObject.value) >= parseInt(ControlObject.Low) & parseInt(ControlObject.value) <= parseInt(ControlObject.High))))
                    {
                        ControlObject.className = 'inputCorrected';
                        if(ControlObject.MsgControl != '')
                        {
                            document.getElementById(ControlObject.MsgControl).style.display = 'none';
                        }
                    }
                    else
                    {
                        ControlObject.className = 'inputError';
                    }
                }
            break;
        }
    }
}


function DropdownSelected(ControlIndex, MsgControl)
{
    intControlIndex = 0;
    
    if(ControlIndex != null)
    {
        intControlIndex = ControlIndex.selectedIndex;
    }
    else
    {
        intControlIndex = ControlIndex;
    }
    
    if(intControlIndex == 0)
    {
        if(MsgControl != null)
        {
            document.getElementById(MsgControl).style.display = 'inline';
        }
        
        if(ControlIndex != null)
        {
            ControlIndex.className='selectError';
            CreateAttribute(ControlIndex,'Required','True');
//            CreateAttribute(ControlIndex,'onchange','OverrideError(this)');
//            CreateAttribute(ControlIndex,'onblur','OverrideError(this)');
        }
        return false;
    }
    else
    {
        if(MsgControl != null)
        {
            document.getElementById(MsgControl).style.display = 'none';
        }
        
        if(ControlIndex != null)
        {
            ControlIndex.className='';
        }
        return true
    }
}

function RadioButtonIsSelected(Control,MsgControl) 
{
    Status = false;
    if(Control == null)
    {
        alert('Control to be validated cannot be found!');
    }
    Count = 0;
    try
    {
      do
      {
        if(Control[Count].checked == true)
        {
            Status = true
        }
        Count++;
      }while(Count != -1)  
    }
    catch(e){}
    
    if(!Status)
    {
        document.getElementById(Control[0].id).className = 'inputError';
        
//        Count = 0;
//        try
//        {
//          do
//          {
//            Control[Count].onclick="OverrideError('" + Control + "')";
//            Count++;
//          }while(Count != -1)  
//        }
//        catch(e){}        
    }
    else
    {
        document.getElementById(Control[0].id).className = '';
    }
    
    
    

    if(Status == false)
    {
        document.getElementById(MsgControl).style.display = 'inline';
    }
    else
    {
        document.getElementById(MsgControl).style.display = 'none';
    }
    
    return Status;
}

function RadioButtonSelectedValue(Control)  
{
    Value = '';
    Count = 0;
    try
    {
      do
      {
        if(Control[Count].checked == true)
        {
            Value = Control[Count].value;
        }
        Count++;
      }while(Count != -1)  
    }
    catch(e)
    {
    
    }
    return Value;
}

function IsListItemSelected(ListControl,MsgControl)  
{
    Status = false;
    Count = 0;
    Control = ReturnListControlArray(ListControl);
    try
    {
        do
        {
            if(Control[Count].type == "checkbox" | Control[Count].type == "radio")
            {
                if(Control[Count].checked == true)
                {
                    Status = true
                }
            }
            Count++;
         }while(Count != -1)  
    }
    catch(e){}
    
    if(Status == false)
    {
        ListControl.className = 'inputError';
        if(MsgControl != null)
        {
            document.getElementById(MsgControl).style.display = 'inline';
        }
    }
    else
    {
        ListControl.className = '';
        if(MsgControl != null)
        {
            document.getElementById(MsgControl).style.display = 'none';
        }
    }
    return Status;
}

function ReturnListControlArray(ListControl)
{
    return document.getElementsByName(ReplaceAll(ListControl.id,'_','$')); 
}


function DropdownHasDuplicate(Control, Value, MsgControl)
{
    IsDuplicate = false;
    if(Control != null && Value != "")
    {
        if(Control.length > 0)
        {
            Value = Trim(Value.toUpperCase());
            for(count = 0; count <= Control.length - 1; count++)
            {
                if(Trim(Control.options[count].innerText.toUpperCase()) == Value)
                {
                    IsDuplicate = true;
                    break;
               }
            }
        }
    }
    else
    {
        IsDuplicate = false;
    }
    
    if(IsDuplicate)
    {
        document.getElementById(MsgControl).style.display = 'inline';
    }
    else
    {
        document.getElementById(MsgControl).style.display = 'none';
    }
    return IsDuplicate;
}


function CheckResult(result,MsgControl) 
{
    if(result == false)
    {
        document.getElementById(MsgControl).style.display = 'inline';
        return false;
    }
    else
    {
        document.getElementById(MsgControl).style.display = 'none';
        return true;
    }
}

function FieldModified(FlagName)
{
    document.getElementById(FlagName).value = 'true';
}

function Trim(inString)
{
    return inString.replace(/^\s+|\s+$/g,'');
}

function ReplaceAll(Value, ReplaceValue, NewValue)
{
    return Value.replace(new RegExp(ReplaceValue,'g'), NewValue );    
}

function GetDatePart(Part,Value)
{
    Month = 0;
    Day = 0;
    Year = 0;
    
    if(Value.indexOf('/')>-1)
    { 
        slash1 = Value.indexOf('/');
        slash2 = Value.indexOf('/',slash1 + 1);
        
        //Note Len position character is not inclusive
        Month = Value.substring(0,slash1);
        Day = Value.substring(slash1 + 1,slash2);
        Year = Value.substring(slash2 + 1, Value.length);
    }
    else
    {
        Month = parseInt(Value.substring(0,2));
        Day = parseInt(Value.substring(2,4));
        Year = parseInt(Value.substring(4,9));
    }
    
    switch(Part.toUpperCase())
    {
        case "MONTH":
            return Month;
            break;
            
        case "DAY":
            return Day;
            break;
            
        case "YEAR":
            return Year;
            break;
    }
}

function EncodeHTML(TextboxControl)
{
    HTML = document.getElementById(TextboxControl).innerText;
    HTML = HTML.replace(/&/gi, '&amp;').replace(/\"/gi, '&quot;').replace(/</gi, '&lt;').replace(/>/gi, '&gt;');
    document.getElementById(TextboxControl).innerText = HTML;
}

function DecodeHTML(TextboxControl)
{
    HTML = document.getElementById(TextboxControl).innerText;
    HTML = HTML.replace(/&amp;/gi,'&').replace(/&quot;/gi,'\"').replace(/&lt;/gi, '<').replace(/&gt;/gi, '>');
    document.getElementById(TextboxControl).innerText = HTML;
}

function FindOccurrence(Value, Character, OccurrenceNumber)
{
    Position = -1;
    Counter = 0;
    Occurrence = 0;
    do
    {
        Occurrence = Value.indexOf(Character,Position +1)
        Position = Occurrence;
        if(Occurrence != -1)
        {
            Counter++;
        }
    }while(Counter != OccurrenceNumber & Occurrence != -1 )  
    
    return Occurrence;
}

function ReturnCookie(NameID)
{
	SetCookie("ReturnNameID", NameID);
	window.close();	
}

function SetCookie(name, value) 
{  
	var argv = SetCookie.arguments;  
	var argc = SetCookie.arguments.length;  
	var expires = (argc > 2) ? argv[2] : null;  
	var path = (argc > 3) ? argv[3] : "/";  
	var domain = (argc > 4) ? argv[4] : "alacourt.org";  
	var secure = (argc > 5) ? argv[5] : false;  
	document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
		
	CookieValue = ReadCookie(name);
	if(CookieValue == '' || CookieValue === undefined)
	{
	    alert('Browser cookies must be enabled in order to use this application. Contact AOC IT Support for further assisstance.');
	}
}

function ReadCookie(CookieName) 
{
	var CookieString = document.cookie;
	var CookieSet = CookieString.split (';');
	var SetSize = CookieSet.length;
	var CookiePieces
	var ReturnValue = "";
	var x = 0;
	//alert('Cookie contents: ' + CookieString);
	for (x = 0; ((x < SetSize) && (ReturnValue == "")); x++) 
	{
	
		CookiePieces = CookieSet[x].split ('=');
	    if (CookiePieces[0].substring (0,1) == ' ') 
	    {
	    	CookiePieces[0] = CookiePieces[0].substring (1, CookiePieces[0].length);
	    }
	    if (CookiePieces[0] == CookieName) 
	    {
	       
	    	ReturnValue = CookiePieces[1];
	    }	     
	}
	return ReturnValue;
}

function DeleteCookie(CookieName)
{
    var CurrentDate = new Date();
    document.cookie = CookieName + "=1;expires=" + CurrentDate.toGMTString() + ";" + ";";
    //alert(document.cookie);
}

function GetControlPrefix(Control, ControlType)
{
    ControlType = ControlType.toLowerCase();
    Position = 0;
    if(Control !='')
    {
        try
        {
            //Control parameter is of a STRING type
            Position = Control.lastIndexOf('_');
            ControlName = Control.substring(0,Position + 1);
        }
        catch(e)
        {
            //Control Parameter is of OBJECT type
            Position = Control.id.lastIndexOf('_');
            ControlName = Control.id.substring(0,Position + 1);
        }
    }
    else
    {
        Count = 1;
        ControlName = null;
        
        object = document.getElementsByTagName(ControlType.toLowerCase());
        
        do{
            try
            {
                switch(object[Count].type)
                {
                    case 'image':
                         Position = FindOccurrence(object[Count].id, '_', 2);
                         if(Position != -1)
                         {
                            //Control requesting image swap was a child user control of parent user control
                            ControlName = object[Count].id.substring(0,Position + 1);
                         }
                         else
                         {
                            //Control requesting image swap was a parent user control
                            Position = FindOccurrence(object[Count].id, '_', 1);
                            ControlName = object[Count].id.substring(0,Position + 1);
                         }
                    break;
                    
                    case 'checkbox':
                         Position = FindOccurrence(object[Count].id, '_', 2);
                         if(Position != -1)
                         {
                            //Control requesting image swap was a child user control of parent user control
                            ControlName = object[Count].id.substring(0,Position + 1);
                         }
                         else
                         {
                            //Control requesting image swap was a parent user control
                            Position = FindOccurrence(object[Count].id, '_', 1);
                            ControlName = object[Count].id.substring(0,Position + 1);
                         }            
                    break;
                }
            }
            catch(e)
            {
                ControlName = "";  //No prefix was found
            }
        }while (ControlName == null)   
    }
    return ControlName;
}

function GetControlSuffix(Control)
{
    try
    {
        //Control parameter is of a STRING type
        Position = Control.lastIndexOf('_');
        ControlName = Control.substring(Position,Control.length);
    }
    catch(e)
    {
        //Control Parameter is of OBJECT type
        Position = Control.id.lastIndexOf('_');
        ControlName = Control.id.substring(Position,Control.id.length);
    }
    
    return ControlName;
}

function GetControlParent(Control,HasMasterPages)
{
    Position = 0;
     try
    {   //Control parameter is of a STRING type
        if(HasMasterPages)
        {
            Position = FindOccurrence(Control, '_', 2);
        }
        else
        {
            Position = FindOccurrence(Control, '_', 1);
        }
        
        ControlName = Control.substring(0,Position + 1);
       
    }
    catch(e)
    {
        //Control Parameter is of OBJECT type
        if(HasMasterPages)
        {
            Position = FindOccurrence(Control.id, '_', 2);
            
        }
        else
        {
            Position = FindOccurrence(Control.id, '_', 1);
        }
        ControlName = Control.id.substring(0,Position + 1);
    }
    return ControlName;
}


function PadInput(ControlName, PadDirection, PadCharacter, PadLength, Value)
{
    if(ControlName != "")
    {
        //Set value to a control directly

        if(Trim(document.getElementById(ControlName).value).length > 0 & Trim(Value).length < PadLength)
        {
            if(PadDirection.toUpperCase() == 'L')
            {
                PadDirection = 'LEFT';
            }
            else if(PadDirection == 'R')
            {
                PadDirection = 'RIGHT';
            }
            
            
           do{
               switch(PadDirection.toUpperCase())
               {
                    case 'LEFT':
                        Value = PadCharacter + Value;
                    break;
                    
                    case 'RIGHT':
                        Value = Value + PadCharacter; 
                    break;
               }
           }while(Value.length < PadLength)
           document.getElementById(ControlName).value = Value.toString();
        }
    }
    else
    {
        //Return a string value with the padded result
        if(Trim(Value).length < PadLength)
        {
          do{
               switch(PadDirection.toUpperCase())
               {
                    case 'LEFT':
                        Value = PadCharacter + Value;
                    break;
                    
                    case 'RIGHT':
                        Value = Value + PadCharacter; 
                    break;
               }
           }while(Value.length < PadLength)        
        }
        
        return Value.toString();
    }
}


function GetReturnValue(ControlName,FunctionCallName)
{
    frameCount = 0;
    thisFrame = null;
    
    do
    {
        thisFrame = document.frames[frameCount].document.forms[0].document;
        frameCount++;
    
    }while(thisFrame == null & frameCount < 10);
    
    Status = '';
    
    if(ControlName == '' || FunctionCallName == '')
    {
        Status = 'Required ControlName or FunctionCallName is missing.';
    }
    else
    {
        try
        {
            //hidden control
            
            thisReturnValue = thisFrame.getElementById(ControlName);
            if(thisReturnValue == null)
            {
                thisFrame = document.frames[frameCount].document.forms[0].document;
                thisReturnValue = thisFrame.getElementById(ControlName);
                if(thisReturnValue == null)
                {
                    try
                    {
                        //TextBox or Label
                        thisReturnValue = thisFrame.getElementById(ControlName).innerText;
                    }
                    catch(e)
                    {
                        //Status = 'ERROR: Cannot find Control ' + ControlName + ' to get a returnValue.';
                    }    
                } 
                else
                {
                    thisReturnValue = thisFrame.getElementById(ControlName).value;
                }
            }
        }
        catch(e){}
        
        if(Status == '')
        {
            if(FunctionCallName.indexOf('(') > -1)
            {
                if(FunctionCallName.indexOf('()') > -1)
                {
                    //Remove parethises and evaluate process
                    FunctionCallName = ReplaceAll(FunctionCallName,'()','');
                    eval(FunctionCallName + "('" + thisReturnValue + "')");
                }
                else
                {
                    FunctionCallName = FunctionCallName.replace('(','(\''); //add ( and a tick
                    if(FunctionCallName.indexOf(',') > -1)
                    {
                        //Has multiple parameters so reformat as ','
                        FunctionCallName = ReplaceAll(FunctionCallName,',','\',\'');
                    }
                    else
                    {
                        //Has a single veriable so replace the ) with a tick so the returnValue can be appended to the expression
                        FunctionCallName = FunctionCallName.replace(')','\'');
                    }
                    
                    try
                    {
                        eval(FunctionCallName + "," + " '" + thisReturnValue.value + "')");
                    }
                    catch(e)
                    {
                        alert('Error: GetReturnValue CallMethod is improperly formatted.');
                    }
                }
            }
            else
            {
                try
                {
                    eval(FunctionCallName + "('" + thisReturnValue.value + "')");  //Evaluate AS IS
                }
                catch(e)
                {
                    alert('Error: GetReturnValue CallMethod is improperly formatted.');
                }
            }
        }
    }
    
    if(Status != '')
    {
        alert(Status);
    }
}
