/*------*/
function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function replaceHTML(el, htmlTree) {
  if (el != null) {
    clearText(el);
	for (var i = 0; i < htmlTree.childNodes.length; i++) {
		el.appendChild(htmlTree.childNodes[i]);
	}
  }
}

function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}

function simpleHtmlEncode(str){
	var pattern = [];
	pattern[0] = new Array(/&lt;/g,"<");
	pattern[1] = new Array(/&gt;/g, ">");
	pattern[2] = new Array(/&quot;/g, '"');	
	
	for(var i=0; i<pattern.length;i++){
		str = str.replace(pattern[i][0], pattern[i][1]);		
	}
  
	return str;
}


// http://www.somacon.com/p355.php

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


/*------*/
function enter(form){	
	fields=form.getElementsByTagName('input');
	var a=[];	
	//get active page
	var pages = byId('pages_b').getElementsByTagName('a');
	for(var ni=0; ni<pages.length; ni++){		
		if(pages[ni].className == 'active'){var next_p = pages[ni+1] || pages[0];}
		//else{var next_p = pages[0];}
	}

	// add ENTER listeners
	var re = new RegExp("\\bhidden\\b");
	
	for(var ii=fields.length; ii--;) {
		var m = re.exec(fields[ii].parentNode.className);
	
		if(fields[ii].type=="text" && fields[ii].disabled == false && m == null ) {
			_e(fields[ii], "onkeypress", enterListener);
			a[a.length]=fields[ii];
		}
	}
	
	a.reverse();
	// init the tabIndex behavior
	/*
	a.sort(
	function(a,b){
		return a.tabIndex > b.tabIndex ? 1 : -1;
	}
	);*/

	for(var j=0; j<a.length; j++){
		//a[j].next = a[j+1]||a[0];
		a[j].next = a[j+1]||next_p;
	}
	
	setFocus(form);
	// add the submission manager
	/*_e(
		form,
		"onsubmit",
		function(evt){
			if(form.hasEntered) {
				form.hasEntered=false;
				return false;
			}
			return true;
		}
	);*/

	// ENTER listener
	function enterListener(evt){
		evt=evt||window.event;
		var el=this;
		if( ((evt.keyCode||evt.which)==13) || ((evt.keyCode||evt.which)==9) ) {				
				
			//if(el.next.nodeName.toLowerCase() == 'a'){												
			//	setTimeout( function(){ 
			//		var isLastPage = (el.next.className.indexOf('pass') > -1)? true : false;
			//		if(check(el, isLastPage)){ if(!isLastPage){doFunction(pLinks[el.next.id]);}}
			//		
			//		}, 1);				
			//}else{
				setTimeout( function(){ if(check(el)){ el.next.focus();} }, 1);
			//}
		}		
		//alert((evt.keyCode||evt.which));
	}
}

// event manager
function _e(obj, evt, func){
	if(obj[evt]) {
		obj[evt]=(function(h){
			return function(evt){
				return func.call(this, evt) && h.call(this, evt);
			}
		})(obj[evt]);
	} else {
		obj[evt]=func;
	}
}

// http://www.dustindiaz.com/javascript-no-no/
function byId(el) {
  return document.getElementById(el);
}

// http://www.dustindiaz.com/javascript-no-no/
function toggle() {  for (var i=0, el; el = byId(arguments[i]); i++) {    el.style.display = (el.style.display != 'none' ? 'none' : '' );  }}

function show(){
  for (var i=0, el; el = byId(arguments[i]); i++) {
    el.style.display = 'block';
  }
}

function hide(){
  for (var i=0, el; el = byId(arguments[i]); i++) {
    el.style.display = 'none';
  }
}

function addLoadEvent(func) {	
	var oldonload = window.onload;	
	if (typeof window.onload != 'function') {		
		window.onload = func;	
	} else {
		window.onload = function() {
			oldonload();			
			func();		
			}	
	}
}

var addEvent = function() {
	if (window.addEventListener) {
		return function(el, type, fn) {
			el.addEventListener(type, fn, false);    
		};
	} else if (window.attachEvent) {
		return function(el, type, fn) {
			var f = function() {
				fn.call(el, window.event);      
			};      
		el.attachEvent('on' + type, f);    
		}; 
	}
}();


function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

/* grab Elements from the DOM by className */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
// taken from http://www.w3schools.com/jsref/jsref_onkeypress.asp
//adapted by tvorog
function noNorL(e,what)
	{
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	keychar = String.fromCharCode(keynum);
	if(keynum == 8){
		return keychar;
	}else{	
		numcheck = (what == 1)? /\d/: /[^\d]/;
		return numcheck.test(keychar);
	}
		return keychar;
}

/*----------------------------------*/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
	
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function createCookie(name,value,days) {
if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//from http://www.itnewb.com/v/Creating-the-Smooth-Scroll-Effect-with-JavaScript

function currentYPosition() {
  // Firefox, Chrome, Opera, Safari
  if (self.pageYOffset) return self.pageYOffset;
  // Internet Explorer 6 - standards mode
  if (document.documentElement && document.documentElement.scrollTop)
	  return document.documentElement.scrollTop;
  // Internet Explorer 6, 7 and 8
  if (document.body.scrollTop) return document.body.scrollTop;
  return 0;
}

function elmYPosition(eID) {
  var elm = document.getElementById(eID);
  var y = elm.offsetTop;
  var node = elm;
  while (node.offsetParent && node.offsetParent != document.body) {
	  node = node.offsetParent;
	  y += node.offsetTop;
  } return y;
}


function smoothScroll(eID) {
  var startY = currentYPosition();
  var stopY = elmYPosition(eID);
  var distance = stopY > startY ? stopY - startY : startY - stopY;
  if (distance < 100) {
	  scrollTo(0, stopY); return;
  }
  var speed = Math.round(distance / 100);
  var step = Math.round(distance / 25);
  var leapY = stopY > startY ? startY + step : startY - step;
  var timer = 0;
  if (stopY > startY) {
	  for ( var i=startY; i<stopY; i+=step ) {
		  setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
		  leapY += step; if (leapY > stopY) leapY = stopY; timer++;
	  } return;
  }
  for ( var i=startY; i>stopY; i-=step ) {

	  setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
  leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
  }
  return false;
}


/***************************************************************/

function clean_up_array(arr,sort){
	if(typeof sort == 'undefined'){sort = 1;}
	var newArray = new Array();
	for(var i = 0, l=arr.length; i< l;i++){
		var counter = newArray.length
		if(typeof arr[i] != 'undefined'){
			newArray[counter] = arr[i];
			counter++;
		}
	}
	if(sort == 1){newArray = sortBy(newArray, 3);}
	//newArray.sort(sortByinfinitive);
	return newArray;
}


shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};

function sortBy(array, f){
	function sorting(a, b) {
		if(typeof f == 'undefined' || f == ''){f = 3;}
	    var x = a[f];
	    var y = b[f];
	    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
	}
	array.sort(sorting);
	return array;
}