﻿// JScript File
Type.registerNamespace("QPM");
Type.registerNamespace("QPM.Web");
Type.registerNamespace("QPM.Web.UI");
Type.registerNamespace("QPM.Web.UI.Controls");

// Don't remember to register your new class below the script
//


// GLOBAL CONSTANTS
//

var enableButtonTimeout = 1000;


/****************************************/
/* QPM.Web.UI.Controls.Control				 **/
/****************************************/

QPM.Web.UI.Controls.Control = function(ElementId)
{
	QPM.Web.UI.Controls.Control.initializeBase(this, [$get(ElementId)]);	
}

QPM.Web.UI.Controls.Control.prototype = 
{
	show : function()
	{
		if (this.isShow()) return;		
		this.changeState();
	},
	
	hide : function()
	{
		if (!this.isShow()) return;		
		this.changeState();
	},

	changeState : function(e)
	{
		Sys.UI.DomElement.toggleCssClass(this.get_element(), "hide");
	},

	isShow : function(e)
	{
		return !Sys.UI.DomElement.containsCssClass(this.get_element(), "hide");	
	}
}

QPM.Web.UI.Controls.Control.registerClass('QPM.Web.UI.Controls.Control', Sys.UI.Control);


/****************************************/
/* QPM.Web.UI.Controls.LanguageList 	 **/
/****************************************/
QPM.Web.UI.Controls.LanguageList = function(
		ElementId,
		MainCheckBoxId,
		HiddenId
		) {
	QPM.Web.UI.Controls.LanguageList.initializeBase(this, [$get(ElementId)]);
	this.get_element().instance = this;
	this.mainCheckBoxId = MainCheckBoxId;
	this.hiddenId = HiddenId;

}

QPM.Web.UI.Controls.LanguageList.prototype =
{
    moveUp: function() {
        var childs = new Array();
        eval("childs = childs_" + this.mainCheckBoxId + ";");
        for (var i = 0; i <= childs.length - 1; i++) {
            if (i > 0) {
                var oChild = $get(childs[i]);
                if (oChild) {
                    if ($get(childs[i - 1]) && !$get(childs[i - 1]).checked) {
                        if (oChild.checked) {
                            oChild.checked = false;
                            this.moveUpLanguage(oChild.attributes['LanguageID'].value);
                            $get(childs[i - 1]).checked = true;
                            this.moveUpCaption(oChild);
                        }
                    }
                }
                else return;
                var oChild1 = $get(childs[i - 1]);
                if (oChild1) {
                    var tmp = oChild.attributes['LanguageID'].value;
                    oChild.attributes['LanguageID'].value = oChild1.attributes['LanguageID'].value
                    oChild1.attributes['LanguageID'].value = tmp;
                }
                else return;
            }
        }
    },
    moveDown: function() {
        var childs = new Array();
        eval("childs = childs_" + this.mainCheckBoxId + ";");
        for (var i = childs.length; i >= 0; i--) {
            if (i < childs.length - 1) {
                var oChild = $get(childs[i]);
                if (oChild) {
                    if ($get(childs[i + 1]) && !$get(childs[i + 1]).checked) {
                        if (oChild.checked) {
                            oChild.checked = false;
                            this.moveDownLanguage(oChild.attributes['LanguageID'].value);
                            $get(childs[i + 1]).checked = true;
                            this.moveDownCaption(oChild);
                        }
                    }
                }
                else return;
                var oChild1 = $get(childs[i + 1]);
                if (oChild1) {
                    var tmp = oChild.attributes['LanguageID'].value;
                    oChild.attributes['LanguageID'].value = oChild1.attributes['LanguageID'].value
                    oChild1.attributes['LanguageID'].value = tmp;
                }
                else return;
            }
        }
    },

    moveUpCaption: function(checkbox) {

        var oRow = checkbox.parentNode.parentNode;
        if (oRow.rowIndex <= 0) return;
        var oRow1 = this.get_element().rows[oRow.rowIndex - 1];
        if (oRow.cells[1].textContent) {
            var tmpText = oRow.cells[1].textContent;
            oRow.cells[1].textContent = oRow1.cells[1].textContent;
            oRow1.cells[1].textContent = tmpText;
        }
        else {
            var tmpText = oRow.cells[1].innerText;
            oRow.cells[1].innerText = oRow1.cells[1].innerText;
            oRow1.cells[1].innerText = tmpText;
        }
    },
    moveDownCaption: function(checkbox) {
        var oRow = checkbox.parentNode.parentNode;
        if (oRow.rowIndex >= this.get_element().rows.length) return;
        var oRow1 = this.get_element().rows[oRow.rowIndex + 1];
        if (oRow.cells[1].textContent) {
            var tmpText = oRow.cells[1].textContent;
            oRow.cells[1].textContent = oRow1.cells[1].textContent;
            oRow1.cells[1].textContent = tmpText;
        }
        else {
            var tmpText = oRow.cells[1].innerText;
            oRow.cells[1].innerText = oRow1.cells[1].innerText;
            oRow1.cells[1].innerText = tmpText;
        }
    },
    moveUpLanguage: function(languageId) {
        var langs = $get(this.hiddenId).value;
        var langArray = langs.split(String.fromCharCode(124));
        for (var i = 0; i < langArray.length; i++) {
            if (langArray[i] == languageId) {
                if (i > 0) {
                    var tmp = langArray[i];
                    langArray[i] = langArray[i - 1];
                    langArray[i - 1] = tmp;
                }
                $get(this.hiddenId).value = langArray.join(String.fromCharCode(124));
                return;
            }
        }
    },
    moveDownLanguage: function(languageId) {
        var langs = $get(this.hiddenId).value;
        var langArray = langs.split(String.fromCharCode(124));
        for (var i = 0; i < langArray.length; i++) {
            if (langArray[i] == languageId) {
                if (i < langArray.length - 1) {
                    var tmp = langArray[i];
                    langArray[i] = langArray[i + 1];
                    langArray[i + 1] = tmp;
                }
                $get(this.hiddenId).value = langArray.join(String.fromCharCode(124));
                alert($get(this.hiddenId).value);
                return;
            }
        }
    }
};

QPM.Web.UI.Controls.LanguageList.registerClass('QPM.Web.UI.Controls.LanguageList', Sys.UI.Control);


/****************************************/
/* QPM.Web.UI.Controls.LisEdit				 **/
/****************************************/

QPM.Web.UI.Controls.ListEdit = function(
		ElementId,
		ControlId,
		MainCheckBoxId,
		MessageEmpty,
		MessageNotUnique,
		TrNoItemsElementId
		) {
	QPM.Web.UI.Controls.ListEdit.initializeBase(this, [$get(ElementId)]);

	this.get_element().instance = this;
	this.mainCheckBoxId = MainCheckBoxId;
	this.messageEmpty = MessageEmpty;
	this.messageNotUnique = MessageNotUnique;
	this.trNoItemsElement = $get(TrNoItemsElementId);

	this.id = ElementId;
	this.controlId = ControlId;
	this.count = 0;
	this.index = 0;
	this.editState = false;
	this.editStateElementId = null;
	this.editTMPstring = "";
}

QPM.Web.UI.Controls.ListEdit.prototype =
{
    // Events
    //
    onChange: function() { },
    onUniqueValidate: function(input) { return true; }, // false - not unique

    // Methods
    //
    getItems: function() {
        return document.getElementsByName("item_text_" + this.id);
    },

    getItemsCheckBoxList: function() {
        return document.getElementsByName("item_checkbox_" + this.id);
    },

    getItemsSelectedCheckBoxList: function() {
        var checkboxList = this.getItemsCheckBoxList();
        var list = new Array();

        for (var i = 0; i < checkboxList.length; i++) {
            if (checkboxList[i].checked == false) continue;
            list.push(checkboxList[i]);
        }

        return list;
    },

    getItemsTrIdList: function() {
        var checkboxList = this.getItemsCheckBoxList();
        var trIdList = new Array();
        if (checkboxList.length == 0) return trIdList;

        for (var i = 0; i < checkboxList.length; i++) {
            trIdList.push('tr_' + checkboxList[i].attributes['itemId'].value);
        }

        return trIdList;
    },

    focus: function(id) {
        $get("item_text_" + id).focus();
    }
	,

    remove: function() {
        var checkboxList = this.getItemsSelectedCheckBoxList();

        if (checkboxList.length == 0) return;

        var trIdList = new Array();

        for (var i = 0; i < checkboxList.length; i++) {
            trIdList.push('tr_' + checkboxList[i].attributes['itemId'].value);
        }

        if (trIdList.length == 0) return;

        for (var i = 0; i < trIdList.length; i++) {
            var oItem = $get(trIdList[i]);
            var parent = oItem.parentNode;
            parent.removeChild(oItem);
        }

        if (this.itemsCount() == 0) {
            Sys.UI.DomElement.removeCssClass(this.trNoItemsElement, "hide");
        }

        this.onChange();
    },

    add: function(text, nofocus) {
        var instance = this;

        var currIndex = this.getItems().length;

        var currId = currIndex + "_" + this.id;

        var auto_id = this.controlId + "_" + currIndex;

        var oTBody = document.createElement("TBODY");

        var oTr = document.createElement("TR");
        oTr.id = "tr_" + currId;
        oTr.className = "teableList";

        this.get_element().appendChild(oTBody);

        oTBody.appendChild(oTr);

        var oTd = document.createElement("TD");
        oTd.className = "teableList checkboxContainer";

        oTd.innerHTML += "<input type='checkbox' id='item_checkbox_" + currId + "' item_index='" + currIndex + "' auto_id='" + currId + "' itemId = '" + currId + "' name='item_checkbox_" + this.id + "' auto_id='checkbox_" + auto_id + "'/>";

        oTr.appendChild(oTd);

        oTd = document.createElement("TD");
        oTd.className = "teableList";

        oTd.innerHTML += "<input style='width:100%' type='text' class='text' item_index='" + currIndex + "' id='item_text_" + currId + "' name='item_text_" + this.id + "' value='" + text.asAttribute() + "' auto_id='textbox_" + auto_id + "' itemId = '" + currId + "' />";

        oTd.innerHTML +=
						"<div></div><div class='validator-container1'><div class='validator-container2'>"
					+ "<span id='validatorEmpty_" + currId + "' class='validator' style='color:Red;display:none;'>" + this.messageEmpty + "</span>"
					+ "</div></div>";

        oTd.innerHTML +=
						"<div></div><div class='validator-container1'><div class='validator-container2'>"
					+ "<span id='validatorNotUnique_" + currId + "' class='validator' style='color:Red;display:none;'>" + this.messageNotUnique + "</span>"
					+ "</div></div>";

        oTr.appendChild(oTd);

        eval("RegisterChild" + this.mainCheckBoxId)("item_checkbox_" + currId);

        // Register validator
        //
        $addHandler($get("item_text_" + currId), 'keydown', function(e) {
            if (e.keyCode == 13) {
                this.blur();
                e.preventDefault();
            }
        });

        $addHandler($get("item_text_" + currId), 'keyup', function(e) {
            instance.validate();
        });

        $addHandler($get("item_text_" + currId), 'blur', function(e) {
            if ($get("item_text_" + currId).value == "") {
                instance.removeByItemTextId("item_text_" + currId);
            }
        });

        if (!nofocus) $get("item_text_" + currId).focus();

        Sys.UI.DomElement.addCssClass(this.trNoItemsElement, "hide");

        this.onChange();
        return currId;
    },
    removeByItemTextId: function(testId) {
        var textItem = $get(testId);
        if (textItem == null) return;
        var textTr = textItem.parentNode.parentNode;
        textTr.parentNode.removeChild(textTr);
    },

    validate: function() {
        var isValid = true;

        var items = this.getItems();

        for (var i = 0; i < items.length; i++) {
            // Empty
            //
            if (items[i].value.trim().length == 0) {
                $get("validatorEmpty_" + items[i].attributes["itemId"].value).style.display = "block";
                isValid = false;
            }
            else {
                $get("validatorEmpty_" + items[i].attributes["itemId"].value).style.display = "none";
            }

            if (items[i].value.trim().length > 0) {
                // Unique
                //
                if (this.onUniqueValidate(items[i]) == false) {
                    $get("validatorNotUnique_" + items[i].attributes["itemId"].value).style.display = "block";
                    isValid = false;
                }
                else {
                    $get("validatorNotUnique_" + items[i].attributes["itemId"].value).style.display = "none";
                }
            }
        }

        return isValid;
    },

    itemsCount: function() {
        return this.getItems().length;
    },

    getElementId: function(index) {
        return index + "_" + this.id;
    },

    move: function(direction) {
        var selecetedCheckBoxes = this.getItemsSelectedCheckBoxList();

        switch (direction) {
            case 'up':
                for (var i = 0; i < selecetedCheckBoxes.length; i++) {
                    var chk = selecetedCheckBoxes[i];
                    var chkIndex = parseInt(chk.attributes['item_index'].value);

                    if (chkIndex == 0) continue;
                    if ($get('item_checkbox_' + this.getElementId(chkIndex - 1)).checked) continue;

                    var tmp = $get('item_text_' + this.getElementId(chkIndex - 1)).value;
                    $get('item_text_' + this.getElementId(chkIndex - 1)).value = $get('item_text_' + chk.attributes['itemId'].value).value;
                    $get('item_text_' + chk.attributes['itemId'].value).value = tmp;
                    chk.checked = false;
                    $get('item_checkbox_' + this.getElementId(chkIndex - 1)).checked = true;
                }
                break;

            case 'down':
                for (var i = selecetedCheckBoxes.length - 1; i >= 0; i--) {
                    var chk = selecetedCheckBoxes[i];
                    var chkIndex = parseInt(chk.attributes['item_index'].value);

                    if (chkIndex + 1 >= this.getItems().length) continue;
                    if ($get('item_checkbox_' + this.getElementId(chkIndex + 1)).checked) continue;

                    var tmp = $get('item_text_' + this.getElementId(chkIndex + 1)).value;
                    $get('item_text_' + this.getElementId(chkIndex + 1)).value = $get('item_text_' + chk.attributes['itemId'].value).value;
                    $get('item_text_' + chk.attributes['itemId'].value).value = tmp;
                    chk.checked = false;
                    $get('item_checkbox_' + this.getElementId(chkIndex + 1)).checked = true;
                }
                break;
        }
    }
}

QPM.Web.UI.Controls.ListEdit.registerClass('QPM.Web.UI.Controls.ListEdit', Sys.UI.Control);



/****************************************/
/* QPM.Web.UI.Controls.SpinBox         **/
/****************************************/
QPM.Web.UI.Controls.SpinBox = function(ElementId, ButtonIncreaseId, ButtonDecreaseId, MinValue, MaxValue)
{
	QPM.Web.UI.Controls.SpinBox.initializeBase(this, [$get(ElementId)]);	

	this.buttonIncrease = $get(ButtonIncreaseId);
	this.buttonDecrease = $get(ButtonDecreaseId);
	this.maxValue = MaxValue;
	this.minValue = MinValue;
	this.interval = 1;
	this.repeatDelay = 400;
	this.repeatInterval = 25;

	var instance = this;	

	$addHandler(this.buttonIncrease, 'mousedown', function(e) { if (instance.enabled()) instance.mouseDownIncrease(e); });
	$addHandler(this.buttonDecrease, 'mousedown', function(e) { if (instance.enabled()) instance.mouseDownDecrease(e); });
	$addHandler(document, 'mouseup', function(e) { instance.mouseUp(e); });
	$addHandler(document, 'mouseup', function(e) { instance.mouseUp(e); });
	$addHandler(this.get_element(), 'blur', function() { if (instance.enabled()) instance.blur(); });

	$addHandler(this.get_element(), 'keydown', function(e) { if (instance.enabled()) instance.keyDown(e); });
	
	$addHandler(this.get_element(), 'mousewheel', function(e) { if (instance.enabled()) instance.mouseWheel(e); })
	// For Firefox
	$addHandler(this.get_element(), "DOMMouseScroll", function(e) { if (instance.enabled()) instance.mouseWheel(e); });	
	
	this.validate();
	
	this.timeOut = null;
}

QPM.Web.UI.Controls.SpinBox.prototype = 
{
	// Events
	//
	onChange : function() { },
	onValidate : function() { },

	// Methods
	//
	keyDown : function(e)
	{
		if (!e.keyCode) return;

		if (e.keyCode == Sys.UI.Key.up)
		{
			this.increaseValue(1);
		}

		if (e.keyCode == Sys.UI.Key.down)
		{
			this.decreaseValue(1);
		}
	}
	,

	blur : function()
	{
		this.validate();
		this.onChange();
	}
	,
	
	enabled : function()
	{
		return !this.get_element().disabled;
	}
	
	,
	validate : function()
	{
		if (this.maxValue < this.minValue) this.maxValue = this.minValue;
		if (this.minValue > this.maxValue) this.minValue = this.maxValue;
	
		if (!this.isCorrectValue())
		{
			if (this.maxValue >= 0 && this.minValue <= 0)
			{
				this.setValue(0);
			}
			else
			{	
				this.setValue(this.minValue);
			}
			return;
		}
		
		var value = this.getValue();
		
		if (value > this.maxValue) this.setValue(this.maxValue);
		if (value < this.minValue) this.setValue(this.minValue);
		
		this.get_element.value = value;
		
		this.onValidate();
	}
	,
	
	mouseWheel : function(e)
	{
		var delta;
	  
		// e.rawEvent is undocumented property of Sys.UI.DomEvent.
		var rawEvent = e.rawEvent;
	  
		// If AJAX APIs exposed a Sys.UI.DomEvent.delta property, I wouldn't have to write this code.
	  
		if (rawEvent.wheelDelta) 
		{ 
			// IE and Opera use wheelDelta, which is a multiple of 120 (possible values -120, 0, 120).
			delta = rawEvent.wheelDelta / 120;
		}
		else if (rawEvent.detail) 
		{ 
			// Firefox uses detail property, which is a multiple of 3.
			delta = -rawEvent.detail / 3;
		}
	  
		// Now create delta property on the fly and assign its value.
		// When MS gets around to supporting it we can just remove the code above.
		e.delta = delta;	
		
		if (delta > 0)
			this.increaseValue(1);
		else
			this.decreaseValue(1);		
	}
	,
	
	mouseDownIncrease : function(e)
	{
		this.increaseValue(this.interval);
		var instance = this;
		this.timeOut = setTimeout(function() { instance.timeOutIncreaseValue() }, this.repeatDelay);
	}
	,
	
	mouseDownDecrease : function(e)
	{
		this.decreaseValue(this.interval);
		var instance = this;
		this.timeOut = setTimeout(function() { instance.timeOutDecreaseValue() }, this.repeatDelay);
	}
	,

	mouseUp : function(e)
	{
		clearTimeout(this.timeOut);
	}
	,
	
	timeOutIncreaseValue : function()
	{
		this.increaseValue(this.repeatInterval);
		var instance = this;
		this.timeOut = setTimeout(function() { instance.timeOutIncreaseValue() }, this.repeatDelay);
	}
	,
	
	timeOutDecreaseValue : function()
	{
		this.decreaseValue(this.repeatInterval);
		var instance = this;
		this.timeOut = setTimeout(function() { instance.timeOutDecreaseValue() }, this.repeatDelay);
	}
	,

	increaseValue : function(interval)
	{
		var value = this.getValue();
		if ((value + interval) <= this.maxValue) 
		{
			this.setValue(value + interval);	
		}
		else
		{
			this.setValue(this.maxValue);
		}
	}
	,

	decreaseValue : function(interval)
	{
		var value = this.getValue();
		if (value - interval >= this.minValue) 
			this.setValue(value - interval);	
		else
			this.setValue(this.minValue);
	}
	,
	
	setValue : function(value)
	{
		if (this.get_element().value == value) return;
		
		this.get_element().value = value;		
		this.validate();
		this.onChange();
	}
	,
	
	getValue : function()
	{
		return !isNaN(parseInt(this.get_element().value)) ? parseInt(this.get_element().value) : 0;
	}
	,

	setMaxValue : function(value)
	{
		if (this.maxValue == value) return;
		
		this.maxValue = value;
		this.validate();
		this.onChange();
	}
	,

	setMinValue : function(value)
	{
		if (this.minValue == value) return;

		this.minValue = value;
		this.validate();
		this.onChange();
	}
	,

	getMaxValue : function()
	{
		return this.maxValue;
	}
	,
	
	getMinValue : function()
	{
		return this.minValue;
	}
	,

	isCorrectValue : function()
	{
		var value = this.get_element().value;
	
		//Console.write(1);

		if (value != parseInt(value)) 
		{
			return false;
		}

		value = parseInt(value);

		if (isNaN(value)) 
		{
			return false;
		}

		return true
	}
}

QPM.Web.UI.Controls.SpinBox.registerClass("QPM.Web.UI.Controls.SpinBox", Sys.UI.Control);



/****************************************/
/* QPM.Web.UI.Controls.Panel           **/
/****************************************/

QPM.Web.UI.Controls.Panel = function(ElementId, StateControlElementId, Enabled, FocusControlElementId)
{
	QPM.Web.UI.Controls.Panel.initializeBase(this, [ElementId]);

	this.stateControlElement = $get(StateControlElementId);
	this.enabled = Enabled;
	this.focusControlElement = $get(FocusControlElementId);

	var instance = this;

	if (instance.stateControlElement != null)
	{
		Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function()
		{
			switch (instance.stateControlElement.type)
			{
				case "radio":
					var inputArray = document.getElementsByName(instance.stateControlElement.name);

					for (var i = 0; i < inputArray.length; i++)
					{
						if (inputArray[i] != instance.stateControlElement)
						{
							$addHandler(inputArray[i], "click", function(e)
							{
								instance.disable();
							});
						}
					}

					$addHandler(instance.stateControlElement, "click", function(e)
					{
						instance.enable(true);
					});

					break;
				default:
					$addHandler(instance.stateControlElement, "click", function(e)
					{
						if (instance.stateControlElement.checked)
						{
							instance.enable(true);
							if (instance.focusControlElement != null)
							{
								instance.focusControlElement.focus();
							}
						}
						else
						{
							instance.disable();
						}
					});
			}

			instance.enabled = instance.stateControlElement.checked;

			if (instance.enabled)
			{
				Sys.UI.DomElement.removeCssClass(instance.get_element(), "disabled");
			}
			else
			{
				Sys.UI.DomElement.addCssClass(instance.get_element(), "disabled");
				instance.disableChildControls();
			}
		});
	}

	this.childElements = null;
	this.childElementsDisabledState = null;
}

QPM.Web.UI.Controls.Panel.prototype =
{
	enable: function(enableValidators)
	{
		if (this.enabled) return;
		this.enabled = true;
		Sys.UI.DomElement.removeCssClass(this.get_element(), "disabled");
		this.enableChildControls(enableValidators);
	}
	,

	disable: function()
	{
		if (!this.enabled) return;
		this.enabled = false;
		Sys.UI.DomElement.addCssClass(this.get_element(), "disabled");
		this.disableChildControls();
	}
	,

	enableChildControls: function(enableValidators)
	{
		if (this.childElements == null) return;

		for (var i = 0; i < this.childElements.length; i++)
		{
			if (this.childElements[i].disabled && this.childElementsDisabledState[i] == false)
			{
				this.childElements[i].disabled = false;
				Sys.UI.DomElement.removeCssClass(this.childElements[i], "disabled");
				if (enableValidators) this.enableControlValidators(this.childElements[i], true);
			}
		}

		this.childElements = null;
		this.childElementsDisabledState = null;
	}
	,

	disableChildControls: function()
	{
		this.rememberElements();

		for (var i = 0; i < this.childElements.length; i++)
		{
			this.childElementsDisabledState[i] = this.childElements[i].disabled;
			this.childElements[i].disabled = true;
			Sys.UI.DomElement.addCssClass(this.childElements[i], "disabled");
			this.enableControlValidators(this.childElements[i], false);
		}

	}
	,

	enableControlValidators: function(controlObj, state)
	{
		if (typeof (Page_Validators) != 'undefined')
		{
			for (var i = 0; i < Page_Validators.length; i++)
			{
				if (controlObj.id == Page_Validators[i].controltovalidate)
				{
					ValidatorEnable(Page_Validators[i], state);
					//alert("Validator: " + Page_Validators[i].id + " state: " + state);
				}
			}
		}
	}
	,

	rememberElements: function()
	{
		this.childElements = new Array();
		this.childElementsDisabledState = new Array();

		// collect elements and disabled states
		//
		var elementTypes = new Array();

		elementTypes.push("input");
		elementTypes.push("textarea");
		elementTypes.push("button");
		elementTypes.push("select");

		for (var j = 0; j < elementTypes.length; j++)
		{
			var oNodeList = this.get_element().getElementsByTagName(elementTypes[j]);
			for (var i = 0; i < oNodeList.length; i++)
			{
				this.childElements.push(oNodeList[i]);
				this.childElementsDisabledState.push(oNodeList[i].disabled);
			}
		}
	}
}

QPM.Web.UI.Controls.Panel.registerClass("QPM.Web.UI.Controls.Panel", QPM.Web.UI.Controls.Control);




/*******************************************/
/* QPM.Web.UI.Controls.ListBoxController  **/
/*******************************************/

QPM.Web.UI.Controls.ListBoxController = function( listBoxArray, questionsArray)
{
	QPM.Web.UI.Controls.ListBoxController.initializeBase(this);	

	this._listBoxArray = listBoxArray;
	
	this._questionsArray = questionsArray;
	
	this.run();
	
	var instance = this;
	
	for(var i = 0; i < this._listBoxArray.length; i++) 
	{
		$addHandler(this._listBoxArray[i], "change", function() { instance.updateListItems(); } );
	}
}


QPM.Web.UI.Controls.ListBoxController.prototype = 
{
	run : function()
	{
		// check for unique selects
		// 		
		var selectedIndexes = new Array();

		for(var i = 0; i < this._listBoxArray.length; i++)
		{
			var listBox = this._listBoxArray[i];

			if (listBox.selectedIndex == 0) continue;
			
			if (this.arrayContains( selectedIndexes, listBox.selectedIndex) >= 0)
			{
				listBox.selectedIndex = 0;
			} 
			else 
			{
				selectedIndexes.push(listBox.selectedIndex);
			}
		}		
		
		this.updateListItems();
	}
	,
	
	arrayContains : function( arrayObj, item)
	{
		for(var i = 0; i < arrayObj.length; i++) 
			if (arrayObj[i] == item) return i;
		return -1;
	}
	,
	
	updateListItems : function(e)
	{
		// collect selected values
		//
		var selectedValues = new Array();
		var selectedControls = new Array();

		for(var i = 0; i < this._listBoxArray.length; i++)
		{
			var listBox = this._listBoxArray[i];
			
			if (listBox.selectedIndex == 0) continue;
			
			selectedValues.push( listBox.options[listBox.selectedIndex].value );
			selectedControls.push( listBox );
		}		
		
		// remove all options from control
		//
		for(var i = 0; i < this._listBoxArray.length; i++)
		{
			var listBox = this._listBoxArray[i];
			
			while(listBox.options.length > 1)
			{
				listBox.options[listBox.options.length - 1] = null;
			}
		}
				
		// Update controls 		
		//
		for(var i = 0; i < this._listBoxArray.length; i++)
		{
			var listBox = this._listBoxArray[i];
			var selectedValue = listBox.options[listBox.selectedIndex];

			for(var j = 0; j < this._questionsArray.length; j++)
			{
				var question = this._questionsArray[j];
				
				if (j == 0) continue;
				
				// if this question was selected in another control
				//
				var index = this.arrayContains(selectedValues, question);
				
				if ( index >= 0 && selectedControls[index] != listBox ) continue;
								
				// Add options
				//
				listBox.options[listBox.options.length] = new Option(question, question);
			}
			
			// restore selected index
			//
			for(var j = 0; j < selectedControls.length; j++)
			{
				if (selectedControls[j] == listBox)
				{
					for(var m = 0; m < listBox.options.length; m++)
					{
						if (selectedValues[j] == listBox.options[m].value) listBox.selectedIndex = m;
					}	
				}
			}
		}
		
	}

}

QPM.Web.UI.Controls.ListBoxController.registerClass("QPM.Web.UI.Controls.ListBoxController");


/****************************************/
/* QPM.Web.UI.Controls.FieldSet        **/
/****************************************/

QPM.Web.UI.Controls.FieldSet = function(ElementId, 
																				contentElementId,
																				stateButtonElementId, 
																				captionLinkElementId, 
																				stateFieldElementId, 
																				configuredControlStateElementId,
																				configuredCheckBoxId)
{
	QPM.Web.UI.Controls.FieldSet.initializeBase(this, [$get(ElementId)]);	

	this._contentElementId = $get(contentElementId);	
	this._stateButton = $get(stateButtonElementId);	
	this._captionLink = $get(captionLinkElementId);	
	this._stateField = $get(stateFieldElementId);
	this._configuredControlState = $get(configuredControlStateElementId);

	this._configuredCheckBox = (configuredCheckBoxId != "") ? $get(configuredCheckBoxId) : "";

	var instance = this;
	$addHandler(this._stateButton, "mouseover", function() { instance.active(); }); 
	$addHandler(this._captionLink, "mouseover", function() { instance.active(); }); 

	$addHandler(this._stateButton, "mouseout", function() { instance.deactive(); }); 
	$addHandler(this._captionLink, "mouseout", function() { instance.deactive(); }); 
	
	$addHandler(this._stateButton, "click", function(e) { e.preventDefault(); instance.changeState(); }); 
	$addHandler(this._captionLink, "click", function(e) { e.preventDefault(); instance.changeState(); }); 
	
	if (configuredCheckBoxId != "")
	{
		$addHandler(this._configuredCheckBox, "click", function(e) { instance.changeConfiguredState(); }); 
	}
}

QPM.Web.UI.Controls.FieldSet.prototype = 
{
	active : function()
	{
		Sys.UI.DomElement.addCssClass(this._stateButton, "hover");							
		Sys.UI.DomElement.addCssClass(this._captionLink, "hover");							
	}
	,
	deactive : function()
	{
		Sys.UI.DomElement.removeCssClass(this._stateButton, "hover");							
		Sys.UI.DomElement.removeCssClass(this._captionLink, "hover");							
	}
	,
	changeState : function()
	{
		if (this.isOpened())
			this.close();
		else
			this.open();
	}
	,
	open : function()
	{
		Sys.UI.DomElement.removeCssClass(this._contentElementId, "hide");									
		Sys.UI.DomElement.removeCssClass(this.get_element(), "fieldset-closed");									
		Sys.UI.DomElement.addCssClass(this.get_element(), "fieldset-opened");									
		this._stateField.value = "opened";	
	}
	,
	close : function()
	{
		Sys.UI.DomElement.addCssClass(this._contentElementId, "hide");									
		Sys.UI.DomElement.removeCssClass(this.get_element(), "fieldset-opened");									
		Sys.UI.DomElement.addCssClass(this.get_element(), "fieldset-closed");									
		this._stateField.value = "closed";	
	}
	,
	isOpened : function()
	{
		return Sys.UI.DomElement.containsCssClass(this.get_element(), "fieldset-opened");
	}
	,
	changeConfiguredState : function()
	{
		if (this._configuredCheckBox.checked)
		{
			Sys.UI.DomElement.addCssClass(this._configuredControlState, "hide");									
		}
		else
		{
			Sys.UI.DomElement.removeCssClass(this._configuredControlState, "hide");									
		}
	}
}

QPM.Web.UI.Controls.FieldSet.registerClass("QPM.Web.UI.Controls.FieldSet", Sys.UI.Control);





/********************************/
/* QPM.Web.UI.Controls.InfoBox **/
/********************************/

QPM.Web.UI.Controls.InfoBox = function(containerElementId, captionLinkId, imageLinkId, textLinkId)
{
	QPM.Web.UI.Controls.InfoBox.initializeBase(this, [$get(containerElementId)]);	
	
	var instance = this;
	
	this._captionLink = $get(captionLinkId);
	this._imageLink = $get(imageLinkId);
	this._textLink = $get(textLinkId);

	$addHandler(this._captionLink, "mouseover", function() { instance.active(); }); 
	$addHandler(this._imageLink, "mouseover", function() { instance.active(); }); 
	$addHandler(this._textLink, "mouseover", function() { instance.active(); }); 
	$addHandler(this._captionLink, "mouseout", function() { instance.deactive(); }); 
	$addHandler(this._imageLink, "mouseout", function() { instance.deactive(); }); 
	$addHandler(this._textLink, "mouseout", function() { instance.deactive(); }); 
}

QPM.Web.UI.Controls.InfoBox.prototype = 
{
	active : function()
	{
		Sys.UI.DomElement.addCssClass(this._captionLink, "hover");		
		Sys.UI.DomElement.addCssClass(this._imageLink, "hover");		
		Sys.UI.DomElement.addCssClass(this._textLink, "hover");		
	},
	
	deactive : function()
	{
		Sys.UI.DomElement.removeCssClass(this._captionLink, "hover");		
		Sys.UI.DomElement.removeCssClass(this._imageLink, "hover");		
		Sys.UI.DomElement.removeCssClass(this._textLink, "hover");		
	}
}

QPM.Web.UI.Controls.InfoBox.registerClass("QPM.Web.UI.Controls.InfoBox", Sys.UI.Control);


/*******************************/
/* QPM.Web.UI.Controls.Button **/
/*******************************/

var button_elementsToWatch = new Array();
var button_elementsToWatchValues = new Array();
var button_elementsToWatchChecked = new Array();
var button_element = null;
var button_enableChangeContent = null;

function button_testForElementValueChanges() 
{
	var changed = false;
	
	// collect count of elements
	//
	var count = 0;
	
	// input
	//
	var oNodeList = button_enableChangeContent.getElementsByTagName("input");
	for(var i = 0; i < oNodeList.length; i++) 
	{
		//if (oNodeList[i].type == "hidden") continue;
		count ++;
	}

	// select
	//
	var oNodeList = button_enableChangeContent.getElementsByTagName("select");
	for(var i = 0; i < oNodeList.length; i++) 
	{
		count ++;
	}	
	
	if (count != button_elementsToWatch.length) 
	{
		changed = true;
	}
	
	for(var i = 0; i < button_elementsToWatch.length && (changed == false); i++)
	{
		switch(button_elementsToWatch[i].nodeName)
		{	
			case "INPUT":
				switch(button_elementsToWatch[i].type)
				{
					case "checkbox":
					case "radio":
						changed = (button_elementsToWatch[i].checked != button_elementsToWatchValues[i]);
						break;
					default:
						changed = (button_elementsToWatch[i].value != button_elementsToWatchValues[i]);
						break;
				}
				break;
			case "SELECT":
				changed = (button_elementsToWatch[i].selectedIndex != button_elementsToWatchValues[i]);
				break;
		}
	}
	
	if (changed && button_element != null) 
	{
		button_element.enable();
		return;
	}

	//if (!changed && button_element != null) button_element.disable();
	
	setTimeout(button_testForElementValueChanges, enableButtonTimeout);		
}

QPM.Web.UI.Controls.Button = function(containerElementId, buttonElementId, enableChangeContentId)
{
	QPM.Web.UI.Controls.Button.initializeBase(this, [$get(buttonElementId)]);	
	
	this._containerElement = $get(containerElementId);	
	this._enableChangeContent = (enableChangeContentId != "") ? $get(enableChangeContentId) : null;
	
	this._eventOnClickHandler = null;
	this._eventOnClickAttached = false;
		
	if (this._enableChangeContent != null)
	{
		this.disable();
		this.setChangeWatcher();
	}
}

QPM.Web.UI.Controls.Button.prototype = 
{
	setChangeWatcher : function()
	{
			button_element = this;
			button_enableChangeContent = this._enableChangeContent;

			// collect elemnents
			//
			
			// input
			//
			var oNodeList = this._enableChangeContent.getElementsByTagName("input");
			for(var i = 0; i < oNodeList.length; i++) 
			{
				//if (oNodeList[i].type == "hidden") continue;
				
				button_elementsToWatch.push(oNodeList[i]);				

				switch(oNodeList[i].type)
				{
					case "checkbox":
					case "radio":
						button_elementsToWatchValues.push(oNodeList[i].checked);
						break;
					default:
						button_elementsToWatchValues.push(oNodeList[i].value);
						break;
				}
				
			}

			// select
			//
			var oNodeList = this._enableChangeContent.getElementsByTagName("select");
			for(var i = 0; i < oNodeList.length; i++) 
			{
				button_elementsToWatch.push(oNodeList[i]);				
				button_elementsToWatchValues.push(oNodeList[i].selectedIndex);
			}
			
			setTimeout(button_testForElementValueChanges, enableButtonTimeout);
	}
	
	,
	
	isEnable : function()
	{
		return !Sys.UI.DomElement.containsCssClass(this._containerElement, "disabled");
	},

	enable: function() 
	{
		Sys.UI.DomElement.removeCssClass(this._containerElement, "disabled");
		Sys.UI.DomElement.addCssClass(this._containerElement, "enabled");
		this.get_element().disabled = "";
	},

	disable: function() 
	{
		Sys.UI.DomElement.removeCssClass(this._containerElement, "enabled");
		Sys.UI.DomElement.addCssClass(this._containerElement, "disabled");
		this.get_element().disabled = "disabled";
	},	

	show: function() 
	{
		if (this.isShow()) return;
		this.changeShowState();			
	},

	hide: function() 
	{
		if (!this.isShow()) return;
		this.changeShowState();			
	},
	
	changeShowState: function() 
	{
		Sys.UI.DomElement.toggleCssClass(this._containerElement, "hide");
	},	
	
	isShow: function() 
	{
		return (!Sys.UI.DomElement.containsCssClass(this._containerElement, "hide"));
	},
	
	attachOnClickEvent: function(eventHandler) 
	{
		this._eventOnClickAttached = true;
		this._eventOnClickHandler = eventHandler;
		$addHandler(this.get_element(), "click", this._eventOnClickHandler); 
	},

	detachOnClickEvent: function() 
	{
		this._eventOnClickAttached = false;
		$removeHandler(this.get_element(), "click", this._eventOnClickHandler);	
	}
}

QPM.Web.UI.Controls.Button.registerClass('QPM.Web.UI.Controls.Button', Sys.UI.Control);


/****************************************/
/* QPM.Web.UI.Controls.RadioButtonList **/
/****************************************/

QPM.Web.UI.Controls.RadioButtonList = function(ElementId)
{
	var radioName = new String(ElementId);
	
	while(radioName.indexOf('_') > 0) { radioName = radioName.replace('_', '$'); }
	
	this._radioList = document.getElementsByName(radioName);
	
	QPM.Web.UI.Controls.RadioButtonList.initializeBase(this, [$get(ElementId)]);	
}

QPM.Web.UI.Controls.RadioButtonList.prototype = 
{
	selectedValue : function()
	{
		for(var i = 0; i < this._radioList.length; i++)
		{
			var radio = this._radioList[i];
			if (radio.checked) 
			{
				return radio.value;
			}
		}
		return null;		
	}
}

QPM.Web.UI.Controls.RadioButtonList.registerClass('QPM.Web.UI.Controls.RadioButtonList', Sys.UI.Control);

/****************************************/
/* QPM.Web.UI.Controls.ErrorManager    **/
/****************************************/

QPM.Web.UI.Controls.ErrorManager = function(ElementId, PageValidationElementID, defaultState)
{
	QPM.Web.UI.Controls.ErrorManager.initializeBase(this, [$get(ElementId)]);	
	this._defaultState = (defaultState == "True");
	this._pageValidationElement = $get(PageValidationElementID);
}

QPM.Web.UI.Controls.ErrorManager.prototype = 
{
	show : function()
	{
		if (this.isShow()) return;		
		this.changeState();
	},
	
	hide : function(dontEnableValidation)
	{
		if (!this.isShow()) return;		
		this.changeState();
	},

	showPageValidation : function()
	{
		if (this.isShow() && this.isShowPageValidation()) return;		
		if (!this.isShow()) this.changeState();
		if (!this.isShowPageValidation()) this.changeStatePageValidation();
	},
	
	hidePageValidation : function(pageValidationFailedOnly)
	{
		if (pageValidationFailedOnly)  // only page validation failed
		{
			if (this.isShow()) this.changeState();
		}
		if (this.isShowPageValidation()) this.changeStatePageValidation();
	},

	changeState : function(e)
	{
		Sys.UI.DomElement.toggleCssClass(this.get_element(), "hide");
	},

	changeStatePageValidation : function(e)
	{
		Sys.UI.DomElement.toggleCssClass(this._pageValidationElement, "hide");
	},

	isShow : function(e)
	{
		return !Sys.UI.DomElement.containsCssClass(this.get_element(), "hide");	
	},

	isShowPageValidation : function(e)
	{
		return !Sys.UI.DomElement.containsCssClass(this._pageValidationElement, "hide");	
	}
}

QPM.Web.UI.Controls.ErrorManager.registerClass('QPM.Web.UI.Controls.ErrorManager', Sys.UI.Control);


/****************************************/
/* QPM.Web.UI.Controls.WarningPanel    **/
/****************************************/

QPM.Web.UI.Controls.WarningPanel = function(ElementId, panelElementId, containerElementId)
{
	var instance = this;
	QPM.Web.UI.Controls.WarningPanel.initializeBase(this, [$get(ElementId)]);
	this._elementId = ElementId;
	this.panelElement = (panelElementId != null) ? $get(panelElementId) : null;
	this.containerElement = (containerElementId != null) ? $get(containerElementId) : null;
	this.centerContent();
	$addHandler(window, "resize", function() { instance.centerContent(); });
	$addHandler(window, "scroll", function() { instance.centerContent(); });
}

QPM.Web.UI.Controls.WarningPanel.prototype =
{
	centerContent: function()
	{
		if (document.body)
		{
			if (this.panelElement)
			{
				this.panelElement.style.height = document.body.scrollHeight + "px";
			}
			if (this.containerElement)
			{
				var bounds = Sys.UI.DomElement.getBounds(this.containerElement);
				this.containerElement.style.top = QPM.Web.windowCenter().y - bounds.height / 2;
				this.containerElement.style.left = QPM.Web.windowCenter().x - bounds.width / 2;
			}
		}
	},

	show: function()
	{
		if (this.isShow()) return;
		this.changeState();
		this.centerContent();
		Page_ValidationActive = false; // disable page validation as default
	},

	hide: function(dontEnableValidation)
	{
		if (!this.isShow()) return;
		this.changeState();

		if (dontEnableValidation != true)
		{
			Page_ValidationActive = true; // enable page validation as default
		}
	},

	changeState: function(e)
	{
		Sys.UI.DomElement.toggleCssClass(this.get_element(), "hide");
	},

	isShow: function(e)
	{
		return !Sys.UI.DomElement.containsCssClass(this.get_element(), "hide");
	}
}

QPM.Web.UI.Controls.WarningPanel.registerClass('QPM.Web.UI.Controls.WarningPanel', Sys.UI.Control);


/****************************************/
/* QPM.Web.UI.Controls.ToolBarWarning  **/
/****************************************/

QPM.Web.UI.Controls.ToolBarWarning = function(ElementId)
{
	QPM.Web.UI.Controls.ToolBarWarning.initializeBase(this, [$get(ElementId)]);	
}

QPM.Web.UI.Controls.ToolBarWarning.prototype = 
{
	show : function(e)
	{
		if (this.isShow()) return;		
		this.changeState();
	},
	
	hide : function(e)
	{
		if (!this.isShow()) return;		
		this.changeState();
	},

	changeState : function(e)
	{
		Sys.UI.DomElement.toggleCssClass(this.get_element(), "hide");
	},

	isShow : function(e)
	{
		return !Sys.UI.DomElement.containsCssClass(this.get_element(), "hide");	
	}
}

QPM.Web.UI.Controls.ToolBarWarning.registerClass('QPM.Web.UI.Controls.ToolBarWarning', Sys.UI.Control);



/*******************************************/
/* QPM.Web.UI.Controls.HyperLinkExpander  **/
/*******************************************/

QPM.Web.UI.Controls.HyperLinkExpander = function(ElementId, CheckBoxControllerId, ControledPanelElement)
{
	QPM.Web.UI.Controls.HyperLinkExpander.initializeBase(this, [$get(ElementId)]);

	var instance = this;

	this.controledPanel = ControledPanelElement;

	this.hyperLinkText = this.get_element().innerHTML;

	this.checkBoxController = $get(CheckBoxControllerId);

	this.openState = (this.checkBoxController) ? this.checkBoxController.checked : false;

	$addHandler(this.get_element(), 'click', function(e)
	{
		e.preventDefault();
		
		if (instance.get_element().disabled == true) return;
		
		if (instance.get_element().attributes["disabled"])
			if (instance.get_element().attributes["disabled"].value == "disabled") return;
			
		instance.changeState();
	});

	this.updateHyperLinkText();

	if (this.openState == true) this.open(); else this.close();

}

QPM.Web.UI.Controls.HyperLinkExpander.prototype =
{
	updateHyperLinkText: function()
	{
		this.get_element().innerHTML = this.hyperLinkText + "&nbsp;" + ((this.openState == true) ? '&lt;&lt;&lt;' : '&gt;&gt;&gt;');
	}
	,
	changeState: function()
	{
		if (this.openState == true)
		{
			this.close();
		}
		else
		{
			this.open();
		}

		this.updateHyperLinkText();
	}
	,
	open: function(e)
	{
		this.openState = true;
		if (!this.controledPanel.isShow()) this.controledPanel.show();
		if (this.checkBoxController)
			if (this.checkBoxController.checked == false) this.checkBoxController.click();
	}
	,
	close: function(e)
	{
		this.openState = false;
		if (this.controledPanel.isShow()) this.controledPanel.hide();
		if (this.checkBoxController)
			if (this.checkBoxController.checked == true) this.checkBoxController.click();
	}
}

QPM.Web.UI.Controls.HyperLinkExpander.registerClass('QPM.Web.UI.Controls.HyperLinkExpander', Sys.UI.Control);




// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
