//----------------------------------------------------
// JavaScript common functions
// Author : J.Dvorak
// Date   : 26.11.2002
//----------------------------------------------------
// Opens new browser window with specified content
function OpenNewWindow(WinID, VSize, HSize, ReqURL) {
   if (!navigator.mWins) 
      navigator.mWins = new Array();
   CurrWinIndex=navigator.mWins.length;
   CurrWinID=open(ReqURL, WinID, "scrollbars=yes, menubar=no, locationbar=no, personalbar=no, status=no, width="+VSize+", height="+HSize+", resizable=yes");
   navigator.mWins[CurrWinIndex]=CurrWinID;
   CurrWinID.focus();
}

function OpenLookupWindow(Formular, FItem1, FItem2, SQLCmd) {
   FValue=Formular.elements[FItem2].value;
   ReqURL='/admin/lookup.php?ParentForm='+Formular.name+'&ParentItem1='+FItem1+'&ParentItem2='+FItem2+'&InitialValue='+FValue+'&SelectCmd='+SQLCmd;
   OpenNewWindow('LookupWindow',400,400,ReqURL);
}

// Close all child browser windows opened by OpenNewWindow
function CloseAllWindows() {
   for (i=0; i<navigator.mWins.length; i++) 
      navigator.mWins[i].close();
   navigator.mWins = new Array();
}

// Close of current window
function CloseThisWindow() {
   opener.location.reload(true);
   self.close();
}

function MAProdSetup(Formular, FItem, FValue) {
   Formular.elements[FItem].value=FValue;
   Formular.submit();
}

// Operations with parent forms
function ClickOnParent(ParentForm, ParentItem) {
   window.opener.document.forms[ParentForm].elements[ParentItem].click();
   self.close();
}

function GetParentValue(ThisForm, ThisItem, ParentForm, ParentItem) {
   ThisForm.ThisItem.value=window.opener.document.forms[ParentForm].elements[ParentItem].value;
   alert(ThisForm.ThisItem.value);
}

// Open help window
function ShowHelp(HlpType, TblName, ColName, ScriptName, TxtDescr, Server) {
   HelpURL=Server+"/help.php?HlpType="+HlpType+"&TblName="+TblName+"&ColName="+ColName+"&ScriptName="+ScriptName+"&TxtDescr="+TxtDescr;
   HelpWinName="HelpWindow";
   OpenNewWindow(HelpWinName, 640, 480, HelpURL) ;
   //HelpWindow=open(Server+"/help.php?HlpType="+HlpType+"&TblName="+TblName+"&ColName="+ColName+"&ScriptName="+ScriptName+"&TxtDescr="+TxtDescr, "HelpWindow", "scrollbars=yes, menubar=no, locationbar=no, personalbar=no, statusbar=no, width=640, height=480, resizable=yes, dependent=yes");
   //HelpWindow.focus();
}

// Pemission formular
function MAPermSetup(Formular, Action, PermClass, PermID, Perm) {
   Formular.PERMAction.value=Action;
   Formular.PERMClass.value=PermClass;
   Formular.PERMID.value=PermID;
   Formular.PERMData.value=Perm;
   Formular.submit();
}

// Tabs
function MATabs(Formular, Action, HDRActual) {
   Formular.Action.value=Action;
   Formular.HDRActual.value=HDRActual;
   Formular.submit();
}

// Management of TP fields
function MATP(Formular, Action, FieldName, ReqID) {
   Formular.Action.value=Action;
   Formular.elements[FieldName].value=ReqID;
   Formular.submit();
}

// Change record navigation bar position
function SetRecPage(Formular, RC, RPP, WhereToMove) {
   RecFrom=Number(Formular.RecFrom.value);
   RecTo=Number(Formular.RecTo.values);
   RecPerPage=Number(RPP);
   RecCount=Number(RC);
   RecPage=Number(Formular.RecPage.value) - 1;
   if (WhereToMove == 2) { //Go to previous record
      if ((RecFrom - RecPerPage) < 0) {
         WhereToMove=1;
      } else {
         PomFrom=RecFrom - RecPerPage;
         Formular.RecFrom.value=PomFrom;
         Formular.RecTo.value=PomFrom + RecPerPage;
      }
   }
   if (WhereToMove == 3) { //Go to next record
      if ((RecFrom + RecPerPage + 1) > RecCount) {
         WhereToMove=4;
      } else {
         PomFrom=RecFrom + RecPerPage;
         Formular.RecFrom.value=PomFrom;
         Formular.RecTo.value=PomFrom + RecPerPage;
      }
   }
   if (WhereToMove == 5) { //Go to selected page
      PomFrom=RecPage * RecPerPage;
      if (PomFrom > RecCount) {
         WhereToMove=4;
      } else {
         Formular.RecFrom.value=PomFrom;
         Formular.RecTo.value=PomFrom + RecPerPage;
      }
   }
   if (WhereToMove == 1) { //Go to first record
      Formular.RecFrom.value=0;
      Formular.RecTo.value=RecPerPage;
   }
   if (WhereToMove == 4) { //Go to last record
      Formular.RecTo.value=RecCount;
      if ((RecCount - RecPerPage) < 0)
         Formular.RecFrom.value=0;
      else
         Formular.RecFrom.value=RecCount - RecPerPage;
   }
   Formular.submit();
}

var id,pause=0,position=0;
function ScrollingMsg(FormName, FieldName, Msg) {
   var i,k;
   OrigMsg=Msg;
   k=(150/Msg.length)+1;
   for (i=0; i<=k; i++)
      OrigMsg+=" "+OrigMsg;
   document.forms[FormName].elements[FieldName].value=OrigMsg.substring(position, position+250);
   //Set new position
   if (position ++== 250) position=0;
   //Repeat at entered speed 
   id=setTimeout("ScrollingMsg('"+FormName+"','"+FieldName+"','"+Msg+"')",200);
}

// Show, Hide an element
function ShowHideElement(ElementID, ElementVisibility) {
   theDIV=document.getElementById(ElementID).style;
   theDIV.visibility=ElementVisibility;
}

