
var OptionPickerClass = function()
{	
	// function call before the checked action
	this.selectionPreCheckFunc = null					// String for eval func purpose	
	this.selectionPostCheckFunc = null					// String for string eval purpose
	
	this.registerPreFunc = function( string )
	{
		this.selectionPreCheckFunc = string
	}	
	
	this.registerPostFunc = function( string )
	{
		this.selectionPostCheckFunc = string
	}	

	//check if the current document inside the OPP query iframe	
	this.isInsideOPPQueryIFrame = function()
	{
		//the marking is hardcoded, regarless of the prefix
		if( document.getElementById("opp_query_iframe_mark") )	
		{
			return true;
		}		
		return false;
	}	

	// check the hidden comma-delimited list value
	this.addOPPId = function(idList, value)
	{	
		var result
		
		if(idList.length==0){
			result=value;
		}else{
			idList=","+idList+",";
		
			if(idList.indexOf(","+value+",") >=0)	//if find the original one
			{	//do nothing	
			}else{
				idList = idList + value + ","
			}			
			result = idList.substr(1, idList.length-2);	//remove the extra commas
		}
		
		return result;
	}
	
	this.extractEntryIDFromRow = function(row)
	{
		var info = new Array();
		info = row.id.split("_");
		return info[info.length-1];	//return the last element, separated using '_'
	}

	this.removeOPPId = function(idList, value)
	{
		var result 
		
		idList=","+idList+",";
		idList = idList.replace(","+value+"," , ",");
		
		if(idList.length == 1){
			result = "";
		}else{
			result = idList.substr(1, idList.length-2);	//remove the extra commas
		}
				
		return result;
	}

	//move the selected row between table for OPP
	this.moveSelectedRowBetweenTablesForOPP = function(acpPrefix, tableId, sourceRowId, e)
	{	
		var acpTableName = acpPrefix + "_table";
		var acpFrameName = acpPrefix + "_query_iframe";
		var acpSearchFieldName = acpPrefix + "_search_field";
		
		sourceTable = document.getElementById(acpTableName);
		sourceRow = sourceTable.rows[String(sourceRowId)];
		
		if ( sourceRow ) sourceRowIndex = sourceRow.rowIndex;
		
		// sourceRow : Row object of clicked row
		// checkedID : the id of the checked checkBox
		
		// get the value from the hidden field
		var hiddenValueId = sourceRowId + '_hidden';	
		var checkedID = document.getElementById(hiddenValueId).value;		
		var outputObj, passerObj, bAddSelected, bCond
		
		bCond = true
				
		if(this.isInsideOPPQueryIFrame())	//move from query iframe to parent frame
		{	// inside iframe			
			targetTable		= window.parent.document.getElementById(acpTableName);
			outputObj		= window.parent.document.getElementById(acpPrefix);			
			bAddSelected	= true			
		}
		else	//move from the parent frame to the query iframe
		{	// Parent			
			targetTable		= document.frames[acpFrameName].document.getElementById(acpTableName);
			outputObj		= document.getElementById(acpPrefix);			
			bAddSelected	= false			
		}
		
		passerObj = new PasserClass( this, targetTable, outputObj, bAddSelected, sourceTable, sourceRow );

		// =======================================================
		// On Check PreAction
		// e.g. 'FuncName( passerObj )'	
		// bCond = FuncName( passerObj, checkedID )  
		// look at PasserClass below please
				
		if ( this.selectionPreCheckFunc ) eval( this.selectionPreCheckFunc );
		// =======================================================

		if ( !bCond ) return;

		passerObj.MoveValue( checkedID );		

		//place the focus to the next checkbox
		if ( sourceTable.rows.length > sourceRowIndex )	//the row in the middle is deleted
		{
			sourceTable.rows[sourceRowIndex].cells[0].childNodes[0].focus();
		}
		else if ( sourceTable.rows.length > 0 )	//the last row is deleted
		{
			sourceTable.rows[sourceRowIndex-1].cells[0].childNodes[0].focus();
		}
		else	//all rows are deleted
		{
			if(this.isInsideOPPQueryIFrame()){//call from iframe
				searchField = document.getElementById(acpSearchFieldName)
				searchField.focus();	//focus on the text field
				CursorUtils.positionCursorAtEnd(searchField);
			}
		}		

		// =======================================================
		// On Check Post Action
		if ( this.selectionPostCheckFunc ) eval( this.selectionPostCheckFunc );
		// =======================================================
	}	

	//this move selection may be copied as utils, however
	//it only copies a amount of information between the cells and rows
	this.moveSelectedRowBetweenTables = function(targetTableObj, sourceTableObj, sourceRowIndex )
	{	
		var targetDocument;
		var sourceRow;
		
		sourceRow = sourceTableObj.rows[sourceRowIndex];		
		
		//append the row to the targetTable
		newRow = targetTableObj.insertRow(targetTableObj.rows.length);
		
		var hiddenObj = $(sourceRow.id + '_hidden')
				
		//copy the row attributes
		newRow.onclick=sourceRow.onclick;
		newRow.bgColor=sourceRow.bgColor;
		newRow.id=sourceRow.id;
		newRow.name=sourceRow.name;
	
		var theValue = hiddenObj.value
				
		//copy the cells inside the row
		for(var i = 0; i < sourceRow.cells.length; i++)
		{
			var newCell;
			newCell = newRow.insertCell(i);
			newCell.id = sourceRow.cells[i].id;
			newCell.bgColor= sourceRow.cells[i].bgColor;
			if ( i == 0 ) {
				newCell.innerHTML = sourceRow.cells[i].innerHTML + hiddenObj.outerHTML ;
			}else if ( i == 1 ){
				newCell.innerHTML = theValue
			}else newCell.innerHTML = sourceRow.cells[i].innerHTML 
		}
		
		//remove the source row from the source table
		sourceRowIndex = sourceRow.rowIndex;
		sourceTableObj.deleteRow(sourceRowIndex);	
	}
	
	
	// inner class
	function PasserClass( OptionPickerObj, targetTableObj, outputObj, bAdd, sourceTable, sourceRow )
	{	
		this.OptionPicker	= OptionPickerObj
		this.targetTable	= targetTableObj
		this.outputObject	= outputObj		 // the hidde
		this.bAdd			= bAdd
		this.sourceRowObj	= sourceRow
		this.hiddenObj		= $( sourceRow.id + '_hidden' )

		this.MoveValue = function( checkedID )
		{
			// change the hidden return value
			if ( this.bAdd == true ){
				outputObj.value = this.OptionPicker.addOPPId( this.outputObject.value, checkedID );
				//window.status = outputObj.value
			}else{
				outputObj.value = this.OptionPicker.removeOPPId( this.outputObject.value, checkedID );
				//window.status = outputObj.value
			}

			//move the select row from one table into another			
			this.OptionPicker.moveSelectedRowBetweenTables(targetTable, sourceTable, this.sourceRowObj.rowIndex )
		}
	}
}
