﻿/*  Prototype JavaScript framework
 *  (c) 2005 Sam Stephenson <sam@conio.net>
 *  Prototype is freely distributable under the terms of an MIT-style license.
 *  For details, see the Prototype web site: http://prototype.conio.net/
/*--------------------------------------------------------------------------*/

var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

Object.extend = function(destination, source) {
	for (property in source) destination[property] = source[property];
	return destination;
}

Function.prototype.bind = function(object) {
	var __method = this;
	return function() {
		return __method.apply(object, arguments);
	}
}

Function.prototype.bindAsEventListener = function(object) {
var __method = this;
	return function(event) {
		__method.call(object, event || window.event);
	}
}

function $() {
	if (arguments.length == 1) return get$(arguments[0]);
	var elements = [];
	$c(arguments).each(function(el){
		elements.push(get$(el));
	});
	return elements;

	function get$(el){
		if (typeof el == 'string') el = document.getElementById(el);
		return el;
	}
}

if (!window.Element) var Element = new Object();

Object.extend(Element, {
	remove: function(element) {
		element = $(element);
		element.parentNode.removeChild(element);
	},

	hasClassName: function(element, className) {
		element = $(element);
		if (!element) return;
		var hasClass = false;
		element.className.split(' ').each(function(cn){
			if (cn == className) hasClass = true;
		});
		return hasClass;
	},

	addClassName: function(element, className) {
		element = $(element);
		Element.removeClassName(element, className);
		element.className += ' ' + className;
	},
  
	removeClassName: function(element, className) {
		element = $(element);
		if (!element) return;
		var newClassName = '';
		element.className.split(' ').each(function(cn, i){
			if (cn != className){
				if (i > 0) newClassName += ' ';
				newClassName += cn;
			}
		});
		element.className = newClassName;
	},

	cleanWhitespace: function(element) {
		element = $(element);
		$c(element.childNodes).each(function(node){
			if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) Element.remove(node);
		});
	},

	find: function(element, what) {
		element = $(element)[what];
		while (element.nodeType != 1) element = element[what];
		return element;
	}
});

AjaxEngine = Class.create();
AjaxEngine.prototype = {
	initialize: function(url, options){
	    this.xmlHttp;
		this.requests = new Array();
		this.requestCount = 0;	
	},

	request: function(url, options){	
	    var request = this.createRequest(url, options);
        if (request.failed) return;                 
        var async = (typeof(request.onComplete) == 'function');   
        if(async) request.xmlHttp.onreadystatechange = this.onStateChange.bind(request);
        if (request.method == 'GET' && request.parameters != null) {
            url += (url.indexOf('?') == -1) ? '?' : '&';
            url += request.parameters;	                               
        }
        request.xmlHttp.open(request.method, url, async);        
        if (request.method == 'POST') {
		    request.xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
		    if (request.xmlHttp.overrideMimeType) request.xmlHttp.setRequestHeader('Connection', 'close');
	    }    
        this.requestStarted(request);
        request.xmlHttp.send(request.parameters);    	
        if(!async) return this.getResponse(request);
	},

	createRequest: function(url, options) {	
        for( var i = 0; i < this.requests.length; i++ ) {
		    if (this.requests[i].xmlHttp.readyState == 4 ) {
			    this.requests[i].xmlHttp.abort(); 
			    return this.requests[i];
		    }
	    }	  
	    var request = { 
	            parameters: options.parameters || '',
	            method: options.method || 'GET',
	            onComplete: options.onComplete || null,
	            onLoading: options.onLoading || null,
	            onError: options.onError || null,
	            onStateChanged: options.onStateChanged || null,
	            failed: false,
	            url: url,
	            xmlHttp: null,
	            ajaxEngine: this
	    };	         
    	var clsids = ["Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","Msxml2.XMLHTTP.4.0","Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP.2.6","Microsoft.XMLHTTP.1.0","Microsoft.XMLHTTP.1","Microsoft.XMLHTTP"];
		for(var i=0; i<clsids.length && request.xmlHttp == null; i++)
			request.xmlHttp = this.getXmlHttp(clsids[i]);	
		if(!request.xmlHttp && typeof XMLHttpRequest != 'undefined') request.xmlHttp = new XMLHttpRequest();
		if (!request.xmlHttp && window.createRequest) { try { request.xmlHttp = window.createRequest(); } catch (ex) { } }
		if (!request.xmlHttp) request.failed = true; return request; 
		return this.requests[this.requests.length] = request;
	},
	
    onStateChange: function() {
        if (typeof this.onStateChanged == 'function')  this.onStateChanged(this.xmlHttp.readyState);
        if (this.xmlHttp.readyState == 4) this.onComplete(this.ajaxEngine.getResponse(this));
	},
	
	getXmlHttp: function(clsid) {
		try { return new ActiveXObject(clsid); } catch(e){}
	},
	
	getResponse: function(request) {
	    var response = new Object();
	    response.error = null;
        response.value = null;
        response.request = request.xmlHttp;
        if(request.xmlHttp.status == 200) {
	        try { response.value = request.xmlHttp.responseText; } catch(e) { response.error = e; }
	        try { response.responseXml = request.xmlHttp.responseXML; } catch(e) { response.error = e; } 		
        } else {
	        response.error = 'http exception, the returned status was ' + request.status;
        } 
        if(response.error != null && typeof request.onError == "function")
			try { request.onError(response.error); } catch(e){}    
        this.requestCompleted(request);
        return response;
    },
	
	requestStarted: function(request) {
	    this.requestCount++;
	    this.checkRequestState(request);
	},
	
	requestCompleted: function(request) {
	    this.requestCount--;
	    this.checkRequestState(request);
	},
	
	checkRequestState: function(request) {
	    if (typeof(request.onLoading) == 'function')
	        if (this.requestCount > 0) { request.onLoading(true); } else { request.onLoading(false); }         
	},
	
	getFormElements: function(form) {
      var queryString = "";
      for (i=0; i<form.childNodes.length; i++) {
         if (form.childNodes[i].tagName == "INPUT") {
            if (form.childNodes[i].type == "text") {
               queryString += form.childNodes[i].name + "=" + form.childNodes[i].value + "&";
            }
            if (form.childNodes[i].type == "checkbox") {
               if (form.childNodes[i].checked) {
                  queryString += form.childNodes[i].name + "=" + form.childNodes[i].value + "&";
               } else {
                  queryString += form.childNodes[i].name + "=&";
               }
            }
            if (form.childNodes[i].type == "radio") {
               if (form.childNodes[i].checked) {
                  queryString += form.childNodes[i].name + "=" + form.childNodes[i].value + "&";
               }
            }
         }   
         if (form.childNodes[i].tagName == "SELECT") {
            var sel = form.childNodes[i];
            queryString += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }         
      }
      
      return queryString;
    }
};