var mouseX     = 100;
var mouseY     = 100;
var mousePageX = 100;
var mousePageY = 100;
var scrollShiftX = 0;
var scrollShiftY = 0;

var lt = 'lt.gif'; // left-top
var ct = 'ct.gif'; // center-top
var rt = 'rt.gif'; // right-top
var lc = 'lc.gif'; // left-center
var rc = 'rc.gif'; // right-center
var lb = 'lb.gif'; // left-bottom
var cb = 'cb.gif'; // center-bottom
var rb = 'rb.gif'; // right-bottom


/**
 *
 */
if ( document.getElementById && ! document.all )
{
  document.onmousemove = mouseMoveHandler;
  if ( document.addEventListener )
  {
    document.addEventListener ( "onmousemove", mouseMoveHandler, false );
  }
}


/**
 *
 */
function mouseMoveHandler ( e )
{
  if ( e != null )
  {
    mouseX = e.screenX;
    mouseY = e.screenY;
    mousePageX = e.pageX;
    mousePageY = e.pageY;
  }
}


/**
 *
 */
function initScrollShift ( )
{
  if ( document.body && document.body.scrollLeft )
  {
    scrollShiftX = document.body.scrollLeft;
    scrollShiftY = document.body.scrollTop;
  }
}


/**
 * Constructor and definition of the class "Point"
 */
function Point ( pointX, pointY )
{
  var x;
  var y;
  this.x = pointX;
  this.y = pointY;
}


/**
 *
 */
function getClientWidth ( )
{
  if ( window.screen && window.screen.availWidth ) return window.screen.availWidth;
  if ( document.body.clientWidth ) return document.body.clientWidth;
  return 0;
}


/**
 *
 */
function getClientHeight ( )
{
  if ( window.screen && window.screen.availHeight ) return window.screen.availHeight;
  if ( document.body.clientHeight ) return document.body.clientHeight;
  return 0;
}


/**
 *
 */
function getScrollX ( )
{
  if ( document.body.scrollLeft ) return document.body.scrollLeft;
  if ( document.body.offsetLeft ) return document.body.offsetLeft;
  if ( document.documentElement ) return document.documentElement.scrollLeft;
  if ( window.pageXOffset       ) return window.pageXOffset;
  return 0;
}


/**
 *
 */
function getScrollY ( )
{
  if ( document.body.scrollTop  ) return document.body.scrollTop;
  if ( document.body.offsetTop  ) return document.body.offsetTop;
  if ( document.documentElement ) return document.documentElement.scrollTop;
  if ( window.pageYOffset       ) return window.pageYOffset;
  return 0;
}


/**
 *
 */
function getClickedPoint ( )
{
  var x = 0;
  var y = 0;
  if ( window.event )
  {
    //x = window.event.clientX + this.getScrollX ( );
    //y = window.event.clientY + this.getScrollY ( );
    x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft - document.body.clientLeft - scrollShiftX;
    y = window.event.clientY + document.documentElement.scrollTop  + document.body.scrollTop  - document.body.clientTop  - scrollShiftY;
  }
  else
  {
    x = mousePageX; //- this.getScrollX ( );
    y = mousePageY; //- this.getScrollY ( );
  }
  return new Point ( x + 3, y + 3 );
}


/**
 *
 */
function getPopupOptions ( x, y, w, h )
{
  var options = 'resizable=yes,scrollbars=no,toolbar=no,location=no,' +
                'directories=no,status=no,menubar=no,copyhistory=no,' +
                'dependent=yes,titlebar=no,alwaysRaised=yes,statusbar=no,title=no';

  options += ',width='  + w;
  options += ',height=' + h;

  if ( x == 0 && y == 0 )
  {
    x = this.mouseX;
    y = this.mouseY;
    if ( window.event )
    {
      x = window.event.clientX;
      y = window.event.clientY;
    }
  }
  options += ',screenX=' + x + ',left=' + x;
  options += ',screenY=' + y + ',top='  + y;

  return options;
}


/**
 *
 */
function showPopup ( winId, url, w, h )
{
  var options = this.getPopupOptions ( 0, 0, w, h );

  var winHandler = window.open ( url, winId, options );
  if ( winHandler.opener == null ) winHandler.opener = self;

  winHandler.blur ( );
  if ( window.focus ) winHandler.focus ( );

  return winHandler;
}


/**
 *
 */
function showFullscreenPopup ( winId, url )
{
  var options = 'resizable=yes,scrollbars=yes,toolbar=no,location=no,' +
                'directories=no,status=no,menubar=no,copyhistory=no,' +
                'dependent=yes,titlebar=no,alwaysRaised=yes,statusbar=no,title=no';
  options += ',width=0,height=0';
  options += ',screenX=0,left=0,screenY=0,top=0';

  var winHandler = window.open ( url, winId, options );
  if ( winHandler.opener == null ) winHandler.opener = self;

  winHandler.blur ( );
  if ( window.focus ) winHandler.focus ( );

  return winHandler;
}


/**
 *
 */
function showPopupHtml ( winId, html, w, h )
{
  var popupX = ( this.getClientWidth  ( ) - w ) / 2;
  var popupY = ( this.getClientHeight ( ) - h ) / 2;
  var options = this.getPopupOptions ( popupX, popupY, w, h );

  var winHandler = window.open ( "", winId, options );
  if ( winHandler.opener == null ) winHandler.opener = self;

  winHandler.document.open ( );
  winHandler.document.write ( html );
  winHandler.document.close ( );
  winHandler.blur ( );
  if ( window.focus ) winHandler.focus ( );

  return winHandler;
}


/**
 *
 */
function showTextEditorPopup ( data, action, callback )
{
  var html = '';
  html += '<html><head><body bgcolor="#F0F0FF" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0"><center>';
  html += '<form id="editor" name="editor" action="' + action + '">';
  html += '<input type="text" class="text" id="edit" name="edit" style="border:1px solid #000080; background:#FFFFF0; width:400px; height:20px;"/>';
  html += '<br/><input type="submit" onClick="' + callback + '; window.close ( );" value="OK"></input>';
  html += '</form></center></body></html>';

  var winHandler = this.showPopupHtml ( "TextEdit", html, 420, 40 );

  var form = winHandler.document.getElementById ( "edit" );
  if ( form ) form.value = data;

  winHandler.blur ( );
  if ( window.focus ) winHandler.focus ( );

  return winHandler;
}


/**
 *
 */
function showTextAreaEditorPopup ( data, action, callback )
{
  var html = '';
  html += '<html><head><body bgcolor="#F0F0FF" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0"><center>';
  html += '<form id="editor" name="editor" action="' + action + '">';
  html += '<textarea id="edit" name="edit" style="background:#FFFFF0; width:400px; height:200px;"></textarea>';
  html += '<br/><input type="submit" onClick="' + callback + '; window.close ( );" value="OK"></input>';
  html += '</form></center></body></html>';

  var winHandler = this.showPopupHtml ( "TextEdit", html, 410, 240 );

  var form = winHandler.document.getElementById ( "edit" );
  if ( form ) form.value = data;

  winHandler.blur ( );
  if ( window.focus ) winHandler.focus ( );

  return winHandler;
}


/**
 *
 */
function showImageBrowsePopup ( action, callback )
{
  var html = '';
  html += '<html><head><body bgcolor="#F0F0FF" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0"><center>';
  html += '<form id="editor" name="editor" enctype="multipart/form-data" method="post" action="' + action + '">';
  html += '<table width="100%" cellpadding="2" cellspacing="3" border="0"><tr>';
  html += '<td valign="top"><input id="imageFile" name="imageFile" style="height:22px;" type="file" accept="image/gif" size="40"/></td>';
  html += '<td valign="top"><input type="submit" style="height:22px;" onClick="' + callback + '; window.close ( );" value="OK"/></td>';
  html += '</tr></table>';
  html += '<input type="hidden" name="MAX_FILE_SIZE" value="200000"/>';
  html += '</form></center></body></html>';

  var winHandler = this.showPopupHtml ( "ImageBrowse", html, 400, 35 );
  winHandler.blur ( );
  if ( window.focus ) winHandler.focus ( );

  return winHandler;
}


/**
 * Can be used as showPopupDiv ( divId )
 * Also can be used as showPopupDiv ( divId, offsetLeft, offsetTop )
 */
function showPopupDiv ( )
{
  var argv  = showPopupDiv.arguments;
  var divId = argv [ 0 ];
  var point = this.getClickedPoint ( );
  if ( argv.length == 3 )
  {
    point.x = argv [ 1 ] + 5;
    point.y = argv [ 2 ] + 5;
  }

  var dir;
  var pos;
  var popupDiv = document.getElementById ( divId );
  var divWidth = 0;

  if ( popupDiv )
  {
    if ( popupDiv.style.visibility == "visible" )
    {
      popupDiv.style.visibility = "hidden";
    }
    else
    {
      popupDiv.style.position = "absolute";
      dir   = document.body.dir.toLowerCase ( );
      divWidth = popupDiv.style.width;
      if ( dir != 'rtl' )
      {
        var pos = divWidth.indexOf ( 'px' );
        if ( pos > 0 ) divWidth = divWidth.substring ( 0, pos );
        if ( divWidth > point.x ) divWidth = point.x;
        point.x = point.x - divWidth;
      }
      var cWidth;
      if ( document.body.clientWidth )
      {
        cWidth = document.body.clientWidth;
        //alert ( "cWidth = " + cWidth + "; divWidth = " + divWidth + "; x = " + point.x );
        var bWidth = cWidth - divWidth;
        if ( point.x > bWidth ) point.x = bWidth;
      }
      if ( point.x < 0 ) point.x = 0;
      popupDiv.style.left = point.x;
      popupDiv.style.top  = point.y + 3;
      popupDiv.style.visibility = "visible";
    }
  }
  else
  {
    alert ( "Popup " + divId + " is unavailable" );
  }
}


/**
 *
 */
function setDirection ( )
{
  var dir = document.body.dir;
  if ( dir == 'rtl' || dir == 'RTL' )
  {
    this.lt = 'rt.gif';
    this.ct = 'ct.gif';
    this.rt = 'lt.gif';
    this.lc = 'rc.gif';
    this.rc = 'lc.gif';
    this.lb = 'rb.gif';
    this.cb = 'cb.gif';
    this.rb = 'lb.gif';
  }
  else
  {
    this.lt = 'lt.gif';
    this.ct = 'ct.gif';
    this.rt = 'rt.gif';
    this.lc = 'lc.gif';
    this.rc = 'rc.gif';
    this.lb = 'lb.gif';
    this.cb = 'cb.gif';
    this.rb = 'rb.gif';
  }
}

/**
 *
 */
function roundTableHeader ( base )
{
  this.setDirection ( );
  if ( base == null ) base = '';
  base += 'images/table/';
  document.write (
  '<table cellpadding="0" cellspacing="0" width="100%" bgcolor="#FFFFFF">' +
  '<tr>' +
  '<td valign="top" style="width:11px; height:11px;"><img height="11" src="' + base + this.lt + '" width="11"  ></img></td>' +
  '<td valign="top" style="width:100%; height:11px;"><img height="11" src="' + base + this.ct + '" width="100%"></img></td>' +
  '<td valign="top" style="width:11px; height:11px;"><img height="11" src="' + base + this.rt + '" width="11"  ></img></td>' +
  '</tr>' +
  '<tr>' +
  '<td valign="top" style="width:11px;" background="' + base + this.lc + '">&nbsp;</td>' +
  '<td valign="top">\r\n' );
}


/**
 *
 */
function roundTableFooter ( base )
{
  this.setDirection ( );
  if ( base == null ) base = '';
  base += 'images/table/';
  document.write (
  '</td>' +
  '<td valign="top" style="width:11px;" background="' + base + this.rc + '">&nbsp;</td>' +
  '</tr>' +
  '<tr style="height:11px;">' +
  '<td valign="top" style="width:11px; height:11px;"><img height="11" src="' + base + this.lb + '" width="11"  ></img></td>' +
  '<td valign="top" style="width:100%; height:11px;"><img height="11" src="' + base + this.cb + '" width="100%"></img></td>' +
  '<td valign="top" style="width:11px; height:11px;"><img height="11" src="' + base + this.rb + '" width="11"  ></img></td>' +
  '</tr>' +
  '</table>\r\n' );
}


/**
 * Duplicate keys are not allowed and are ignored
 */
function addKeyValue ( keys, values, key, value )
{
  var count = keys.length;
  for ( var k = 0; k < count; k ++ )
  {
    if ( keys [ k ] == key ) return;
  }
  keys   [ count ] = key;
  values [ count ] = value;
}


/**
 * Adds keys and values from url, returns new url without parameters
 */
function addUrlKeyValues ( keys, values, url )
{
  var parms = null;
  var pos = url.indexOf ( "?" );
  if ( pos > 0 )
  {
    var parmsText = url.substring ( pos + 1 );
    url = url.substring ( 0, pos );
    if ( parmsText.indexOf ( "&" ) > 0 )
    {
      parms = parmsText.split ( "&" );
    }
    else
    {
      parms = new Array ( );
      parms [ 0 ] = parmsText;
    }
    for ( var k = 0; k < parms.length; k ++ )
    {
      pos = parms [ k ].indexOf ( "=" );
      addKeyValue ( keys, values, parms [ k ].substring ( 0, pos ), parms [ k ].substring ( pos + 1 ) );
    }
  }
  return url;
}


/**
 *
 */
function getActionFormHtml ( action, target, keys, values )
{
  var html = "";

  html += '<html>';
  html += '<body>';
  html += '<form id="actionForm" name="actionForm" method="post"';
  if ( target != null && target.length > 0 ) html += ' target="' + target + '"';
  html += ' action="' + action + '">';

  if ( keys != null && values != null )
  {
    var count = keys.length;
    if ( values.length < count ) count = values.length;
    for ( var k = 0; k < count; k ++ )
    {
      html += '<input type="hidden" id="' + keys [ k ] + '" name="' + keys [ k ] + '" value="' + values [ k ] + '" />';
    }
  }
  html += '</form></body></html>';
  return html;
}


/**
 *
 */
function getIFrame ( frame )
{
  var iFrameObj = null;
  if ( document.frames )
  {
    iFrameObj = document.frames [ frame ];
  }
  else if ( window.frames )
  {
    iFrameObj = window.frames [ frame ];
  }
  else if ( document.all )
  {
    iFrameObj = document.all ( frame );
  }
  else
  {
    iFrameObj = document.getElementById ( frame );
  }
  if ( iFrameObj == null )
  {
    alert ( "No frame object for '" + frame + "' frame" );
  }
  return iFrameObj;
}


/**
 *
 */
function getIFrameDocument ( frame )
{
  var iFrameDoc = null;
  var iFrameObj = getIFrame ( frame );
  if ( iFrameObj )
  {
    if ( iFrameObj.contentDocument )    // For NS6
    {
      iFrameDoc = iFrameObj.contentDocument;
    }
    else if ( iFrameObj.contentWindow )    // For IE5.5 and IE6
    {
      iFrameDoc = iFrameObj.contentWindow.document;
    }
    else if ( iFrameObj.document )    // For IE5
    {
      iFrameDoc = iFrameObj.document;
    }
    if ( iFrameDoc == null )
    {
      alert ( "No document object for '" + frame + "' frame" );
    }
    return iFrameDoc;
  }
}


/**
 *
 */
function setIFrameContent ( frame, html )
{
  var iFrameDoc = getIFrameDocument ( frame );
  if ( iFrameDoc != null )
  {
    iFrameDoc.open ( );
    iFrameDoc.write ( html );
    iFrameDoc.close ( );
  }
}


/**
 *
 */
function submitActionForm ( html, actionFrame )
{
  if ( actionFrame && html != null && html.length > 0 )
  {
    cDoc = actionFrame.document;
    cDoc.open ( );
    cDoc.write ( html );
    cDoc.close ( );
    cDoc.forms [ "actionForm" ].submit ( );
  }
}


/**
 *
 */
function getElementValue ( id )
{
  var element = null;
  if ( document.getElementById )
  {
    element = document.getElementById ( id );
  }
  else if ( document.all )
  {
    element = document.all ( id );
  }
  if ( ! element )
  {
    alert ( "getElementValue ( '" + id + "' ): No such element" );
    return null;
  }
  return element.value;
}


/**
 *
 */
function setElementValue ( id, value )
{
  var element = null;
  if ( document.getElementById )
  {
    element = document.getElementById ( id );
  }
  else if ( document.all )
  {
    element = document.all ( id );
  }
  if ( element ) element.value = value;
}


/**
 *
 */
function checkDigits ( id )
{
  var value = getElementValue ( id );
  if ( value == null ) return false;
  if ( value.length == 0 ) return false;
  for ( var k = 0; k < value.length; k ++ )
  {
    var c = value.charAt ( k );
    if ( '0123456789'.indexOf ( c ) < 0 )
    {
      return false;
    }
  }
  return true;
}


/**
 *
 */
function checkPhone ( id )
{
  var value = getElementValue ( id );
  if ( value == null ) return false;
  if ( value.length > 0 && value.length < 4 ) return false;
  for ( var k = 0; k < value.length; k ++ )
  {
    var c = value.charAt ( k );
    if ( '0123456789()-/+, '.indexOf ( c ) < 0 )
    {
      return false;
    }
  }
  return true;
}


/**
 *
 */
function checkName ( id )
{
  var value = getElementValue ( id );
  if ( value == null ) return false;
  //if ( value.length == 0 ) return false;
  for ( var k = 0; k < value.length; k ++ )
  {
    var c = value.charAt ( k );
    if ( 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'.indexOf ( c ) < 0 )
    {
      return false;
    }
  }
  return true;
}


/**
 *
 */
function checkRequired ( id )
{
  var value = getElementValue ( id );
  if ( value == null ) return false;
  return ( value.length > 0 );
}


/**
 *
 */
function checkIsraelIdNumber ( id )
{
  var fullIdNumber = getElementValue ( id );
  if ( fullIdNumber == null ) return true;

  var idLength = fullIdNumber.length;
  if ( idLength == 0 ) return true;
  if ( idLength > 9 ) return false;

  var tempDigit;
  var tempDigitSum = 0;
  var checkDigit = fullIdNumber.substr ( idLength - 1, 1 );

  for ( var k = 0; k < ( 9 - idLength ); k ++ )
  {
    fullIdNumber = '0' + fullIdNumber;
  }

  var idNumber = fullIdNumber.substr ( 0, 8 );
  for ( var k = 0; k < 8; k ++ )
  {
    var m = parseInt ( idNumber.substr ( k, 1 ) );
    if ( k % 2 != 0 )
    {
      tempDigit = m * 2;
      if ( tempDigit > 9 ) tempDigit = 1 + tempDigit % 10;
    }
    else
    {
      tempDigit = m;
    }
    tempDigitSum = tempDigitSum + tempDigit;
  }
  tempDigit = tempDigitSum % 10;
  if ( tempDigit != 0 ) tempDigit = 10 - tempDigit;
  return ( checkDigit == tempDigit );
}


/**
 *
 */
function validateEmail ( email )
{
  if ( email == null || email.length == 0 ) return false;
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  return ( ! reg1.test ( email ) && reg2.test ( email ) );
}


/**
 *
 */
function checkEmail ( id )
{
  return this.validateEmail ( getElementValue ( id ) );
}


/**
 *
 */
function makeEmail ( )
{
  var argv = makeEmail.arguments;
  var argc = argv.length;
  var email = "";
  for ( var i = 0; i < argc; i++ )
  {
    email += argv [ i ];
  }
  if ( email.indexOf ( '@' ) < 0 ) email += '@' + 'main' + 'trend' + '.' + 'net';
  document.write ( "<a class=\"email\" style=\"font:8pt Arial;\" href=\"mailto:" + email + "\">" + email + "</a>" );
}


/**
 *
 */
function getDocumentElementsByTag ( doc, tag )
{
  var elements = null;
  if ( ! doc ) return elements;
  if ( doc.getElementsByTagName )
  {
    var elements = doc.getElementsByTagName ( tag );
  }
  else if ( doc.all )
  {
    var elements = doc.all.tags ( tag );
  }
  return elements;
}


/**
 *
 */
function getElementsByTag ( tag )
{
  return getDocumentElementsByTag ( document, tag );
}

