<!--


//====== CONSTANTS ====================

	var csAppPath;                  // application path
	var csActiveTableID = "1";        // table in main document
	var csAllCharsTableID = "1";        // table in main document
	var csAllCharsDivID = "divAllChars";        

	var cnStartRow = 2;
	var cnCharIDCol = 0;
	var cnCharNameCol = 1;
	var cnPriorityCol = 3;
	var cnPriorityRankCol = 4;
	var cnDeleteCol = 5;

	var csRowStyle = "w5_tr";
	var csSelectedRowStyle = "w5_tr_selected";
	var csCellStyle = "w5_td";

	var csPriorityInputName = "CharPriority";  // name of select object
	var csCharIDInputName = "CharID";			// name of input hidden object

	var csCharIDAttribute = "W5_CharID";    // row CharID attribute

	var csSliderMinValueAttribute = "to";    
	var csSliderMaxValueAttribute = "from";    


	var currentRow = 0;
	var slidersLoaded = false;

	// images array { delete image, up image }
	var w5images; 


// Characteristics Priority Page JS


// associate sliders with displays
// called in window.onload()
function setSliderDisplays(){
	var input, slider;
	var i;
  var table=getActiveCharsTable();
  var tr;
	for (i=cnStartRow; i<table.rows.length; i++){
		tr=table.rows[i];
		input=getSliderInput(tr);
		slider=getSlider(tr);
		slider.setAttribute("display", input.id);
		slider.title=getCharName(tr);
	}

	// load images	
	w5images = new Array(2);
	w5images[0] = new Image();
	w5images[0].src = "../w5/img/del.gif";
	w5images[0].border=0;
	w5images[0].style.cursor="hand";
	w5images[0].title='Remove from my priority list';
	
	w5images[1] = new Image();
	w5images[1].src = "../w5/img/up.gif";
	w5images[1].border=0;
	w5images[1].style.cursor="hand";
	w5images[1].title='Add to my priority list';

	slidersLoaded = true;
}

// show/hide all chars
function SetAllChars(button){
	var divChars = document.getElementById(csAllCharsDivID);
	var visible = (divChars.style.display=='inline');
	if (visible){
		divChars.style.display='none';
		button.value='More '+String.fromCharCode(8250);		
		button.title='Show additional characteristics';	
	}
	else{
		divChars.style.display='inline';	
		button.value=String.fromCharCode(8249)+' Less';		
		button.title='Hide additional characteristics';	
	}
}

// changes hidden inputs (CharId) and  names - for using on server
function onSubmit(){
	var input, hid;
	var i;
  var table=getActiveCharsTable();
	for (i=cnStartRow; i<table.rows.length; i++){
		input=getSliderInput(table.rows[i]);
		input.name = csPriorityInputName;
		hid=getInputHidden(table.rows[i]);
		hid.name = csCharIDInputName;
	}
	return true;
}

// Reset button onclick
function Reset(){
	var href = document.location.href;
	var pos = href.indexOf("?");
	if (pos>0)
		href = href.substring(0, href.indexOf("?"));
	document.location = href+"?rqDefault=1";
}

// slider change handler
function onSliderChange(sliderInput){
	ReorderPriority(sliderInput);
}

function updateSliderRank(label, value){
	label.innerHTML = value;
}


// reorder priority
function ReorderPriority(sliderInput){
	if (!slidersLoaded) 
		return;
	
	var tr = getRowBySliderInput(sliderInput);
	var nIndex = getIndex(tr);
	var i;
	
	var nNewPriority = Number(sliderInput.value);
	var nOldPriority = W5_Array_CharPriority[nIndex];
	if (nOldPriority==nNewPriority)
		return;

	updateSliderRank(getSliderLabel(tr), nNewPriority);

	///show('Reorder: '+nOldPriority+ ' -> '+nNewPriority);

	var input;
	if (nNewPriority > nOldPriority) {
		//+1;
		for (i=0; i<W5_Array_CharPriority.length; i++){
			if (W5_Array_CharPriority[i]!=0 && W5_Array_CharPriority[i] > nOldPriority && W5_Array_CharPriority[i] <= nNewPriority){
				W5_Array_CharPriority[i]--;
				tr = getRowByIndex(i);
				input = getSliderInput(tr);
				///show(' - '+input.value+' -> '+W5_Array_CharPriority[i]);
				input.value = W5_Array_CharPriority[i];
				//
				setSliderPos(getSlider(tr).id, W5_Array_CharPriority[i]);
				updateSliderRank(getSliderLabel(tr), W5_Array_CharPriority[i]);
			}
		}
	}
	else { // nNewPriority <= nOldPriority
		for (i=0; i<W5_Array_CharPriority.length; i++){
			if (W5_Array_CharPriority[i]!=0 && W5_Array_CharPriority[i] < nOldPriority && W5_Array_CharPriority[i] >= nNewPriority){
				W5_Array_CharPriority[i]++;
				//alert(i + ' row '+getRowByIndex(i));
				tr = getRowByIndex(i);
				input = getSliderInput(tr);
				///show(' + '+input.value+' -> '+W5_Array_CharPriority[i]);
				input.value = W5_Array_CharPriority[i];
				//
				setSliderPos(getSlider(tr).id, W5_Array_CharPriority[i]);
				updateSliderRank(getSliderLabel(tr), W5_Array_CharPriority[i]);
			}
		}
	}
	W5_Array_CharPriority[nIndex] = nNewPriority;
	
}





// functions returning page objects, depending on defined table structure

// returns active chars table's tBody
function getActiveCharsTable(){
	return document.getElementById(csActiveTableID).tBodies[0];
}

// returns active chars count
function getActiveCharsCount(){
	return (document.getElementById(csActiveTableID).tBodies[0].rows.length-cnStartRow);
}

// returns all chars table's tBody
function getAllCharsTable(){
	return document.getElementById(csAllCharsTableID).tBodies[0];
}

// returns row input hidden with CharId value
function getInputHidden(tr){
	return tr.cells[cnCharIDCol].childNodes[0];
}

// returns row by index in W5_Array_RowID array
function getRowByIndex(index){
	var id = W5_Array_RowID[index];
	//alert(id);
	return document.getElementById(id);
}

// returns Char Name
function getCharName(tr){
	return tr.cells[cnCharNameCol].innerHTML;
}


// slider element structure:
//	<tr>...
//		<td>
//			<div>
//				<div>
//					<div></div>
//					<div SLIDER></div>
//				</div>
//			</div>
//		</td>
//		<td>
//			<div>
//				<INPUT>
//				<SPAN>.</SPAN>
//			</div>

// returns row by slider input
function getRowBySliderInput(input) {
	return input.parentNode.parentNode.parentNode;
}

// returns slider input
function getSliderInput(tr){
	return tr.cells[cnPriorityRankCol].childNodes[0].childNodes[0];
}

// returns slider lable
function getSliderLabel(tr){
	return tr.cells[cnPriorityRankCol].childNodes[0].childNodes[1];
}

// returns slider div
function getSlider(tr){
	return tr.cells[cnPriorityCol].childNodes[0].childNodes[0].childNodes[1];
}



// returns row index in W5 arrays
function getIndex(tr){
 var charID=tr.cells[cnCharIDCol].childNodes[0].value;
 var i, index;
 for (i=0; i<W5_Array_CharID.length; i++)
	if (charID==W5_Array_CharID[i])	{
		index=i;
		break;
	}
 return index;
}




// functions for dinamically moving rows

// moves row to 'Active Chars' table from 'All Chars' table
function addChar(tr){
	if (!slidersLoaded) 
		return;
	
	var table=getActiveCharsTable();
	var number=table.rows.length - cnStartRow + 1;
	var nIndex=getIndex(tr);

	setSliders(number);

	// modify row
	// priority slider column
	tr.cells[cnPriorityCol].innerHTML="";
	tr.cells[cnPriorityCol].appendChild(createSlider(number, nIndex, getCharName(tr)));

	// priority rank
	var td;
	td=document.createElement("TD");
	td.className=csCellStyle;
	tr.appendChild(td);
	tr.cells[cnPriorityRankCol].appendChild(createSliderDisplay(number, nIndex));

	// delete column
	td=document.createElement("TD");
	td.className=csCellStyle;
	td.align="center";
	tr.appendChild(td);
	tr.cells[cnDeleteCol].innerHTML="";
	var button=createDeleteButton();
	button.onclick=new Function("deleteChar(this.parentNode.parentNode)");
	tr.cells[cnDeleteCol].appendChild(button);

	table.appendChild(tr);

	activateSlider("slider"+nIndex, number);
	W5_Array_CharPriority[nIndex]=number;
}

// moves row to 'All Chars' table from 'Active Chars' table
function deleteChar(tr){
	if (!slidersLoaded) 
		return;
	
	var table=getAllCharsTable();
	var nIndex=getIndex(tr);

	//alert(nIndex);
	var input = getSliderInput(tr);
	input.value = input.getAttribute(csSliderMaxValueAttribute);
 	//setSliderPos(getSlider(tr).id, input.value);
	
	ReorderPriority(input);

	W5_Array_CharPriority[nIndex]=0;

	//alert(nIndex+' - '+W5_Array_CharID.length);
	if (nIndex<W5_Array_CharID.length-1){
	// start from 1 to skip first header
		for (i=1; i<table.rows.length; i++){
			//alert(getIndex(table.rows[i])+" - "+nIndex);
			if (getIndex(table.rows[i]) > nIndex){
				//alert("found");
				break;
			}
		}
		table.insertBefore(tr, table.rows[i]);
	}
	else
		table.appendChild(tr);

	tr.cells[cnPriorityCol].innerHTML="";
	tr.cells[cnPriorityCol].appendChild(createButton());
	tr.removeChild(tr.cells[cnDeleteCol]);
	tr.removeChild(tr.cells[cnPriorityRankCol]);

	setSliders(getActiveCharsCount());

}


// set slider valuecount = number (in all sliders in active chars table)
function setSliders(number){
	var input, slider;
	var i, j;
  var table=getActiveCharsTable();
	for (i=cnStartRow; i<table.rows.length; i++){
		slider=getSlider(table.rows[i]);
		input=getSliderInput(table.rows[i]);
		input.setAttribute("valuecount", number);
		input.setAttribute(csSliderMaxValueAttribute, number)
		setSliderPos(slider.id, input.value);
		updateSliderRank(getSliderLabel(table.rows[i]), input.value);
	}
}





// returns 'delete' button
function createDeleteButton(){
	var img = w5images[0].cloneNode(true);
	//img=document.createElement("IMG");
	//img.src="../w5/img/del.gif";
	//img.border=0;
	//img.style.cursor="hand";
	var link=document.createElement("A");
	link.href="javascript://";
	link.appendChild(img);
	return link;
}

// returns 'add char' button
function createButton(){
	var img = w5images[1].cloneNode(true);
	//img=document.createElement("IMG");
	//img.src="../w5/img/up.gif";
	//img.border=0;
	//img.style.cursor="hand";
	var link=document.createElement("A");
	link.href="javascript://";
	link.onclick=new Function("addChar(this.parentNode.parentNode);");
	link.appendChild(img);
	return link;
}

// returns slider
// number - slider value count
// index - row index (for unique control id)
function createSlider(number, index, title){
	var divCombo=document.createElement("DIV");
	divCombo.className="carpe_horizontal_slider_display_combo";
	var div1=document.createElement("DIV");
	div1.className="carpe_horizontal_slider_track";
	var div2=document.createElement("DIV");
	div2.className="carpe_slider_slit";
	div2.innerHTML="&nbsp;";
	div1.appendChild(div2);
	
	div2=document.createElement("DIV");
	div2.className="carpe_slider";
	div2.innerHTML="&nbsp;";
	div2.id="slider"+index;
	div2.setAttribute("display", "display"+index);
	div2.title=title;
	div1.appendChild(div2);
	divCombo.appendChild(div1);

	/*div1=document.createElement("DIV");
	div1.className="carpe_slider_display_holder";
	var input=document.createElement("INPUT");
	input.type="text";
	input.className="carpe_slider_display";
	input.id="display"+index;
	input.setAttribute(csSliderMinValueAttribute, 1);
	input.setAttribute(csSliderMaxValueAttribute, number);
	input.setAttribute("valuecount", number);
	input.setAttribute("value", number);
	div1.appendChild(input);
	divCombo.appendChild(div1);*/
	
	return divCombo;	
}

// returns slider
// number - slider value count
// index - row index (for unique control id)
function createSliderDisplay(number, index){
	var div1=document.createElement("DIV");
	div1.className="carpe_slider_display_holder";
	var input=document.createElement("INPUT");
	input.type="hidden";
	//input.className="carpe_slider_display";
	input.id="display"+index;
	input.setAttribute(csSliderMinValueAttribute, 1);
	input.setAttribute(csSliderMaxValueAttribute, number);
	input.setAttribute("valuecount", number);
	input.setAttribute("value", number);
	div1.appendChild(input);
	var span=document.createElement("SPAN");
	span.className="carpe_slider_display";
	span.innerHTML = number;
	div1.appendChild(span);
	
	return div1;	
}


//-->
