/** ----------------------------------------------------------------------- 
      general.js    This file has functions that deal with the control.
       
      7/14/2005      Initial draft by achan
      8/30/2005      Function getTodayInStr  added by achan
      9/12/2005      trim function added by achan
      9/13/2005      getFormValue added by achan
      9/14/2005      setSelectBoxValue added by achan
      9/23/2005      toggleTextbox added by achan
      9/28/2005      getFormObj, removeAllOptionsInSelectBox, addDayOption
                        added by achan
      11/15/2005     isControlHidden added by achan
      11/17/2005     disableRadioGroup, enableRadioGroup, setRadioButtonValue by achan
      12/06/2005     setReadOnly by achan
      4/13/2006      showTableRow by achan
      5/10/2006      popUp by achan
      1/18/2009      goToUrl by achan
  -----------------------------------------------------------------------  */
  
/* -------------------------------------------------------------------------
   goToUrl          - To go to url in the same window
   example        - javascript:goToUrl('http://www.yahoo.com')
   -------------------------------------------------------------------------*/
function goToUrl(url2Go){
   if (url2Go != '')
   {
      document.location.href = url2Go;
   }
}

/* -------------------------------------------------------------------------
   popUp          - To pop up a window
   parameter(s)   - URL - the URL to display for that window.
                  - width - the width of window.
                  - height - the height of window.
                  - resizable - indicates if that window is resizable.
   example        - javascript:popUp('http://www.yahoo.com',event, .....)
   -------------------------------------------------------------------------*/
function popUp(URL2Show, status, width, height, resizable, evt, left, top){
   
   width     = isNaN(width) ? "520" : width;
   height    = isNaN(height)? "360" : height;
   if (resizable== "" || resizable == null || resizable == undefined)
   {
      resizable = 1;
   }
   status    = (status== "")   ? 'no'  : 'yes';
   evt = (evt)? evt : ((window.event) ? window.event : null);
   
   if ((evt) && (left != '') && (top != '')) {
      xPos = evt.screenX + left;
      yPos = evt.screenY + top;
      param = 'status=' + status + ',width=' + width + ',height=' + height + ',resizable=' + resizable +
               ',left=' + xPos + ',top=' + yPos
   }
   else {
      param = 'status=' + status + ',width=' + width + ',height=' + height + ',resizable=' + resizable 
   }
   
   window2Pop = window.open(URL2Show, 'ACWin', param);
   if (window.focus) {  window2Pop.focus(); }
	   return false;
}

/* -------------------------------------------------------------------------
   showTableRow   - To show a table row
   parameter(s)   - tableRow - the table row to be shown.  
   note           - this method ONLY applies to Firefox browser
   -------------------------------------------------------------------------*/
function showTableRow(tableRow){
   if (document.getElementById(tableRow)) {
      document.getElementById(tableRow).style.display="table-row"; 
   }
}

/* -------------------------------------------------------------------------
   setReadOnly - To set the field to be readonly
   parameter(s)  - field2Set - the field to be set.
   -------------------------------------------------------------------------*/
function setReadOnly(field2Set) {
   if (document.getElementById(field2Set)) {
      document.getElementById(field2Set).readOnly = "false";
   }
}

/* -------------------------------------------------------------------------
   setRadioButtonValue - To set the value in the group of radio button
   parameter(s)   -  radioGroupObj - the radio group object
                     value2Set - the value of the radio button to be set.
   -------------------------------------------------------------------------*/
function setRadioButtonValue(radioGroupObj, value2Set) {
	if(!radioGroupObj)
		return;
	var radioLength = radioGroupObj.length;
	if(radioLength == undefined) {
		radioGroupObj.checked = (radioGroupObj.value == value2Set.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioGroupObj[i].checked = false;
		if(radioGroupObj[i].value == value2Set.toString()) {
			radioGroupObj[i].checked = true;
		}
	}
}

/* -------------------------------------------------------------------------
   disableRadioGroup - To disable the entire radio group
   parameter(s)   -  formName - the name of the form that has the 
                        radio group.
                     radioGroup2Disable - the radio group to be disabled.  
   -------------------------------------------------------------------------*/
function disableRadioGroup(formName, radioGroup2Disable) {
  for (var i=0; i<formName.elements.length; i++) 
  {
    if (formName.elements[i].name == radioGroup2Disable) 
    {
      formName.elements[i].disabled = true;
    }
  }
}

/* -------------------------------------------------------------------------
   enableRadioGroup - To enable the entire radio group
   parameter(s)   - formName - the name of the form that has the 
                        radio group.
                     radioGroup2Enable - the radio group to be enabled.  
   -------------------------------------------------------------------------*/
function enableRadioGroup(formName, radioGroup2Enable) {
   for (var i=0; i<formName.elements.length; i++) 
   {
      if (formName.elements[i].name == radioGroup2Enable) 
      {
         formName.elements[i].disabled = false;
      }
   }
}

/* --------------------------------------------------------------------------
   isControlHidden - to return true if the control is hidden or false
                        if otherwise
   parameter(s)   - control2Test - the control to be tested
   --------------------------------------------------------------------------*/
function isControlHidden(control2Test){
   var ans = false;
   if (document.getElementById(control2Test)) {
   
      var control = document.getElementById(control2Test);
      if (control.style.display == "none")
      {
         ans = true;
      }
   }
   return ans;
}

/* --------------------------------------------------------------------------
   addDayOption - to add options to a day select box
   parameter(s)   - selectBoxObj - the object of the select box whose options
                    to be removed
                  - days2Add - number of days to be added
   note           - the number of days to be added(days2Add) can be 28,
                     29,30,31
   --------------------------------------------------------------------------*/
function addDayOption(selectBoxObj, days2Add){
  var dayArr = new Array()
  
  for(var i=0 ; i<days2Add; i++){
      dayArr[i] = i + 1;
  }
  addOptionInSelectBox(selectBoxObj,dayArr);
}

/* --------------------------------------------------------------------------
   addOptionInSelectBox - to add options to select box
   parameter(s)   -  selectBoxObj - the object of the select box whose options
                     to be removed
                  -  content2Add - the array of content that is to be added
                     to the select box object
   Note(s)        -  the following works in IE4+ ONLY.
   --------------------------------------------------------------------------*/
function addOptionInSelectBox(selectBoxObj, content2Add){
   var contentLength = content2Add.length;
   //var doc = selectBoxObj.ownerDocument;  // new
   for(var i=0; i<contentLength; i++){   
      newOpt = document.createElement("option");    // old
      //newOpt = doc.createElement('OPTION');
      //newOpt.value = content2Add[i];   // added later
      newOpt.text = content2Add[i]; 
      //alert('text=' + content2Add[i]):
      // selectBoxObj.add(newOpt, selectBoxObj.options[i]); FF
      selectBoxObj.options.add(newOpt);
      
   //   selectObj.options.add(newOpt);   ///, selectObj.options[1]);
      
   }
}

/* --------------------------------------------------------------------------
   removeAllOptionsInSelectBox - to remove all options in a select box
   parameter(s)   -  selectBoxObj - the object of the select box whose options
                     to be removed
   --------------------------------------------------------------------------*/                                       
function removeAllOptionsInSelectBox(selectBoxObj){
   var selectBoxLength = selectBoxObj.length;
   
   for(var i=selectBoxLength-1; i >=0; i--){      
      selectBoxObj.options[i] = null;
   }      
   selectBoxObj.selectedIndex = -1;
}
  
/* --------------------------------------------------------------------------
   toggleTextbox - to enable/disable a textfield based upon the current state
                   of the toggle box (checkbox).  If it is checked, the 
                   pass-in parameter, txtfield, is to be enabled or disabled
                   if otherwise
   parameter(s)   -  chkBox - the id of the check box
                  -  txtfield - the id of textfield to be enabled/disabled
   --------------------------------------------------------------------------*/  
function toggleTextbox(chkBox, txtfield){
   if ( document.getElementById(chkBox).checked == true ){    
      enableControl (txtfield);
   }
   else if (document.getElementById(chkBox).checked == false){
      disableControl(txtfield);
   }  
}

/* --------------------------------------------------------------------------
   setSelectBoxValue - to set the value in the select box and thus the value is on focus
   parameter(s)   -  controlName - the id of the select box
                  -  value2Set - the state to be set        
   Note(s)        The value to be set MUST exist in one (and ONLY one) value of the
                  select box                                       
   --------------------------------------------------------------------------*/                                       
function setSelectBoxValue(selectBoxName, value2Set){
   if (document.getElementById(selectBoxName)){
      document.getElementById(selectBoxName).value= value2Set;
   }
}

/* --------------------------------------------------------------------------
   trim    - To remove any leading or trailing space(s) of string
   parameter(s) - string2Trim - the string to be trimmed. 
   --------------------------------------------------------------------------*/
function trim (string2Trim) {
  string2Trim = this != window? this : string2Trim;
  return string2Trim.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

/* --------------------------------------------------------------------------
   hideControl    - To hide a control 
   parameter(s)   - control2Hide - the one to be hidden. 
   --------------------------------------------------------------------------*/
function hideControl(control2Hide){
   if (document.getElementById(control2Hide)) {
      document.getElementById(control2Hide).style.display="none";   
   }
}

/* -------------------------------------------------------------------------
   showControl    - To show a control 
   parameter(s)   - control2Show - the one to be shown.  
   -------------------------------------------------------------------------*/
function showControl(control2Show){
   if (document.getElementById(control2Show)) {
      document.getElementById(control2Show).style.display="inline"; 
   }
}

/* -------------------------------------------------------------------------
   disableControl - To disable a control 
   parameter(s)   - control2Disable - the one to be disabled.  
   -------------------------------------------------------------------------*/
function disableControl(control2Disable){
   if (document.getElementById(control2Disable)) {
      document.getElementById(control2Disable).disabled = "true";
   }
}

/* -------------------------------------------------------------------------
   enableControl  - To enable a control 
   parameter(s)   - control2Enable - the one to be enabled.  
   -------------------------------------------------------------------------*/
function enableControl(control2Enable){
   if (document.getElementById(control2Enable)) {
      document.getElementById(control2Enable).disabled = "" ;
   }
}

/* -------------------------------------------------------------------------
   setFormValue - set value of a form
   parameters   - theName - the name of element of a form
                   theValue - the new value being assigned to that element  
   -------------------------------------------------------------------------*/
function setFormValue(theName, theValue){
   if (document.getElementById(theName)) {
      document.getElementById(theName).value = theValue;   
   }
}

/* -------------------------------------------------------------------------
   getFormObj - retrieve the obj from a form.  If the form value does 
                  not exist, empty string is retrieved.   
   parameters   - theName - the name of element of a form
   note       - it returns ONLY the object but not the value.  For value,
                please use getFormValue()
   -------------------------------------------------------------------------*/
function getFormObj(theName){
   if (document.getElementById(theName)) {
      return document.getElementById(theName);   
   }
}

/* -------------------------------------------------------------------------
   getFormValue - retrieve the value from a form.  If the form value does 
                  not exist, empty string is retrieved.
   parameters   - theName - the name of element of a form
   -------------------------------------------------------------------------*/
function getFormValue(theName){
   if (document.getElementById(theName)) {
      return document.getElementById(theName).value;   
   }
}