// function to open the popup window
function UniCMSOrderableRefSelectWidget_openBrowser(portal_url, fieldId, fieldType, multiVal, close_window)
{
    if (-1 == close_window)
        close_window = 1 - multiVal
    window.open(portal_url + '/UniCMSOrderableRefSelectWidget_popup?fieldId=' + fieldId + '&fieldType=' + fieldType + '&multiVal:int=' + multiVal + '&close_window:int='+close_window, 'UniCMSOrderableRefSelectWidget_popup','dependent=yes,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=500,height=550');
}

// function to return a reference from the popup window back into the widget
function UniCMSOrderableRefSelectWidget_setReference(fieldId, fieldType, username, label, multiVal)
{
  // differentiate between the single and mulitselect widget
  // since the single widget has an extra label field.
  pos = label.indexOf(' ')
  uid = label.slice(0, pos) 
  nameStart = label.indexOf(' ', pos+1)
  email = label.slice(pos+1,nameStart)
  login = email.slice(0,email.indexOf('@'))
  name = label.slice(nameStart+1);

  //pos = label.indexOf(' ')
  //email = label.slice(0, pos)
  //login = email.slice(0, email.indexOf('@'))
  //uidStart = label.indexOf(' ', pos+1); //??? 
  //name = label.slice(pos + 1, uidStart )
  //uid = label.slice( uidStart+1 );

  if (fieldType == 'id') {
    //label = username+' ('+fullname+')'
    label = username+', '+name +' ('+ login + ')'
    if (multiVal==0) {
        element=document.getElementById(fieldId)
        label_element=document.getElementById(fieldId + '_label')
        element.value=username
        label_element.value=label
     }  else {
         list=document.getElementById(fieldId)
         // check if the item isn't already in the list
          for (var x=0; x < list.length; x++) {
          test = list[x].value
          tes2 = uid
            if (list[x].value == uid ) {
              return false;
            }
          }         
          // now add the new item
          theLength=list.length;
          list[theLength] = new Option(label);
          list[theLength].selected='selected';
          
          //TODO: get the objects UID and set it (controller awaits the correct UID)
          // use uid_catalog
          //refUID = 'cc64a42ba2ae1156f273a7c6f4a5f4f2';
          //alert ( "setting uid : " + uid );
		    list[theLength].value = uid;
     }
   } else {
     // email
     if (fieldType == 'nameemail') {
         label = '"' + name + '" <' + email + '>'
     } else {
         label = email
     }
     element=document.getElementById(fieldId)
     if (multiVal==0) {
         element.value=label
     }  else {
         element.value += label + '\n'
     }
   }
}

// function to clear the reference field or remove items
// from the multivalued reference list.
function UniCMSOrderableRefSelectWidget_removeReference(widget_id, multi)
{
    if (multi) {
        list=document.getElementById(widget_id)
        for (var x=list.length-1; x >= 0; x--) {
          if (list[x].selected) {
            list[x]=null;
          }
        }
     
    } else {
        element=document.getElementById(widget_id);
        label_element=document.getElementById(widget_id + '_label');
        label_element.value = "";
        element.value="";
    }
   
}

// dynamic select all functionality start---------------------------------------

// 0 if list-item is unselected, 1 otherwise
var saveArray = new Array();
var comingFromOutside = new Boolean(true);

		// moving mouse indide-->out
		function selectAll(fieldId){
		var list = document.getElementById(fieldId);
			for (var i=0; i < list.length; i++) {
   				list[i].selected = true;
			}
			
		}

		function saveSelection(fieldId){
		var list = document.getElementById(fieldId);
			for (var i=0; i< list.length; i++){
				if (list[i].selected)  saveArray[i] = true;
					else saveArray[i] = false;
			}
		}
		
		function restoreSelection(fieldId){
		var list = document.getElementById(fieldId);
			for (var i=0; i<list.length; i++){
				list[i].selected = saveArray[i];
			}
		}

// dynamic select all functionality start---------------------------------------


// stuff from orderableRefernce field start

function oref_up(obj) { /*updated from version 1.2*/
	var obj_string = obj;
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var sel = new Array();
	for (var i=0; i<obj.length; i++) {
		if (obj[i].selected == true) {
			sel[sel.length] = i;
			
		}
	}
	for (i in sel) {
	    if (sel[i] != 0) {
			if (obj[sel[i]-1]) {
				if (!obj[sel[i]-1].selected) {
				var tmp = new Array((document.body.innerHTML ? obj[sel[i]-1].innerHTML : obj[sel[i]-1].text), obj[sel[i]-1].value, obj[sel[i]-1].style.color, obj[sel[i]-1].style.backgroundColor, obj[sel[i]-1].className, obj[sel[i]-1].id);
				if (document.body.innerHTML) obj[sel[i]-1].innerHTML = obj[sel[i]].innerHTML;
				else obj[sel[i]-1].text = obj[sel[i]].text;
				obj[sel[i]-1].value = obj[sel[i]].value;
				obj[sel[i]-1].style.color = obj[sel[i]].style.color;
				obj[sel[i]-1].style.backgroundColor = obj[sel[i]].style.backgroundColor;
				obj[sel[i]-1].className = obj[sel[i]].className;
				obj[sel[i]-1].id = obj[sel[i]].id;
				if (document.body.innerHTML) obj[sel[i]].innerHTML = tmp[0];
				else obj[sel[i]].text = tmp[0];
				obj[sel[i]].value = tmp[1];
				obj[sel[i]].style.color = tmp[2];
				obj[sel[i]].style.backgroundColor = tmp[3];
				obj[sel[i]].className = tmp[4];
				obj[sel[i]].id = tmp[5];
				obj[sel[i]-1].selected = true;
				obj[sel[i]].selected = false;
				}
			}
		}
	}
	saveSelection(obj_string);
	//inout_selectAllWords(obj_string);
}

function oref_down(obj) {
	var obj_string = obj;
	obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var sel = new Array();
	for (var i=obj.length-1; i>-1; i--) {
		if (obj[i].selected == true) {
			sel[sel.length] = i;
		}
	}
	for (i in sel) {
		if (sel[i] != obj.length-1) {
			if (obj[sel[i]+1]) {
				if (!obj[sel[i]+1].selected) {
					var tmp = new Array((document.body.innerHTML ? obj[sel[i]+1].innerHTML : obj[sel[i]+1].text), obj[sel[i]+1].value, obj[sel[i]+1].style.color, obj[sel[i]+1].style.backgroundColor, obj[sel[i]+1].className, obj[sel[i]+1].id);
					if (document.body.innerHTML) obj[sel[i]+1].innerHTML = obj[sel[i]].innerHTML;
					else obj[sel[i]+1].text = obj[sel[i]].text;
					obj[sel[i]+1].value = obj[sel[i]].value;
					obj[sel[i]+1].style.color = obj[sel[i]].style.color;
					obj[sel[i]+1].style.backgroundColor = obj[sel[i]].style.backgroundColor;
					obj[sel[i]+1].className = obj[sel[i]].className;
					obj[sel[i]+1].id = obj[sel[i]].id;
					if (document.body.innerHTML) obj[sel[i]].innerHTML = tmp[0];
					else obj[sel[i]].text = tmp[0];
					obj[sel[i]].value = tmp[1];
					obj[sel[i]].style.color = tmp[2];
					obj[sel[i]].style.backgroundColor = tmp[3];
					obj[sel[i]].className = tmp[4];
					obj[sel[i]].id = tmp[5];
					obj[sel[i]+1].selected = true;
					obj[sel[i]].selected = false;
				}
			}
		}
	}
	saveSelection(obj_string);
	//inout_selectAllWords(obj_string);
}


function inout_selectAllWords(theList) {
  myList = document.getElementById(theList);
  for (var x=0; x < myList.length; x++) {
    myList[x].selected="selected";
  }
}

// test funtion
function addDummy(fieldId){
	list=document.getElementById(fieldId)
   	theLength=list.length;
    //deselect all items
    for (var x=0; x < theLength; x++) {
    	list[x].selected=0;
  	}
    list[theLength] = new Option('dummy'+x);
    
	// BEWARE: correct UID needed, so svaing this will give you this error :
	// Invalid target UID (because the target's value list[].value is no UID at all but a string 
	list[theLength].value='dummyValue';
	//select added item    
    list[theLength].selected='selected';
    saveSelection(fieldId);
}


// stuff from orderableRefernce field end


















