﻿// 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.LisEdit				 **/
/****************************************/

QPM.Web.UI.Controls.ListEdit = function(
		ElementId, 
		ButtonAcceptTitle, 
		ButtonCancelTitle, 
		ButtonDeleteTitle,
		ConfirmDeleteText,
		AlertSaveOrCancel,
		AlertEmpty
		)
{
	QPM.Web.UI.Controls.ListEdit.initializeBase(this, [$get(ElementId)]);	

	this.get_element().instance = this;
		
	this.id = ElementId;
	this.count = 0;
	this.index = 0;
	this.editState = false;
	this.editStateElementId = null;
	this.editTMPstring = "";

	this.ButtonAcceptTitle = ButtonAcceptTitle;
	this.ButtonCancelTitle = ButtonCancelTitle;
	this.ButtonDeleteTitle = ButtonDeleteTitle;
	this.ConfirmDeleteText = ConfirmDeleteText;
	this.AlertSaveOrCancel = AlertSaveOrCancel;
	this.AlertEmpty = AlertEmpty;
}

QPM.Web.UI.Controls.ListEdit.prototype =
{
	// Events
	//
	onChange : function() {},
	onValidate : function(text) { return true },
	
	// Methods
	//
	getItems : function()
	{
		/*var elements = this.get_element().getElementsByName(this.id);
		for(var i = 0; i < elements.length; i++)
		{
			alert(elements[i].id);
		}*/
		return document.getElementsByName("item_" + this.id);
	},
	
	focus : function( id ) 
	{
		$get("input_" + id).focus();
	}
	,	
	
	checkEditState : function ()
	{
		if (this.editState)
		{
			alert(this.AlertSaveOrCancel);
			this.focus(this.editStateElementId);
			return true;
		}
		return false;
	}		
	,
	
	add : function( text )
	{
		if (this.checkEditState()) return null;

		var currId = this.index + "_" + this.id;
		var oDiv = document.createElement("DIV");
		oDiv.id = "item_" + currId;
		this.get_element().appendChild(oDiv);

		oDiv.innerHTML =  
								"<div class='listEdit--item'>"
							+ "<table class='table-item' cellpadding='0' cellspacing='0'>"
								+ "<tr>"
									+ "<td>"
										+	"<div class='listEdit--item--text'>"
										+		"<a class='text' id='hrefText_" + currId + "' href='#'>" + text + "</a>"
										+		"<input style='display:none' type='text' class='text' id='input_" + currId + "' name='item_"+ this.id +"' value='" + text.asAttribute() + "'/>"
										+	"</div>"
									+ "</td>"
									+ "<td class='td-control'>"										
										+	"<div class='listEdit--item--control'>"
											+ "<table cellpadding='0' cellspacing='0'>"
												+ "<tr>"
													+ "<td style='width:30px;'>"
													+		"<a style='display:none;' id='hrefCommit_" + currId + "' href='#' title='" + this.ButtonAcceptTitle + "'>"
													+			"<div class='icon icon-href icon-25 icon-25-2state icon-25-buttonAccept'></div>"
													+		"</a>"
													+ "</td>"
													+ "<td style='width:30px;'>"
													+		"<a style='display:none;' id='hrefCancel_" + currId + "' href='#' title='" + this.ButtonCancelTitle + "'>"
													+			"<div style='margin-left:2px;' class='icon icon-href icon-25 icon-25-2state icon-25-buttonCancel'></div>"
													+		"</a>"
													+ "</td>"
													+ "<td style='width:30px;'>"
													+		"<a style='display:block;' id='hrefDelete_" + currId + "' href='#' title='" + this.ButtonDeleteTitle + "'>"
													+			"<div class='icon icon-href icon-25 icon-25-2state icon-25-buttonDelete'></div>"
													+		"</a>"
													+ "</td>"
												+ "</tr>"
											+ "</table>"
										+	"</div>"
									+ "</td>"
								+ "</tr>"
							+ "</table>"
							+ "</div>";
		this.index ++;
		
		var instance = this;
		
		// Handlers
		//
		$addHandler($get("hrefText_" + currId), 'click', function(e)
		{
			e.preventDefault();
			instance.onEdit(currId);
		});
		
		$addHandler($get("input_" + currId), 'keypress', function(e)
		{
			instance.onKeyPress(e, currId);
		});
		
		$addHandler($get("hrefCommit_" + currId), 'click', function(e)
		{
			e.preventDefault();
			instance.onCommit(currId);
		});
				
		$addHandler($get("hrefCancel_" + currId), 'click', function(e)
		{
			e.preventDefault();
			instance.onCancel(currId);
		});

		$addHandler($get("hrefDelete_" + currId), 'click', function(e)
		{
			e.preventDefault();
			instance.onDelete(currId);
		});

		this.onChange();
		return currId;
	}
	,
	
	onEdit : function ( id )
	{	
		if (this.checkEditState()) return;

		var oHrefText = $get("hrefText_" + id);
		var oInput = $get("input_" + id);
		var oHrefDelete = $get("hrefDelete_" + id);
		var oHrefCommit = $get("hrefCommit_" + id);
		var oHrefCancel = $get("hrefCancel_" + id);
		this.editTMPstring = oInput.value;
		oHrefText.style.display = 'none';
		oHrefDelete.style.display = 'none';
		oHrefCommit.style.display = 'block';
		oHrefCancel.style.display = 'block';
		oInput.style.display = 'block';
		oInput.focus();
		this.editState = true;
		this.editStateElementId = id;
	}
	,
	
	onDelete : function( id )
	{
		var oInput = $get("input_" + id);
		
		if (this.checkEditState()) return false;

		if (oInput.value.length != 0 && !confirm( this.ConfirmDeleteText )) {
			return;
		}

		var oItem = $get("item_" + id);		
		var parent = oItem.parentNode;
		parent.removeChild(oItem);
		this.onChange();		
	}	
	,
	
	onCancel : function( id )
	{
		var oHrefText = $get("hrefText_" + id);
		var oInput = $get("input_" + id);
		var oHrefDelete = $get("hrefDelete_" + id);
		var oHrefCommit = $get("hrefCommit_" + id);
		var oHrefCancel = $get("hrefCancel_" + id);

		oInput.value = this.editTMPstring;
		if (oInput.value.length == 0)
		{
			this.editState = false;
			this.onDelete(id);
			return;
		}

		oHrefText.style.display = 'block';
		oHrefDelete.style.display = 'block';
		oHrefCommit.style.display = 'none';
		oHrefCancel.style.display = 'none';
		oInput.style.display = 'none';

		this.editState = false;
	}
	,
	
	onKeyPress : function ( e, id )
	{
		var keyCode = e.keyCode ? e.keyCode: e.charCode;
		switch(keyCode)
		{
			case 13:
				e.preventDefault();
				this.onCommit(id);
				break;
			case 27:
				e.preventDefault();
				this.onCancel(id);
				break;
		}
	}	
	,
	
	onCommit : function( id )
	{
		var oHrefText = $get("hrefText_" + id);
		var oInput = $get("input_" + id);
		var oHrefDelete = $get("hrefDelete_" + id);
		var oHrefCommit = $get("hrefCommit_" + id);
		var oHrefCancel = $get("hrefCancel_" + id);

		if (oInput.value.length == 0)
		{
			alert(this.AlertEmpty);
			return;
		}

		if (this.onValidate(oInput) == false) return;

		oInput.value = oInput.value.trim();

		oHrefText.innerHTML = oInput.value.asEscapedHtml();
		oHrefText.style.display = 'block';
		oHrefDelete.style.display = 'block';
		oHrefCommit.style.display = 'none';
		oHrefCancel.style.display = 'none';
		oInput.style.display = 'none';

		this.editState = false;
		this.onChange();
	}		
}

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, [$get(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();
					});
					
					break;
				default:
					$addHandler(instance.stateControlElement, "click" , function(e) 
					{
						if (instance.stateControlElement.checked) 
						{
							instance.enable();
							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()
	{
		if (this.enabled) return;
		this.enabled = true;
		Sys.UI.DomElement.removeCssClass(this.get_element(), "disabled");		
		this.enableChildControls();
	}
	,
	
	disable : function()
	{
		if (!this.enabled) return;
		this.enabled = false;
		Sys.UI.DomElement.addCssClass(this.get_element(), "disabled");		
		this.disableChildControls();
	}
	,
		
	enableChildControls : function()
	{
		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");
			}
		}		
		
		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");
		}		
	}
	,
	
	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", Sys.UI.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)
{
	QPM.Web.UI.Controls.WarningPanel.initializeBase(this, [$get(ElementId)]);	
	this._elementId = ElementId;
}

QPM.Web.UI.Controls.WarningPanel.prototype = 
{
	show : function()
	{
 		if (this.isShow()) return;		
		this.changeState();

		if (this.hasBodyContent()) 
		{
			if (!this.isBodyContentShow()) return;
			this.changeBodyContentState();
		}
		Page_ValidationActive = false; // disable page validation as default
	},
	
	hide : function(dontEnableValidation)
	{
		if (!this.isShow()) return;		
		this.changeState();
		
		if (this.hasBodyContent()) 
		{
			if (this.isBodyContentShow()) return;
			this.changeBodyContentState();
		}
				
		if (dontEnableValidation != true)
		{
			Page_ValidationActive = true; // enable page validation as default
		}
	},

	hasBodyContent : function()
	{
		return $get("bodyContent") != null;
	},

	changeState : function(e)
	{
		Sys.UI.DomElement.toggleCssClass(this.get_element(), "hide");
	},

	changeBodyContentState : function(e)
	{
		if (this.isBodyContentShow() == false && AllowBodyContentShow == false) return;
		
		Sys.UI.DomElement.toggleCssClass($get("bodyContent"), "hide");			
	},
	
	isShow : function(e)
	{
		return !Sys.UI.DomElement.containsCssClass(this.get_element(), "hide");	
	},	

	isBodyContentShow : function(e)
	{
		return !Sys.UI.DomElement.containsCssClass($get("bodyContent"), "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);


// 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();
