function redirect (url) {
	location.href = url;
}

function change_lang (lang) {
	href_add_params ("change_lang=" + lang);
}

function href_add_params (paramsStr) {
	var href = location.href;
	if (href.indexOf("?") > 0)
		href += "&" + paramsStr;
	else
		href += "?" + paramsStr;
	location.href = href;
}

function newClass(parent, prot) {
	var cls = prot.constructor ? prot.constructor : function() {};
	cls.prototype = prot;
	return cls;
}

function json(val) {
	var result = null;
	if (typeof(val) == "String") {
		result = eval('(' + val + ')');
	} else if (typeof(val) == "object" && val.responseText) {
		result = eval('(' + val.responseText + ')');
	} else {
		result = {};
	}
	return result;
}

function showErrors(result) {
	var errorsStr = "";
	for (var i = 0; i < result.errors.length; i++) {
		var text = result.errors[i].message;
		if (!text)
			text = result.errors[i].id;
		errorsStr += text + "\n";
	}	
	alert(errorsStr);
}

function showAjaxError(type, r) {
	var str = r ? r.statusText : ""; 
	alert("Ajax error: " + type + " - ");
}

function clearNode(node) {
   if(!node)
      return;
  	 var len = node.childNodes.length;
   for(var i = len -1 ; i >= 0; i--)
      node.removeChild(node.childNodes[i]);
}



function newClass(parent, prop) {
  // Dynamically create class constructor.
  var clazz = function() {
    // Stupid JS need exactly one "operator new" calling for parent
    // constructor just after class definition.
    if (clazz.preparing) return delete(clazz.preparing);
    // Call custom constructor.
    if (clazz.constr) {
      this.constructor = clazz; // we need it!
      clazz.constr.apply(this, arguments);
    }
  }
  clazz.prototype = {}; // no prototype by default
  if (parent) {
    parent.preparing = true;
    clazz.prototype = new parent;
    clazz.prototype.constructor = parent;
    clazz.constr = parent; // BY DEFAULT - parent constructor
  }
  if (prop) {
    var cname = "constructor";
    for (var k in prop) {
      if (k != cname) clazz.prototype[k] = prop[k];
    }
    if (prop[cname] && prop[cname] != Object)
      clazz.constr = prop[cname];
  }
  return clazz;
}


Function.prototype.bind = function(obj) {
  var method = this,
   temp = function() {
    return method.apply(obj, arguments);
   };
 
  return temp;
 } 
 
 var isIE = false;
 if (typeof Element == "undefined") {
 	isIE = true;
 	var Element = function() {}
 	Element.prototype = {}; 
 }

 function $(id) {
	var obj =  wrapElem(document.getElementById(id));
	return obj;
}

function wrapElem(obj) {
	if (isIE) {
		for (var func in Element.prototype)
			obj[func] = Element.prototype[func];
		obj.__wrapped = true;
	}
	return obj;
}

function createDiv(className) {
	return createElem("div", className);
}

function createElem(tag, className, attributes) {
	if (tag == "input" && attributes && (attributes.type == "radio" || attributes.type == "checkbox") && attributes.name) { // IE bug with radio and checkbox elements
			var elem = document.createElement("<input type='" + this.type + "' name='" + this.name + "'>");
	} else
		var elem = document.createElement(tag);
	
	
	elem.show = window.showElem;
	elem.hide = window.hideElem;
	if (className)
		elem.className = className;
	
	if (attributes) {
		for (var attName in attributes)
			elem.setAttribute(attName, attributes[attName]);
	}
	
	return wrapElem(elem);	
}

if (!Array.prototype.push) {
	Array.prototype.push = function(elem) {
		this[this.length] = elem;
	}
}

Element.prototype.setText = function(text) {
	var node = document.createTextNode(text);
	this.appendChild(node);
}

addClass = function(elem, className) {
	if (!haveClass(elem, className))
		elem.className += " " + className;		
}

haveClass = function(elem, className) {
	var classes = elem.className.split(" ");
	for (var i = 0; i < classes.length; i++)
		if (classes[i] == className) return true;
	return false;
}

removeClass = function(elem, className) {
	var classes = elem.className.split(" ");
	var newClasses = new Array();
	for (var i = 0; i < classes.length; i++)
		if (classes[i] != className) newClasses.push(classes[i]);
	elem.className = newClasses.join(" ");
}


Element.prototype.addClass = function(className) {
	if (!this.haveClass(className))
		this.className += " " + className;		
}

Element.prototype.haveClass = function(className) {
	var classes = this.className.split(" ");
	for (var i = 0; i < classes.length; i++)
		if (classes[i] == className) return true;
	return false;
}


Element.prototype.removeClass = function(className) {
	var classes = this.className.split(" ");
	var newClasses = new Array();
	for (var i = 0; i < classes.length; i++)
		if (classes[i] != className) newClasses.push(classes[i]);
	this.className = newClasses.join(" ");
}

Element.prototype.getChildrenTagByClassName = function (tag, className) {
	var children = this.getElementsByTagName(tag);
	var result = new Array();
	for (var i = 0; i < children.length; i++) {
		var child = wrapElem(children[i]);
		if (child.haveClass(className))
			result.push (child);
	}
	return result;
}


function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function isArray(obj) {
    return obj.constructor == Array;
}


function isObject(o) {return (o && "object" == typeof o) || isFunction(o);}   

function isFunction(o) {return 'function' == typeof o;}   

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}




/*********************************************************************
 * Get an object, this function is cross browser
 * *** Please do not remove this header. ***
 * This code is working on my IE7, IE6, FireFox, Opera and Safari
 * 
 * Usage: 
 * var object = get_object(element_id);
 *
 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function get_object(id) {
	var object = null;
	if( document.layers )	{			
		object = document.layers[id];
	} else if( document.all ) {
		object = document.all[id];
	} else if( document.getElementById ) {
		object = document.getElementById(id);
	}
	return object;
}
/*********************************************************************/

/*********************************************************************
 * No onMouseOut event if the mouse pointer hovers a child element 
 * *** Please do not remove this header. ***
 * This code is working on my IE7, IE6, FireFox, Opera and Safari
 * 
 * Usage: 
 * <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
 *		So many childs 
 *	</div>
 *
 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}
function fixOnMouseOut(element, event, fn) {
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		fn(event);
	}
}


function dbg (str) {
	console.debug (str);
}
