function trim(str)
{
  return str.replace(/^\\s*|\\s*$/g,'');
}; 

function insertOptionBefore(selectName, num, text, value)
{
  var elSel = document.getElementsByName(selectName)[0];
    
  if (elSel.selectedIndex >= 0) {
    var elOptNew = document.createElement('option');
    elOptNew.text = text;
    elOptNew.value = value;
    var elOptOld = elSel.options[elSel.selectedIndex];  
    try {
      elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew, elSel.selectedIndex); // IE only
    }
  }
};

function JS_ChangeGroup(Index, mas, combousername, combogroupname, selectauser)
{       
  var combo = document.getElementsByName(combousername)[0];      
  var combo2 = document.getElementsByName(combogroupname)[0];
  var x = combo2.options[Index].value; 
                                     
  combo.length = 0; 
  var oOption = document.createElement("OPTION");
  oOption.text=selectauser;
  oOption.value=-1;        
  combo.options.add(oOption);	  
	  
  for (i=0;i<mas.length;i=i+3) 
  {        
    if (mas[i] == x) 
    {          
      oOption = document.createElement("OPTION");
      oOption.text=mas[i+2];
      oOption.value=mas[i+1];        
	  combo.options.add(oOption);
    }; 
  };	  	
  combo.selectedIndex = 0;
}; 

function JS_SetGroupSelect(group_id, user_id, combousername, combogroupname, mas, selectauser)
{
  var user = document.getElementsByName(combousername)[0];      
  var group = document.getElementsByName(combogroupname)[0];
  
  for (i=0; i<group.options.length; i++)
  {
    if (group.options[i].value == group_id)
      break;    
  };  
  group.selectedIndex = i;
  JS_ChangeGroup(i, mas, combousername, combogroupname, selectauser);
  
  for (i=0; i<user.options.length; i++)
  {
    if (user.options[i].value == user_id)
      break;    
  };  
  user.selectedIndex = i; 
};

function ChangeVisibleElementById(Aid)
{
  var elem = document.getElementById(Aid);
	  	  
  if (elem.style.display != "none")
  {
    elem.style.display = "none";		
  } else
  {
	elem.style.display = "block";		
  };
};

function DoIt(Aquestion)
{
  ok = 0;     
  if (confirm(Aquestion))
  {      
    ok = 1;
  };
   
  if (ok == 1) { return true; } else {return false; }; 
};
  
function CheckForm(Anames, $Atext)
{
  ok = 1;
  
  var mas = Anames.split(",");
  var control;
  
  for (i=0; i<mas.length; i++)
  {    
    control = document.getElementsByName(mas[i])[0];    
    
    if (control.id == "date")
    {
      if (!IsDate(control.value))
      {
        control.focus();
        alert(NotValidDateStr);
        ok = 0;      
        break;
      };
    };
    
    if (control.id == "int")
    {
      if (!IsNumberInt(control.value))
      {               
        control.focus();
        alert(NotValidNumberIntStr);
        ok = 0;      
        break;
      };
    };
    
    if (control.id == "float")
    {
      if (!IsNumberFloat(control.value))
      {               
        control.focus();
        alert(NotValidNumberFloatStr);
        ok = 0;      
        break;
      };
    };
    
    if (trim(control.value) == '' || control.value == '-1')
    {      
      control.focus();
      alert($Atext);
      ok = 0;      
      break;
    };    
  };
   
  if (ok == 1) { return true; } else {return false; }; 
};

function CheckFormById(Aids, $Atext)
{
  ok = 1;  
  var mas = Aids.split(",");
  var control;
    
  for (i=0; i<mas.length; i++)
  {    
    control = document.getElementById(mas[i]);        
    
    if (trim(control.value) == '' || control.value == '-1')
    {      
      control.focus();
      alert($Atext);
      ok = 0;      
      break;
    };    
  };
   
  if (ok == 1) { return true; } else {return false; }; 
};

function CheckFindForm($Atext)
{
  ok = 1;

  if (document.finddoc.list_inbox.checked == false &&
    document.finddoc.list_created.checked == false && 
    document.finddoc.list_control.checked == false &&
    document.finddoc.list_taken.checked == false &&
    document.finddoc.list_signed.checked == false &&
    document.finddoc.list_arhive.checked == false)
  {
    alert($Atext);
    ok = 0;
  };
   
  if (ok == 1) { return true; } else {return false; }; 
};

function CheckReportForm($Atext)
{
  ok = 0;
  if (document.report.reportcode.length == null)
  {
    if (document.report.reportcode.checked)
      ok = 1;
  } else 
  {
    for (i=0; i<document.report.reportcode.length; i++) 
    {
      if (document.report.reportcode[i].checked) 
      {
        ok = 1;
      };
    };
  };
   
  if (ok == 1) { return true; } else { alert($Atext); return false; }; 
};

function CheckSetDocUserGroupNextForm($Aformname, $Atext1, $Atext2)
{
  ok = 0;
  var f = document.getElementsByName($Aformname)[0];
  
  if (f.docusergroup1_id.value == -1 || f.docusergroup2_id.value == -1 || 
    f.docusergroup1_id.value != f.docusergroup2_id.value)
  {       
    for (i=0; i<f.nextif.length; i++) 
    {      
      if (f.nextif[i].checked)       
        ok = 1;    
    };
    
    if (ok == 0)
      alert($Atext1);      
  } else 
  {
    alert($Atext2);
  };
  
  if (ok == 1){ return true; } else{ return false; }; 
};

function CheckDocUserGroupChangeNameForm($Aformname, $Atext)
{
  ok = 1;
  var f = document.getElementsByName($Aformname)[0];
  
  if (trim(f.docusergroup_name.value) == '')
  {
    ok = 0;
    alert($Atext);
  };
  
  if (ok == 1){ return true; } else{ return false; };
};