/*
SimpleJS ver 0.02 beta
----------------------
SimpleJS is developed by Christophe "Dyo" Lefevre (http://bleebot.com/)

$ajax function is based on Simple AJAX Code-Kit (SACK)
Gregory Wild-Smith (http://www.twilightuniverse.com/)
*/
var enableCache = false;
var jsCache = new Array();
var DynObj = new Array();

function pause(millisecondi)
{
    var now = new Date();
    var exitTime = now.getTime() + millisecondi;

    while(true)
    {
        now = new Date();
        if(now.getTime() > exitTime) return;
    }
}

function $ajax(file) {
   this.xmlhttp = null;
   this.resetData = function() {
      this.method = "POST";
      this.queryStringSeparator = "?";
      this.argumentSeparator = "&";
      this.URLString = "";
      this.encodeURIString = true;
      this.execute = false;
      this.element = null;
      this.elementObj = null;
      this.requestFile = file;
      this.vars = new Object();
      this.responseStatus = new Array(2);
      }
   ;
   this.resetFunctions = function() {
      this.onLoading = function() {
         }
      ;
      this.onLoaded = function() {
         }
      ;
      this.onInteractive = function() {
         }
      ;
      this.onCompletion = function() {
         }
      ;
      this.onError = function() {
         }
      ;
      this.onFail = function() {
         }
      ;
      }
   ;
   this.reset = function() {
      this.resetFunctions();
      this.resetData();
      }
   ;
   this.crAjx = function() {
      try {
         this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
         }
      catch (e1) {
         try {
            this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
         catch (e2) {
            this.xmlhttp = null;
            }
         }
      if (! this.xmlhttp) {
         if (typeof XMLHttpRequest != "undefined") {
            this.xmlhttp = new XMLHttpRequest();
            }
         else {
            this.failed = true;
            }
         }
      }
   ;
   this.setVar = function(name, value) {
      this.vars[name] = Array(value, false);
      }
   ;
   this.encVar = function(name, value, returnvars) {
      if (true == returnvars) {
         return Array(encodeURIComponent(name), encodeURIComponent(value));
         }
      else {
         this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
         }
      }
   this.processURLString = function(string, encode) {
      encoded = encodeURIComponent(this.argumentSeparator);
      regexp = new RegExp(this.argumentSeparator + "|" + encoded);
      varArray = string.split(regexp);
      for (i = 0; i < varArray.length; i++) {
         urlVars = varArray[i].split("=");
         if (true == encode) {
            this.encVar(urlVars[0], urlVars[1]);
            }
         else {
            this.setVar(urlVars[0], urlVars[1]);
            }
         }
      }
   this.createURLString = function(urlstring) {
      if (this.encodeURIString && this.URLString.length) {
         this.processURLString(this.URLString, true);
         }
      if (urlstring) {
         if (this.URLString.length) {
            this.URLString += this.argumentSeparator + urlstring;
            }
         else {
            this.URLString = urlstring;
            }
         }
      // prevents caching of URLString
      this.setVar("rndval", new Date().getTime());
      urlstringtemp = new Array();
      for (key in this.vars) {
         if (false == this.vars[key][1] && true == this.encodeURIString) {
            encoded = this.encVar(key, this.vars[key][0], true);
            delete this.vars[key];
            this.vars[encoded[0]] = Array(encoded[1], true);
            key = encoded[0];
            }
         urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
         }
      if (urlstring) {
         this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
         }
      else {
         this.URLString += urlstringtemp.join(this.argumentSeparator);
         }
      }
   this.runResponse = function() {
      eval(this.response);
      }
   this.runAJAX = function(urlstring) {
      if (this.failed) {
         this.onFail();
         }
      else {
         this.createURLString(urlstring);
         if (this.element) {
            this.elementObj = $(this.element);
            }
         if (this.xmlhttp) {
            var self = this;
            if (this.method == "GET") {
               totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
               this.xmlhttp.open(this.method, totalurlstring, true);
               }
            else {
               this.xmlhttp.open(this.method, this.requestFile, true);
               try {
                  this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded") }
               catch (e) {
                  }
               }
            this.xmlhttp.onreadystatechange = function() {
               switch (self.xmlhttp.readyState) {
                  case 1 : self.onLoading();
                  break;
                  case 2 : self.onLoaded();
                  break;
                  case 3 : self.onInteractive();
                  break;
                  case 4 : self.response = self.xmlhttp.responseText;
                  self.responseXML = self.xmlhttp.responseXML;
                  self.responseStatus[0] = self.xmlhttp.status;
                  self.responseStatus[1] = self.xmlhttp.statusText;
                  if (self.execute) {
                     self.runResponse();
                     }
                  if (self.elementObj) {
                     elemNodeName = self.elementObj.nodeName;
                     elemNodeName.toLowerCase();
                     if (elemNodeName == "input" || elemNodeName == "select" || elemNodeName == "option" || elemNodeName == "textarea") {
                        self.elementObj.value = self.response;
                        }
                     else {
                        self.elementObj.innerHTML = self.response;
                        }
                     }
                  if (self.responseStatus[0] == "200") {
                     self.onCompletion();
                     }
                  else {
                     self.onError();
                     }
                  self.URLString = "";
                  break;
                  }
               }
            ;
            this.xmlhttp.send(this.URLString);
            }
         }
      }
   ;
   this.reset();
   this.crAjx();
   }
function ajax_installScript(script) {
   if (!script) return;
   if (window.execScript) {
      window.execScript(script);
      }
   else if(window.jQuery && jQuery.browser.safari) {
      STO(script, 0);
      }
   else {
      STO( script, 0 );
      }
   }
function $ajax_show(divId, Aorj, url, loader, effect) {
   if (effect == 'appear') $opacity(divId, 0, 101, 600); pause(250);
   if (effect == 'highlight') $highlight(divId);
   var targetObj = $(divId);
   targetObj.innerHTML = DynObj[Aorj].response;
   if (effect == 'blind') {
   $(divId).style.position = '';
   $blinddown(divId);
   }
   if(enableCache) {
      jsCache[url] = DynObj[Aorj].response;
      }
   DynObj[Aorj] = false;
   ajax_parseJs(targetObj) }
function $ajaxreplace(divId, url) {
   $opacity(divId, 100, 0, 400);
   $(divId).style.height = '';
   scr = "$ajaxload('" + divId + "','" + url + "',false,'appear',false)";
   STO(scr, 400);
   }
function $ajaxload(divId, url, loader, effect, ecache) {
   if (effect == 'appear') changeOpac(0, divId);
   if (effect == 'blind') {
	  var ids = $(divId).style;
      ids.overflow = 'hidden';
      ids.display = 'block';
      ids.height = '0px';
      }
   if (ecache) {
      if(enableCache && jsCache[url]) {
		 if (effect == 'appear') $opacity(divId, 0, 101, 600);
   		 if (effect == 'highlight') $highlight(divId);
         $(divId).innerHTML = jsCache[url];
		 if (effect == 'blind') {
   		 $(divId).style.position = '';
   		 $blinddown(divId);
   		 }
	     return;
         }
      }
   var Aorj = DynObj.length;
   if (loader != false)
   {
   	changeOpac(100, divId);
   	$(divId).innerHTML = loader;
   }
   DynObj[Aorj] = new $ajax();
   DynObj[Aorj].requestFile = url;
   DynObj[Aorj].onCompletion = function() {
      if (effect == 'appear') changeOpac(0, divId);
      pause(250);
      $ajax_show(divId, Aorj, url, loader, effect);
      }
   ;
   DynObj[Aorj].runAJAX();
   }
function ajax_parseJs(obj) {
   var scriptTags = obj.getElementsByTagName('SCRIPT');
   var string = '';
   var jsCode = '';
   for(var no = 0; no < scriptTags.length; no++) {
      if(scriptTags[no].src) {
         var head = document.getElementsByTagName("head")[0];
         var scriptObj = document.createElement("script");
         scriptObj.setAttribute("type", "text/javascript");
         scriptObj.setAttribute("src", scriptTags[no].src);
         }
      else {
         if(DHTMLSuite.clientInfoObj.isOpera) {
            jsCode = jsCode + scriptTags[no].text + '\n';
            }
         else jsCode = jsCode + scriptTags[no].innerHTML;
         }
      }
   if(jsCode)ajax_installScript(jsCode);
   }
//generic fonctions
function $(id) {
   return document.getElementById(id);
   }
function STO(func,time) {
	return window.setTimeout(func,time);
	}
function DecToHexa(DecNb) {
   var digits = parseInt(DecNb).toString(16);
    if (DecNb < 16) digits = '0' + digits;
   return digits;
   }
function addslashes(str) {
   str = str.replace(/\"/g,"\\\"");
   str = str.replace(/\'/g,"\\\'");
   return str;
   }
//blinds effects
function $toggle(id) {
   if (act_height(id) == 0) $blinddown(id);
   else $blindup(id);
   }
function act_height(id) {
   height = $(id).clientHeight;
   if(height == 0) height = $(id).offsetHeight;
   return height;
   }
function act_width(id) {
   width = $(id).clientWidth;
   if(width == 0) width = $(id).offsetWidth;
   return width;
   }
function max_height(id) {
   var ids = $(id).style;
   ids.overflow = 'hidden';
   if (act_height(id)!=0) return act_height(id);
   else
   {
   origdisp = ids.display;
   origheight = ids.height;
   origpos = ids.position;
   origvis = ids.visibility;
   ids.visibility = 'hidden';
   ids.height = '';
   ids.display = 'block';
   ids.position = 'absolute';
   height = act_height(id);
   ids.display = origdisp;
   ids.height = origheight;
   ids.position = origpos;
   ids.visibility = origvis;
   return height;
   }
   }
function $blindup(id, time) {
   if (!time)time = 200;
   acth = act_height(id);
   maxh = max_height(id);
   if (acth == maxh) {
      $(id).style.display = 'block';
      var steps;
      steps = Math.ceil(time / acth);
      for(i = 0; i <= acth; i++) {
         newh = acth - i;
         STO("$('" + id + "').style.height='" + newh + "px'", steps * i);
         }
      }
   }
function $blinddown(id, time) {
   if (!time)time = 200;
   acth = act_height(id);
   if (acth == 0) {
      maxh = max_height(id);
      $(id).style.display = 'block';
      $(id).style.height = '0px';
      var steps;
      steps = Math.ceil(time / maxh);
      for(i = 1; i <= maxh; i++) {
         STO("$('" + id + "').style.height='" + i + "px'", steps * i);
         }
      }
   }
//$opacity effects
function $opacity(id, opacStart, opacEnd, time) {
   if($(id).style.width==0) $(id).style.width=act_width(id);
   var speed = Math.round(time / 100);
   var timer = 0;
   if(opacStart > opacEnd) {
      for(i = opacStart; i >= opacEnd; i--) {
         STO("changeOpac(" + i + ",'" + id + "')", (timer * speed));
         timer++;
         }
      }
   else if(opacStart < opacEnd) {
      for(i = opacStart; i <= opacEnd; i++) {
         STO("changeOpac(" + i + ",'" + id + "')", (timer * speed));
         timer++;
         }
      }
   }
function changeOpac(opacity, id) {
   var ids = $(id).style;
   ids.opacity = (opacity / 100);
   ids.MozOpacity = (opacity / 100);
   ids.KhtmlOpacity = (opacity / 100);
   ids.filter = "alpha(opacity=" + opacity + ")";
   }
function $shiftOpacity(id, time) {
   if($(id).style.opacity < 0.50) {
      $opacity(id, 0, 100, time);
      }
   else {
      $opacity(id, 100, 0, time);
      }
   }
function currentOpac(id, opacEnd, time) {
   var currentOpac = 100;
   if($(id).style.opacity < 100) {
      currentOpac = $(id).style.opacity * 100;
      }
   $opacity(id, currentOpac, opacEnd, time)}
//$colorize effects
function $highlight(id, time, ncol, hcol) {
   if(time) milli = time;
   else milli = 900;
   if(ncol) endcol = ncol;
   else endcol = '#FFFFFF';
   if(hcol) origcol = hcol;
   else origcol = '#FFFFA6';
   $colorize(origcol, endcol, id, milli, 'high');
   }
function $textColor(id, startcol, endcol, time) {
   if(time) milli = time;
   else milli = 900;
   $colorize(startcol, endcol, id, milli, 'text');
   }
function $morphColor(id, starttextcol, endtextcol, startbackcol, endbackcol, startbordercol, endbordercol, time) {
   if(time) milli = time;
   else milli = 900;
   $colorize(starttextcol, endtextcol, id, milli, 'text');
   $colorize(startbackcol, endbackcol, id, milli, 'back');
   if (startbordercol != false) $colorize(startbordercol, endbordercol, id, milli, 'border');
   }
function $colorize(start, end, id, time, elem) {
   dr = parseInt(start.substring(1, 3), 16);
   dg = parseInt(start.substring(3, 5), 16);
   db = parseInt(start.substring(5, 7), 16);
   fr = parseInt(end.substring(1, 3), 16);
   fg = parseInt(end.substring(3, 5), 16);
   fb = parseInt(end.substring(5, 7), 16);
   steps = time / 10;
   cr = dr;
   cg = dg;
   cb = db;
   sr = (fr - dr) / steps;
   sg = (fg - dg) / steps;
   sb = (fb - db) / steps;
   var zzi = 10;
   for (var x = 0; x < steps; x++) {
      color = '#' + DecToHexa(cr) + DecToHexa(cg) + DecToHexa(cb);
	  if (x==(steps-1)) {
	  if (elem == "high") color='';
	  else color=end;
	  }
      mytime = (x);
      if (elem == "back" || elem == "high")
	  newfonc = '$("' + id + '").style.backgroundColor="' + color + '";';
      else if (elem == "text")
	  newfonc = '$("' + id + '").style.color="' + color + '";';
      else if (elem == "border")
	  newfonc = '$("' + id + '").style.borderColor="' + color + '";';
      STO(newfonc, zzi);
      cr += sr;
      cg += sg;
      cb += sb;
      zzi += 10;
      }
   }
