﻿	function checkOnchange(checkBok)
	{
		var selectedItem = document.getElementById(this._selectItemIdBase+checkBok.attributes["index"].value);

		if (checkBok.checked) {	
			if (this._toolTip && !(this._toolTipShown)) {
				//this._toolTipShown = true;
				//$('#' + this._formID).qtip("show");
				//$("[name='"+this._scanButtonID+"']").qtip("show");
				//$("[name='"+this._scanButtonID+"']").qtip("destroy");
				
				//$(checkBok).qtip("show");
			}
			this._totalSelected++;
			if (selectedItem) {
				selectedItem.bgColor = this._selectedBgColor;
				selectedItem.style.backgroundColor = this._selectedBgColor;
			}
		}
		else {
			if (!this._initSelected && this._totalSelected > 0) this._totalSelected--;
			if (selectedItem) {
				selectedItem.bgColor = this._UnselectedBgColor;
				selectedItem.style.backgroundColor = this._UnselectedBgColor;
			}
		}
		var scanButtons = document.getElementsByName(this._scanButtonID);
		for(var i=0;i<scanButtons.length;i++)
		{
			var scanButton = scanButtons[i];
			if (this._totalSelected <= 0) {
				scanButton.disabled = true;
				scanButton.className	= this._scanButtonDisClass;
			}
			else {
				scanButton.disabled = false;					
				scanButton.className= this._scanButtonClass;
			}
		}
	}
	
	function scanSelected()
	{
		var displayCode = 0;
		var scanValues  = '';
		var scanForm = document.getElementById(this._formID)
		if (scanForm) 
		{
			var selectedCodes = document.getElementsByName(this._checkBoxesName);
			for(var i=0;i<selectedCodes.length;i++)
			{ 
				if (selectedCodes[i].checked) {					
					if (!displayCode) displayCode = selectedCodes[i].value;
					scanValues += ',' + selectedCodes[i].value;
				}
			}
			scanValues = scanValues.substr(1);
			scanForm.DisplayCodeArray.value = scanValues;
			scanForm.displayCode.value 		= displayCode;
			scanForm.displayIndex.value 	= 0;
			scanForm.action 				= scanForm.action+'?'+ scanForm.CodeField.value + '=' + displayCode
			scanForm.submit()	
		
		}	
		return 0	
	}
	
	function removeAllSelected()
	{
		totalSelected = 0;
		var selectedCodes = document.getElementsByName(this._checkBoxesName);
		for(var i=0;i<selectedCodes.length;i++)
		{
			selectedCodes[i].checked = false;
			this.checkOnchange(selectedCodes[i]);						
		}	
	
	}
	function selectAll()
	{
		var selectedCodes = document.getElementsByName(this._checkBoxesName);
		for(var i=0;i<selectedCodes.length;i++)
		{
			selectedCodes[i].checked = true;					
			this.checkOnchange(selectedCodes[i]);						
		}	
	
	}

	function initSelected() 
	{
		//alert('Setting Selected');
		if (!this._doneWait && thisBrowser.msie) {
			this._doneWait		= true;
			window.setTimeout(this._objectName+".initSelected()", 100);
		}
		else {

			var scanButtons = document.getElementsByName(this._scanButtonID);
			for(var i=0;i<scanButtons.length;i++)
			{
				var scanButton = scanButtons[i];
				scanButton.onclick = Function(this._objectName + '.scanSelected()');
				scanButton.disabled = true;
				scanButton.className= this._scanButtonDisClass;
			}
			
			var selectedCodes = document.getElementsByName(this._checkBoxesName);
			for(var i=0;i<selectedCodes.length;i++)
			{
				var curBox = selectedCodes[i]
				
				// setting the button & checkBox values
				curBox.onclick = Function(this._objectName + '.checkOnchange(this)');
				this.checkOnchange(selectedCodes[i]);
			}
			// creating the toolTip - to apear only on 1st click
			if (this._toolTip) {
				this._toolTipShown = false;
				//$("[name='"+this._scanButtonID+"']").qtip(
				$("[name='"+this._checkBoxesName+"']").qtip(
				//$("#"+this._formID).qtip(
				{
					  content: 'לאחר סימון הפריטים הנבחרים, לחץ על כפתור ה"צפייה בנבחרים"', // Give it a loading message while request is being sent
					  position: {
						//target: 'mouse' ,
						corner: {
							target: 'topMiddle',
							tooltip: 'bottomMiddle'
						 }
					  },
					  show: {
						 //when: '', // Show never - only on our call
						 solo: true // ...but hide all others when its shown
					  },
						//hide: { when: 'inactive', delay: 1000 }, // Hide when it loses focus...
					  style: {
						 tip: true, // Create speech bubble tip at the set tooltip corner above
						 textAlign: 'center',
						 name: 'cream'
					  }

				});
			}
			this._initSelected	= false;
		}
	}

	function ScanForm(formID,checkBoxesName,objectName,params)
	{
		this._initSelected 		= true;	
		this._doneWait 			= false;
		this._totalSelected		= 0;
		
		this._checkBoxesName	= checkBoxesName;
		this._formID			= formID;
		this._objectName		= objectName;
		
		this._selectedBgColor	= setDefault(params["selectedBgColor"],"#c0c0c0");
		this._UnselectedBgColor	= setDefault(params["UnselectedBgColor"],"");
		this._selectItemIdBase	= setDefault(params["selectItemIdBase"],"scanRow");
		
		this._scanButtonID		= setDefault(params["scanButtonID"],formID+"scanButton");
		this._scanButtonDisClass	= setDefault(params["scanButtonDisClass"],"scanButtonDis");
		this._scanButtonClass		= setDefault(params["scanButtonClass"],"scanButton");
		this._toolTip				= setDefault(params["toolTip"],false);
		this._toolTipContent		= setDefault(params["toolTipContent"],"");

	}				
	ScanForm.prototype.checkOnchange	= checkOnchange;
	ScanForm.prototype.initSelected		= initSelected;
	ScanForm.prototype.scanSelected		= scanSelected;
	ScanForm.prototype.removeAllSelected	= removeAllSelected;
	ScanForm.prototype.selectAll		= selectAll;