var isopen=0;
var gridID2=new Array();
var gmouse={x:0, y:0};
var oPopup=null;
var oRPSdiv=null;
var fgrT=null;

if (!document.all) { // Not IE
   window.captureEvents(Event.MOUSEMOVE);
}
document.onmousemove=mouseTrack;

function cPAGESIZE() { // object definition
   this.resetNavButtons=resetNavButtons;
}

cPAGESIZE.prototype.resetSize=doResetPgSize;
cPAGESIZE.prototype.movePage=goMovePageEx;
cPAGESIZE.prototype.fu=0;
cPAGESIZE.prototype.pg=1;
cPAGESIZE.prototype.epage=0;

var cPageSize=new cPAGESIZE();

function cDYNPAGER(_ogrid, _pgsz, _pg) { // object definition
   this.ogrid = _ogrid;
   this.pgsz = _pgsz;
   this.pg = _pg;
   
   cPAGESIZE.prototype.resetSize=dynResetPageSize;
   cPAGESIZE.prototype.movePage=dynMovePage;
}

var cDynPager=null;

function dynResetPageSize(fx)
{ 
   var newsz=parseInt($('#pgsize').val());
   if (isNaN(newsz))
      return;
      
   var d1 = new Date();
   d1.setYear( d1.getFullYear()+1 ); // increase by 1 year
   document.cookie='ADOPAGESIZE='+newsz+';expires='+d1.toGMTString()+';';
   
   pgsz=newsz;
   cDynPager.ogrid.clearAll(); // need call this because we may gona have less records
   cDynPager.reloadPage();      
}

function dynMovePage(fx,pg2)
{  
   cDynPager.pg=pg2;
   cDynPager.ogrid.clearAll(); // need call this because we may gona have less records
   cDynPager.reloadPage();
}


function mouseTrack(evt)
{
   if (document.all) { // IE
      gmouse.x = event.clientX;
      gmouse.y = event.clientY;
   }
   else { // not IE
      gmouse.x = evt.pageX;
      gmouse.y = evt.pageY;
   }
}

function endofPage() {
   if (isopen) 
      helpWin.close();
}
function goTo(fx) {
  	var g=document.goForm;
	if (g==undefined) {
      g=document.createElement('FORM');

      o2=document.createElement('INPUT');
      o2.type='hidden';
      o2.name='FUNCTION';
      o2.value=fx;

      g.appendChild(o2);

      document.body.appendChild(g);
   }
   else {
      g.FUNCTION.value=fx;
  	}
  	g.submit();
}
function goDelete(fx)
{
   if (!window.confirm('Πραγματικά να σβύσει όλο το αρχείο?'))
		return;

   return goTo(fx);
}
function goFX(a,fx) {
	var g=document.goForm;
	if (g==undefined) {
      g=document.createElement('FORM');

      o2=document.createElement('INPUT');
      o2.type='hidden';
      o2.name='FUNCTION';

      g.appendChild(o2);

      document.body.appendChild(g);
   }
	g.action=a;
  	g.FUNCTION.value=fx;
  	g.submit();
}
function goSecure(f) {
  var myRef='http://'+window.location.hostname+'/x/ishopm1.cgi';
  document.goForm.action=myRef;
  document.goForm.FUNCTION.value=f;
  document.goForm.submit();
}
function goBase(f) {
  var myRef='http://'+window.location.hostname+'/x/ishopm1.cgi';
  document.goForm.action=myRef;
  document.goForm.FUNCTION.value=f;
  document.goForm.submit();
}
function myHelp(lang,id) {
  var helpUrl='/syfax/help_adm_'+lang+'.htm#'+id;
  helpWin=window.open(helpUrl,'preview','scrollbars=yes,resizable=yes,height=320,width=500');
  isopen=1;
  return ;
}
function onAction(myForm,myValue) {
  myForm.todo.value=myValue;
  myForm.submit();
}
function onAction2(myForm,myValue) {
  myForm.FUNCTION.value="12";
  myForm.todo.value=myValue;
  myForm.submit();
}
function clearIt(myfield) {
  myfield.value="";
  return ;
}
function viewIt(myOrder) {
  document.goForm.FUNCTION.value="2";
  document.goForm.ORDERID.value=myOrder;
  document.goForm.submit();
  return ;
}
function checkPass(f) {
  if (f.PASS1.value.length<4) {
	window.alert('Minimum 4 chars. required!');
	return false;
  }
  if (f.PASS1.value!=f.PASS2.value) {
	window.alert('Verification error! Please re-type.');
	f.reset();
	return false;
  }
}
function on_page(pg) {
  var g=document.goForm;
  g.FUNCTION.value='142';
  g.PAGENO.value=pg;
  g.submit();
}
function on_edit_plan(id) {
  return goRecord(12,x);
}
function on_delpg(pg) {
	if (!window.confirm('Πραγματικά να σβύσει η εγγραφή?'))
		return;

  	var g=document.goForm;
  	g.FUNCTION.value='145';
  	g.PAGENO.value=pg;
  	g.submit();
}
// To be disabled
function goMovePage(pg) {
  var g=document.goForm;
  g.RSPAGE.value=pg;
  g.submit();
}
// The new one
function goToPagex(f,pg) {
  var g=document.goForm;
  g.FUNCTION.value=f;
  g.RSPAGE.value=pg;
  g.submit();
}
function on_delete() {
  document.myform.FUNCTION.value='144';
  document.myform.submit();
}
function on_addnew() {
  document.myform.FUNCTION.value='145';
  document.myform.submit();
}
function on_record(x) {
   // we should find this function wherever it exists and replace it
   goRecord('152',x);
}
function goRecord(fx,rid) {
	var f=document.goForm;
  	f.FUNCTION.value=fx;
  	f.RECORDID.value=rid;
  	f.submit();
}
function on_delrec(x) {
   goDelRec(155,x);
}
function goDelRec(fx,rid) {
   if (window.confirm('Πραγματικά να σβύσει η εγγραφή?'))
      goRecord(fx,rid);
}
function on_glacc(x) {
   return goRecord(157,x);
}
function on_maziki(x) {
   return goRecord(158,x);
}
function on_maz_cust(x) {
	var f=document.goForm;
  	f.FUNCTION.value='4';
  	f.MBR_LOGIN.value=x;
  	f.submit();
}
function on_maz_custx(x,f,t) {
	var g=document.goForm;
  	g.FUNCTION.value='4';
  	g.MBR_LOGIN.value=x;
  	g.YMON_FROM.value=f;
  	g.YMON_TO.value=t;
  	g.submit();
}
function on_incom_cust(x,y) {
	var f=document.goForm;
  	f.FUNCTION.value='6';
  	f.MBR_LOGIN.value=x;
  	f.SENTDATE.value=y;
  	f.submit();
}
function on_order(x) {
   return goRecord(161,x);
}
function on_del_mbr() {
  if (!window.confirm('Πραγματικά να σβύσει η εγγραφή?'))
		return;

  document.myform.FUNCTION.value='156';
  document.myform.submit();
}
function usage_details(d1) {
   var f=document.goForm;
   f.FUNCTION.value='301';
   f.TRDATE.value=d1;
   f.submit();
}
function su_use_details(d1) {
   var f=document.goForm;
   f.FUNCTION.value='511';
   f.TRDATE.value=d1;
   f.submit();
}
function su_use_users(d1) {
   var f=document.goForm;
   f.FUNCTION.value='514';
   f.TRDATE.value=d1;
   f.submit();
}
function CopyTitle() {
   var f=document.goForm;
   f.TITLE.value=f.ABBREV.value;
}
function onS80Day(x) {
   var f=document.goForm;
   f.FUNCTION.value=2;
   f.S80DAY.value=x;
   f.submit();
}
function on_qgroup(x)
{
   return goRecord(26,x);
}
function on_detail(x)
{
   return goRecord(10,x);
}
function on_mbrdetail(x)
{
   return goRecord(11,x);
}
function on_delqgrp(x)
{
   if (!window.confirm('Πραγματικά να σβύσει η εγγραφή?'))
		return;

   return goRecord(28,x);
}
function on_question(x)
{
   return goRecord(16,x);
}
function on_delque(x)
{
   if (!window.confirm('Πραγματικά να σβύσει η εγγραφή?'))
		return;

   return goRecord(18,x);
}
function on_answer(a,b,c,d)
{
   var f=document.goForm;
   f.CREDATE.value=a;
   f.CRETIME.value=b;
   f.MBR_LOGIN.value=c;
   f.RESELID.value=d;
   f.FUNCTION.value='171';
   f.submit();
}
function LogAgent(lpath)
{
 	var f=document.goForm;
 	f.LPATH2.value=lpath;
 	f.FUNCTION.value='99';
 	f.submit();
}
function onDocMat()
{
   var f=document.goForm;
   var url2="/syfax/syfa_docmat.php?SESSIONID=" + f.SESSIONID.value + "&DELTA=" + f.DELTA.value + "&FUNCTION=1";
   window.open(url2,"doc_mat","resizable=1,toolbar=1,status=1,scrollbars=1,hotkeys=0");
}
function onMbrLoginMat()
{
   var f=document.goForm;
   var url2="/syfax/syfa_loginmat.php?FUNCTION=1";
   url2 += "&SEARCH="+f.MBR_LOGIN.value;
   window.open(url2,"doc_mat","resizable=1,toolbar=1,status=1,hotkeys=0");
}
function onDocPreview(x)
{
   return goRecord(163,x);
}
function doDocSelect(x)
{
   var f=window.opener.goForm;
   f.ADVDOC.value=x;
   self.close();
}
function doMbrLoginSelect(x)
{
   var f=window.opener.goForm;
   f.MBR_LOGIN.value=x;
   self.close();
}
function onLevelChange()
{
   var f=document.goForm;

   if (f.LEVEL2.selectedIndex==0) {
      f.LEVEL3.selectedIndex=0;
      f.LEVEL4.selectedIndex=0;

      document.all.Level3.style.display='none';

      document.all.Level4.style.display='none';
   }
   else {
      document.all.Level3.style.display='inline';
   }

   if (f.LEVEL3.selectedIndex==0) {
      f.LEVEL4.selectedIndex=0;

      document.all.Level4.style.display='none';
   }
   else {
      document.all.Level4.style.display='inline';
   }
}
function onURLpreview(xurl)
{
   window.open(xurl,'preview','scrollbars=yes,resizable=yes,height=450,width=700');
}
function onShowHide(xId)
{
   if (xId.style.visibility=='visible') {
      xId.style.display='none';
   }
   else {
      xId.style.display='inline';
   }
}
function onEditDDComments(j)
{
   L1="TXLINE"+j;
   L2="TXAREA"+j;

   onShowHide(document.all(L1));
   onShowHide(document.all(L2));
}
function onUpdateDDComments(id,j)
{
   var f=document.goForm;

   L1="TXT"+j;

   f.FUNCTION.value="5";
   f.RECORDID.value=id;
   f.COMMENTS.value=f.all(L1).value;
   f.submit();
}
function goDDTable(xpid,tt)
{
   var f=document.goForm;
   f.FUNCTION.value="10";
   f.XPID.value=xpid;
   f.TABLEID.value=tt;
   f.submit();
}

var oRPSdiv=null;

function resetNavButtons() { // cPAGESIZE.resetNavButtons
   var dis='disabled';
   
   var btn=$('#movePageFirst');
   if (this.pg>1) {
      btn.removeAttr(dis).unbind('click').click(function(){ onMovePageEx('F'); });
   }
   else {
      btn.attr(dis,dis).unbind('click');
   }      
   
   var btn=$('#movePagePrev');
   if (this.pg>1) {
      btn.removeAttr(dis).unbind('click').click(function(){ onMovePageEx('P'); });
   }
   else {
      btn.attr(dis,dis).unbind('click');
   }
   
   var btn=$('#movePageNext');
   if (this.pg<this.epage) {
      btn.removeAttr(dis).unbind('click').click(function(){ onMovePageEx('N'); });
   }
   else {
      btn.attr(dis,dis).unbind('click');
   }
   
   var btn=$('#movePageLast');
   if (this.pg<this.epage) {
      btn.removeAttr(dis).unbind('click').click(function(){ onMovePageEx('L'); });
   }
   else {
      btn.attr(dis,dis).unbind('click');
   }
   
   if (this.epage>1) {
      $('#movePager').html( '<a id=xtdpgGo href="javascript:;">' + this.pgLabel + '</a> ' +
	      '<span id=xtdpgNo>' + this.pg + '/' + this.epage + '</span>' +
         '<span id=xtdpgSel style="display:none;"></span>' );
      
      var currentObject=this;         
      $('#xtdpgGo').click( xtdPage );
   }
   else {
      $('#movePager').html( this.pgLabel + ' ' + this.pg + '/' + this.epage );
   }
}

function onResetPgSize(fx) {
   cPageSize.resetSize(fx);
   $(oRPSdiv).hide();
}

function doResetPgSize(fx)
{
   var f=document.goForm;
	if (f==null) {
      f=document.createElement('FORM');
      f.name='goForm';
      f.method='POST';
      f.action=window.location.pathname;
      document.body.appendChild(f);
   }
   
   if (fx>0) {
      var o2=f.FUNCTION;
      if (o2==null) {
         o2=document.createElement('INPUT');
         o2.type='HIDDEN';
         o2.name='FUNCTION';
         f.appendChild(o2);
      }
      o2.value=fx;
   }
   
   var newsz=parseInt($('#pgsize').val());
   if (isNaN(newsz))
      return;
      
   d1 = new Date();
   d1.setYear( d1.getFullYear()+1 ); // increase by 1 year
   document.cookie='ADOPAGESIZE='+newsz+';expires='+d1.toGMTString()+';';

   f.submit();
}

function onMovePageEx(act) {
   var fx=cPageSize.fu;
   
   switch (act)
   {
   case 'F':
      var pg=1;
      break;
   case 'P':
      var pg=cPageSize.pg-1;
      break;
   case 'N':
      var pg=cPageSize.pg+1;
      break;
   case 'L':
      var pg=cPageSize.epage;
      break;
   default: // goto specific page
      var pg=(typeof act=='number')?act:1;
      break;   
   }
   
   cPageSize.movePage(fx, pg);
}

function goResetPgSize(fx,oldsz) {
   if (oRPSdiv==null) {
		oRPSdiv=document.createElement('DIV'); 
		oRPSdiv.className='POPMENU'; 
		document.body.appendChild(oRPSdiv);
      $(oRPSdiv).html('<table cellspacing=0>' +
		   '<tr><td colspan=2 class=bgHeader>Reset page size</td></tr>' +
			'<tr><td class=bgLabel>New value (minimum 10)</td>' +
         '<td class=bgInput><input type=TEXT id=pgsize value="'+oldsz+'" size=5></td></tr>' +
			'<tr><td></td>' +
         '<td><button type=button onclick="onResetPgSize(' + fx + ')">OK</button></td></tr>' +
			'</table>');
	}
   show_popup(oRPSdiv);
	$('#pgsize').focus();
}


function rsPage(f,pg) {
	if (f.RSPAGE==undefined) {
		o2=document.createElement('INPUT');
      o2.type='hidden';
      o2.name='RSPAGE';
      o2.value=pg;
      f.appendChild(o2);
	}
	else {
		f.RSPAGE.value=pg;
	}
	document.body.onbeforeunload=bodyUnloading;
  	f.submit();
}
function goMovePageEx(fx,pg) {
  	var f=document.goForm;
   if (f==null) {
      f=document.createElement('FORM');
      f.name='goForm';
      f.method='POST';
      f.action=window.location.pathname;
      document.body.appendChild(f);
   }
  	if (fx>0) {
   	if (f.FUNCTION)
      	f.FUNCTION.value=fx;
      else if (f.FU)
      	f.FU.value=fx;
      else {
         o2=document.createElement('INPUT');
         o2.type='HIDDEN';
         o2.name='FUNCTION';
         f.appendChild(o2);
         o2.value=fx;
      }
   }
  	rsPage(f,pg);
}
/*
function doPgSel(fu,osel)
{
   var i=osel.selectedIndex;
   if (i<0) i=0;
   goMovePageEx(fu,i+1);
}
*/
function doPgSel(osel)
{
   var i=osel.selectedIndex;
   if (i<0) { i=0; }
   onMovePageEx(i+1);
}
function makePgSel(fu,pg,epage)
{
   var a='<select onchange="doPgSel('+fu+',this)">\n';
   for (i=1;i<=epage;i++) {
      a += '<option';
      if (i==pg) {
         a += ' SELECTED';
      }
      a += '>'+i+'\n';
   }
   a += '</select>';

   return a;
}
/*
function xtdPage(fu,pg,epage)
{
   var o1=$('#xtdpgNo');
   var o2=$('#xtdpgSel');

   if (o1.css('display')=='none') {
      o1.show();
      o2.hide();
   }
   else {
      if (o2.html()=='') {
         o2.html( makePgSel(fu,pg,epage) );
      }
      o1.hide();
      o2.show();
   }
}
*/
function makePgSel() 
{
   var a='<select onchange="doPgSel(this)">\n';
   for (var i=1;i<=cPageSize.epage;i++) {
      a += '<option';
      if (i==cPageSize.pg) {
         a += ' SELECTED';
      }
      a += '>'+i+'</option>\n';
   }
   a += '</select>';

   return a;
}
function xtdPage() 
{               
   var o1=$('#xtdpgNo').get(0);
   var o2=$('#xtdpgSel').get(0);
          
   if (o2.style.display=='none') {
      if ($(o2).html()=='') {
         $(o2).html( makePgSel() );
      }
   }
   $(o1).toggle();
   $(o2).toggle();
}

function bodyUnloading()
{
   document.body.disabled=true;
   save_awcols();
}
function afterGResize(i)
{    
   fgrT=null;
   var ogrid=gridID2[i];
   ogrid.setSizes();
}
function forGResize()
{     
   var n=gridID2.length;
   var ogrid;
   
   for (var i=0;i<n;i++) {
      ogrid=gridID2[i];

      var odiv=document.getElementById(ogrid._iid+'_div');
      if (odiv==null)
         continue;
         
      if (fgrT!=null) {
         window.clearTimeout(fgrT);
      }
      
      // Set width
      var pobj=$(odiv).parent('form:not').get(0);
      if (!pobj || (pobj.tagName && pobj.tagName=='BODY')) {
         $(odiv).width( $(window).width() - 4 );
      }
      else {
         $(odiv).width( $(pobj).innerWidth() - 4 );
      }
      
      // Set height
      var wh=$(window).height();
      
      if (document.getElementById('tbar2')) {
         var delta = $(odiv).outerHeight() - $(odiv).height(); 
         var h1 = wh - $(odiv).offset().top - $('#tbar2').outerHeight() - delta - 1;
      }
      else {
         var bmargin = ogrid._ibottom_margin+5;
	      var h1 = wh - $(odiv).offset().top - bmargin;
      }
      
      if (h1<200) { h1=200; }
      $(odiv).height( h1 );

      // force grid redraw
      fgrT=window.setTimeout( 'afterGResize('+i+')', 100);
   }
}function save_awcols()
{
   var n=gridID2.length;
   for (var i=0;i<n;i++) {
      do_save_awcols2(i);
   }
}
function do_save_awcols2(ii)
{
   var idg=gridID2[ii];
   var n=idg.getColumnsNum();
   if (n==0)
    	return;

   var cw=new Array();
   for (i=0;i<n;i++) {
      cw.push(idg.getColWidth(i));
   }

   var val;
   var s1=pathnameEx().split('.');
   n=s1.length;
   if (n>=2) {
      s1.pop();
      val=s1.join('.');
   }
   else {
      val=pathnameEx();
   }
	var val2 = val+'.'+idg._iid+':'+cw.join(':')
   var ckName='aw-cols['+ii+']';
   var ck=ckName + '=' + escape(val2);

   // Add new cookie
   document.cookie=ck;
}
function pathnameEx()
{
	val=location.pathname;
	// Correct HTA bug
   if (val.substr(0,1)!='/')
   	val = '/'+val;

   return val;
}
function doResetPgSize(fx) {
   var f=document.goForm;
	if (f==null) {
      f=document.createElement('FORM');
      f.name='goForm';
      f.method='POST';
      f.action=window.location.pathname;
      document.body.appendChild(f);
   }
   
   if (fx>0) {
      var o2=f.FUNCTION;
      if (o2==null) {
         o2=document.createElement('INPUT');
         o2.type='HIDDEN';
         o2.name='FUNCTION';
         f.appendChild(o2);
      }
      o2.value=fx;
   }
   
   var newsz=parseInt(document.getElementById('pgsize').value);
   if (isNaN(newsz))
      return;
      
   var d1 = new Date();
   d1.setYear( d1.getFullYear()+1 ); // increase by 1 year
   document.cookie='ADOPAGESIZE='+newsz+';expires='+d1.toGMTString()+';';
   document.body.onbeforeunload=bodyUnloading;
   f.submit();
}
function goResetPgSize(fx,oldsz) {
   if (oRPSdiv==null) {
		oRPSdiv=document.createElement('DIV'); 
		oRPSdiv.className='POPMENU'; 
		$(oRPSdiv).html( '<table cellspacing=0>' +
		   '<tr><td colspan=2 class=bgHeader>Reset page size</td></tr>' +
			'<tr><td class=bgLabel>New value (minimum 10)</td>' +
         '<td class=bgInput><input type=TEXT id=pgsize value="'+oldsz+'" size=5></td></tr>' +
			'<tr><td></td>' +
         '<td><button type=button onclick="doResetPgSize(' + fx + ')">OK</button></td></tr>' +
			'</table>' );
		document.body.appendChild(oRPSdiv);
		$(oRPSdiv).keypress( function(e){ 
         var code = (e.keyCode ? e.keyCode : e.which);
         if (code==27)
            hide_popup(); 
      } );
	}
	show_popup(oRPSdiv);
	$('#pgsize').focus();
}
function show_popup(o1)
{
   if (typeof o1=='string')
      o1=document.getElementById(o1);
      
   // If 'nothing' so far, return
   if (o1==null)
      return;

   if (oPopup!=null) {
      oPopup.style.display='none';
   }
   oPopup=o1;

   // We need first apply display=inline, so that we can then measure dimensions.
	o1.style.display='inline';

	var x=gmouse.x + document.body.scrollLeft - 16;
	var oWidth=o1.clientLeft * 2 + o1.clientWidth;
	if (x+oWidth > document.body.clientWidth + document.body.scrollLeft) {
		x=document.body.clientWidth + document.body.scrollLeft - oWidth;
		if (x<0) x=0;
	}
	o1.style.left=x+'px';

	var y=gmouse.y + document.body.scrollTop - 16;
	var oHeight=o1.clientTop * 2 + o1.clientHeight;
	if (y+oHeight > document.body.clientHeight + document.body.scrollTop) {
		y=document.body.clientHeight + document.body.scrollTop - oHeight;
		if (y<0) y=0;
	}
	o1.style.top=y+'px';
	
	if (window.addEventListener) { // Mozilla, Netscape, Firefox
      window.setTimeout('document.addEventListener("click", forPopClick, false); document.addEventListener("keydown", forPopClick, false)', 200);
	}
	else if (document.attachEvent) { // IE
      window.setTimeout('document.attachEvent("onclick",hidePopupIE); document.attachEvent("onkeydown",hidePopupIE)', 200);
   }
}
function forPopClick(evt)
{
   doHidePopup(evt);
}
function hidePopupIE()
{
   doHidePopup(event);
}
function doHidePopup(evt)
{
   if (evt==null || oPopup==null)
      return;

   if (evt.type=='keydown' && evt.keyCode!=27) {
      return;
   }

   if (evt.type=='click') {
      var cx = evt.clientX + document.body.scrollLeft;
      var cy = evt.clientY + document.body.scrollTop;
      if (cx>=oPopup.offsetLeft && cx<=(oPopup.offsetLeft+oPopup.offsetWidth)
         && cy>=oPopup.offsetTop && cy<=(oPopup.offsetTop+oPopup.offsetHeight))
      {
         return;
      }
   }

   oPopup.style.display='none';
   if (window.removeEventListener) { // Mozilla, Netscape, Firefox
      document.removeEventListener("click", forPopClick, false);
      document.removeEventListener("keydown", forPopClick, false);
   }
   else if (document.detachEvent) { // IE
      document.detachEvent("onclick",hidePopupIE);
      document.detachEvent("onkeydown",hidePopupIE);
   }
}
function hide_popup()
{
   if (oPopup==null)
      return;

   //oPopup.style.visibility='hidden';
   oPopup.style.display='none';
   if (window.removeEventListener) { // Mozilla, Netscape, Firefox
      document.removeEventListener("click", forPopClick, false);
   }
   else if (document.detachEvent) { // IE
      document.detachEvent("onclick",hidePopupIE);
   }
}


