﻿// JScript File
var LastSender               = null;
var SupressFocus             = false;
var SupressValidCheckCurItem = null;
var SupressFocusControls     = new Array();
var JScriptOnChange          = false;

function GetFrameDocument(Frame)
{
    var Doc = null;
    try 
    {
        Doc = Frame.contentDocument;
        if (Doc == null)
            Doc = Frame.document;
    }
    catch (err) {
        Doc = Frame.document;
    }
    return Doc;
}

function SetOnceSuppressControl(Sender) 
{
    SupressFocusControls.push(Sender);
}

function GetPopupPlacement(Sender, Width)
{
        var CurCtl                          = Sender;
        var Left                            = 0; 
        var Top                             = 0;
        var ParentWidth                     = 0;
        var CtlWidth                        = CurCtl.clientWidth;
        while (CurCtl != null)
        {
            if (CurCtl.offsetLeft > 0)
                Left   = Left + CurCtl.offsetLeft;
            if (CurCtl.offsetTop > 0)
                Top    = Top  + CurCtl.offsetTop - CurCtl.scrollTop;    
            try { if (CurCtl.clientWidth > 0) ParentWidth = CurCtl.clientWidth; } catch (err) { }
            CurCtl = CurCtl.offsetParent;
        }
        if (Width > CtlWidth + 20)     
            Left = Left - (Width - CtlWidth - 20) / 2;

        if ((Left + Width > ParentWidth) && (ParentWidth > 0))
            Left                            = ParentWidth - Width;
            
        var Result = new Object();
        Result.Left = Left;
        Result.Top  = Top;
        return Result;    
}

function GetRelativePlacement(Sender, Parent)
{
        var CurCtl                          = Sender;
        var Left                            = 0; 
        var Top                             = 0;
        var ParentWidth                     = 0;
        var CtlWidth                        = CurCtl.clientWidth;
        while ((CurCtl != null) && (CurCtl != Parent))
        {
            if (CurCtl.offsetLeft > 0)
                Left   = Left + CurCtl.offsetLeft;
            if (CurCtl.offsetTop > 0)
                Top    = Top  + CurCtl.offsetTop - CurCtl.scrollTop;    
            try { if (CurCtl.clientWidth > 0) ParentWidth = CurCtl.clientWidth; } catch (err) { }
            CurCtl = CurCtl.offsetParent;
        }

        var Result = new Object();
        Result.Left = Left;
        Result.Top  = Top;
        return Result;    
}

function PopupListEditKeyPress(Sender, Width, Height, URL, StyleString, AddToBody, SecondValueEdit, UseEditHeight) 
{
    if (window.event != null)
        if ((window.event.keyCode == 9) || (window.event.keyCode == 16))
            return;

    var EditHeight = 22;

    if (UseEditHeight == null)
        UseEditHeight = false;
        
    if (AddToBody == null)
        AddToBody = true;

    if (SupressFocus)
        return;    

    if (Sender == null)
        Sender = LastSender;

    if (Sender == null)
        return;
        
    for (I = 0; I < SupressFocusControls.length; I++)
    {
        if (Sender.id == SupressFocusControls[I])
        {
            SupressFocusControls.splice(I, 1);
            return;
        }
        if (Sender.id == SupressFocusControls[I])
        {
            SupressFocusControls.splice(I, 1);
            return;
        }
    }
        
    try
    {
        if (Width == null)
            Width = 150;
            
        if (Height == null)
            Height = 200;
        
        var FrameName                       = Sender.id;
        var Frame                           = null;
        try { Frame = document.frames(FrameName + '$PopupFrame'); } catch(err) { }
        if (Frame == null)
        {
            var FrameString = '<iframe name="' + FrameName + '$PopupFrame' + '" id="' + FrameName + '$PopupFrame' + '" + src="' + URL + '" + ' + StyleString + ' ></iframe>';
            Frame = document.createElement(FrameString);
            Frame.style.visibility          = 'hidden';
            Frame.style.position            = 'absolute';
            Frame.style.zIndex              = 1000;
            
            if (AddToBody)
                if (Sender.ownerDocument.forms.length > 0)
                    Sender.ownerDocument.forms(0).appendChild(Frame);
                else
                    Sender.ownerDocument.body.appendChild(Frame);
            else
                Sender.parentElement.insertBefore(Frame, Sender);
        }
/*
        var CurCtl                          = Sender;
        var Left                            = 0; 
        var Top                             = 0;
        var ParentWidth                     = 0;
        var CtlWidth                        = CurCtl.clientWidth;
        while (CurCtl != null)
        {
            if (CurCtl.offsetLeft > 0)
                Left   = Left + CurCtl.offsetLeft;
            if (CurCtl.offsetTop > 0)
                Top    = Top  + CurCtl.offsetTop;    
            try { if (CurCtl.clientWidth > 0) ParentWidth = CurCtl.clientWidth; } catch (err) { }
            CurCtl = CurCtl.offsetParent;
        }
        Top                                 = Top + 22;
        //Left                                = Left + 20;
        if (Width > CtlWidth + 20)     
            Left = Left - (Width - CtlWidth - 20) / 2;

        if ((Left + Width > ParentWidth) && (ParentWidth > 0))
            Left                            = ParentWidth - Width;
*/

        if (UseEditHeight)
            EditHeight                      = Sender.offsetHeight;
            
        var Position                        = GetPopupPlacement(Sender, Width);            
        var Left                            = Position.Left;
        var Top                             = Position.Top + EditHeight;
        
        var CallerID                        = Frame.document.getElementById("PopupCallerID");
        CallerID.value                      = Sender.id;
        if (Frame.window.frameElement.style.visibility != "visible")
        {
            Frame.frameElement.style.left       = Left;
            Frame.frameElement.style.top        = Top;
            Frame.frameElement.style.width      = Width;
            Frame.frameElement.style.height     = Height;
            var ListBox                         = Frame.document.getElementById("ItemsList");
            if (ListBox != null)
            {
                ListBox.style.width                 = Width;                    
                ListBox.style.height                = Height;                    
            }
        }
        var ValueTextBox                    = Frame.document.getElementById("ValueTextBox");
        ValueTextBox.value                  = Sender.value;
        
        if ((SecondValueEdit != null) && (SecondValueEdit != ""))
        try {
            var SecondValueTextBox          = Frame.document.getElementById("SecondValueTextBox");
            var SecondValueControl          = document.getElementById(SecondValueEdit);
            if ((SecondValueTextBox != null) && (SecondValueControl != null))
                SecondValueTextBox.value    = SecondValueControl.value;
        }
        catch (errSec) { }

        Frame.window.frameElement.style.visibility = "visible";
        Frame.execScript("__doPostBack(\"ValueTextBox\",\"\")", "javascript");
        LastSender                          = null;
    }
    catch(err)
    {
        LastSender = Sender;
        window.setTimeout("PopupListEditKeyPress(null, " + Width + "," + Height + ", '" + URL + "', '" + StyleString + "', " + AddToBody + ", '" + SecondValueEdit + "')", 500);
        return;
    }
}


function PopedUpListValueSelected(Sender) 
{
    var FrameWin  = window;
    var Frame     = window.frameElement;
    if ((Frame == null) || (Frame.id.indexOf('$PopupFrame') == -1))
    {
        var FrameName                        = Sender.id;
        try { var FrameWin                   = document.frames(FrameName + '$PopupFrame'); } catch (err) { }
        if (FrameWin != null)
            Frame                            = FrameWin.frameElement;
    }
    
    if ((Frame != null) && (Frame.style.visibility != "hidden"))
    {
        var TextBoxName                      = Frame.id;
        TextBoxName                          = TextBoxName.substr(0, TextBoxName.indexOf('$PopupFrame'));
        var TextBox                          = FrameWin.parent.document.getElementById(TextBoxName);
        var ValueTextBox                     = FrameWin.document.getElementById("ValueTextBox");
        if ((TextBox != null) && (ValueTextBox != null))
	{
            if (TextBox.value != ValueTextBox.value)
            {
                TextBox.value                    = ValueTextBox.value;
                TextBox.focus();
//                TextBox.fireEvent("onchange");
                try {
                    TextBox.fireEvent('onchange');            
                    }
                catch (err) {
                    var e = document.createEvent('HTMLEvents'); 
                    e.initEvent('change', false, false); 
                    TextBox.dispatchEvent(e); 
                }
            }
	    if (Frame.src.toUpperCase().indexOf('AUTOPOSTBACK=T') != -1)
                TextBox.ownerDocument.parentWindow.execScript("__doPostBack('" + TextBox.id + "', '');");
	}

        FrameWin.execScript("try { DocumentHasLostFocus(); } catch (err) { }");
        Frame.style.visibility               = "hidden";
        LastSender                           = null;
    }
}

function PopedUpListEditLostFocus(Sender) 
{
    if ((document.activeElement != Sender) && (document.activeElement.id != Sender.id + '$PopupFrame'))
    {
        var FrameName                       = Sender.id;
        var Frame                           = null;
        try {
            Frame                           = document.getElementById(FrameName + '$PopupFrame');
        }
        catch (err) 
        {
            Frame                           = window;
        }
        try { 
            if (Frame.frameElement.id.indexOf('PopupFrame') != -1)
	            Frame.frameElement.style.visibility = "hidden"; 
        } 
        catch (err) 
        { 
            if (Frame != null)
                Frame.style.visibility = "hidden"; 
        }
        LastSender                          = null;
    }
}

function PopedUpPanelScroll() 
{
    if (document.activeElement == null)
        return;

    var FrameName                       = document.activeElement.id;
    var Frame                           = null;
    try {
        Frame                           = document.frames(FrameName + '$PopupFrame');
    }
    catch (err) 
    {
    }
    try { Frame.frameElement.style.visibility = "hidden"; } catch (err) { }

    LastSender                          = null;
}

function FocusBackEditBox(Sender)
{
    var CallerID   = document.getElementById("PopupCallerID");    
    var CallerName = CallerID.value;
    var Caller     = window.parent.document.getElementById(CallerName);
    window.parent.execScript("SupressFocus   = true;",  "javascript"); 
    Caller.focus();
    window.parent.execScript("SupressFocus   = false;", "javascript"); 
}

var VALID        = 0;
var NOT_VALID    = 1;
var NOT_IN_RANGE = 2;

function ValidateInteger(Value)
{
    var ParsedInt  = parseInt(Value);
    var ParsedInt2 = Number(Value);
    if (!isNaN(ParsedInt) && !isNaN(ParsedInt2))
        return true;
    else
        return false;
}

function ValidateDate(Value, AllowDate, AllowTime, AllowEmpty)
{
	var ParsedDate = GetDate(Value);
	if (!isNaN(ParsedDate))
	{
		var d = GetDate(Value);
		if (d == null)
		{
		    if ((AllowEmpty != null) && (!AllowEmpty))
		        return false;
		        
		    return true;
		}
		    
		if ((AllowDate != null) && (!AllowDate))
			if ((d.getDate() != 1) || (d.getMonth() != 0) || (d.getFullYear() != 1970))
				return false;
			
		if ((AllowTime != null) && (!AllowTime))
			if ((d.getHours() != 0) || (d.getMinutes() != 0) || (d.getSeconds() != 0) || (d.getMilliseconds() != 0))
				return false;

		return true;
	}
	else
		return false;
}

function CheckIntInRange(Value, From, To)
{
	if ((From != null) && (To != null))
	{
		if ((Value >= From) && (Value <= To))
			return true;
		else
	        return false;
	}
	else if ((From == null) && (To != null))
	{
		if (Value <= To)
			return true;
		else
	        return false;
    }
	else if ((To == null) && (From != null))
	{
		if (Value >= From)
			return true;
		else
	        return false;
    }
    else
		return true;
}

function CheckIntValue(Value, From, To)
{
	if (!ValidateInteger(Value))
		return NOT_VALID;
	else
	{
		if (!CheckIntInRange(parseInt(Value), From, To))
			return NOT_IN_RANGE;
	}
	return VALID;
}

function GetDate(DateValue)
{
	if (DateValue.constructor==Date)
		return DateValue;
	if (DateValue.constructor==String)
	{
	    if ((DateValue.toUpperCase() == 'NOW') || ((DateValue.toUpperCase() == 'TODAY')))
	    {
    		var d = new Date();
		    d.setTime(Date.parse(Date()));
		    return d;
	    }
	
		if (DateValue == '')
			return null;

		var ParsedDate = Date.parse(DateValue);
		if (isNaN(ParsedDate))
			ParsedDate = Date.parse('1/1/1970 ' + DateValue);

		var d = new Date()
		d.setTime(ParsedDate);
		return d;
	}
	if (DateValue.constructor==Number)
	{
		var d = new Date()
		d.setTime(DateValue);
		return d;
	}
	throw "Not a date value";
}

function CheckDateValue(Value, From, To, AllowDate, AllowTime)
{
	if (!ValidateDate(Value, AllowDate, AllowTime))
		return NOT_VALID;
	else
	{
		if (!CheckIntInRange(GetDate(Value), GetDate(From), GetDate(To)))
			return NOT_IN_RANGE;
	}
	return VALID;
}

function CheckStringLength(Value, From, To)
{
	if (!CheckIntInRange(Value.length, From, To))
		return NOT_IN_RANGE;

	return VALID;
}

function CheckStringInList(Value, List)
{
	if (List.indexOf(Value) == -1)
		return NOT_IN_RANGE;

	return VALID;
}

function CheckSameFocusedElement(Element)
{
    var CurItemBefore        = SupressValidCheckCurItem;
    SupressValidCheckCurItem = null;
    var CurElement           = document.activeElement;
    if ((CurElement != null) && (Element != null))
    {
        if (CurElement.id == Element.id)
            return true;
            
        if (CurElement.id == Element.id + '$PopupFrame')
            return true;

        if (CurElement.id == Element.id + '$PopupFrame')
            return true;
            
        if (CurItemBefore == CurElement)
            return true;
    }
    
    return false;
}

function PutColorOfOptions(Element)
{
    for (I = 0; I < Element.length; I++)
    {
        var OptionText   = Element.options[I].text;
        var OptionUpper  = OptionText.toUpperCase();
        var StartIndex   = OptionUpper.indexOf("{COLOR:");   
        var IndexOfClose = OptionUpper.indexOf("}");
        if ((StartIndex != -1) && (IndexOfClose != -1))
        {
            var Color = OptionText.substring(StartIndex + "{COLOR:".length, IndexOfClose);
            Element.options[I].text        = OptionText.substring(IndexOfClose + 1);
            Element.options[I].style.color = Color;
        }

        StartIndex   = OptionUpper.indexOf("{BACKCOLOR:");   
        IndexOfClose = OptionUpper.indexOf("}");
        if ((StartIndex != -1) && (IndexOfClose != -1))
        {
            var Color = OptionText.substring(StartIndex + "{BACKCOLOR:".length, IndexOfClose);
            Element.options[I].text                  = OptionText.substring(IndexOfClose + 1);
            Element.options[I].style.backgroundColor = Color;
        }

        OptionText   = Element.options[I].value;
        OptionUpper  = OptionText.toUpperCase();
        StartIndex   = OptionUpper.indexOf("{COLOR:");   
        IndexOfClose = OptionUpper.indexOf("}");
        if ((StartIndex != -1) && (IndexOfClose != -1))
        {
            var Color = OptionText.substring(StartIndex + "{COLOR:".length, IndexOfClose);
            Element.options[I].value       = OptionText.substring(IndexOfClose + 1);
            Element.options[I].style.color = Color;
        }

        StartIndex   = OptionUpper.indexOf("{BACKCOLOR:");   
        IndexOfClose = OptionUpper.indexOf("}");
        if ((StartIndex != -1) && (IndexOfClose != -1))
        {
            var Color = OptionText.substring(StartIndex + "{BACKCOLOR:".length, IndexOfClose);
            Element.options[I].value                 = OptionText.substring(IndexOfClose + 1);
            Element.options[I].style.backgroundColor = Color;
        }
    }
    
    return false;
}

function ToggleAdvancedElement(Element)
{   
    var AdvancedVisible = Element.style.visibility;
    if (AdvancedVisible == 'visible')
    {
        Element.style.position = 'absolute'; 
        Element.style.visibility = 'hidden';
    }
    else
    {
        Element.style.position = ''; 
        Element.style.visibility = 'visible';
    }
    return true;
}

function LoadFile(FileUpload, CallerElement)
{   
    var FileUploadElement = document.getElementById(FileUpload);
    var Element = document.getElementById(CallerElement);
    if (FileUploadElement != null)
    {
        FileUploadElement.click();
        if (FileUploadElement.value != null)
            if (Element != null)
                __doPostBack(CallerElement);
    }
    return true;
}

function RoundDisplayFloat(Num, Digits)
{
    if (Digits == null)
        Digits = 2;
        
	var NumStr = String(Num);
	return NumStr .substr(0, NumStr.indexOf('.') + Digits + 1);
}

function Bounce(Sender)
{
    var Sender = document.getElementById(Sender);
    if (Sender == null)
        return;
    
    if (Sender.style.position != 'relative')
    {
        Sender.style.position = 'relative';
        Sender.style.left  = '-2px';
        Sender.style.top   = '-2px';
        setInterval('Bounce("' + Sender.id + '")', 150);
    }
    else if ((Sender.style.left == '-2px') && (Sender.style.top == '-2px'))
    {
        Sender.style.left  = '2px';
        Sender.style.top   = '-2px';
    }
    else if ((Sender.style.left == '2px') && (Sender.style.top  == '-2px'))
    {
        Sender.style.left  = '2px';
        Sender.style.top   = '2px';
    }
    else if ((Sender.style.left == '2px') && (Sender.style.top == '2px'))
    {
        Sender.style.left  = '-2px';
        Sender.style.top   = '2px';
    }
    else if ((Sender.style.left == '-2px') && (Sender.style.top == '2px'))
    {
        Sender.style.left  = '-2px';
        Sender.style.top   = '-2px';
    }
}

function ResizeFrame(FrameElement, ExtraWidth, ExtraHeight, WhenHidden, AllowZeroValues, PageComplete, IgnoreReductionOf)
{
    if (IgnoreReductionOf == null)
        IgnoreReductionOf = 20;
        
    if (PageComplete == null)
        PageComplete = false;

    if (ExtraWidth == null)
        ExtraWidth = 0;
        
    if (ExtraHeight == null)
        ExtraHeight = 0;
        
    if (WhenHidden == null)
        WhenHidden = false;
        
    if (AllowZeroValues == null)
        AllowZeroValues = true;
                
    if (FrameElement.constructor==String)
        FrameElement = document.getElementById(FrameElement);
        
    if (FrameElement != null)
        if (WhenHidden || FrameElement.style.visibility != 'hidden')
        try { 
            if (!PageComplete)
                PageComplete = (FrameElement.contentWindow.document.readyState == 'complete');
        
            if (PageComplete)
            { 
                var width  = FrameElement.contentWindow.document.body.offsetWidth + ExtraWidth;
                var height = FrameElement.contentWindow.document.body.offsetHeight + ExtraHeight;
                
                if (AllowZeroValues || ((width != 0) && (height != 0)))
                {
                    var IgnoreWidth = false;
                    if (IgnoreReductionOf > 0)
                    {
                        var WidthGap = FrameElement.style.width.replace('px', '') - width;
                        if ((WidthGap > 0) && (WidthGap <= IgnoreReductionOf))
                            IgnoreWidth = true;
                    }

                    var IgnoreHeight = false;
                    if (IgnoreReductionOf > 0)
                    {
                        var HeightGap = FrameElement.style.height.replace('px', '') - height;
                        if ((HeightGap > 0) && (HeightGap <= IgnoreReductionOf))
                            IgnoreHeight = true;
                    }

                    if (!IgnoreWidth)
                        FrameElement.style.width  = width.toString() + 'px';
                    if (!IgnoreHeight)
                        FrameElement.style.height = height.toString() + 'px';
                }
            }
        }
        catch (err) { }
}

function GetFrameContentHeight(ExtraHeight, AllowZeroValues)
{
    if (ExtraHeight == null)
        ExtraHeight = 0;
        
    if (AllowZeroValues == null)
        AllowZeroValues = true;
                
    try { 
        if (document.readyState == 'complete') 
        { 
            height = document.body.offsetHeight + ExtraHeight;
            if (AllowZeroValues || (height != 0))
                return height;
        }
    }
    catch (err) { }
    return '';
}

function Trim(Str)
{
    while (Str.indexOf(' ') == 0)
        Str = Str.substr(1);

    if (Str.length > 0)
        while (Str.lastIndexOf(' ') == Str.length - 1)
            Str = Str.substr(0, Str.length - 1);
        
    return Str;
}

var FramesAutoResizeInterval = null;
var FramesToResize = new Array();

function AddFrameToAutoCheck(FrameElement, ExtraWidth, ExtraHeight, WhenHidden, AllowZeroValues)
{
    var FrameResizeParams = new Object();
    FrameResizeParams.FrameElement    = FrameElement;
    FrameResizeParams.ExtraWidth      = ExtraWidth;
    FrameResizeParams.ExtraHeight     = ExtraHeight;
    FrameResizeParams.WhenHidden      = WhenHidden;
    FrameResizeParams.AllowZeroValues = AllowZeroValues;

    FramesToResize.push(FrameResizeParams);
    if (FramesToResize.length > 0)
        if (FramesAutoResizeInterval == null)
            FramesAutoResizeInterval = window.setInterval("CheckAllResizableFrames()", 100);
}

function CheckAllResizableFrames()
{
    for (CurFrame in FramesToResize)
        ResizeFrame(FramesToResize[CurFrame].FrameElement, FramesToResize[CurFrame].ExtraWidth, FramesToResize[CurFrame].ExtraHeight, FramesToResize[CurFrame].WhenHidden, FramesToResize[CurFrame].AllowZeroValues);
}

function Initialize2StateImg(ValueElementID, ElementName) 
{ 
    var Element = document.getElementById(ValueElementID); 
    if (Element != null) 
        if (Element.value == '1') 
        { 
            var ImgElement = document.getElementById(ElementName); 
            if (ImgElement != null) 
            { 
                Element.value = '0'; 
                ImgElement.click(); 
            } 
        } 
}

function Switch2StateImg(ValueElementID, ThisElement, Images)
{
    var Element = document.getElementById(ValueElementID); 
    if (Element != null) 
        ThisElement.src = Images[Number(Element.value)];
}

function Change2State(ValueElementID, ThisElement, Images, AdvanceElement)
{
    var Element = document.getElementById(ValueElementID); 
    if (Element != null) 
    { 
        if (Element.value == '1') 
            Element.value = '0'; 
        else  
            Element.value = '1'; 
        
        ThisElement.src = Images[Number(Element.value)]; 
        if (AdvanceElement != null)
        {
            var AdvancedElement = document.getElementById(AdvanceElement); 
            if (AdvancedElement != null) 
                ToggleAdvancedElement(AdvancedElement); 
        }
    }
}

var AjaxPopupFieldName = "";
var FocusedElement = null;
function PopupListEditKeyPressAjax(Sender, Width, Height, URL, StyleString, AddToBody, SecondValueEdit, UseEditHeight, RTL) 
{
    if (window.event != null)
        if ((window.event.keyCode == 9) || (window.event.keyCode == 16))
            return;
 
    var EditHeight = 22;

    if (UseEditHeight == null)
        UseEditHeight = false;
        
    if (RTL == null)
        RTL = false;
        
    if (AddToBody == null)
        AddToBody = true;

    if (SupressFocus)
        return;    

    if (Sender == null)
        Sender = LastSender;

    if (Sender == null)
        return;
        
    for (I = 0; I < SupressFocusControls.length; I++)
    {
        if (Sender.id == SupressFocusControls[I])
        {
            SupressFocusControls.splice(I, 1);
            return;
        }
        if (Sender.id == SupressFocusControls[I])
        {
            SupressFocusControls.splice(I, 1);
            return;
        }
    }
        
    try
    {
        if (Width == null)
            Width = 150;
            
        if (Height == null)
            Height = 200;
        
        var DomDiv = document.getElementById("FloatingDiv");
        if (DomDiv == null)
        {
            DomDiv    = document.createElement("Div");
            DomDiv.id = "FloatingDiv";

            if (AddToBody)
                if (document.forms.length > 0)
                    document.forms[0].appendChild(DomDiv);
                else
                    document.body.appendChild(DomDiv);
            else
                document.insertBefore(DomDiv, Sender);
        }
        
        var FrameName    = Sender.id + '$PopupFrame';
        var Frame        = document.getElementById(FrameName);
        if (Frame == null)
        {
            var FrameString  = '<select size="4" name="' + FrameName + '" id="' + FrameName + '" ' + StyleString + ' OnFocus="FocusedElement=this;" OnKeyUp="if (event.keyCode == 13) this.click();" OnClick="SelectAjaxPopupValue(\'' + Sender.id + '\', this);" onblur="FocusedElement = null; try { document.getElementById(\'FloatingDiv\').style.visibility = \'hidden\'; } catch (err) { }"></select>';
            DomDiv.innerHTML = FrameString;

            Frame = document.getElementById(FrameName);
            DomDiv.style.visibility         = 'hidden';
            DomDiv.style.position           = 'absolute';
            DomDiv.style.zIndex             = 1000;
            if (RTL)
                Frame.dir                   = "rtl";
        }

        if (UseEditHeight)
            EditHeight                      = Sender.offsetHeight;
            
        var Position                        = GetPopupPlacement(Sender, Width);            
        var Left                            = Position.Left;
        var Top                             = Position.Top + EditHeight;
        
        if (DomDiv.visibility != "visible")
        {
            DomDiv.style.left  = Left + 'px';
            DomDiv.style.top   = Top + 'px';
            Frame.style.width  = Width + 'px';
            Frame.style.height = Height + 'px';
        }
        
        var SecondValue = '';
        if (SecondValueEdit != null)
        try {
            var SecondValueControl = document.getElementById(SecondValueEdit);
            SecondValue            = SecondValueControl.value;
        }
        catch (errSec) { }

        AjaxPopupFieldName         = Sender.id;
        var Param1                 = StringToUnicode(Sender.value, '|');
        var Param2                 = StringToUnicode(SecondValue, '|');
//        alert('Param1 = ' + Param1 + ' Param2 = ' + Param2 + ' URL = ' + URL);
        PlayAjax(URL, FillListBox, Sender.name, Param1, Param2);
        LastSender                          = null;
    }
    catch(err)
    {
        //alert(err);
        LastSender = Sender;
//        window.setTimeout("PopupListEditKeyPress(null, " + Width + "," + Height + ", '" + URL + "', '" + StyleString + "', " + AddToBody + ", '" + SecondValueEdit + "')", 500);
        return;
    }
}

function PopedUpListEditLostFocusAjax(Sender) 
{
    window.setTimeout('DoPopedUpListEditLostFocusAjax("' + Sender.id + '");', 5);
}

function DoPopedUpListEditLostFocusAjax(SenderID) 
{
    var Sender = document.getElementById(SenderID);
    var DomDiv = document.getElementById("FloatingDiv");
    if (DomDiv != null)
    {
        var Elem = DomDiv.firstChild;
        if (Elem.id == Sender.id + '$PopupFrame')
        {
            if (FocusedElement == null)
                DomDiv.style.visibility = 'hidden';
        }
    }
    LastSender = null;
}

function SelectAjaxPopupValue(SenderId, Popup)
{
    var TargetElement = document.getElementById(SenderId); 
    if (TargetElement != null) 
    { 
        var CurValue = TargetElement.value; 
        if (Popup.selectedIndex == -1)
            return;
            
        TargetElement.value = Popup.options[Popup.selectedIndex].text; 
        if (CurValue != TargetElement.value) 
        { 
            TargetElement.focus(); 
            try {
                TargetElement.fireEvent('onchange');            
                }
            catch (err) {
                var e = document.createEvent('HTMLEvents'); 
                e.initEvent('change', false, false); 
                TargetElement.dispatchEvent(e); 
            }
        } 

        var DomDiv       = document.getElementById("FloatingDiv");
        DomDiv.style.visibility = 'hidden';
        //Popup.style.visibility = 'hidden';
    }
}

function FillListBox()
{
    if ((xmlHttp.readyState == 4) || (xmlHttp.readyState == "complete"))
    { 
        var Element = document.getElementById(AjaxPopupFieldName);
        if (Element != null)
        {
            var PopupID      = AjaxPopupFieldName + '$PopupFrame';
            var PopupListBox = document.getElementById(PopupID); 
            var DomDiv       = document.getElementById("FloatingDiv");
            DomDiv.style.visibility = 'visible';
            var Strings      = xmlHttp.responseText.split('|');
            var Options      = "";
            while (PopupListBox.options.length > 0)
                PopupListBox.remove(0);
                
            for (X in Strings)
            {
                if (Strings[X] != '')
                {
                    var Option  = document.createElement("OPTION");
                    Option.text = Strings[X];
                    if (navigator.appVersion.indexOf('MSIE') >= 0)
                        PopupListBox.add(Option);
                    else
                        PopupListBox.appendChild(Option);
                }
            }   
        }
    } 
}

function StringToUnicode(Str, Delimiter)
{
    if (Delimiter == null)
        Delimiter = ",";
        
    var Result = '';
    for (x = 0; x < Str.length; x ++)
    {
        if (Result != '')
            Result += Delimiter;
            
        Result += Str.charCodeAt(x);
    }
    
    return Result;
}

// AJAX
function GetXmlHttpObject(handler)
{ 
    var objXMLHttp = null
    if (window.XMLHttpRequest)
    {
        objXMLHttp = new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    try {
        objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP")
        }
    catch(e)
    {
        try {    
            objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e2)
            {
            }
    }
    return objXMLHttp
}

function PlayAjax(URL, DestFunction, Param1, Param2, Param3, Param4, Param5)
{
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
        return false;

    var Params = "";
    if (Param1 != null)
    {
        if (Params == "")
            Params = "?";
        else 
            Params += "&";
        
        Params += "Param1="+ Param1;
    }

    if (Param2 != null)
    {
        if (Params == "")
            Params = "?";
        else 
            Params += "&";
        
        Params += "Param2="+ Param2;
    }

    if (Param3 != null)
    {
        if (Params == "")
            Params = "?";
        else 
            Params += "&";
        
        Params += "Param3="+ Param3;
    }
        
    if (Param4 != null)
    {
        if (Params == "")
            Params = "?";
        else 
            Params += "&";
        
        Params += "Param4="+ Param4;
    }
        
    if (Param5 != null)
    {
        if (Params == "")
            Params = "?";
        else 
            Params += "&";
        
        Params += "Param5="+ Param5;
    }
        
    URL += Params;
    xmlHttp.onreadystatechange = DestFunction
    xmlHttp.open("GET", URL, true)
    xmlHttp.send(null)
    return true;
}

function ChangeExtendedPanelColor(Table, OldBorderColor, NewBorderColor, OldBackColor, NewBackColor)
{
    if (document.readyState != 'loading') 
        for (var J=0; J < Table.rows.length; J++) 
        {
            var Row = Table.rows[J];
            for (var I=0; I < Row.cells.length; I++) 
            { 
                if (Row.cells[I].firstChild.tagName == 'TABLE') 
                    ChangeExtendedPanelColor(Row.cells[I].firstChild, OldBorderColor, NewBorderColor, OldBackColor, NewBackColor)
            
                if (Row.cells[I].style.backgroundColor != '') 
                    Row.cells[I].style.backgroundColor = NewBackColor; 
                if (Row.cells[I].style.backgroundImage != '') 
                    Row.cells[I].style.backgroundImage = Row.cells[I].style.backgroundImage.replace(OldBorderColor.replace("#", ""), NewBorderColor.replace("#", "")).replace(OldBackColor.replace("#", ""), NewBackColor.replace("#", "")); 
                if (Row.cells[I].colSpan == '3') 
                    try { Row.cells[I].firstChild.style.backgroundColor = NewBackColor;  } catch (e) { } 
                if (Row.cells[I].firstChild != null) 
                    if (Row.cells[I].firstChild.tagName == 'IMG') 
                        Row.cells[I].firstChild.src = Row.cells[I].firstChild.src.replace(OldBorderColor.replace("#", ""), NewBorderColor.replace("#", "")).replace(OldBackColor.replace("#", ""), NewBackColor.replace("#", "")); 
                if (Row.cells[I].style.borderBottom != '') 
                    Row.cells[I].style.borderBottom = Row.cells[I].style.borderBottom.replace(OldBorderColor.toLowerCase(), NewBorderColor.toLowerCase()); 
                if (Row.cells[I].style.borderTop != '') 
                    Row.cells[I].style.borderTop = Row.cells[I].style.borderTop.replace(OldBorderColor.toLowerCase(), NewBorderColor.toLowerCase()); 
                if (Row.cells[I].style.borderLeft != '') 
                    Row.cells[I].style.borderLeft = Row.cells[I].style.borderLeft.replace(OldBorderColor.toLowerCase(), NewBorderColor.toLowerCase()); 
                if (Row.cells[I].style.borderRight != '') 
                    Row.cells[I].style.borderRight = Row.cells[I].style.borderRight.replace(OldBorderColor.toLowerCase(), NewBorderColor.toLowerCase()); 
            }
        }
}

function LateLoadImage(CtlId, Src)
{
    var Element = document.getElementById(CtlId);
    try { Element.src = Src; if (Element.style.visibility == "hidden") Element.style.visibility = "visible"; } catch (ex) { }
}

function ReadjustExtendedPanel(PanelName)
{
	var DivElem  = document.getElementById(PanelName); 
	var TopElem  = document.getElementById(PanelName + 'topCell'); 
	var LeftElem = document.getElementById(PanelName + 'leftImg'); 
	if ((DivElem != null) && (TopElem != null))
	{
		var Wd = DivElem.offsetWidth;
		Wd -=  LeftElem.offsetWidth * 2 - 2;
		TopElem.style.width = Wd + 'px';
	}
}

function CheckBoxesByEditValue(ValueTextBox)
{
    var CurValueElem    = document.getElementById(ValueTextBox);
    var CurValue        = CurValueElem.value;
    var CheckBoxes      = document.getElementsByTagName("INPUT");
    var Labels          = document.getElementsByTagName("LABEL");
    var ValueStrings    = CurValue.split(',');

    var FirstChk       = 0;
    for (FirstChk = 0; FirstChk < CheckBoxes.length; FirstChk ++)
        if (CheckBoxes[FirstChk].type == "checkbox")
            break;
    
    var I, J;
    for (I = FirstChk; I < CheckBoxes.length; I ++)
    {
        var Label = null;
        if (I < Labels.length)
            if (Labels[I - FirstChk].htmlFor == CheckBoxes[I].id)
                Label = Labels[I - FirstChk];

        if (Label == null)
            for (J = 0; J < Labels.length; J ++)
                if (Labels[J].htmlFor == CheckBoxes[I].id)
                {
                    Label = Labels[J];
                    break;
                }

        CheckBoxes[I].checked = false;
        if (Label != null)
        {               
            if (Label.innerText != null)
            {
                var InnerText = Trim(Label.innerText);
                var CurElem;
                for (CurElem = 0; CurElem < ValueStrings.length; CurElem ++)
                    if (ValueStrings[CurElem] == InnerText)
                    {
                        CheckBoxes[I].checked = true;
                        break;
                    }
            }
        }
    }
}

function EditValueByCheckBoxes(ValueTextBox)
{
    var Result         = "";
    var CheckBoxes     = document.getElementsByTagName("INPUT");
    var Labels         = document.getElementsByTagName("LABEL");
    var FirstChk       = 0;
    for (FirstChk = 0; FirstChk < CheckBoxes.length; FirstChk ++)
        if (CheckBoxes[FirstChk].type == "checkbox")
            break;
    
    var I, J;
    for (I = FirstChk; I < CheckBoxes.length; I ++)
    {
        if (CheckBoxes[I].checked)
        {
            var Label = null;
            if (I < Labels.length)
                if (Labels[I - FirstChk].htmlFor == CheckBoxes[I].id)
                    Label = Labels[I - FirstChk];

            if (Label == null)
                for (J = 0; J < Labels.length; J ++)
                    if (Labels[J].htmlFor == CheckBoxes[I].id)
                    {
                        Label = Labels[J];
                        break;
                    }

            if (Label != null)
            {               
                if (Result != "")
                    Result = Result + ",";
                Result = Result + Label.innerText;
            }
        }
    }

    var ValueTextBox   = document.getElementById(ValueTextBox); 
    ValueTextBox.value = Result;  
}


