// Modal Dialog Box
// copyright 8th July 2006 by Stephen Chapman
// http://javascript.about.com/
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration

function encodeUrl( text )
{
  text = text.replace( /0x/g, "0x00" );
  
  text = text.replace( /\%/g, "0x25" );
  text = text.replace( /\$/g, "0x24" );
  text = text.replace( /\&/g, "0x26" );
  text = text.replace( /\+/g, "0x2B" );
  text = text.replace( /\,/g, "0x2C" );
  text = text.replace( /\//g, "0x2F" );
  text = text.replace( /\:/g, "0x3A" );
  text = text.replace( /\;/g, "0x3B" );
  text = text.replace( /\=/g, "0x3D" );
  text = text.replace( /\?/g, "0x3F" );
  text = text.replace( /\ /g, "0x20" );
  text = text.replace( /\"/g, "0x22" );
  text = text.replace( /\'/g, "0x27" );
  text = text.replace( /\</g, "0x3C" );
  text = text.replace( /\>/g, "0x3E" );
  text = text.replace( /\#/g, "0x23" );
  text = text.replace( /\{/g, "0x7B" );
  text = text.replace( /\}/g, "0x7D" );
  text = text.replace( /\|/g, "0x7C" );
  text = text.replace( /\\/g, "0x5C" );
  text = text.replace( /\^/g, "0x5E" );
  text = text.replace( /\~/g, "0x7E" );
  text = text.replace( /\[/g, "0x5B" );
  text = text.replace( /\]/g, "0x5D" );
  text = text.replace( /\`/g, "0x60" );
  return( text );
}

function decodeUrl( text )
{
  text = text.replace( /0x25/g, "%" );
  text = text.replace( /0x24/g, "$" );
  text = text.replace( /0x26/g, "&" );
  text = text.replace( /0x2B/g, "+" );
  text = text.replace( /0x2C/g, "," );
  text = text.replace( /0x2F/g, "/" );
  text = text.replace( /0x3A/g, ":" );
  text = text.replace( /0x3B/g, ";" );
  text = text.replace( /0x3D/g, "=" );
  text = text.replace( /0x3F/g, "?" );
  text = text.replace( /0x20/g, " " );
  text = text.replace( /0x22/g, "\"" );
  text = text.replace( /0x27/g, "'" );
  text = text.replace( /0x3C/g, "<" );
  text = text.replace( /0x3E/g, ">" );
  text = text.replace( /0x23/g, "#" );
  text = text.replace( /0x7B/g, "{" );
  text = text.replace( /0x7D/g, "}" );
  text = text.replace( /0x7C/g, "|" );
  text = text.replace( /0x5C/g, "\\" );
  text = text.replace( /0x5E/g, "^" );
  text = text.replace( /0x7E/g, "~" );
  text = text.replace( /0x5B/g, "[" );
  text = text.replace( /0x5D/g, "]" );
  text = text.replace( /0x60/g, "`" );

  text = text.replace( /0x00/g, "0x" );
  return( text );
}

function makeInputUrlSafe()
{
  var value;
  tag=document.getElementsByTagName('input');
  for(i=tag.length-1;i>=0;i--)
  {  
    if ( tag[i].type != "submit" )
    {
      value = encodeUrl( tag[i].value );
      if ( value != tag[i].value )
      {
        // alert( tag[i].name + " value: " + tag[i].value + " -> " + value + " nodename: " + tag[i].nodeName + " type: " + tag[i].type );
        tag[i].value = value;
      }
    }
  }

}

function pageWidth() 
{
  return window.innerWidth != null? window.innerWidth: document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth:document.body != null? document.body.clientWidth:null;
}

function pageHeight() 
{
  return window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
}

function posLeft() 
{
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset:document.documentElement && document.documentElement.scrollLeft? document.documentElement.scrollLeft:document.body.scrollLeft? document.body.scrollLeft:0;
}

function posTop() 
{
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;
}

function $(x)
{
  return document.getElementById(x);
}

function scrollFix()
{
  var obol=$('overlay');
  obol.style.top=posTop()+'px';
  obol.style.left=posLeft()+'px'
}

function sizeFix()
{
  var obol=$('overlay');
  obol.style.height=pageHeight()+'px';
  obol.style.width=pageWidth()+'px';
}

function kp(e)
{
  ky=e?e.which:event.keyCode;
  if(ky==88||ky==120)hideDialogBox();
  return false
}

function parentDiv( elem )
{
  if ( elem.nodeName == "DIV" )
  {
    return( elem );
  }
  if ( elem.parentNode == null )
  {
    return( null );
  }
  return( parentDiv(elem.parentNode) );
}

function inf(h,obl)
{
  var div;
  
  tag=document.getElementsByTagName('select');
  for(i=tag.length-1;i>=0;i--)
  {
    div = parentDiv( tag[i] );
    if ( div.id == obl )
    {
      if ( h == 'visible' )
      {
        tag[i].style.visibility='hidden';
      }
      else
      {
        tag[i].style.visibility='visible';
      }
    }
    else
    {
      tag[i].style.visibility=h;
    }
  }
  
  tag=document.getElementsByTagName('iframe');
  for(i=tag.length-1;i>=0;i--)
  {
    tag[i].style.visibility=h;
  }
  
  tag=document.getElementsByTagName('object');
  for(i=tag.length-1;i>=0;i--)
  {  
    tag[i].style.visibility=h;
  }
}

function showDialogBox(obl)
{
  var h='hidden';
  var b='block';
  var p='px';

  var obol=$('overlay'); 
  obol.style.height=pageHeight()+p;
  obol.style.width=pageWidth()+p;
  obol.style.top=posTop()+p;
  obol.style.left=posLeft()+p;
  obol.style.display=b;

  var obbx = $(obl);
  var tp=posTop()+((pageHeight()-parseFloat(obbx.style.height))/2)-12;
  var lt=posLeft()+((pageWidth()-parseFloat(obbx.style.width))/2)-12;  

  if ( obl != 'dialog_addpic' && obl != 'dialog_link' && obl != 'dialog_newgroup' && obl != 'dialog_addgroup')
  {
    obbx.style.top=(tp<0?0:tp)+p;
    obbx.style.left=(lt<0?0:lt)+p;
    if ( obl == 'dialog_multiselect' )
    {
      obbx.style.top = (parseFloat(obbx.style.top) + 50) + p;
      obbx.style.left = (parseFloat(obbx.style.left) + 50) + p;
    }
  }
  obbx.style.display=b;

  inf(h,obl);

  $(obl).style.display=b;
}

function comparison2str( comparison, value )
{
  if ( comparison == "=" )
  {
    return( "equals" );
  }
  if ( comparison == "<>" )
  {
    return( "does not equal" );
  }
  if ( comparison == ">" )
  {
    return( "is greater than" );
  }
  if ( comparison == ">=" )
  {
    return( "is greater than or equal to" );
  }
  if ( comparison == "<" )
  {
    return( "is less than" );
  }
  if ( comparison == "<=" )
  {
    return( "is less than or equal to" );
  }
  if ( comparison == "like" )
  {
    var ex = /^\%[^\%]*\%$/;
    
    if ( ex.exec(value) )
    {
      return( "contains" );
    }
    ex = /^[^\%]*\%$/;    
    if ( ex.exec(value) )
    {
      return( "begins with" );
    }
    ex = /^\%[^\%]*$/;    
    if ( ex.exec(value) )
    {
      return( "ends with" );
    }
    alert( "Unknown comparison: " + comparison + " with value '" + value + "'" );
  }
  if ( comparison == "not like" )
  {
    var ex = /^\%[^\%]*\%$/;
    
    if ( ex.exec(value) )
    {
      return( "does not contain" );
    }
    ex = /^[^\%]*\%$/;    
    if ( ex.exec(value) )
    {
      return( "does not begin with" );
    }
    ex = /^\%[^\%]*$/;    
    if ( ex.exec(value) )
    {
      return( "does not end with" );
    }
    alert( "Unknown comparison: " + comparison + " with value '" + value + "'" );
  }
  alert( "Unknown comparison: " + comparison );
}

function stripPerc( text )
{
  var ex = /^[\%]*([^\%]*)[\%]*$/;  
  var result = ex.exec(text);
  if ( result == null )
  {
    return( text );
  }
  return( result[1] );
}

function filterComboName( fieldname )
{
  return( fieldname.toLowerCase().replace(/ /g, "_") + '_custom' );
}

function initDialogBox(obl, name, fieldname)
{
  var value = $(name).value;
  
  var customValue = decodeUrl( $(filterComboName(fieldname)).value );

  var listbox = $(name);
  var comparison1 = $('dialog_custom_comparison1');
  var value1 = $('dialog_custom_value1');
  var comparison2 = $('dialog_custom_comparison2');
  var value2 = $('dialog_custom_value2');
  var radio1 = $('dialog_custom_radio1');
  var radio2 = $('dialog_custom_radio2');

  var i;

  $('dialog_custom_fieldname').innerHTML = fieldname;
  $('dialog_custom_field').value = name;
  
  for(i=0;i<listbox.length;i++)
  {
    if ( listbox.options[i].value == customValue )
    {
      customValue = "";
    }
  } 
  var exComparison = "(=|\<\>|\<|\>|\<=|\>=|like|not like)";
  var exField = "([a-zA-Z0-9_]+)";
  var exValue = "\\'(.*)\\'";
  var exWhiteSpace = " ";
  var exCondition = exField + exWhiteSpace + exComparison + exWhiteSpace + exValue;

  var ex = new RegExp( exCondition + exWhiteSpace + "and" + exWhiteSpace + exCondition);
  var result = ex.exec( customValue ); 
  if ( result == null )
  {
    ex = new RegExp( exCondition + exWhiteSpace + "or" + exWhiteSpace + exCondition);
    result = ex.exec( customValue ); 
    if ( result == null )
    {
      ex = new RegExp( exCondition );
      result = ex.exec( customValue ); 
        
      if ( result == null )
      {
        comparison1.value = "";
        value1.value = "";
        radio1.checked = false;
        comparison2.value = "";
        value2.value = "";
        radio2.checked = false;
      }
      else
      {
        radio1.checked = false;
        radio2.checked = false;
        comparison1.value = comparison2str( result[2], result[3] );
        value1.value = stripPerc( result[3] );
        comparison2.value = "";
        value2.value =  "";
      }
    }
    else
    {
      radio1.checked = false;
      radio2.checked = true;
      comparison1.value = comparison2str( result[2], result[3] );
      value1.value = stripPerc( result[3] );
      comparison2.value = comparison2str( result[5], result[6] );
      value2.value = stripPerc( result[6] );
    }
  }
  else
  {
    radio2.checked = false;
    radio1.checked = true;
    comparison1.value = comparison2str( result[2], result[3] );
    value1.value = stripPerc( result[3] );
    comparison2.value = comparison2str( result[5], result[6] );
    value2.value = stripPerc( result[6] );
  }
  customComparisonChanged( comparison1 );
  customComparisonChanged( comparison2 );
}

function checkCustomValue( field, value )
{
  var exp = new RegExp( '\%|\'|\\\?|\\\$|\\\\|\\\"' );
  var result = exp.exec( value ); 
  if ( result != null )
  {
    return( 0 );
  }
  return( 1 );
}

function checkCondition( field, comparison, value )
{
  if( comparison == "" )
  {
    return( 1 );
  }
  if ( ! checkCustomValue( field, value ) )
  {
    return( 0 );
  }
  if ( comparison != "equals" && comparison != "does not equal" )
  {
    var exp = new RegExp( "\," );
    var result = exp.exec( value ); 
    if ( result != null )
    {
      return( 0 );
    }
  }
  return( 1 );
}

function conditionSql( field, comparison, value )
{
  var sql;
  
  if( comparison == "" )
  {
    return( "" );
  }
  sql = field;
  if( comparison == "equals" )
  {
      sql += " = '" + value + "'";
  }
  else if( comparison == "does not equal" )
  {
      sql += " <> '" + value + "'";
  }
  else if( comparison == "is greater than" )
  {
      sql += " > '" + value + "'";
  }
  else if( comparison == "is greater than or equal to" )
  {
      sql += " >= '" + value + "'";
  }
  else if( comparison == "is less than" )
  {
      sql += " < '" + value + "'";
  }
  else if( comparison == "is less than or equal to" )
  {
      sql += " <= '" + value + "'";
  }
  else if( comparison == "begins with" )
  {
      sql += " like '" + value + "%'";
  }
  else if( comparison == "does not begin with" )
  {
      sql += " not like '" + value + "%'";
  }
  else if( comparison == "ends with" )
  {
      sql += " like '%" + value + "'";
  }
  else if( comparison == "does not end with" )
  {
      sql += " not like '%" + value + "'";
  }
  else if( comparison == "contains" )
  {
    if ( value == "" )
    {
      alert( "Please specify a value." );
      return;
    }
    sql += " like '%" + value + "%'";
  }
  else if( comparison == "does not contain" )
  {
      sql += " not like '%" + value + "%'";
  }
  else
  {
    alert( "Unknown comparison: " + comparison );
    return( "" );
  }
  return( sql );
}

function customSelection(obl)
{
  var fieldname = $('dialog_custom_fieldname');
  var field = $('dialog_custom_field');
  var comparison1 = $('dialog_custom_comparison1').value;
  var value1 = $('dialog_custom_value1').value;
  var comparison2 = $('dialog_custom_comparison2').value;
  var value2 = $('dialog_custom_value2').value;
  var radio1 = $('dialog_custom_radio1');
  var radio2 = $('dialog_custom_radio2');
  var sql;
  var sql1;
  var sql2;

  if ( !checkCondition( field.value, comparison1, value1 ) )
  {
    alert( "Value '" + value1 + "' is not a valid " + fieldname.innerHTML + "." );
    return;
  }
  if ( !checkCondition( field.value, comparison2, value2 ) )
  {
    alert( "Value '" + value2 + "' is not a valid " + fieldname.innerHTML + "." );
    return;
  }

  sql1 = conditionSql( field.value, comparison1, value1 );
  sql2 = conditionSql( field.value, comparison2, value2 );

  if ( sql1 == "" )
  {
    sql = sql2;
  }
  else if ( sql2 == "" )
  {
    sql = sql1;
  }
  else
  {
    if ( radio1.checked )
    {
      sql = sql1 + " and " + sql2;
    }
    else if ( radio2.checked )
    {
      sql = sql1 + " or " + sql2;
    }
    else
    {
      alert( "When indicating two conditions please select 'And' or 'Or'." );
      return;
    }
  }
  
  $(filterComboName(fieldname.innerHTML)).value = encodeUrl( sql );  
  
  if ( sql == "" )
  {
    var listbox = $(field.value);
    var i;
    var valueSet;
    
    valueSet = 0;
    
    for(i=0;i<listbox.length;i++)
    {
      if ( listbox.options[i].value == "All" )
      {
        listbox.selectedIndex = i;
        valueSet = 1;
      }
    }
    if ( ! valueSet )
    {
      alert( "value '" + customValue + "' not set for listbox " + field.value );
    }
  }
  hideDialogBox(obl);  
  startLoading();  
  $('main_form').submit();
}

function customCancel(obl)
{
  var fieldname = $('dialog_custom_fieldname');
  var field = $('dialog_custom_field');

  hideDialogBox(obl);
  
  var listbox = $(field.value);
  var customValue = decodeUrl( $(filterComboName(fieldname.innerHTML)).value );
  var i;
  var valueSet;
  
  valueSet = 0;
  for(i=0;i<listbox.length;i++)
  {
    if ( listbox.options[i].value == customValue )
    {
      listbox.selectedIndex = i;
      valueSet = 1;
    }
  }
  if ( ! valueSet )
  {
    customValue = "Custom";
    for(i=0;i<listbox.length;i++)
    {
      if ( listbox.options[i].value == customValue )
      {
        listbox.selectedIndex = i;
        valueSet = 1;
      }
    }
  }
  if ( ! valueSet )
  {
    alert( "value '" + customValue + "' not set for listbox " + field.value );
  }
}

function hideDialogBox(obl)
{
  var v='visible';
  var n='none';
  $('overlay').style.display=n;
  $(obl).style.display=n;
  inf(v,obl);
  document.onkeypress=''
}

function multiselect( textboxName, field )
{
  var listbox = $(field);
  var i;
  var opt;
  var text;
  var list;
  var j;
  var found;
  
  var textbox = $(textboxName);  
  
  $('dialog_multiselect_field').value = textboxName;
  
  var included = $("dialog_multiselect_included");
  var excluded = $("dialog_multiselect_excluded");
  showDialogBox('dialog_multiselect')
  
  included.options.length = 0;
  excluded.options.length = 0;
 
  list = textbox.value.split(",").sort();
  for(i=0;i<list.length;i++)
  {
    text = list[i];
    if ( text != "Custom" && text != "-" && text != "All" && text != "" )
    {
      opt = document.createElement('option');
      opt.text = text;  
      try {
        included.add(opt, null); // standards compliant; doesn't work in IE
      }
      catch(ex) {
        included.add(opt); // IE only
      }
    }
  }
  
  for(i=0;i<listbox.length;i++)
  {
    text = listbox.options[i].value;
    if ( text != "Custom" && text != "-" && text != "All" && text != "" )
    {
      found = 0;
      for(j=0;j<list.length;j++)
      {
        if ( list[j] == text )
        {
          found = 1;
        }
      }
      if ( ! found )
      {
        opt = document.createElement('option');
        opt.text = text;  
        try {
          excluded.add(opt, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
          excluded.add(opt); // IE only
        }
      }
    }
  }
}

function multiselectOk()
{
  var text;

  var included = $("dialog_multiselect_included");

  text = "";
  for(i=0;i<included.length;i++)
  {
    if ( text != "" )
    {
      text += ",";
    }
    text += included.options[i].text;
  }
  var textboxName = $('dialog_multiselect_field').value;
  
  $(textboxName).value = text;

  hideDialogBox('dialog_multiselect');  
}

function multiselectCancel()
{
  hideDialogBox('dialog_multiselect');  
}

function multiSelectInclude()
{
  var included = $("dialog_multiselect_included");
  var excluded = $("dialog_multiselect_excluded");
  moveSelectedItems(excluded,included);
}

function multiSelectExclude()
{
  var included = $("dialog_multiselect_included");
  var excluded = $("dialog_multiselect_excluded");
  moveSelectedItems(included,excluded);
}

function sortedListboxAdd( listbox, text )
{      
  var i;
  var opt;

  opt = document.createElement('option');
  opt.text = text;  
  opt.value = text;
  for(i=0;i<listbox.options.length;i++)
  {
    if ( opt.text == listbox.options[i].text )
    {
      return;
    }
    if ( opt.text < listbox.options[i].text )
    {
      try {
        listbox.add(opt, listbox.options[i]); // standards compliant; doesn't work in IE
      }
      catch(ex) {
        listbox.add(opt, i); // IE only
      }
      return;
    }
  }      
  try
  {
    listbox.add( opt, null ); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    listbox.add( opt ); // IE only
  }    
}

function moveSelectedItems( from, to )
{
  var i;
  
  for(i=from.options.length-1;i>=0;i--)
  {
    if ( from.options[i].selected )
    {
      sortedListboxAdd( to, from.options[i].text )
      from.remove(i);
    }
  }
}

function customComparisonChanged( listbox )
{
  var button;
  
  if ( listbox.id == 'dialog_custom_comparison1' )
  {
    button = $('dialog_custom_multiselect1');
  }
  else
  {
    button = $('dialog_custom_multiselect2');
  }
  
  if( listbox.value == "equals" || listbox.value == "does not equal" )
  {
    button.disabled = false;
  }
  else
  {
    button.disabled = true;
  }
}

function initmb()
{
  var ab='absolute';
  var n='none';
  
  var obody=document.getElementsByTagName('body')[0];
  var frag=document.createDocumentFragment();  
  var obol=document.createElement('div');  
  obol.setAttribute('id','overlay');
  obol.style.display=n;
  obol.style.position=ab;
  obol.style.top=0;
  obol.style.left=0;
  obol.style.zIndex=998;
  obol.style.width='100%';
  frag.appendChild(obol);

  obody.insertBefore(frag,obody.firstChild);
  window.onscroll = scrollFix; 
  window.onresize = sizeFix;
}

function startLoading()
{
  var el;
  el = document.getElementById("loading");
  el.style.display = 'block';
}

function endLoading()
{
  var el;
  el = document.getElementById("loading");
  el.style.display = 'none';
}



window.onload = initmb;
                  
