/* --- General functions --- */
function $() 
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
Array.prototype.inArray = function(search_term) 
{
		for (var i = 0; i < this.length; i++) 
		{
  		if (this[i] == search_term) 
  		{
     		return true;
  		}
  	}
  	return false;
}
function inputEscape(input)
{
	return "<![CDATA[" + input.replace(/,/g,"#.#.#").replace(/&/g,"%26") + "]]>";

	var string = input.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);

        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
	
	alert(utftext);
	return "<![CDATA[" + utftext + "]]>";
	
	var SAFECHARS = "0123456789" +					
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					
	var HEX = "0123456789ABCDEF";
	
	var result = "";
	for (var i = 0; i < input.length; i++ ) 
	{
		var ch = input.charAt(i);
	    if (ch == " ") {
		    result += " ";
		} else if (ch == ",")
		{
			result += "%ac";
		} else if (SAFECHARS.indexOf(ch) != -1) {
			result += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) 
			{
				   // Invalid char
			} else {
				result += "%";
				result += HEX.charAt((charCode >> 4) & 0xF);
				result += HEX.charAt(charCode & 0xF);
			}
		}
	}
	
	return "<![CDATA[" + result + "]]>";
}

function typeOf(value) 
{
    var type = typeof value;
    if (type === 'object') 
    {
        if (value) 
        {
            if (value instanceof Array) 
            {
                return 'array';
            }
        } else 
        {
            return 'void';
        }
    }
    return type;
}

/* --- HTML class managing functions --- */

function addClass(id,aClass)
{
	var element = document.getElementById(id);
	var classes = element.className.split(' ');
	if(!classes.inArray(aClass))
	{
		classes.push(aClass);
	}
	element.className = classes.join(' ');
}
function removeClass(id,aClass)
{
	var element = document.getElementById(id);
	var classes = element.className.split(' ');
	
	for(var i=0; i < classes.length; i++)
	{
		if(classes[i] == aClass)
		{
			classes.splice(i,1);
		}
	}
	
	element.className = classes.join(' ');
}

/* --- Ajax functions --- */

function getXmlHttp()
{
	var xmlHttp;
	try
	{
		xmlHttp=new XMLHttpRequest(); 		// Firefox, Opera 8.0+, Safari
	} catch (e)
	{
		try // Internet Explorer
		{
			xmlHttp=new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e)
		{
			xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	return xmlHttp;
}

//function Request()
//{
//	this.xmlHttp = getXmlHttp();
//	//this.xmlHttp.onreadystatechange = this.onreadystatechange;
//}
//Request.prototype.onreadystatechange;
//Request.prototype.xmlHttp;
//Request.prototype.response = '';
//Request.prototype.send = function(method,location,aSynchronous)
//{
//	var debug = '';
//	var xmlHttp = this.xmlHttp;
//	debug = xmlHttp.readyState;
//	if((xmlHttp.readyState > 0) && (xmlHttp.readyState < 4))
//	{
//		debug = debug + ' !! Already in Progress!! ';
//		xmlHttp = getXmlHttp(); // Current request is still open, creating a new XmlHttp Request
//	}
//	debug = debug + '(' + location + ')';
//	$('debug').innerHTML = debug + '<br>' + $('debug').innerHTML;
//	
//	this.xmlHttp.open(method,location,aSynchronous);
//	this.xmlHttp.send(null);
//	
//	if(aSynchronous == false)
//	{
//		return xmlHttp.responseText;
//	}
//}
function Request(id)
{
	this.id = id;
}
Request.prototype.id = '';
//Request.prototype.onreadystatechange;
//Request.prototype.xmlHttp;
Request.prototype.send = function(method,location,aSynchronous,data)
{
	requestCounter++;
	data = data + "&prefix=" + requestCounter;
	var xmlHttp = getXmlHttp();
	var id = this.id;
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			try
			{
				var result = fromServer(xmlHttp.responseText);
				var html = result['html'];
				var javascript = result['javascript'];
			
				eval('Ajax' + id + '(html)');
				installScript(javascript);
			}
			catch (err)
			{
				// do nothing...
			}
		}
	}
	
	xmlHttp.open(method,location,aSynchronous);
	if(data)
	{
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send(data);
	} else
	{
		xmlHttp.send(null);	
	}
	
}

//function fromXml(root)
//{
//	alert(root.tagName);
//	if(root.tagName == "void")
//	{
//		return "";
//	} else if(root.tagName == "xhtml")
//	{
//		alert(root.innerHTML);
//		return root.innerHTML;
//	}else if(root.tagName == "string")
//	{
//		return root.firstChild.nodeValue;
//	} else if(root.tagName == "number")
//	{
//		return root.firstChild.nodeValue;
//	} else if(root.tagName == "array")
//	{
//		childNodes = root.childNodes;
//		var result = new Array();
//		for(var i=0; i<childNodes.length; i++)
//		{
//			var node = childNodes[i];
//			result[node.getAttribute('key')] = fromXml(node.firstChild);
//		}
//		return result;
//	} else
//	{
//		return root;
//	}
//}

function fromServer(input)
{
	// seperate the html from the javascript
	var r = new RegExp("^([\\w\\W]*)<script>([\\w\\W]*)<");
	var result = r.exec(input);
	var html = result[1];
	var javascript = result[2];
	
	var returnArray = new Array();
	returnArray['html'] = html;
	returnArray['javascript'] = javascript;
	
	return returnArray;
	
	//Add the javascript to the global scope
	 if (window.execScript)
	 {
	 	window.execScript(javascript); // Internet Explorer
	 }else
	 {
	 	window.setTimeout(javascript,0); // other browsers
	 }
	 
//	 if (inputxml)
//	 {
//	 	var scripts = inputxml.getElementsByTagName("script");
//	 
//		 for(var x=0;x<scripts.length;x++)
//	 	{
//		 	eval(scripts[x].innerHTML);
//	 	}
//	 
//		 // return the html
//	 }

	 return html;
	

}
	function getHtml(input)
	{
		//var r = new RegExp("([.\\n]*)(<script>[.\n]*<[/]{1}script>)");
		var r = new RegExp("^([\\w\\W]*)<script>([\\w\\W]*)<");
		var result = r.exec(input);
		installScript(result[2]);
	}
	function installScript(script)
	{
	    if (window.execScript)
	    {
	    	if (script.length > 2)
	    		window.execScript(script); // Internet Explorer
		   
	    }else
	    {
			window.setTimeout(script,0);	    	
	    }
	}

function toXml(input)
{
	var type = typeOf(input);
	if(type == 'void')
	{
		return '<void></void>';
	} else if(type == 'string')
	{
		return '<string>' + input + '</string>';
	} else if(type == 'number')
	{
		return '<number>' + input + '</number>';
	} else if(type == 'array')
	{
		var result = '<array>';
		for(var key in input)
		{
			if(typeOf(input[key]) != 'function')
			{
				result += '<e key%3D"' + key + '">' + toXml(input[key]) + '</e>';
			}
		}
		result += '</array>';
		return result;
	} else
	{
		return input;
	}
}
function getNodeValue(obj,tag)
{
	return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}

/* --- Timer --- */
function Timer(action,time, repeat)
{
	this.action = action;
	this.time = time;
	this.repeat = repeat;
}
Timer.prototype.start = function()
{
	if (this.repeat > 0)
	{
		this.timerId = setInterval(this.action,this.time);
	}
	else
	{
		this.timerId = setTimeout(this.action,this.time);
	}
}
Timer.prototype.reset = function()
{
	clearTimeout(this.timerId);
	this.start();
}
Timer.prototype.cancel = function()
{
	clearTimeout(this.timerId);
}
Timer.prototype.action = '';
Timer.prototype.time = 0;
Timer.prototype.timerId = '';
Timer.prototype.repeat = 0;

/**
 Radio Button functions
**/

function getRadioButtonValue(container)
{
	var radiobuttons = container.getElementsByTagName("input");
	for(var i=0;i<radiobuttons.length;i++)
	{
		if (radiobuttons[i].checked == true)
			return radiobuttons[i].value;
	}
	return false;
}

function setRadioButtonValue(container, value)
{
	var radiobuttons = container.getElementsByTagName("input");
	for(var i=0;i<radiobuttons.length;i++)
		radiobuttons[i].checked = (radiobuttons[i].value == value);
}
 