// ----------------------------------------------------
// ---------------- Registration Class ----------------
// ----------------------------------------------------

// ------ Main Array Object ------
registration.prototype.objArrMain;

// ------ Main Table ------
registration.prototype.mainSetup = mainSetup;
registration.prototype.mainTableSetup = mainTableSetup;
registration.prototype.addAnotherSetup = addAnotherSetup;
registration.prototype.Build_Main = buildMain;

// ------ Category Setup (Skills) ------
registration.prototype.objArrCategory;
registration.prototype.elementsSetup = elementsSetup;
registration.prototype.categorySetup = categorySetup;
registration.prototype.relationWithCategory = relationWithCategory;
registration.prototype.catOnClick = categoryOnClick;
registration.prototype.catSelSubLevel = categorySelectSubLevel;
registration.prototype.Build_Categories = buildCategories;

// ------ Sub Main Table (Choose Level) ------
registration.prototype.objArrSecondary;
registration.prototype.chooseText;
registration.prototype.secondarySetup = secondarySetup;
registration.prototype.relationWithSecondary = relationWithSecondary;
registration.prototype.BuildSubMenu = buildSubMenu;

// ------ Search Table (Job Search) ------
registration.prototype.tableSearchSetup = tableSearchSetup;
registration.prototype.tableSearchCannotFind = tableSearchCannotFind;
registration.prototype.Build_Search = buildSearch;

// ------ Lookup Table (Preferences) ------
registration.prototype.localBuildCheckbox;
registration.prototype.counterSetup = counterSetup;
registration.prototype.lookupSetup = lookupSetup;
registration.prototype.lookupOnClick = lookupOnClick;
registration.prototype.lookupOnMouseOver = lookupOnMouseOver;
registration.prototype.deleteFromMainArray = deleteFromMainArray;
registration.prototype.Build_Checkbox = buildCheckbox;

// ------ Actions ------
registration.prototype.addRow = addRow;
registration.prototype.del = deleteRow;
registration.prototype.chSec = chooseSecondary;
registration.prototype.chgSec = changeSecondary;

// ------ XML ------
registration.prototype.XMLnodeName = XMLnodeName;
registration.prototype.XMLsetAttributes = XMLsetAttributes;
registration.prototype.XMLget = XMLget;

// -------------------------------------------------------------------------
// ------------------------------ Constructor ------------------------------
// -------------------------------------------------------------------------
function registration( strObjectName, objContainer, strSubMenu, runAfterDeleteRow, runAfterchangeSecondary )
{
	this.addAnotherFuncName = null;
	this.addAnotherText = null;
	this.chooseText = null;
	this.cannotFindLiteral = null;
	this.cannotFindKey = null;
	this.cannotFindFunction = null;
	this.HTMLnewRow = null;
	
	this.XML = new Array();
	this.runAfterDeleteRow = (typeof(runAfterDeleteRow) == 'function')?runAfterDeleteRow:null;
	this.runAfterchangeSecondary = (typeof(runAfterchangeSecondary) == 'function')?runAfterchangeSecondary:null;	

	this.objArrSecondary = null;
	this.strObjectName = strObjectName;

	if( typeof(objContainer)!='undefined' && objContainer != '' )
		this.objContainer = getObj(objContainer);

	if( typeof(strSubMenu)!='undefined' && strSubMenu != '' )
		this.strSubMenu = createSubMenuDIV(strSubMenu, "SubmenuLayer");
}

// -------------------------------------------------------------------------
// -------------------------Common Functions -------------------------------
// -------------------------------------------------------------------------
function getSecondaryText(obj, value, secondaryIdxId, secondaryIdxDesc, chooseText)
{
	var str2Return = '';
	if( isFinite(value) && value >= 0 )
	{
		for(var i=0;i<obj.length;i++)
			if( obj[i][secondaryIdxId] == value)
			{
				str2Return = '[ ' + obj[i][secondaryIdxDesc] + ' ]';
				break;
			}
	}
	
	if( str2Return == '' )
	{			
		if( chooseText != null && chooseText != '' )
			str2Return = chooseText.toString();
		else
			str2Return = 'Choose';
	}

	return str2Return;	
}

// -------------------------------------------------------------------------
// ------------------------------ Main Table -------------------------------
// -------------------------------------------------------------------------
function mainSetup( idxId, idxDescription, idxSubSelection )
{
	this.mainIdxId = idxId;
	this.mainIdxDesc = idxDescription;
	this.idxSubSelection = idxSubSelection;
}

function mainTableSetup( idxMainText, HTMLnewRow )
{
	this.idxMainText = idxMainText;
	this.HTMLnewRow = HTMLnewRow;
}

function addAnotherSetup( strFuncName, strText )
{
	this.addAnotherFuncName = strFuncName;
	this.addAnotherText = strText;
}

function buildMain( )
{
	var HTMLtable = "";

	if( this.objArrMain.length > 0 )
	{
		for(var i=0;i<this.objArrMain.length;i++)
		{
			HTMLtable += '<div class="RegistrationRowItem">';
			HTMLtable += '<div class="RegistrationRowValue">' + this.objArrMain[i][this.idxMainText] + '</div>';
			HTMLtable += '<div class="RegistrationRowDelete"><a href="#" onclick="' + this.strObjectName + '.del(this,' + i + ');return false;"><img src="../Cultures/Images/Icons/DeleteRow.gif" alt="Text delete" width="15" height="16" border="0" /></a></div>';
			if(this.objArrSecondary != null) HTMLtable += '<div class="RegistrationRowParameters" onClick="' + this.strObjectName + '.chSec(this,' + i +')" id="lbl' + this.strObjectName + i.toString() + '">' + getSecondaryText(this.objArrSecondary, this.objArrMain[i][this.idxSubSelection], this.secondaryIdxId ,this.secondaryIdxDesc, this.chooseText) + '</div>';
			HTMLtable += '<div class="ClearBoth"></div>';
			HTMLtable += '</div>';
		}

		if (this.objArrMain.length < 7 && this.addAnotherText != null && this.addAnotherFuncName != null)
			HTMLtable += '<div class="RegistrationRowAdd" id="RegistrationRowAdd"><a href="javascript:' + this.addAnotherFuncName + '"><img src="../Cultures/Images/Icons/AddRow.gif" alt="Add" width="15" height="16" border="0" align="middle" /> ' + this.addAnotherText + '</a></div>'
		else
		{
			HTMLtable += '<div class="RegistrationRowTop"></div>'
		}
	}
	else if(this.HTMLnewRow != null)
		HTMLtable = this.HTMLnewRow;
	
	this.objContainer.innerHTML = HTMLtable;	
}
// -------------------------------------------------------------------------
// ---------------------------- Category Setup -----------------------------
// -------------------------------------------------------------------------
function elementsSetup( objArrElements, idxElementId, idxElementDesc, idxElementCategory )
{
	this.objArrElements = objArrElements;
	this.idxElementId = idxElementId;
	this.idxElementDesc = idxElementDesc;
	this.idxElementCategory = idxElementCategory;
}

function categorySetup( idxId, idxDescription )
{
	this.categoryIdxId = idxId;
	this.categoryIdxDesc = idxDescription;
}

function relationWithCategory( idxInMain, idxInCategory )
{
	this.catRelationId = idxInMain;
	this.catRelationMain = idxInCategory;
}

function buildCategories()
{
	var HTMLtable = '';
	var HTMLskl = new Array();

	var tmpCategories = ',';
	var tmpIdx;

	for(var i=0;i<this.objArrElements.length;i++)
	{
		if(typeof(this.objArrElements[i][this.catRelationId])!='undefined' && this.objArrElements[i][this.catRelationId]!='' && this.objArrElements[i][this.catRelationId]!=null)
		{
			tmpIdx = tmpCategories.indexOf(',' + this.objArrElements[i][this.catRelationId] + ',')
			if( tmpIdx == -1 )
			{
				tmpIdx = tmpCategories.length - 1;
				tmpCategories += this.objArrElements[i][this.catRelationId] + ','
			}
		}

		if(typeof(HTMLskl[tmpIdx]) == 'undefined')
			HTMLskl[tmpIdx] = '';

		HTMLskl[tmpIdx] += '<tr>';
		HTMLskl[tmpIdx] += '<td width="10">&nbsp;</td>';
		HTMLskl[tmpIdx] += '<td width="20"><input type="checkbox" name="chBox' + i.toString() + '" id="chBox' + i.toString() + '" value="' + this.categoryIdxId + '" onClick="' + this.strObjectName + '.catOnClick(this,' + i +')" />';
		HTMLskl[tmpIdx] += '</td>';
		HTMLskl[tmpIdx] += '<td><label for="chBox' + i.toString() + '">' + this.objArrElements[i][this.idxElementDesc] + '</label></td>';
		HTMLskl[tmpIdx] += '<td width="120"><div onClick="' + this.strObjectName + '.catSelSubLevel(this,' + i +',' + this.objArrElements[i][this.idxElementId] +')" id="lbl' + this.strObjectName + i.toString() + '" class="SkilItemBoxChoose">' + getSecondaryText(this.objArrSecondary, null, this.secondaryIdxId ,this.secondaryIdxDesc, this.chooseText) + '</div></td>';
		HTMLskl[tmpIdx] += '</tr>';
	}

	var idx;
	for(var j=0;j<this.objArrCategory.length;j++)
	{
		idx = tmpCategories.indexOf(',' + this.objArrCategory[j][this.categoryIdxId] + ',')
		if( idx > -1 )
		{
			HTMLtable += '<div name="idx_' + j + '" id="idx_' + j + '" class="SkilItemBox" class2="SkilItemBox_Opened">';
			HTMLtable += '<h3 onClick="openCloseCategory( this )"><a href="javascript:void(null)"><img src="../Cultures/Images/pixel.gif" width="11" height="11" border="0"/> ' + this.objArrCategory[j][this.categoryIdxDesc] + '</a></h3>';
			HTMLtable += '<div class="SkilItemBoxContent">';
			HTMLtable += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';

			HTMLtable += HTMLskl[idx];

			HTMLtable += '</table>';
			HTMLtable += '</div>';
			HTMLtable += '</div>';
		}
	}
	
	this.objContainer.innerHTML = HTMLtable;
}

function openCloseCategory( obj )
{
	var tmp = obj.parentNode;

	tmp.setAttribute("tmpClass",tmp.className);
	tmp.className = tmp.getAttribute("class2");
	tmp.setAttribute("class2",tmp.getAttribute("tmpClass"));
}

function categorySelectSubLevel( obj, idx, value )
{
	getObj('chBox' + idx).checked = true;

	var i = this.objArrMain.length;
	this.objArrMain[i] = new Array();
	this.objArrMain[i][this.mainIdxId] = value;

	this.chSec(obj,i);
}

function categoryOnClick( obj, idx )
{
	if(!getObj('chBox' + idx).checked)
	{
		return;
	}
}
// -------------------------------------------------------------------------
// ---------------------------- Secondary Table ----------------------------
// -------------------------------------------------------------------------
function secondarySetup( idxId, idxDescription )
{
	this.secondaryIdxId = idxId;
	this.secondaryIdxDesc = idxDescription;
}

function relationWithSecondary( idxInSecondary, idxInMain )
{
	this.secRelationId = idxInSecondary;
	this.secRelationMain = idxInMain;
}

function buildSubMenu( strDivId, idxMainArr, mainType )
{
	var isMainType = (mainType!=null)?true:false;

	var HTMLsub = '';
	HTMLsub += '<div class="SelectionPop">';
	HTMLsub += '<ul>';

	var HTMLtable = '';

	for(var i=0;i<this.objArrSecondary.length;i++)
	{
	    
		if(!isMainType || mainType == this.objArrSecondary[i][this.secRelationId])
		{
			HTMLsub += '<li><a href="javascript:' + this.strObjectName + '.chgSec(\'' + strDivId + '\',' + idxMainArr + ',' + i + ')">' + this.objArrSecondary[i][this.secondaryIdxDesc] + '</a></li>';
		}
	}
	HTMLsub += '</ul>';
	HTMLsub += '</div>';

	this.strSubMenu.innerHTML = HTMLsub;
}

// -------------------------------------------------------------------------
// ----------------------------- Search Table ------------------------------
// -------------------------------------------------------------------------
function tableSearchSetup( idxSearchText, objSearchDependOf, strFuncOnClick, arrCategory )
{
	this.idxSearchText = idxSearchText;
	this.objSearchDependOf = objSearchDependOf;
	this.strFuncOnClick = strFuncOnClick;
	this.arrCategory = arrCategory;
}

function tableSearchCannotFind( cannotFindLiteral, cannotFindKey, cannotFindFunction )
{
	this.cannotFindLiteral = cannotFindLiteral;
	this.cannotFindKey = cannotFindKey;
	this.cannotFindFunction = cannotFindFunction;
}

function buildSearch( _arr, isFromTyping )
{
    if(typeof(_arr) != 'undefined')
        this.objArrMain = _arr;

    var categoryId = null;
    var categoryName = '';

	var HTMLsub = '';
	HTMLsub += '<div class="EmploymentResults">';
	HTMLsub += '<ul>';
	for(var i=0;i<this.objArrMain.length;i++)
	{
		//if(typeof(this.objArrMain[i][2]) != 'undefined' && categoryId != this.objArrMain[i][2])
		//{
		//	categoryId = this.objArrMain[i][2];
		//	if( categoryId != "-1" )
		//		HTMLsub += '<li class="Category">' + this.arrCategory[categoryId] + '</li>';
		//	else
		//		HTMLsub += '<li class="Category CategoryNotFind">' + this.arrCategory[categoryId] + '</li>';			
		//}
		categoryName = this.arrCategory[this.objArrMain[i][2]];
		//if(categoryName.length > 40)
			//categoryName  = categoryName.substr(0,40) + '...';
		
		HTMLsub += '<li' + ((categoryId == "-1")?' class="RowNotFind" ':'') + '><a href="javascript:' + this.strFuncOnClick + '(' + i + ')">' + this.objArrMain[i][this.idxSearchText] + ' <span>[' + categoryName + ']' +'</span></a></li>';
	}
	
	if( isFromTyping && this.cannotFindFunction != null )
	{
		HTMLsub += '<li class="Category CategoryNotFind">Your title is not here?</li>';
		HTMLsub += '<li class="RowNotFind"><a href="javascript:' + this.cannotFindFunction + '">' + this.cannotFindLiteral + ' "' + eval(this.cannotFindKey) + '"</a></li>';
	}

	HTMLsub += '</ul>';
	HTMLsub += '</div>';

	this.strSubMenu.innerHTML = HTMLsub;

	if (this.strSubMenu.firstChild.firstChild.offsetHeight < this.strSubMenu.firstChild.offsetHeight)
	{
		this.strSubMenu.firstChild.style.height = this.strSubMenu.firstChild.firstChild.offsetHeight + 'px';
	}

	showMenu( getObj(this.objSearchDependOf).id, this.strSubMenu.id );
}

// -------------------------------------------------------------------------
// ----------------------------- Lookup Table ------------------------------
// -------------------------------------------------------------------------
function lookupSetup( lookupArr, lookupId, lookupDesc )
{
	this.lookupArr = lookupArr;
	this.lookupId = lookupId;
	this.lookupDesc = lookupDesc;
}

function counterSetup(strCounterId, strPhraseId)
{
	var thisClass = this;
	this.counterDIV = document.createElement("DIV");                
	this.counterDIV.id = this.strObjectName;
	this.counterDIV.className = 'RemovalPop';
	document.body.appendChild(this.counterDIV)

	this.strCounterId = strCounterId;
	this.strPhraseId  = strPhraseId;

	var obj = getObj(this.strPhraseId);		
	obj.onmouseover = function(){this.focus();thisClass.lookupOnMouseOver()}
	this.counterDIV.onblur = function(){hideMenu();}
	
	updateCounter( this.strCounterId, this.objArrMain );
}

function lookupOnMouseOver()
{
	if(this.objArrMain.length > 0)
	{
		var strHTML = '';
		strHTML += '<ul>';
		for(var i=0;i<this.objArrMain.length;i++)
		{
			strHTML += '<li onMouseDown="' + this.strObjectName + '.deleteFromMainArray(' + i + ');"><a href="javascript:void()">' + this.objArrMain[i][1] + '</a></li>';
		}
		strHTML += '</ul>';

		this.counterDIV.innerHTML = strHTML;

		hideMenu();

		showMenu( this.strPhraseId, this.counterDIV.id );
		
		this.counterDIV.focus();
	}
}

function lookupOnClick( obj, idx )
{
	if( obj.checked )
	{
		this.objArrMain[this.objArrMain.length] = new Array(this.lookupArr[idx][0],this.lookupArr[idx][1]);
	}
	else
	{
		for(var i=0; i<this.objArrMain.length; i++)
		{
			if( this.objArrMain[i][this.lookupId] == this.lookupArr[idx][this.lookupId] )
				this.objArrMain.splice(i, 1);
		}
	}

	updateCounter( this.strCounterId, this.objArrMain );
}

function deleteFromMainArray( idx )
{
    this.objArrMain.splice(idx,1);
    updateCounter( this.strCounterId, this.objArrMain );

    hideMenu();
        
    this.Build_Checkbox();
}

function updateCounter( strCounterId, objArrMain )
{
	if(getObj(strCounterId) != null)
		getObj(strCounterId).innerHTML = objArrMain.length.toString();
}

function buildCheckbox()
{
	if( typeof(localBuildCheckbox) == 'function' )	
		localBuildCheckbox( this );
	else if(this.lookupArr)
	{
		var HTMLtable = '';

		var strSelected = '|';
		for(var j=0; j<this.objArrMain.length; j++)
		{
			strSelected += this.objArrMain[j][this.lookupId] + '|'
		}

		HTMLtable += '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
		for(var i=0;i<this.lookupArr.length;i++)
		{
			HTMLtable += '<tr>';
			HTMLtable += '<th width="20"><input type="checkbox" name="chBox' + this.strObjectName + i.toString() + '" id="chBox' + this.strObjectName + i.toString() + '" onclick="' + this.strObjectName + '.lookupOnClick(this,' + i.toString() + ') "' + ((strSelected.indexOf('|' + this.lookupArr[i][this.lookupId] + '|')>-1)?'checked':'') + '/></th>';
			HTMLtable += '<td><label for="chBox' + this.strObjectName + i.toString() + '">' + this.lookupArr[i][this.lookupDesc] + '</label></td>';
			HTMLtable += '</tr>';
		}
		HTMLtable += '<tr>';
		HTMLtable += '<th>&nbsp;</th>';
		HTMLtable += '<td>&nbsp;</td>';
		HTMLtable += '</tr>';
		HTMLtable += '</table>';
		
		this.objContainer.innerHTML = HTMLtable;
	}
}

// -------------------------------------------------------------------------
// -------------------------------- Actions --------------------------------
// -------------------------------------------------------------------------
function addRow( id, desc )
{
	this.objArrMain[this.objArrMain.length] = new Array();
	this.objArrMain[this.objArrMain.length-1][this.mainIdxId] = id;
	this.objArrMain[this.objArrMain.length-1][this.mainIdxDesc] = desc;

	this.Build_Main();
}

function deleteRow(obj, idxMainArr)
{
	this.objArrMain.splice(idxMainArr, 1);

	var isBuildMain = true;
	if(this.runAfterDeleteRow != null)
		isBuildMain = this.runAfterDeleteRow();		

	if(isBuildMain)
		this.Build_Main();
}

function chooseSecondary( obj, idxMainArr )
{
	var mainType = null;

	if(typeof(this.secRelationMain)!='undefined' && 
		typeof(this.objArrElements)!='undefined' && 
		typeof(this.objArrElements[idxMainArr][this.secRelationMain]) != null)
	{
		mainType = this.objArrElements[idxMainArr][this.secRelationMain];
	}

	this.BuildSubMenu( obj.id, idxMainArr, mainType )
	showMenu( obj.id, this.strSubMenu.id, this.objContainer.id );
	
	this.strSubMenu.firstChild.style.overflowY = "scroll";
	this.strSubMenu.firstChild.style.overflowX = "hidden";	
}

function changeSecondary( strDivId, idxMainArr, idxSecondaryArr )
{
	getObj(strDivId).innerHTML = getSecondaryText(this.objArrSecondary, this.objArrSecondary[idxSecondaryArr][this.secondaryIdxId], this.secondaryIdxId, this.secondaryIdxDesc, this.chooseText);
	this.objArrMain[idxMainArr][this.idxSubSelection] = this.objArrSecondary[idxSecondaryArr][this.secondaryIdxId];

	if(this.runAfterchangeSecondary != null)
		this.runAfterchangeSecondary( false );	

	initiateHideMenu();
}

// -------------------------------------------------------------------------
// ----------------------------- XML functions -----------------------------
// -------------------------------------------------------------------------
function XMLnodeName( nodeName )
{
	this.nodeName = nodeName;
}

function XMLsetAttributes( attName, arrColId )
{
	arrColId = (typeof(arrColId) == 'undefined')?null:arrColId;
	this.XML[this.XML.length] = new Array(attName, arrColId);
}

function XMLget()
{
	return ( getStringForDB( this.objArrMain, this.XML ) );
}