//**************************************
// author  : ogi@keenresearch.com	
// purpose : set of universal functions 
// creation date : 18.07.2005 
//**************************************


//********************************************************************
//purpose : function sets selection to item whose value is
//          equal to given value.
//param selList : selection list whose selection is being changed
//param value : value for comparison with selection list items values
//********************************************************************
function setSelectionByValue(selList, value) {
     for(i = 0 ; i < selList.length; i++) {
       if ( selList.options[i].value == value ) {
         selList.selectedIndex = i;
         i = selList.length;
       }
     }
}

//********************************************************************
//purpose : function sets selection to item whose text is
//          equal to given text.
//param selList : selection list whose selection is being changed
//param text : value for comparison with selection list items texts
//********************************************************************
function setSelectionByText(selList, text) {
     for(i = 0 ; i < selList.length; i++) {
       if ( selList.options[i].text == text ) {
         selList.selectedIndex = i;
         i = selList.length;
       }
     }
}

//********************************************************************
//purpose : function sets selection to item whose id is
//          equal to given text.
//param selList : selection list whose selection is being changed
//param text : value for comparison with selection list items texts
//********************************************************************
function setSelectionById(selList, text) {
     for(i = 0 ; i < selList.length; i++) {
       if ( selList.options[i].id == text ) {
         selList.selectedIndex = i;
         i = selList.length;
       }
     }
}
//********************************************************************
//purpose : function sets disabled attribute of given object to given value
//param formObject : object whose disabled status is being set.
//param value : value for disabled status
//********************************************************************
function setDisabledStatus(formObject, value) {
     formObject.disabled = value;
}

//********************************************************************
//purpose : function opens popup modeless popup window
//param url : URL of the page that will be open in the new window
//param target : name of the popup window
//param width : width of the popup window
//param height : height of the popup window
//********************************************************************
function openWindow(url, target, width, height) {
  hlpWin = open(url,target,"width=" + width + ",height=" + height + ",modal=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,left=100,top=100,screenX=100,screenY=100");
  hlpWin.focus();
}

function openWindowWithOffset(url, target, left, top, width, height) {
  hlpWin = open(url,target,"width=" + width + ",height=" + height + ",modal=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,left=" + left + ",top=" + top + ",screenX=0,screenY=0");
  hlpWin.focus();
}

function openLookupWindow(url, target) {
  hlpWin = open(url,target,"width=800,height=640,modal=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,left=0,top=0,screenX=0,screenY=0");
  hlpWin.focus();
}

function openWindowWithReference(url, target, width, height) {
  hlpWin = window.open(url,target,"width=" + width + ",height=" + height + ",modal=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,left=0,top=0,screenX=0,screenY=0");
  return hlpWin;
}

function openWindowWithOffsetAndReference(url, target, left, top, width, height) {
  hlpWin = window.open(url,target,"width=" + width + ",height=" + height + ",modal=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,left=" + left + ",top=" + top + ",screenX=0,screenY=0");
  return hlpWin;
}

//********************************************************************
//purpose : function appends a class name to the element's className
//          attribute and sets the onchange function.
//param elementId : the id of the element.
//********************************************************************
function updateAjaxElement(elementId) {
	if (document.getElementById) {
	  var el = document.getElementById(elementId);
	  if (el) {
	  	// Only add highlight if not there.
	  	if (el.className.indexOf('highlight') == -1) {
	  		el.className = el.className+' highlight'; 
	  	}
	  	// Use ajaxValue as flag for proper ui experience.
	  	el.ajaxValue = el.value;
	  	// Set the onchange event handler.
	  	el.onchange = removeUpdated;
	  }
	}
}

//********************************************************************
//purpose : function removes the highlight class from the element.
//see: updateAjaxElement()
//param event : the event.
//********************************************************************
function removeUpdated(event) {
	if (this.value == this.ajaxValue) {
		this.ajaxValue = null;
	} else {
		this.className = this.className.replace('highlight', ''); 
	}
} 

// Makes layer visible/invisible
function toggleLayer(whichLayer) {
  if (document.getElementById) {
    // this is the way the standards work
    var style2 = document.getElementById(whichLayer).style;
    style2.display = style2.display? "":"block";
  } else if (document.all) {
    // this is the way old msie versions work
    var style2 = document.all[whichLayer].style;
    style2.display = style2.display? "":"block";
  } else if (document.layers) {
    // this is the way nn4 works
    var style2 = document.layers[whichLayer].style;
    style2.display = style2.display? "":"block";
  }
}  

// Makes layer visible/invisible
function toggleLayer2(whichLayer) {
  if (document.getElementById) {
    // this is the way the standards work
    var style2 = document.getElementById(whichLayer).style;
    if (style2.display == 'block') style2.display = 'none';
    else if (style2.display == 'none') style2.display = 'block';
  } else if (document.all) {
    // this is the way old msie versions work
    var style2 = document.all[whichLayer].style;
    if (style2.display == 'block') style2.display = 'none';
    else if (style2.display == 'none') style2.display = 'block';
  } else if (document.layers) {
    // this is the way nn4 works
    var style2 = document.layers[whichLayer].style;
    if (style2.display == 'block') style2.display = 'none';
    else if (style2.display == 'none') style2.display = 'block';
  }
}
//********************************************************************
//purpose : Add an onLoad event to the body/window.
//param function : the function to add as a variable.
//********************************************************************
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
} 

//********************************************************************
//purpose : Add maxlength functionality to textarea HTML tag
//          and alerts the user when maxlength is reached for both textinput 
//          and textarea fields.
//param function : the text input object object.
//********************************************************************
function alertmaxlength(obj){
  var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
  if ((obj.type=="textarea" && obj.getAttribute && obj.value.length>mlength) ||
      (obj.type=="text" && obj.getAttribute && obj.value.length>=mlength)) {
    alert('Maximal number of characters ('+mlength+') for this field has been reached.');
    obj.value=obj.value.substring(0,mlength);
  } 
}


function alertlength256(obj){
  var mlength= 256;
  if ((obj.type=="textarea" && obj.getAttribute && obj.value.length>mlength) ||
      (obj.type=="text" && obj.getAttribute && obj.value.length>=mlength)) {
    alert('Maximal number of characters ('+mlength+') for this field has been reached.');
    obj.value=obj.value.substring(0,mlength);
  } 
}

//********************************************************************
//purpose : Add date and time separators to text input HTML objects displaying date (and time)
//          and automatically sets maxlength to proper value 
//param field : id of text field
//param event : length of the year field (2 or 4)
//param event : includeTime - whether time entry is needed. (true or false)
//param event : the event
//********************************************************************
function addDateSeparator(field, yearLength, includeTime, e) {
 if (e.keyCode!=8) { // If not backspace
   
   var maxLen=6+yearLength; // The following lines set maxLength f text box
   if (includeTime) maxLen += 6;
   document.getElementById(field).maxLength = maxLen;
   
   var fieldValue = document.getElementById(field).value;
   if ((fieldValue.length==2) && (!isNaN(fieldValue))) // First date separator
     document.getElementById(field).value += '/';
   if (fieldValue.length==3) { // Backspace handling on first date separator
     separator = document.getElementById(field).value.substring(2,3);
     if (!isNaN(fieldValue) && separator!='/') document.getElementById(field).value = document.getElementById(field).value.substring(0,2)+'/'+document.getElementById(field).value.substr(2);
   }
   if (fieldValue.length==5) { // Second date separator
     fieldValue = document.getElementById(field).value.substring(3,5);
     if (!isNaN(fieldValue)) document.getElementById(field).value += '/';
   }
   if (fieldValue.length==6) { // Backspace handling on second date separator
     separator = document.getElementById(field).value.substring(5,6);
     fieldValue = document.getElementById(field).value.substring(3,5);
     if (!isNaN(fieldValue) && separator!='/') document.getElementById(field).value = document.getElementById(field).value.substring(0,5)+'/'+document.getElementById(field).value.substr(5);
   }
   var yearAdder = 6 + yearLength;
   if (fieldValue.length==yearAdder && includeTime) { // Space between date and time
     fieldValue = document.getElementById(field).value.substring(6,6+yearLength);
     if (!isNaN(fieldValue)) document.getElementById(field).value += ' ';
   }         
   if (fieldValue.length==(7+yearLength)) {  // Backspace handling on space between date and time
     separator = document.getElementById(field).value.substring(6+yearLength,7+yearLength);
     fieldValue = document.getElementById(field).value.substring(6,6+yearLength);
     if (!isNaN(fieldValue) && separator!=' ') document.getElementById(field).value = document.getElementById(field).value.substring(0,6+yearLength)+' '+document.getElementById(field).value.substr(6+yearLength);
   }
   yearAdder = 9 + yearLength; // Time separator
   if (fieldValue.length==yearAdder && includeTime) {
     fieldValue = document.getElementById(field).value.substring(7+yearLength,9+yearLength);
     if (!isNaN(fieldValue)) document.getElementById(field).value += ':';
   }
   if (fieldValue.length==(10+yearLength) && includeTime) { // Backspace handling on time separator
     separator = document.getElementById(field).value.substring(9+yearLength,10+yearLength);
     fieldValue = document.getElementById(field).value.substring(7+yearLength,9+yearLength);
     if (!isNaN(fieldValue) && separator!=':') document.getElementById(field).value = document.getElementById(field).value.substring(0,9+yearLength)+':'+document.getElementById(field).value.substr(9+yearLength);
   }
 }
}

//********************************************************************
//purpose : Add time separator to text input HTML objects displaying time
//          and automatically sets maxlength to proper value 
//param field : id of text field
//param event : the event
//********************************************************************
function addTimeSeparator(field, e) {
 if (e.keyCode!=8) { // If not backspace
   
   document.getElementById(field).maxLength = 5;           
   var fieldValue = document.getElementById(field).value;
   if ((fieldValue.length==2) && (!isNaN(fieldValue))) // First time separator
     document.getElementById(field).value += ':';
   if (fieldValue.length==3) { // Backspace handling on first time separator
     separator = document.getElementById(field).value.substring(2,3);
     if (!isNaN(fieldValue) && separator!=':') document.getElementById(field).value = document.getElementById(field).value.substring(0,2)+':'+document.getElementById(field).value.substr(2);
   }
 }
}


  function displaySelectedOptionsFromSelectList(objectId, where) {
  	var items = '';
  	object = document.getElementById(objectId);
	for(i = 0; i < object.options.length; i++) {
		var option = object.options[i];
		if (option.selected==true) {
			if (items!='') items += '<br/>';
			items += option.text;
		}
	}					  	
	document.getElementById(where).innerHTML = items;
  }

  function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}


function moveDownList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

