function WX() {
  this.site     = '';
  this.url      = '';
  this.theme    = '';
  this.browser  = '';
  this.language = 1;
  this.overlap  = new Array();
  this.data     = [];

  this.farsikey = [
    0x0020, 0x0021, 0x061B, 0x066B, 0x00A4, 0x066A, 0x060C, 0x06AF,
    0x0029, 0x0028, 0x002A, 0x002B, 0x0648, 0x002D, 0x002E, 0x002F,
    0x06F0, 0x06F1, 0x06F2, 0x06F3, 0x06F4, 0x06F5, 0x06F6, 0x06F7,
    0x06F8, 0x06F9, 0x003A, 0x06A9, 0x003E, 0x003D, 0x003C, 0x061F,
    0x066C, 0x0624, 0x200C, 0x0698, 0x064A, 0x064D, 0x0625, 0x0623,
    0x0622, 0x0651, 0x0629, 0x00BB, 0x00AB, 0x0621, 0x004E, 0x005D,
    0x005B, 0x0652, 0x064B, 0x0626, 0x064F, 0x064E, 0x0056, 0x064C,
    0x0058, 0x0650, 0x0643, 0x062C, 0x005C, 0x0686, 0x00D7, 0x0640,
    0x200D, 0x0634, 0x0630, 0x0632, 0x064A, 0x062B, 0x0628, 0x0644,
    0x0627, 0x0647, 0x062A, 0x0646, 0x0645, 0x067E, 0x062F, 0x062E,
    0x062D, 0x0636, 0x0642, 0x0633, 0x0641, 0x0639, 0x0631, 0x0635,
    0x0637, 0x063A, 0x0638, 0x007D, 0x007C, 0x007B, 0x007E
  ];
  
  this.Init = function (site, url, theme) {
    this.site  = site;
    this.url   = url;
    this.theme = theme;
    
    var ua = navigator.userAgent;
    this.browser = 'IE';
    if (ua.indexOf('MSIE') >= 0) this.browser = 'IE';
    if (ua.indexOf('Netscape6/') >= 0) this.browser = 'NS';
    if (ua.indexOf('Gecko') >= 0) this.browser = 'NS';
    
    wxTip = new WXTip(); wxTip.Init();
    wxMessage = new WXMessage(); wxMessage.Init();
    wxConfirm = new WXConfirm(); wxConfirm.Reset('');
    wxPost = new WXPost(); wxPost.Init();
    wxQuery = new WXQuery(); wxQuery.Reset('');
    wxAjax = new WXAjax(); wxAjax.Init();
    wxCalendar = new WXCalendar(); wxCalendar.Init();
    wxTree = new WXTree(); wxTree.Init(this.theme + 'treeview/');
  }

  this.AddData = function (data, value) {
    var i = 0;
    for (i=0; i<this.data.length; i++) {
      if (this.data[i][0] == data) {
        this.data[i][1] = value;
        return;
      }
    }
    this.data[this.data.length] = [data, value];
  }
  
  this.GetData = function (data) {
    var i = 0;
    for (i=0; i<this.data.length; i++) {
      if (this.data[i][0] == data) return this.data[i][1];
    }
    return '';
  }
  
  this.Confirm = function (image, todo, content, data, action) {
    wxConfirm.Reset();
    wxConfirm.SetData('image', image);
    wxConfirm.SetData('title', todo);
    wxConfirm.SetData('content', content);
    wxConfirm.SetData('data', data);
    wxConfirm.SetData('action', ['document.location.href = \'' + action + '\'', 'wxMessage.Hide();']);
    wxConfirm.Show();
  }
  
  this.ShowObjects = function() {
    var i = 0;
    for (i=0; i<this.overlap.length; i++) this.overlap[i].style.visibility = 'visible';
    this.overlap = new Array();
  }
  
  this.HideObjects = function() {
    this.ShowObjects();
    var i; var e;
  	var tags = ['SELECT', 'OBJECT', 'IFRAME'];
	
  	for (i=0; i<tags.length; i++) {
      var elements = document.getElementsByTagName(tags[i]);
      for (e=0; e<elements.length; e++){
        if (elements[e].style.visibility != 'hidden') {
          elements[e].style.visibility = 'hidden';
	    	  this.overlap[this.overlap.length] = elements[e];
        }
      }
  	}
  }
  
  this.FixDate = function (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0) date.setTime (date.getTime() - skew);
  }
  
  this.SetCookie = function (name, value, remember, path) {
    var expdate = null;
    if (remember) {
      expdate = new Date(); this.FixDate(expdate);
      expdate.setTime (expdate.getTime() + (365 * 24 * 60 * 60 * 1000)); 
    }
    document.cookie = name + "=" + escape (value) + ((expdate) ? "; expires=" + expdate.toGMTString() : "") + ((path) ? "; path=" + path : "");
  }

  this.FNumber = function (number) {
    var data = [];
    number = number.toString();
    while (number.length > 0) {
      data[data.length] = number.substr(number.length - 3, 3);
      number = number.substr(0, number.length - 3);
    }
    data = data.reverse();
    return data.join(',');
  }
  
  this.PNumber = function (number) {
    var pNum = new Array("۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹");
    var eNum = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");

    var num = number.toString();
    var i = 0;
    for (i=0; i<pNum.length; i++) {
      while (num.lastIndexOf(eNum[i]) != -1) num = num.replace(eNum[i], pNum[i]);
    }
    return num;
  }

  this.CountDown = function (number, id, script) {
    if (number == 0) eval(script);
    else {
      var obj = null;
      if (obj = document.getElementById(id)) {
        obj.innerHTML = wx.PNumber(number);
      
        number--;
        window.setTimeout('wx.CountDown(' + number + ', \'' + id + '\', \'' + script.replace(/\'/g, '\\\'') + '\');', 1000);
      }
    }
  }
  
  this.ChangeLanguage = function () {
    this.language = 1 - this.language;
  }

  this.GetFarsiKey = function (key, language) {
    if (key < 0x0020 || key >= 0x00FF) return key;
    if (language == 1) { //If Farsi
      if (key == 0x005C) key= 67;
      if (key == 0x0054) key= 0x200C;
      if (key == 0x0060) key = 109;
  	  if (key != 0x200C) key = this.farsikey[key - 0x0020];
    }
    return key;
  }
  
  this.FKeyPress = function (id) {
    var language = ((id) ? eval(id + 'Language') : this.language);
    window.event.keyCode = this.GetFarsiKey(window.event.keyCode, language);
    return window.event.keyCode;
  }
  
  this.ShowContent = function (id) {
    var obj = null;
    if (obj = document.getElementById(id)) obj.style.display = '';
  }
  
  this.HideContent = function (id) {
    var obj = null;
    if (obj = document.getElementById(id)) obj.style.display = 'none';
  }
  
  this.Refresh = function () {
    document.location.href = this.url;
  }
  
  this.ShowMenu = function (id) {
    var type = eval(id + 'OC');
    document.getElementById(id).style.display = ((type == 'O') ? 'none' : '');
    eval(id + 'OC = \'' + ((type == 'O') ? 'C' : 'O') + '\';');
  }
  
  this.AddToFavorite = function () {
    var favorite = wx.GetData('addToFavorite');
    window.external.AddFavorite(favorite[0], favorite[1]);
  }
  
  this.PrintData = function () {
    window.open(wx.GetData('printData'));
  }
  
  this.Search = function (module, query, data, type, page) {
    var i = 0;
    
    wxPost.Reset('/search/page,' + page + '/');
    wxPost.AddObject('module', module);
    wxPost.AddObject('query', query);
    data = data.split('|');
    for (i=0; i<data.length; i++) {
      var field = data[i].split('-');
      wxPost.AddObject(field[0], field[1]);
    }
    wxPost.AddObject('type', type);
    wxPost.Post();
  }
  
  this.SetFeed = function (title, data) {
    data = data.split('-');
    if ((title == '') || (data.length != 3)) return;
    var url = wx.site + 'feed/' + data[0] + '/' + data[1] + '/type,' + data[2] + '/';
    
    var type = '';
    switch (data[2]) {
      case 'rss1': type = 'RSS 1'; break;
      case 'rss2': type = 'RSS 2'; break;
      case 'rdf':  type = 'RDF'; break;
      case 'atom': type = 'ATOM'; break;
    }
    var content = '';
    content += 'برای دریافت خروجی اطلاعات ';
    content += '<span style="font-weight: 600;">' + title + '</span>';
    content += ' به فرمت ' + '<span class="EN" dir="ltr">' + type + '</span>';
    content += ' میتوانید از لینک زیر استفاده نمایید.<br>';
    content += '<center>';
    content += '<input type="text" class="cFormObjectEN" style="margin-top: 5px; width: 98%" value="' + url + '">';
    content += '<br><br>';
    content += '<input type="button" class="cFormButton" value="مشاهده خروجی" onClick="document.location.href=\'' + url + '\'">';
    content += '&nbsp;';
    content += '<input type="button" class="cFormButton" value="بازگشت" onClick="wxMessage.Hide();">';
    content += '</center>';
    wxMessage.Show('آدرس خروجی اطلاعات', content, false);
    wxMessage.Resize(400);
  }
}

function WXTip() {
  this.Init = function () {
    if (document.addEventListener) document.addEventListener("mousemove", this.Move, true);
    if (document.attachEvent) document.attachEvent("onmousemove", this.Move);
  }

  this.Show = function(header, content) {
    this.Hide();

    var html = '';
    if (header != '') html += '<div class="cTooltipHeader">' + header + '</div>';
    if (content != '') html += '<div class="cTooltipContent">' + content + '</div>';
    
    var obj = document.createElement('DIV');
    obj.className = 'cTooltip';
    obj.id        = 'idWXTooltip';
    obj.innerHTML = html;
    obj.style.visibility = 'hidden';
    obj.style.zIndex = 100;
    
    wx.HideObjects();
    var bodyRef = document.getElementsByTagName("body").item(0);
    bodyRef.appendChild(obj);                    
  }
  
  this.Move = function(e) {
    var obj = null;
    if (obj = document.getElementById('idWXTooltip')) {
      var left = e.clientX - (obj.offsetWidth / 2);
      var top = e.clientY + 20 + ((wx.browser == 'IE') ? document.body.scrollTop : window.scrollY);
      
      if (left < 0) left = 0;
      obj.style.left = left + 'px';
      obj.style.top  = top + 'px';
      obj.style.visibility = 'visible';
    }
  }
  
  this.Hide = function() {
    wx.ShowObjects();
    
    var obj = null;
    if (obj = document.getElementById('idWXTooltip')) {
      var bodyRef = document.getElementsByTagName("body").item(0);
      bodyRef.removeChild(obj);                    
    }
  }
}

function WXMessage() {
  this.interval = '';
  
  this.Init = function () {
    var table = document.createElement('TABLE');
    table.setAttribute('width', '100%');
    table.setAttribute('height', '100%');
    table.setAttribute('border', '0');
    table.setAttribute('cellpadding', '0');
    table.setAttribute('cellspacing', '0');
    table.setAttribute('id', 'idWXMessageTable');
    table.style.zIndex = 100;
    table.className = 'cMessage';

    var tr = table.insertRow(0);
    var td = tr.insertCell(0);
    td.setAttribute('align', 'center');
    td.setAttribute('valign', 'middle');
    td.setAttribute('width', '100%');
    td.setAttribute('id', 'idWXMessageContent');
    td.className = 'cMessageBody';
    td.innerHTML = '';
    
    var bodyRef = document.getElementsByTagName("body").item(0);
    bodyRef.appendChild(table);
  }
  
  this.Check = function () {
    var tObj = document.getElementById('idWXMessageTable');
    if (!tObj) return;
    
    tObj.style.top = ((wx.browser == 'IE') ? document.body.scrollTop : window.scrollY);
  }
  
  this.Show = function (title, content, close) {
    wx.HideObjects();
    if (close) title = '<img src="' + wx.theme + 'message/close.gif" alt="" width="16" height="16" align="left" style="float: left;" class="cLink" onClick="wxMessage.Hide();">' + title;
    
    var message = '';
    message += '<div class="cMessageMessage" id="idMessageWindow">';
    if (title) message += '<div class="cMessageTitle" id="idMessageTitle" width="100%">' + title + '</div>';
    message += '<div class="cMessageContent" width="100%" id="idMessageContent">' + content + '</div>';
    message += '</div>';
    
    var tObj = document.getElementById('idWXMessageTable');
    var cObj = document.getElementById('idWXMessageContent');
    cObj.innerHTML = message;
    tObj.style.top = ((wx.browser == 'IE') ? document.body.scrollTop : window.scrollY);
    try { tObj.style.display = 'table' } catch (e) { tObj.style.display = 'inline'; }
    this.interval = window.setInterval('wxMessage.Check();', 10);
  }
  
  this.Hide = function () {
    var mObj = document.getElementById('idWXMessageTable');
    mObj.style.display = 'none';
    window.clearInterval(this.interval);
    wx.ShowObjects();
  }
  
  this.SetTitle = function (title, close) {
    var obj = document.getElementById('idMessageTitle');
    if (!obj) return;
    
    if (close) title = '<img src="' + wx.theme + 'message/close.gif" alt="" width="16" height="16" align="left" style="float: left;" class="cLink" onClick="wxMessage.Hide();">' + title;
    obj.innerHTML = title;
  }
  
  this.Change = function (content) {
    var obj = document.getElementById('idMessageContent');
    if (!obj) return;
    
    obj.innerHTML = content;
  }
  
  this.Resize = function (width) {
    var obj = document.getElementById('idMessageWindow');
    if (!obj) return;
    
    obj.style.width = width + 'px';
  }
  
  this.Progress = function (message) {
    var progress = '';
    progress += '<br><center>';
    progress += '<img src="' + wx.theme + 'message/progress.gif" style="margin-bottom: 10px;"><br>';
    progress += message + '</center><br>';
    return progress;
  }
}

function WXConfirm() {
  this.image   = '';
  this.title   = '';
  this.content = '';
  this.data    = '';
  this.action  = '';
  this.button  = new Array();
  
  this.Reset = function () {
    this.image   = '';
    this.title   = '';
    this.content = '';
    this.data    = '';
    this.action  = '';
    this.button  = new Array();
  }
  
  this.SetData = function(data, value) {
    switch (data) {
      case 'image':   this.image = value; break;
      case 'title':   this.title = value; break;
      case 'content': this.content = value; break;
      case 'data':    this.data = value; break;
      case 'action':  this.action = value; break;
    }
  }
  
  this.AddButton = function(value, action) {
    this.button[this.button.length] = [value, action];
  }
  
  this.Show = function () {
    var content = '';
    if (this.image) content += '<img src="' + wx.theme + 'message/confirm/' + this.image + '.gif" alt="" width="48" height="48" border="0" align="left" style="float: left; margin-right: 10px;">';
    content += '<div>' + this.content + '<div><br>';
    if ((this.data[0] != '') && (this.data[1] != '')) content += this.data[0] + ': <span style="font-weight: 600;">' + this.data[1] + '</span><br><br>';
    content += '<center>';
    content += '<input type="button" class="cFormButton" value=" بلی " onClick="' + this.action[0] + '">';
    content += ' ';
    content += '<input type="button" class="cFormButton" value=" خیر " onClick="' + this.action[1] + '">';
    if (this.button.length != 0) content += ' &nbsp; ';
    var i = 0;
    for (i=0; i<this.button.length; i++) {
      if (i != 0) content += ' ';
      content += '<input type="button" class="cFormButton" value="' + this.button[i][0] + '" onClick="' + this.button[i][1] + '">';
    }
    content += '</center>';
    wxMessage.Show(this.title, content, false);
  }
}

function WXPost() {
  this.target = '_self';
  this.method = 'post';
  this.action = '';
  this.object = new Array();
  this.file   = new Array();
  
  this.Init = function () {
    //var form = document.createElement('FORM');
    //form.enctype = 'multipart/form-data';
    //form.method = 'post';
    //form.action = '';
    //form.style.display = 'none';
    //form.id = 'idWXForm';
    
    //var bodyRef = document.getElementsByTagName("body").item(0);
    //bodyRef.appendChild(form);
  }
  
  this.Reset = function (action) {
    this.target = '_self';
    this.method = 'post';
    this.action = action;
    this.object = new Array();
    this.file   = new Array();
  }
  
  this.AddObject = function (name, value) {
    this.object[this.object.length] = [name, value];
  }
  
  this.AddFile = function (id) {
    this.file[this.file.length] = id;
  }
  
  this.SetTarget = function (target) {
    this.target = target;
  }
  
  this.Post = function () {
    var form = document.getElementById('idWXForm');
    while (form.firstChild) form.removeChild(form.firstChild);
    form.target = this.target;
    form.method = this.method;
    form.action = this.action;
    
    var i = 0;
    for (i=0; i<this.file.length; i++) form.appendChild(document.getElementById(this.file[i]));
    for (i=0; i<this.object.length; i++) {
      var obj   = document.createElement('INPUT');
      obj.type  = 'hidden';
      obj.name  = this.object[i][0];
      obj.value = this.object[i][1];
      form.appendChild(obj);
    }
    form.submit();
  }
}

function WXQuery() {
  this.url   = '';
  this.data  = [];
  
  this.Reset = function (url) {
    this.url   = url;
    this.data  = [];
  }
  
  this.AddData = function (name, value) {
    this.data[this.data.length] = [name, value];
  }
}

function WXAjax() {
  this.type    = 0;
  this.enabled = false;
  this.active  = false;
  this.query   = [];
  
  this.Init = function () {
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
      request = new XMLHttpRequest(); this.type = 1;
    } else if (window.ActiveXObject) { // IE
      try {
        request = new ActiveXObject('Msxml2.XMLHTTP'); this.type = 2;
      } catch (e) {
        try {
          request = new ActiveXObject('Microsoft.XMLHTTP'); this.type = 3;
        } catch (e) {}
      }
    }
    this.enabled = (this.type != 0);
    this.active  = false;
    this.query   = [];
    window.setInterval('wxAjax.Check();', 500);
  }
  
  this.AddQuery = function (query) {
    this.query[this.query.length] = query;
  }
  
  this.Check = function () {
    if ((this.query.length == 0) || (this.active)) return;
    
    if (!this.enabled) {
      var content = '';
      content += 'بدلیل عدم پشتیبانی مرورگر مورد استفاده شما، امکان استفاده از امکانات AJAX وجود ندارد.' + '<br><br>';
      content += '<center>' + '<input type="button" value="خروج" class="cFormButton" onClick="wxMessage.Hide();">' + '<center>';
      wxMessage.Show('اشکال در امکانات AJAX', content); this.query = []; return;
    }
    
    this.active = true;
	  var request = this.query[0]
    this.query.splice(0, 1);
 	  var ajxReq = null;
    switch (this.type) {
      case 1: ajxReq = new XMLHttpRequest(); break;
      case 2: ajxReq = new ActiveXObject('Msxml2.XMLHTTP'); break;
      case 3: ajxReq = new ActiveXObject('Microsoft.XMLHTTP'); break;
    }
      
    var i = 0;
    var fields = '';
    for (i=0; i<request.data.length; i++) {
      if (i != 0) fields += "&";
      fields += request.data[i][0] + '=' + request.data[i][1];
    }

    ajxReq.open("POST", request.url, true);
    ajxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    ajxReq.send(fields);
    ajxReq.onreadystatechange = function() {
      if (ajxReq.readyState == 4) {
        if (ajxReq.status == 200) {
          try { eval(ajxReq.responseText); }
          catch (e) { alert(ajxReq.responseText); }
        } else { alert(ajxReq.status); }
        wxAjax.active = false;
      }
    }
  }
}

function WXCalendar() {
  this.id      = '';
  this.string  = '';
  this.month   = [];
  this.current = [];
  this.date    = [];
  
  this.Init = function () {
    this.month = ['فروردین', 'اردیبهشت', 'خرداد', 'تیر', 'مرداد', 'شهریور', 'مهر', 'آبان', 'آذر', 'دی', 'بهمن', 'اسفند'];
  }
  
  this.DayInMonth = function (year, month) {
    var dNum = 0;
	  if ((month >= 1) && (month <= 6)) dNum = 31;
  	if ((month >= 7) && (month <= 11)) dNum = 30;
	  if (month == 12) dNum = (((year - 1383) % 4) == 0) ? 30 : 29;
	
    return dNum;
  }
  
  this.AddDate = function (year, month, day) {
    this.date[0] += year;
    this.date[1] += month;
    this.date[2] += day;
    
    if (this.date[0] <= 1350) this.date[0] = 1350;
    if (this.date[0] > 1450) this.date[0] = 1450;
    if (this.date[1] <= 0) this.date[1] = 1;
    if (this.date[1] > 12) this.date[1] = 12;
    if (this.date[2] <= 0) this.date[2] = 1;
    if (this.date[2] > this.DayInMonth(this.date[0], this.date[1])) this.date[2] = this.DayInMonth(this.date[0], this.date[1]);
    
    this.SetString(this.date);
    document.getElementById(this.id + 'D').value = wx.PNumber(this.date[2]);
    document.getElementById(this.id + 'M').value = this.month[this.date[1] - 1];
    document.getElementById(this.id + 'Y').value = wx.PNumber(this.date[0]);
  }
  
  this.SetString = function (date) {
    var string = '';
    string += date[0] + '-';
    string += ((date[1] < 10) ? '0' : '') + date[1] + '-';
    string += ((date[2] < 10) ? '0' : '') + date[2];
    this.string = string;
  }
  
  this.Show = function (id, date, title) {
    this.id      = id;
    this.current = [parseInt(date[0]), parseInt(date[1]), parseInt(date[2])];
    this.date    = this.current;
    this.SetString(this.date);
    
    var content = '';
    content += '<div align="center">';
    content += '<table cellpadding="2" cellspacing="2" width="95%" border="0">';
    content += '<tr>';
    content += '<td width="2%" align="right" class="cLink" onClick="wxCalendar.AddDate(0, 0, 1);">' + '&lt;&lt;' + '</td><td width="19%" align="center">' + 'روز' + '</td><td width="2%" align="left" class="cLink" onClick="wxCalendar.AddDate(0, 0, -1);">' + '&gt;&gt;' + '</td>';
    content += '<td width="15%">&nbsp;</td>';
    content += '<td width="2%" align="right" class="cLink" onClick="wxCalendar.AddDate(0, 1, 0);">' + '&lt;&lt;' + '</td><td width="19%" align="center">' + 'ماه' + '</td><td width="2%" align="left" class="cLink" onClick="wxCalendar.AddDate(0, -1, 0);">' + '&gt;&gt;' + '</td>';
    content += '<td width="15%">&nbsp;</td>';
    content += '<td width="2%" align="right" class="cLink" onClick="wxCalendar.AddDate(1, 0, 0);">' + '&lt;&lt;' + '</td><td width="19%" align="center">' + 'سال' + '</td><td width="2%" align="left" class="cLink" onClick="wxCalendar.AddDate(-1, 0, 0);">' + '&gt;&gt;' + '</td>';
    content += '</tr>';
    content += '<tr>';
    content += '<td colspan="3"><input id="' + this.id + 'D" type="text" class="cFormObjectFA" style="width: 100%; text-align: center" value="' + wx.PNumber(this.date[2]) + '" readonly></td>';
    content += '<td>&nbsp;</td>';
    content += '<td colspan="3"><input id="' + this.id + 'M" type="text" class="cFormObjectFA" style="width: 100%; text-align: center" value="' + this.month[this.date[1] - 1] + '" readonly></td>';
    content += '<td>&nbsp;</td>';
    content += '<td colspan="3"><input id="' + this.id + 'Y" type="text" class="cFormObjectFA" style="width: 100%; text-align: center" value="' + wx.PNumber(this.date[0]) + '" readonly></td>';
    content += '</tr>';
    content += '<tr><td colspan="11" align="left">';
    content += '<input type="button" class="cFormButton" onClick="document.getElementById(\'' + this.id + '\').value = wxCalendar.string; wxMessage.Hide();" value=" تایید ">';
    content += ' &nbsp; ';
    content += '<input type="button" class="cFormButton" onClick="wxMessage.Hide();" value="انصراف">';
    content += '</td></tr>';
    content += '</table>';
    content += '</div>';
    wxMessage.Show(title, content, false);
  }
  
  this.ShowArchive = function (module, year, block) {
    document.getElementById('idBlock' + block).innerHTML = wxMessage.Progress('دریافت مشخصات آرشیو');

    wxQuery.Reset('/' + module + '/barchive/');
    wxQuery.AddData('year', year);
    wxQuery.AddData('block', block);
    wxAjax.AddQuery(wxQuery);
  }
}

function WXTree() {
  this.dir    = '';
  this.id     = '';
  this.link   = '';
  this.dflt   = 'C';
  this.node   = [];
  this.status = [];

  this.Init = function (dir) {
    this.dir = dir;
  }
  
  this.Reset = function (id, link) {
    this.id   = id;
    this.link = link;
    this.dflt = 'C';
    this.node = [];
  }
  
  this.SetDefault = function (value) {
    this.dflt = value;
  }
  
  this.AddNode = function (parent, id, title, url) {
    var i = 0;
    this.node[this.node.length] = [parent, id, title, url, 0];
    for (i=0; i<this.node.length; i++) {
      if (this.node[i][1] == parent) this.node[i][4]++;
    }
  }
  
  this.SetStatus = function (id, status, value) {
    var i = 0;
    for (i=0; i<this.status.length; i++) {
      if ((this.status[i][0] == id) && (this.status[i][1] == status)) {
        this.status[i] = [id, status, value];
        return;
      }
    }
    
    this.status[this.status.length] = [id, status, value];
  }
  
  this.GetStatus = function (id, node) {
    var i = 0;
    for (i=0; i<this.status.length; i++) {
      if ((this.status[i][0] == id) && (this.status[i][1] == node)) return this.status[i][2];
    }
    return this.dflt;
  }
  
  this.ShowNode = function (id, node) {
    var status  = this.GetStatus(id, node);
    
    document.getElementById(node).style.display = ((status == 'C') ? '' : 'none');
    document.getElementById(node + 'Image').src = this.dir + ((status == 'C') ? 'open.gif' : 'close.gif');
    this.SetStatus(id, node, ((status == 'C') ? 'O' : 'C'));
  }
  
  this.GetView = function (parent) {
    if (!parent) parent = 0;
    var html = '';
    var i = 0;
    for (i=0; i<this.node.length; i++) {
      if (this.node[i][0] == parent) {
        var id    = this.id + 'N' + i;
        var child = (this.node[i][4] != 0);
        if (child) this.SetStatus(this.id, id, this.dflt);
      
        html += '<table cellpadding="0" cellspacing="0" border="0" class="cTreeTable"><tr><td valign="top" class="cTreeImagePlace">';
        html += '<img id="' + id + 'Image" src="' + this.dir + ((child) ? ((this.dflt == 'C') ? 'close' : 'open') : 'page') + '.gif" alt="" width="13" height="15"' + ((child) ? ' class="cTreeImage" onClick="wxTree.ShowNode(\'' + this.id + '\', \'' + id + '\');"' : '') + '">';
        html += '</td><td valign="top" class="cTreeTitlePlace" style="text-align: justify;">';
        html += '<a href="' + this.node[i][3] + '" class="' + this.link + '">' + this.node[i][2] + '</a>';
        if (child) html += '<div id="' + id + '"' + ((this.dflt == 'C') ? ' style="display: none;"' : '') + '>' + this.GetView(this.node[i][1]) + '</div>';
        html += '</td></tr></table>';
      }
    }
    
    return html;
  }
  
  this.Show = function () {
    document.getElementById(this.id).innerHTML = this.GetView();
  }
  
  this.OpenAll = function (id) {
    var i = 0;
    for (i=0; i<this.status.length; i++) {
      if ((this.status[i][0] == id) && (this.status[i][2] == 'C')) this.ShowNode(id, this.status[i][1]);
    }
  }
  
  this.CloseAll = function (id) {
    var i = 0;
    for (i=0; i<this.status.length; i++) {
      if ((this.status[i][0] == id) && (this.status[i][2] == 'O')) this.ShowNode(id, this.status[i][1]);
    }
  }
}