//any page can add funcs to this array, that will be executed at page load
var onLoadFuncs = [];	 

//==============================================================================
// The onPageLoad() func is called for every page load. It's job is to call functions 
// needed for every page, plus call the VERY SPECIAL _onload() func, which is a
// page-level func that is only defined when a specific page needs onload 
// funtionality.
//==============================================================================

function onLoad() {
//this func calls js that needs to be executed on EVERY PAGE OF SITE

	//this loops through array (declared top of this page) of onload funcs that pages can add to 
	for (i=0; i < onLoadFuncs.length; i++) { 
		func = onLoadFuncs[i];
		eval(func + '();');
	}
}

function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function focus_first_field() {
	var set = false;
	for(i=0; i < document.forms[0].length; i++){
		if (document.forms[0][i].type != "hidden" && document.forms[0][i].disabled != true){
			document.forms[0][i].focus();
			var set = true;
		}
		if (set == true){
			break;
		}
	}
}

function inspect (o, cont) 
{
	if (!cont) cont = $('inspect');
	if (typeof(o) == 'string') { 
		$(cont).innerHTML = $(cont).innerHTML + "<p>"+ o +"</p>";
	} else {
		$(cont).innerHTML = $(cont).innerHTML + dump(o);
	}
}
/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects
     for(var item in arr) {
      var value = arr[item];
     
      if(typeof(value) == 'object') { //If it is an array,
       dumped_text += level_padding + "'" + item + "' ...\n";
       dumped_text += dump(value,level+1);
      } else {
       dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
      }
     }
    } else { //Stings/Chars/Numbers etc.
     dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
} 

function setOrder(field) {
	//get full url for page
	loc	= window.location.href; //string


    //if there are query params
    if (loc.include('?')) { //check needed because prototype will assume there is only one param (the url) with no value defined.  
  
        //turn query string into an object
        qo	= loc.toQueryParams();	

		//fix prototypes 'error' of converting '+'
		if (qo.filter) {												//fails if done with no filter present
			qo.filter = qo.filter.replace(/\+/g,' ');
		}
		
    } else {
  
        qo = {} //empty object

    }


	//if not already set, init 
	if (qo.order_by  	   != field ) {
		qo.order_by			= field;
		qo.order_direction	= 'desc';
	//if so, toggle direction
	} else { 
		if (qo.order_direction == 'desc') {
			qo.order_direction	= 'asc';
		} else {
			qo.order_direction	= 'desc';
		}
	}

	//Set page back to page 1, because sorting order has been changed.
	if (qo.page != 1) {
		qo.page = 1;
	}

	//turn object back into query string to append to url
	qs 	= $H(qo).toQueryString();

	//				  domain name                this is the dir and page         query string
	loc	= "http://" + window.location.hostname + window.location.pathname + "?" + qs;

	//go here
	window.location = loc;
}

function setPage(page,per_page) {
	//get full url for page
	loc	= window.location.href; //string


    //if there are query params
    if (loc.include('?')) { //check needed because prototype will assume there is only one param (the url) with no value defined.  
  
        //turn query string into an object
        qo	= loc.toQueryParams();	

		//fix prototypes 'error' of converting '+'
		if (qo.filter) {												//fails if done with no filter present
			qo.filter = qo.filter.replace(/\+/g,' ');
		}
		

    } else {
  
        qo = {} //empty object

    }


	if ( qo.page != page ) {
		qo.page = page;
	}
	if ( qo.per_page != per_page ) {
		qo.per_page = per_page;
	}

	//turn object back into query string to append to url
	qs 	= $H(qo).toQueryString();

	//				  domain name                this is the dir and page         query string
	loc	= "http://" + window.location.hostname + window.location.pathname + "?" + qs;

	//go here
	window.location = loc;
}
