/*
* Clean v3.1, AJAX Engine 
* Copyright 2005-2006 Carlos Eduardo Goncalves (cadu.goncalves@gmail.com)
* 
* This program is free software, you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*/

function Engine(debug){
	// set to false to disable debugging
	Engine.DEBUG = false;
	if(Engine.DEBUG)
	Console.start();
	// Comment out to prevent secondary loader - top-right
	//Engine.buildProgressbar(Engine.PROGRESSBAR_VALUE);
}

Engine.PROGRESSBAR_VALUE = "<p id='loaderText' style='margin-right: 15px;margin-top: 15px;'><img src='/images/loadingAnimation.gif'/>&nbsp;&nbsp;loading...&nbsp;</p>";

Engine.HTTP_METHODS = ["GET", "POST"];

Engine.HTTP_STATUS_CODES = [
	"200 OK", "201 Created", "202 Accepted", "203 Non-Authoritative Information",
	"205 Reset Content", "204 No Content", "206 Partial Content",
	"300 Multiple Choices", "301 Moved Permanently", "302 Found", "303 See Other",
	"304 Not Modified", "305 Use Proxy", "307 Temporary Redirect",
	"400 Bad Request", "401 Unauthorized", "402 Payment Required", 
	"403 Forbidden",  "404 Not Found", "405 Method Not Allowed", "406 Not Acceptable",
	"407 Proxy Authentication Required", "408 Request Timeout", "409 Conflict",
	"410 Gone", "411 Length Required", "412 Precondition Failed", 
	"413 Request Entity Too Large", " 414 Request-URI Too Long", "415 Unsupported Media Type",
	"416 Requested Range Not Satisfiable", "417 Expectation Failed",
	"500 Internal Server Error", "501 Not Implemented", "502 Bad Gateway", 
	"503 Service Unavailable", "504 Gateway Timeout", "505 HTTP Version Not Supported"];

Engine.reportException = function(msg_id, exception){
  if(Engine.DEBUG)
    Console.trace(exception, Console.EXCEPTION);
  else{	  
    msg = MessageQueue.messages[msg_id];
    if((msg == null) || (msg.onError == null))
      throw exception;
    else
      msg.onError(exception);
  }
  MessageQueue.remove(msg_id);		    
}

Engine.buildProgressbar = function(content){
  try{		
    Engine.PROGRESSBAR = document.createElement("div"); 
    Engine.PROGRESSBAR.innerHTML = content;  
    Engine.PROGRESSBAR.align = "right";  
    Engine.PROGRESSBAR.style.left = "0"; 
    Engine.PROGRESSBAR.style.top = "0"; 
    Engine.PROGRESSBAR.style.width = "100%";
    Engine.PROGRESSBAR.style.position = "absolute";
    Engine.PROGRESSBAR.style.display = "none";  
    document.body.appendChild(Engine.PROGRESSBAR);  
    Engine.feedbackProgress();	  
  } catch(e){Engine.reportException(null, e);}  
}

Engine.feedbackProgress = function(){
  Engine.PROGRESSBAR.style.top = document.body.scrollTop;
  if(MessageQueue.isEmpty()){
    if(Engine.PROGRESSBAR.style.display != "none")
      Engine.PROGRESSBAR.style.display = "none";
  }
  else{
    if(Engine.PROGRESSBAR.style.display != "block")	  
      Engine.PROGRESSBAR.style.display = "block";
  }
  setTimeout(Engine.feedbackProgress, 100);	
}

function Message(){
  this.id = null;	
  this.method = null;
  this.address = null;
  this.xslt = null;
  this.value = null;
  this.refresh = true;
  this.document = document;
  this.consumer = null;	
  this.onChange = null;
  this.onError = null;  
  this.onComplete = null;
}

function Connection(){
}

Connection.sendMessage = function(msg){
  try{
    wrapper = MessageQueue.add(msg);
    msg.id = wrapper.id;	
	if(msg.method.toUpperCase() == "GET")
	  wrapper.request.setRequestHeader("If-Modified-Since", "Tue, 1 Jan 1980 00:00:00 GMT");	
    wrapper.request.send(msg.value);  
	Console.trace(msg, Console.REQUEST);		
	return msg.id; 	
  } catch(e){Engine.reportException(msg.id, e);}
}

Connection.sendFormByMessage = function(msg, form){
  try{	
    msg.method = "POST";  
    msg.value = ParserTool.formToUrl(form);	
    wrapper = MessageQueue.add(msg);
    msg.id = wrapper.id;	
    wrapper.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
    wrapper.request.send(msg.value);  
	Console.trace(msg, Console.REQUEST);		
	return msg.id; 	
  } catch(e){Engine.reportException(msg.id, e);}
}

Connection.abortMessage = function(msg_id){
  if(isNaN(msg_id)) 
    return;
  try{
    if(MessageQueue.messages.length > msg_id)	  
      if(MessageQueue.messages[msg_id] != null){
        MessageQueue.messages[msg_id].request.abort();
        MessageQueue.remove(msg_id);
      }
  } catch(e){Engine.reportException(msg_id, e);}
}

function MessageQueue(){
}

MessageQueue.messages = new Array();		

MessageQueue.add = function (msg){ 
  try{
    wrapper = new MessageWrapper();
    wrapper.wrap(msg);
    MessageQueue.messages.push(wrapper);	 
    return wrapper;
  } catch(e){Engine.reportException(MessageQueue.messages.length, e);}
}

MessageQueue.remove = function(msg_id){
  if(isNaN(msg_id)) 
    return;
  if(MessageQueue.messages.length > msg_id){
    MessageQueue.messages[msg_id] = null;
  }
}

MessageQueue.isEmpty = function(){	
  var empty = true;
  for(var i = 0; i < MessageQueue.messages.length; i++) {
    if(MessageQueue.messages[i] != null){
	  empty = false;
	  break;
	}
  }
  if(empty)
    MessageQueue.messages = new Array();
  return empty;
}

function MessageWrapper(){
}

MessageWrapper.prototype.buildRequest = function (){
  try{
    obj = (typeof XMLHttpRequest != "undefined") ? new XMLHttpRequest() : 
	  this.buildActiveX(["Microsoft.XMLHTTP", "Msxml2.XMLHTTP"]);		
	return obj;
  } catch(e){Engine.reportException(null, e);}	
}

MessageWrapper.prototype.buildActiveX = function (names){
    var obj = null;
	for(var i = 0; i < names.length; i++) {
      try {
        obj = new ActiveXObject(names[i]);
		break;
      } catch (e) {}
    }
	return obj;
}

MessageWrapper.prototype.loadFile = function (url){
  try{
    req = this.buildRequest();
    req.open("GET", url, false);
    req.send('');	
    return req.responseXML;
  } catch(e){Engine.reportException(null, e);}
}

MessageWrapper.prototype.transform = function (origin, style){
  try{
    if (typeof XSLTProcessor != "undefined"){			
	  proc = new XSLTProcessor();
	  proc.importStylesheet(style);
	  return (new XMLSerializer()).serializeToString(proc.transformToDocument(origin));
    }
    else{	
	  proc = this.buildActiveX(["Microsoft.XMLDOM", "MSXML2.DOMDocument", "MSXML.DOMDocument"]);
	  proc.async = "false";	  
	  proc.load(origin);	
	  return proc.transformNode(style);	
    }
  } catch(e){Engine.reportException(null, e);}	
}

MessageWrapper.prototype.wrap = function(msg){
  try{	
	if(Engine.HTTP_METHODS.toString().indexOf(msg.method.toUpperCase()) == -1)
	  msg.method = "POST";	
    if(msg.xslt != null)
	  this.style = this.loadFile(msg.xslt);   	
    this.id = MessageQueue.messages.length;   
	this.consumer = msg.consumer; 
	this.refresh = msg.refresh;
    this.document = msg.document; 
	this.address = msg.address;	
    this.request = this.buildRequest();
    this.request.open(msg.method, msg.address);
    this.onError = msg.onError;
    this.onComplete = msg.onComplete;
    if(msg.onChange != null)
	  this.request.onreadystatechange = msg.onChange;   
    else{ 
	  var _this = this;
	  var args = [this];
	  this.request.onreadystatechange = function() {
    		_this.onChange.apply(args[0], args);
  		}
    }
  } catch(e){Engine.reportException(null, e);}
}

MessageWrapper.prototype.onChange = function(){ 
  try{
    if(this.request.readyState == 4){	
      s = this.parseStatus(this.request.status) + ": " + this.address;
      Console.trace(s, Console.RESPONSE);
      if(this.onComplete != null)
	    this.onComplete(this.request.status);              		
      if(this.request.status >= 200 && this.request.status <= 299){		
	    iterator = new DomIterator(this.document);
	    if(this.style != null)
	      iterator.applyValue(this.consumer, this.transform(this.request.responseXML, this.style), this.refresh);
	    else
	      iterator.applyValue(this.consumer, this.request.responseText, this.refresh);
      }  	  
      MessageQueue.remove(this.id);		
    }
  } catch(e){Engine.reportException(null, e);}  
}  	  

MessageWrapper.prototype.parseStatus = function(status){ 
  try{
    var str = "HTTP status " + status;
    for(var i = 0; i < Engine.HTTP_STATUS_CODES.length; ++i){
      if(Engine.HTTP_STATUS_CODES[i].indexOf(status) != -1){
	    str = Engine.HTTP_STATUS_CODES[i];	
	    break;
	  }
    }
    return str;
  } catch(e){Engine.reportException(null, e);}    
}

function DomIterator(doc){
	this.doc = doc;
}

DomIterator.prototype.applyValue = function(id, value, replaceOld){
  try{
    el = this.doc.getElementById(id);
    iframes = this.doc.getElementsByTagName("iframe");
    for(var i = 0; i <= iframes.length; ++i){
	  if(iframes[i] != null) {
  	    if(iframes[i].id == el.id){
  	        el.contentWindow.document.body.innerHTML = (replaceOld) ? value : el.contentWindow.document.body.innerHTML + value;
	        return;
	    }
	  }
    }
	if(el.value != null){
       el.value = (replaceOld) ? value : el.value + value;	  
	   return;
    }
	if(el.innerHTML != null){
       el.innerHTML = (replaceOld) ? value : el.innerHTML + value;	  
	   return;
    }
  } catch(e){Engine.reportException(null, e);}
}

function ParserTool(){
}

ParserTool.formToUrl = function(form){
  var url = "";
  try{
    for(var i = 0; i < form.elements.length; ++i){  
	  if(form.elements[i].type)
        switch(form.elements[i].type.toLowerCase()){	  
	   	  case "button": break;
		  case "reset": break;	
	      case "radio":
	        if(form.elements[i].checked)
		     url += form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
 	        break;		
	      case "checkbox":
	        if(form.elements[i].checked)
		     url += form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
 	        break;	
	      case "select-one":
            url += form.elements[i].name + "=" + escape(form.elements[i].options[form.elements[i].selectedIndex].value) + "&";
            break;
	      case "select-multiple":
	        for(var v = 0; v < form.elements[i].options.length; ++v){
              if(form.elements[i].options[v].selected)
		        url += form.elements[i].name + "=" + escape(form.elements[i].options[v].value) + "&";
		    }
            break;		
	      default:
	        url += form.elements[i].name + "=" + escape(form.elements[i].value) + "&";
	        break;	  
        }
    }
    url = url.substr(0,(url.length - 1));
    return url;
  } catch(e){Engine.reportException(null, e);}
}

ParserTool.jsToHtml = function(value, id){
  try{	
    txt = (id) ? "<div class='hide' id='" + id + "'>" : "</div>";      
    if(value instanceof Object)  
      for(i in value){     
        txt += "<p><b>" + i + "</b> = " + value[i] + "</p>"; 	
	  }
    else
      txt += "<p>" + value + "</p>"; 
    return txt += "</div>" ;
  } catch(e){Engine.reportException(null, e);}  
}

function Console(){	
}

Console.TEMPLATE_FILE = "/clean-console.html";

Console.counter = 1;

Console.window = null;

Console.REQUEST = "request", Console.RESPONSE = "response", Console.EXCEPTION = "exception";

Console.start = function(){
  try{
    opener = document.createElement("div"); 
    opener.align = "right";	
    opener.style.width = "100%";
    opener.style.height = "30px";
    opener.innerHTML = "<a style='color:#000000;' href='javascript:Console.open()'>Clean AJAX Engine Console</a>&nbsp;&nbsp;";
	document.body.insertBefore(opener, document.body.firstChild);
  } catch(e){Engine.reportException(null, e);}   
}

Console.open = function(){
  try{	
    if(!Console.isOpen())
	  Console.window = window.open(Console.TEMPLATE_FILE, "console", "height=350,width=450,scrollbars");
	Console.window.focus();
  } catch(e){Engine.reportException(null, e);} 
}

Console.isOpen = function(){
  if(Console.window == null)
    return false;
  else
    return (!(Console.window.closed) && (Console.window.document != null));
}

Console.trace = function(value, status){
  try{	
    if(Console.isOpen()){	
      stack = ParserTool.jsToHtml(Console.counter, null);
      title = "<a href='javascript:expand(" + Console.counter + ")'>" + status.toUpperCase() + "</a>";
      data = ParserTool.jsToHtml(value, Console.counter);
      ++Console.counter;	
      table = Console.window.document.getElementById("trace_table");
      row = table.insertRow(-1);	
      row.className = status;
      cell_1 = row.insertCell(-1);
      cell_2 = row.insertCell(-1);	
      cell_1.vAlign = "top";
      cell_2.vAlign = "top";
      cell_1.innerHTML = stack;		
      cell_2.innerHTML = title + data;	
    }
  } catch(e){Engine.reportException(null, e);}   
}

startUp = function() {
  clean = (document.body) ? new Engine(true) : setTimeout(startUp, 1000);
}
startUp();