/***************************************************************************
 *   Copyright (C) 2007 by Denis Gabaidulin                                *
 *   sherman@oemdesign.ru                                                  *
 ***************************************************************************/
/* $Id: utils.js 12344 2007-03-14 11:31:12Z sherman $ */	
	// common utils
	
	function parseQueryString(source) 
	{
  		var query = getQueryString(source)
  		
  		var variables = query.split("&");
  		
  		var pairs = new Array();
  		
  		for (var i=0; i<variables.length; i++) {
    		var pair = variables[i].split("=");
    		
    		var variable = ['name', 'value'];
    		
    		variable['name'] 	= pair[0];
    		variable['value'] 	= pair[1];
    		
    		pairs[i] = variable;
    	}
		
    	return pairs;
  	}
  	
  	function getLocation(source)
	{
		if (!source)
			source = window.location;

		var url = new String(source);
			
		var urlParts = url.split("?");
	
		if (urlParts[0])
			return urlParts[0];
		else
			return url;
	}
	
	function getQueryString(source)
	{
		if (!source)
			source = window.location;
		
		return source.search.substring(1);
	}
	
	function toQueryString(scope)
	{
		var newLocation = getLocation();
		
		var curr = 0;
		
		if (scope.length > 0)
			for (var i = 0; i < scope.length; i++) {
				if (curr == 0)
					newLocation += '?';
				else
					newLocation += '&';
					
				newLocation
					+= scope[i]['name'] 
					+ '=' 
					+  scope[i]['value'];
				
				curr++;
			}
		
		return newLocation;
	}
	
	// refactor: make objects, e.g.:
	// variablesScope, variable, etc
  	function has(scope, variableName)
  	{
  		if (scope.length > 0)
  			for (var i=0; i<scope.length; i++)
				if (scope[i]['name'] == variableName)
					return i;
					
		return -1;
  	}
  	
  	function replaceVariable(scope, variable)
  	{
  		if ((index = has(scope, variable['name'])) != -1)
			scope[index]['value'] = variable['value'];
		else {
			scope[scope.length] = variable;
		}
  	}
  	
  	function newVariable(name, value)
  	{
  		var variable = ['name', 'value'];
    	
    	variable['name'] 	= name;
    	variable['value'] 	= value;
    	
    	return variable;
  	}
  	
  	String.prototype.trim = function()
	{
		text = this.toString();
		
		while (
			text.substring(0, 1) == ' '
			|| text.substring(0, 1) == '\n'
			|| text.substring(0, 1) == '\t'
		)
			text = text.substring(1, text.length);
	
		while (
			text.substring(text.length - 1, text.length) == ' '
			|| text.substring(text.length - 1, text.length) == '\n'
			|| text.substring(text.length - 1, text.length) == '\t'
		)
			text = text.substring(0,text.length - 1);
	
		return text;
	}
	
	
	if(window.HTMLElement) {
		HTMLElement.prototype.removeNode = function(removeChildren) {
	  		if (Boolean(removeChildren))
	    		return this.parentNode.removeChild(this);
	 	 	else {
	    		var range = document.createRange();
	    		
	    		range.selectNodeContents(this);
	    		
	   			return
	   				this.parentNode.replaceChild(
	   					range.extractContents(),
	   					this
	   				);
	  		}
		}
	}
	
	function getContent(element)
	{
		if (element) {
			if (isMSIE())
				return element.innerText;
			else
				return element.textContent;
		}
		
		return;
	}
	
	function setContent(element, content)
	{
		if (element) {
			if (isMSIE())
				element.innerText = content;
			else
				element.textContent = content;
		}
		
		return;
	}
	
	Array.prototype.has = function(value)
	{
		if (this.length > 0) {
			for (var i = 0; i < this.length; i++) {
				if (this[i].value == value)
					return true;
			}
		}
		
		return false;
	}
	
	function isMSIE()
	{
		if  (navigator.appName == "Microsoft Internet Explorer")
			return true;
		else
			return false;
	}
	
	function isArray(a)
	{
 		return a && typeof a == 'object' && a.constructor == Array;
	}
	
	function setEvent(element, eventName, funcName, params)
	{
		var paramsString = '';
		
		if (params != null) {
			if (isArray(params)) {
				count = params.length;
				for (var i = 0; i < count; i++) {
					paramsString += params[i];
					if (i < count - 1)
						paramsString += ', ';
				}
			} else {
				paramsString += params;
			}
		}
		
		var stringMethod = funcName + "(" + paramsString + ")";
		
		if (isMSIE()) {
			element.onclick = new Function( funcName +"( " + paramsString + " );" );
		} else {
		    element.setAttribute(eventName, stringMethod);	
		}
	}

