///////////////////////////////////////////////////////////////////////
//  This Script Library contain various javascript library           //
//  Was put together in one js file to make easier to maintenance    //
//  You can read each library disclaimer for each taken library      //
///////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////
//     This fade library was designed by Erik Arvidsson for WebFX    //
//                                                                   //
//     For more info and examples see: http://webfx.eae.net          //
//     or contact Erik at http://webfx.eae.net/contact.html#erik     //
//                                                                   //
//     Feel free to use this code as lomg as this disclaimer is      //
//     intact.                                                       //
///////////////////////////////////////////////////////////////////////

var __fadeArray = new Array();    // Needed to keep track of wich elements are animating

//////////////////  fade  ////////////////////////////////////////////////////////////
//                                                                                  //
//   parameter: fadeIn                                                              //
// description: A boolean value. If true the element fades in, otherwise fades out  //
//              The steps and msec are optional. If not provided the default        //
//              values are used. Opacity is optional and specifies the start/end    //
//              opacity values. onfinish is used when the fadein/out has finished   // 
//              and is called (so pass in a function name)                          //
//////////////////////////////////////////////////////////////////////////////////////

function Fade(el, fadeIn, steps, msec, opacity, onfinish)
{
  if (steps    == null)  steps   = 4;
  if (msec     == null)  msec    = 25;
  if (opacity  == null)  opacity = 100;
  if (onfinish == null)  onfinish = '';

  if (el.fadeIndex == null) 
  {
    el.fadeIndex = __fadeArray.length;
  }
  __fadeArray[el.fadeIndex] = el;

  if (el.style.visibility == "hidden") 
  {
    el.style.display  = 'block';
    el.fadeStepNumber = 0;
  } else 
  {
    el.fadeStepNumber = steps;
  }

  if (fadeIn) 
  {
    el.style.filter = "Alpha(Opacity=0)";
    el.style.opacity = '0';
  } else 
  {
    el.style.filter = "Alpha(Opacity=" + opacity + ")";
    el.style.opacity = opacity / 100;
  }
  window.setTimeout("RepeatFade(" + fadeIn + "," + el.fadeIndex + "," + steps + "," + msec + ", " + opacity + ", '" + onfinish + "')", msec);
}

//////////////////////////////////////////////////////////////////////////////////////
//  Used to iterate the fading                                                      //
//////////////////////////////////////////////////////////////////////////////////////

function RepeatFade(fadeIn, index, steps, msec, opacity, onfinish)
{
  el = __fadeArray[index];
  c = el.fadeStepNumber;
  if (el.fadeTimer != null) 
  {
    window.clearTimeout(el.fadeTimer);
  }

  if (c == 0 && !fadeIn) 
  {            // Done fading out!
    el.style.visibility = "hidden"; // If the platform doesn't support filter it will hide anyway
    el.style.display    = "none"; // If the platform doesn't support filter it will hide anyway
    if (onfinish) 
    {
      eval(onfinish + "()");
    }
    return;
  } else if (c == steps && fadeIn) 
  {    // Done fading in!
    el.style.filter = "Alpha(Opacity=" + opacity + ")";
    el.style.opacity = opacity / 100;
    el.style.visibility = "visible";
    if (onfinish) 
    {
      eval(onfinish + "()");
    }
    return;
  } else 
  {
    fadeIn ? c++ : c--;
    el.style.visibility = "visible";
    el.style.filter = "Alpha(Opacity=" + opacity*c/steps + ")";
    el.style.opacity = (opacity / 100) * c/steps;
    el.fadeStepNumber = c;
    el.fadeTimer = window.setTimeout("RepeatFade(" + fadeIn + "," + index + "," + steps + "," + msec + ", " + opacity + ", '" + onfinish + "' )", msec);
  }
}


//////////////////////////////////////////////////////////////////////////////////////
//  DIALOG LIBRARY                                                                  //
//////////////////////////////////////////////////////////////////////////////////////
/**
* Shows a modal dialog
* 
* @param string contentID ID of content layer to use HTML of
* @param int    width     Width of dialog
*/
__visibleDialog = null;

function ModalDialog_Show(contentID, width)
{
  /**
  * Hide all selects and objects
  */
  if (document.all) 
  {
    var selectObjs = document.getElementsByTagName('select');
    for (var i=0; i<selectObjs.length; ++i) 
    {
      if (!selectObjs[i].md_nohide) 
      {
        selectObjs[i].md_vis = selectObjs[i].style.visibility;
        selectObjs[i].style.visibility = 'hidden';
      }
    }
  }

  /**
  * Create the background layer if necessary
  */
  var dialogBg = document.getElementById('modalBg');
  if (!dialogBg) 
  {
    var bgDiv = document.createElement('div');
    bgDiv.id = 'modalBg';
    bgDiv.className = 'modalBg';
    document.body.appendChild(bgDiv);
    var dialogBg = document.getElementById('modalBg');
  }
    
  /**
  * Create the shadow layer
  */
  var dialogShadow = document.getElementById('modalShadow');
  if (!dialogShadow) 
  {
    var shadowDiv = document.createElement('div');
    shadowDiv.id = 'modalShadow';
    shadowDiv.className = 'modalShadow';
    document.body.appendChild(shadowDiv);
    var dialogShadow = document.getElementById('modalShadow');
  }

  // Show the dialog
  var dialog = document.getElementById(contentID);
  dialog.style.visibility = 'hidden';
  dialog.style.width = width + 'px';
  
  dialogShadow.style.visibility = 'visible';
  dialogShadow.style.width = dialog.offsetWidth + 'px';
  dialogShadow.style.height = dialog.offsetHeight + 'px';

  /**
  * Insert the header into the dialog
  */
  var dialogHeader = document.createElement('div');
  dialogHeader.id = 'dialogHeader';
  dialogHeader.className = 'modalDialogHeader';
  dialog.appendChild(dialogHeader);
  dialogHeader = document.getElementById('dialogHeader');

  Fade(dialogBg, true, null, null, 70);
  Fade(dialog, true, null, null, 100);
  Fade(dialogShadow, true, null, null, 50, 'ModalDialog_FinishFade');
  
    // Moz stuff
//  if (!document.all) 
//  {
    var baseHeight=document.documentElement.clientHeight;
    dialogBg.style.width  = document.body.scrollWidth + 'px';
    var bdHeight=document.body.scrollHeight>baseHeight?document.body.scrollHeight:baseHeight;
    dialogBg.style.height = bdHeight + 'px';
    dialog.style.left = document.body.offsetWidth / 2 - width / 2 + 'px';
    dialog.style.top  = baseHeight / 2 - dialog.offsetHeight / 2 + 'px';
    dialogShadow.style.left = (document.body.offsetWidth / 2 - width / 2) + 4 + 'px';
    dialogShadow.style.top  = (baseHeight / 2 - dialog.offsetHeight / 2) + 4 + 'px';
    dialogHeader.style.width = (dialog.offsetWidth - 2) + 'px';
    dialogHeader.style.paddingTop = '5px';

    // IE stuff
//  } else 
//  {
//    dialogHeader.style.width = dialog.offsetWidth + 'px';
//  }
  // Set visible dialog var
  __visibleDialog = contentID;
}

/**
* Closes a modal dialog
*/
function ModalDialog_Close()
{
  var dialogBg     = document.getElementById('modalBg');
  var dialog       = document.getElementById(__visibleDialog);
  var dialogShadow = document.getElementById('modalShadow');
    
  // Lose the dialog header
  dialog.removeChild(document.getElementById('dialogHeader'));

  // Hide stuff
  dialogBg.style.MozOpacity = 0;
  dialogBg.style.visibility = 'hidden';
    
  dialog.style.MozOpacity = 0;
  dialog.style.visibility = 'hidden';
  dialogShadow.style.visibility = 'hidden';
    
  __visibleDialog = null;
    
  /**
  * Unhide all selects
  */
  var selectObjs = document.getElementsByTagName('select');
  for (var i=0; i<selectObjs.length; ++i) 
  {
    if (!selectObjs[i].md_nohide) 
    {
      selectObjs[i].style.visibility = selectObjs[i].md_vis;
    }
  }
}

function ModalDialog_FinishFade()
{
  var shadow  = document.getElementById('modalShadow');
  var visible = document.getElementById(__visibleDialog)

  shadow.style.width = visible.offsetWidth;
  shadow.style.height = visible.offsetHeight;
  shadow.style.filter = 'Alpha(opacity=50) progid:DXImageTransform.Microsoft.Blur(pixelradius=2)';
}


function showAlert(alertMessage,width)
{
  var messageDiv=document.getElementById('alertMessage');
  if (!messageDiv) 
  {
    var alertDiv = document.createElement('div');
    alertDiv.id = 'modalAlert';
    alertDiv.className = 'modalDialog';
    var messageDiv = document.createElement('div');
    messageDiv.id = 'alertMessage';
    messageDiv.className = 'alertMessage';
    alertDiv.appendChild(messageDiv);
    var alertForm = document.createElement('form');
    var alertButton = document.createElement('input');
    alertButton.type='button';
    alertButton.value='OK';
    alertButton.className='buttonok';
    //alertButton.setAttribute('onclick','ModalDialog_Close()');
    var z;
    alertButton.onclick=(function(z) {return function(){ModalDialog_Close();}})(z);
    alertForm.appendChild(alertButton);
    alertDiv.appendChild(alertForm);
    document.body.appendChild(alertDiv);
    var dialogAlert = document.getElementById('modalAlert');
  }
  if (messageDiv)
  {
    messageDiv.innerHTML=alertMessage;
  } else alert("failed");
  ModalDialog_Show('modalAlert', width)
}
//////////////////////////////////////////////////////////////////////////////////////
//  AJAX LIBRARY                                                                  //
//////////////////////////////////////////////////////////////////////////////////////

function makeRequest(url,data,statusdiv) 
{
  var httpRequest;

  if (window.XMLHttpRequest) 
  {
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType) 
    {
      httpRequest.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) 
  {
    try 
    {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) 
    {
      try 
      {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  if (!httpRequest) 
  {
    alert('Giving up :( Cannot create an XMLHTTP instance');
    return false;
  }
  httpRequest.onreadystatechange = function() { alertContents(httpRequest,statusdiv); };
  httpRequest.open('POST','/?ajaxcall=1&command='+url, true);
  httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  httpRequest.setRequestHeader("Content-length", data.length);
  httpRequest.setRequestHeader("Connection", "close");
  httpRequest.send(data);
}

function ajaxSubmit(url,form,statusdiv)
{
  var eleme=form.elements;
  var data='';
  for (var i=0;i<eleme.length;i++)
  {
    var item=eleme[i];
    if (item.tagName=='INPUT')
    {
      if (item.type=='checkbox')
      {
        if (item.checked)data+=item.name+'='+item.value+'&';
      } else
        data+=item.name+'='+item.value+'&';
      if (item.type=='password')item.value='';
    }
  }
  makeRequest(url,data,statusdiv);
  return false;
}

function alertContents(httpRequest,statusdiv) 
{
  var statdiv=document.getElementById(statusdiv);
  if (httpRequest.readyState == 4) 
  {
    if (httpRequest.status == 200) 
    {
      if (statdiv) statdiv.innerHTML='';
      var xd=httpRequest.responseXML;
      var newMsgs=xd.getElementsByTagName('message');
      if (newMsgs.length>0)
      {
        var newMsg=newMsgs.item(0).firstChild.data;
        showAlert(newMsg,300);
      }

      var newHTMLs=xd.getElementsByTagName('replacehtml');
      if (newHTMLs.length>0)
      for (var i=0;i<newHTMLs.length;i++)//>
      {
        var item=newHTMLs.item(i);
        var replacedId=item.getAttribute('id');
        var tagChange=document.getElementById(replacedId);
        if (item.textContent)
          tagChange.innerHTML=item.textContent;
        else if (item.text)
          tagChange.innerHTML=item.text;
      }
    } else 
    {
//    alert('There was a problem with the request.');
    }
  } else if (statdiv)
  {
    if (httpRequest.readyState == 0)
    {
      statdiv.innerHTML="Ready.";
    } else if (httpRequest.readyState == 1) 
    {
      statdiv.innerHTML="Connecting...";
    } else if (httpRequest.readyState == 2) 
    {
      statdiv.innerHTML="Connected.";
    } else if (httpRequest.readyState == 3) 
    {
      statdiv.innerHTML="Transfering...";
    }
  }
}
