﻿/*********************************************************************
*** (C) OK-Systems,2007 (All rights reserved)                      ***
*** Don't make any changes without reporting to oks@ok-systems.com ***
**********************************************************************
*** File____: ui_select.js                                         ***
*** Project_: Virtual Office                                       ***
*** Language: JavaScript                                           ***
*** Tier____: Presentation-services (Client side)                  ***
*** Description: Functions to manage SELECT elements               ***
*********************************************************************/

//==================================================
// SELECT ELEMENT FUNCTIONS (Combo-box, List-box)
//==================================================

//--------------------
//- Created_: 19-Mar-2001, Antonio Olmedo Soler
//- Modified: 19-Mar-2001, Antonio Olmedo Soler
//- Syntax__: lst_selected(lst)
//- Description: Get the value (if null, the item) of the selected item in a SELECT element.
//--------------------
function lst_selected(lst) {
	if (!lst) return '';
	if (lst.selectedIndex<0) return null;
	if (!lst.options) return false;
	if (lst.options[lst.selectedIndex].value) 
		return lst.options[lst.selectedIndex].value;
	else
		return lst.options[lst.selectedIndex].text;
}

//--------------------
//- Modified: 27-Sep-2000, Antonio Olmedo Soler
//- Syntax__: lst_selected_value(lst)
//- Description: Get the value of the selected item in a SELECT element.
//--------------------
function lst_selected_value(lst) {
	if (!lst) return '';
	if (lst.selectedIndex<0) return null;
	if (lst.options) return lst.options[lst.selectedIndex].value;
}

//--------------------
//- Modified: 27-Sep-2000, Antonio Olmedo Soler
//- Syntax__: lst_selected_item(lst)
//- Description: Get the text of the selected item in a SELECT element.
//--------------------
function lst_selected_item(lst) {
	if (!lst) return '';
	if (lst.selectedIndex<0) return null;
	if (lst.options) return lst.options[lst.selectedIndex].text;
}

//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_select_value(lst,cValue)
//- Description: Given an item's value, select the item in a SELECT element.
//--------------------
function lst_select_value(lst,cValue) {
	if (!lst) return false;
	lst.selectedIndex=-1;
	if (!cValue) return false;
	if (!lst.options) return false;
	//---
	for (var i=0; i<lst.options.length; i++) {
		 if (lst.options[i].value==cValue) {
			lst.selectedIndex=i;
			return true;
		}
	}
	//--- If item not found, don't select anyone (... select first?)
	lst.selectedIndex=-1;
	return false;
}

//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_select_item(lst,cValue)
//- Description: Given a value, select the item in a SELECT element matching it.
//--------------------
function lst_select_item(lst,cValue) {
	//alert('lst_select_item('+lst+','+cValue+')');
	if (!lst) return '';
	if (!lst.options) return false;
	//---
	lst.selectedIndex=null;
	if (cValue=='') return false;
	//---
	var cItem;
	for (var i=0; i<lst.options.length; i++) {
		cItem=lst.options[i].text;
		if (cValue==cItem) {
			lst.options[i].selected=true;
			return true;
		}
	}
	return false;
}

//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_mselect_item(lst,cValues)
//- Description: Given an list of values, select the items in a SELECT element.
//--------------------
function lst_mselect_item(lst,cValues) {
	//alert('lst_mselect_item('+lst+','+cValues+')');
	if (!lst) return false;
	lst.selectedIndex=-1;
	//---
	if (!lst.options) return false;
	if (!cValues) return false;
	cValues=','+cValues+',';
	//---
	var cItem;
	for (var i=0; i<lst.options.length; i++) {
		cItem=','+lst.options[i].value+',';
		//alert(cValues+'\n----------\n'+cItem+'\n'+(cValues.indexOf(cItem)>=0));
		lst.options[i].selected=(cValues.indexOf(cItem)>=0);
	}
	return true;
}

//--------------------
//- Modified: 29-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_add(lst,cValue,[cItem=cValue],[bSelect=true],[cClass])
//- Description: add a new item to a SELECT element.
//--------------------
function lst_add(lst,cValue) {
	if (!lst) return false;
	if (!lst.options) return false;
	//--- Optional arguments
	var cItem=cValue;
	var bSelect=true;
	var cClass='';
	//-
	if (arguments.length>2) if (arguments[2]!=null) cItem=arguments[2];
	if (arguments.length>3) if (arguments[3]) bSelect=arguments[3];
	if (arguments.length>4) if (arguments[4]) cClass=arguments[4];
	//---
	if (cValue==null) cValue=cItem;
	if (cValue==null) return false;
	//alert('lst_add('+lst.name+','+cValue+', '+cItem+', '+bSelect+')');
	//---
	try { 
		//--- Check whether the item is already in the list
		for (var i=0; i<lst.options.length; i++) {
			 if (lst.options[i].value==cValue) {
				//alert('The item '+cValue+' is already in the list !\n(position: '+i+')');
				if (bSelect) lst_select_value(lst,cValue);
				return false;
			}
		}
		//--- If item not found, add it to the list and select it
		var nItem = lst.options.length; //--- Add to end of the list
		if (ns4) {
			lst.options[nItem]=new Option(cItem,cValue);
			if (cClass) lst.options[nItem].className=cClass;
		} else {
			/* IE4
			var newItem = new Option();
			lst.add(newItem,nItem+1);
			lst.options[nItem].text=cItem;
			lst.options[nItem].value=''+cValue;
			*/
			//-NOTE: The previous code was OK in IE4.
			//- IE5 makes error in some cases
			//- (v.g. adding in a Select box from a different window or dialog)
			//- The following is a test version:
			//var newItem = new Option();
			//newItem.text=cItem;
			//newItem.value=''+cValue;
			//---
//			var newItem = new Option(cItem,cValue);
			//alert('newItem='+newItem+'\nnItem='+nItem+'\ncValue='+cValue);
//			lst.add(newItem,nItem+1);
			//--- New test (10-Nov-2000 B)
			//-This version corrects the "bug" of version 5.0, permitting to add items to lists in different frame.
			var oWin=lst.document.parentWindow;
			if (oWin==document.parentWindow) {
				//alert('Same window \n -> '+document.location.href);
				nItem=lst.length;
				lst.options[nItem]=new Option(cItem,cValue);
				if (cClass) lst.options[nItem].className=cClass;
			} else {
				//alert('Different window\n -> '+document.location.href+'\n -> '+oWin.document.location.href);
				oWin.lst_add(lst,cValue,cItem,bSelect,cClass);
			}
		}
		if (bSelect) lst.selectedIndex=nItem; 
		//if (bSelect) lst_select_item(lst,cValue);
	} catch (oError) { Debug_Function('lst_add',oError,arguments); }
	return true;
}

//--------------------
//- Created_:<09-Mar-1999, Antonio Olmedo Soler
//- Modified: 18-Oct-2004, Antonio Olmedo Soler
//- Syntax__: lst_ask_add(lst,[cDefault])
//- Description: Get's a new item asking to user, and adss it to a SELECT element.
//--------------------
function lst_ask_add(lst) {
	//--- Optional arguments
	var cDefault='';
	if (arguments.length>1) if (arguments[1]) cDefault=arguments[1];
	if (cDefault='==') cDefault=lst_selected(lst);
	//---
	//var cItem=window.prompt('Enter the new item\'s value:','');
	var cItem=window.prompt('Introduzca el nuevo valor:',cDefault);
	if ((cItem=='') || (cItem==null)) return false;
	lst_add(lst,cItem,cItem,true);
	return true;
}

//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_mselected_value(lst)
//- Description: Get the list of values of the selected items in a multiple SELECT element.
//--------------------
function lst_mselected_value(lst) {
	if (!lst) return '';
	if (!lst.options) return '';
	//---
	var cValue='';
	for (var i=0; i<lst.options.length; i++) {
		if (lst.options[i].selected) {
			if (cValue!='') cValue+=',';
			cValue+=lst.options[i].value;
		}
	}
	//---	
	return cValue;
}

//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_mselected_item(lst)
//- Description: Get the list of values of the selected item labels in a multiple SELECT element.
//--------------------
function lst_mselected_item(lst) {
	if (!lst) return '';
	if (!lst.options) return '';
	//---
	var cValue='';
	for (var i=0; i<lst.options.length; i++) {
		if (lst.options[i].selected) {
			if (cValue!='') cValue+=',';
			cValue+=lst.options[i].text;
		}
	}
	//---	
	return cValue;
}

//--------------------
//- Modified: 27-Mar-2001, Antonio Olmedo Soler
//- Syntax__: lst_remove_selected (lst)
//- Description: Remove the selected items from a SELECT element.
//--------------------
function lst_remove_selected(lst) {
	if (!lst) return '';
	if (!lst.options) return '';
	//---
	for (var i=(lst.options.length-1); i>=0; i--) {
		if (lst.options[i].selected) {
			//alert(i+'='+lst.options[i].text);
			lst.remove(i);
		}
	}
	//---	
	return true;
}

//--------------------
//- Created_: 26-Sep-2000, Antonio Olmedo Soler
//- Modified: 26-Sep-2000, Antonio Olmedo Soler
//- Syntax__: lst_remove_all (lst)
//- Description: Remove all the items from a SELECT element.
//--------------------
function lst_remove_all (lst) {
	if (!lst) return '';
	if (!lst.options) return '';
	//---
	for (var i=(lst.options.length-1); i>=0; i--) { lst.remove(i); }
	//---	
	return true;
}

//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_select_all(lst)
//- Description: Select all the items in a SELECT element.
//--------------------
function lst_select_all (lst) {
	if (!lst) return false;
	if (!lst.options) return false;
	//---
	lst.selectedIndex=null;
	for (var i=0; i<lst.options.length; i++) {
		lst.options[i].selected=(lst.options[i].value!='');	
	}
	return true;
}

//--------------------
//- Created_: 31-Aug-2000, Antonio Olmedo Soler
//- Modified: 21-Jul-2001, Antonio Olmedo Soler
//- Syntax__: lst_select_none(lst)
//- Description: Unselect all the items in a SELECT element.
//--------------------
function lst_select_none (lst) {
	if (!lst) return false;
	if (!lst.options) return false;
	//---
	lst.selectedIndex=null;
	for (var i=0; i<lst.options.length; i++)  { lst.options[i].selected=false;	}
	//for (var i in lst.options) { lst.options[i].selected=false;	}
	return true;
}


//--------------------
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: AddItems2List (cName)
//- Description: This function was first defined in the file of_form.asp 
//  It's a provisional patch trigerred from the elements created with the function 
//  MF_ChooseObject, solving a problem in certain versions of the browser that don't 
//  permit to add items to a select list directly from a dialog window.
//  Returned value is stored in a hidden field. This function adds the items to the list.
//--------------------
function AddItems2List (cName) {
	//---Load and validate arguments
	var lst=eval('document.frm.'+cName);
	if (!lst) return false;
	var txt=eval('document.frm.'+cName+'_add');
	var cValue,cLabel;
	var cValueList='';
	var cList='';
	if (txt) cList=txt.value;
	if (cItem=='') return false;
	//--- Add item(s) to the list
	try {
		var cItem;
		var nI=0;
		var bContinue=true;
		do {
			nI++;
			cItem=Token(cList,nI,'|');
			//alert(nI+' -> '+cItem);
			cValue=Token(cItem,1,'·');
			cLabel=Token(cItem,2,'·')
			lst_add(lst,cValue,cLabel);
			//if (cValueList!='') cValueList+=',';
			cValueList+=cValue;
			if (cItem=='') {bContinue=false; break;}		
		} while (bContinue);
		if (lst.type=='select-one') {
			lst_select_value(lst,cValueList);
		} else {
			lst_mselect_item(lst,cValueList);
		}
		//---
		txt.value='';
		//txt.focus();
		return true;
	} catch (oError) { Debug_Function('AddItems2List',oError,arguments); }
}

//--------------------
//- Created_: 27-Mar-2001, Antonio Olmedo Soler
//- Modified: 21-Jun-2001, Antonio Olmedo Soler
//- Syntax__: lst_move2List(lst1,lst2)
//- Description: Move the selected items from one list to another
//--------------------
function lst_move2List (lstSource,lstTarget) {
	if (!lstSource) return false;
	if (!lstTarget) return false;
	if (!lstSource.options) return false;
	if (!lstSource.selectedIndex<0) return false;
	//---
	try {
		for (var i=0; i<lstSource.options.length; i++) {
		//for (var i in lstSource.options) {
			if (lstSource.options[i].selected) {
				lst_add(lstTarget,lstSource.options[i].value,lstSource.options[i].text,false);
			}
		}
		lst_remove_selected(lstSource);
		return true;
	} catch (oError) { Debug_Function('lst_move2List',oError,arguments); }
}

//--------------------
//- Created_: 26-Sep-2000, Antonio Olmedo Soler
//- Modified: 25-Oct-2001, Antonio Olmedo Soler
//- Syntax__: lst_get_values(lst,[cDelim=','])
//- Description: Get a delimited list with all the values of a Select list
//--------------------
function lst_get_values(lst) {
	//Debug_Function('lst_get_values',null,arguments);
	if (!lst) return '';
	if (!lst.options) return '';
	//--- Optional arguments
	var cDelim=',';
	if (arguments.length>1) if (arguments[1]) cDelim=arguments[1];
	//---
	var cOut='';
	for (var i=0; i<lst.options.length; i++) cOut+=(cOut?cDelim:'')+lst.options[i].value;
	//---	
	return cOut;
}

//--------------------
//- Created_: 27-Mar-2001, Antonio Olmedo Soler
//- Modified: 25-Oct-2001, Antonio Olmedo Soler
//- Syntax__: lst_get_items(lst,[cDelim=','])
//- Description: Get a delimited list with all the items of a Select list
//--------------------
function lst_get_items(lst) {
	if (!lst) return '';
	if (!lst.options) return '';
	//--- Optional arguments
	var cDelim=',';
	if (arguments.length>1) if (arguments[1]) cDelim=arguments[1];
	//---
	var cOut='';
	for (var i=0; i<lst.options.length; i++) cOut+=(cOut?cDelim:'')+lst.options[i].text;
	//---	
	return cOut;
}

//--------------------
//- Created_: 05-Nov-2001, Antonio Olmedo Soler
//- Modified: 08-jul-2002, Antonio Olmedo Soler
//- Syntax__: lst_get_items(lst,cPos)
//- Description: Move the selected item(s) to the given position(Up,Down,Top,Bottom)
//--------------------
function lst_moveItems(lst,cPos) {
	//alert('lst_moveItems('+lst+','+cPos+')');
	if (!lst) return '';
	if (!lst.options) return '';
	//---
	var aV=new Array(); // New values
	var aT=new Array(); // New text
	var aS=new Array(); // Item selection Status
	var cValue,cText;
	//---
	switch(cPos) {
	case 'U': //Up
		for (var i=1; i<lst.options.length; i++) {
			//- If the previous element is selected, don't move item
			if (lst.options[i].selected && !lst.options[i-1].selected) { 
				cValue=lst.options[i-1].value;					cText=lst.options[i-1].text;
				lst.options[i-1].value=lst.options[i].value;	lst.options[i].value=cValue;
				lst.options[i-1].text=lst.options[i].text;	lst.options[i].text=cText;
				lst.options[i-1].selected=true;					lst.options[i].selected=false;
			}
		}
		return void(null);
		break;
	case 'D': //Down
		for (var i=lst.options.length-2; i>=0; i--) {
			//- If the next element is selected, don't move item
			if (lst.options[i].selected && !lst.options[i+1].selected) { 
				cValue=lst.options[i+1].value;					cText=lst.options[i+1].text;
				lst.options[i+1].value=lst.options[i].value;	lst.options[i].value=cValue;
				lst.options[i+1].text=lst.options[i].text;	lst.options[i].text=cText;
				lst.options[i+1].selected=true;					lst.options[i].selected=false;
			}
		}
		return void(null);
		break;
	case 'T': //To the top
		for (var i=0; i<lst.options.length; i++) // First round: add first the seletect items
			if (lst.options[i].selected) { aV[aV.length]=lst.options[i].value; aT[aT.length]=lst.options[i].text; aS[aS.length]=true; }
		for (var i=0; i<lst.options.length; i++) // Recond round: add the rest of the items
			if (!lst.options[i].selected) { aV[aV.length]=lst.options[i].value; aT[aT.length]=lst.options[i].text; aS[aS.length]=false;}
		break;
	case 'B': //To the bottom
		for (var i=0; i<lst.options.length; i++) // First round: add first the not seletect items
			if (!lst.options[i].selected) { aV[aV.length]=lst.options[i].value; aT[aT.length]=lst.options[i].text; aS[aS.length]=false; }
		for (var i=0; i<lst.options.length; i++) // Recond round: add the selected items at the end
			if (lst.options[i].selected) { aV[aV.length]=lst.options[i].value; aT[aT.length]=lst.options[i].text; aS[aS.length]=true;}
		break;
	}
	//--- Replace current list with new one
	for (var i=(lst.options.length-1); i>=0; i--) lst.remove(i);
	for (var i=0; i<aV.length; i++)	{
		lst.options[i]=new Option(aT[i],aV[i]);
		if (aS[i]) lst.options[i].selected=true;
	}
	//---
	return void(null);
}

//--------------------
//- Created_: 08-Jul-2002, Antonio Olmedo Soler
//- Modified: 08-Jul-2002, Antonio Olmedo Soler
//- Syntax__: lst_sort(lst)
//- Description: sorts the items of a Select list
//--------------------
function lst_sort(lst) {
	if (!lst) return '';
	if (!lst.options) return '';
	//---
	var cDelim='·';
	var aList=new Array();
	var aItem;
	//-
	for (var i=0; i<lst.options.length; i++) {
		aList[aList.length]=lst.options[i].text+cDelim+lst.options[i].value+cDelim+(lst.options[i].selected?'Y':'N');
	}
	//-
	//alert(aList);
	aList.sort();
	//alert(aList);
	//- Clear select element and add sorted items
	lst_remove_all(lst);
	for (var i=0; i<aList.length; i++) {
		aItem=aList[i].split(cDelim);
		lst.options[i]=new Option(aItem[0],aItem[1]);
		if (aItem[2]=='Y') lst.options[i].selected=true;
	}
	//---	
	return cOut;
}


//==================================================

