/*----------------------------------------------
	addLoadEvent() checks if an onload event has
	been added. If so, the passed function is
	appended to the event; if not, it becomes the 
	the onload event
------------------------------------------------*/
// Pass a function without the parentheses's: 'foo', not 'foo()'

function addLoadEvent(func) {
	var old_onload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			old_onload();
			func();
		}
	}
}


/*----------------------------------------------
	linkClass() is for the images with a class of
	"link". It loops thru all images to find them, 
	then attaches imgOver and imgOff functions
	to the onmouseover and onmouseout events.
	Also, preloads the rollover images.
------------------------------------------------*/
function linkClass() {
	var imgs = document.getElementsByTagName( "img" );
	for (var loop = 0; loop < imgs.length; loop++) {
		var img = imgs[loop];
		
		if (img.className == 'link') {
			var roll = new Image();
      if(img.src.indexOf('_on') == -1){
        img.onmouseover=imgOver;
  			img.onmouseout=imgOff;
        roll.src = img.src.replace(/(\.[a-z0-9]+)$/i,'_on$1');
      }else{
        img.onmouseover=imgOff;
        img.onmouseout=imgOver;
        roll.src = img.src.replace(/_on(\.[a-z0-9]+)$/i,'$1');
      }
		}
	}
}

function imgOver() {
  if(this.src.indexOf('_sel') == -1){
	  this.src = this.src.replace(/(\.[a-z0-9]+)$/i,'_on$1');
  }
}

function imgOff() {
  if(this.src.indexOf('_sel') == -1){
	  this.src = this.src.replace(/_on(\.[a-z0-9]+)$/i,'$1');
  }
}

function postLinkClass(imgID) {
  var img = document.getElementById(imgID); 
  if(img){
	  var roll = new Image();
    if(img.src.indexOf('_on') == -1){
      img.onmouseover=imgOver;
			img.onmouseout=imgOff;
      roll.src = img.src.replace(/(\.[a-z0-9]+)$/i,'_on$1');
    }else{
      img.onmouseover=imgOff;
      img.onmouseout=imgOver;
      roll.src = img.src.replace(/_on(\.[a-z0-9]+)$/i,'$1');
    }  
  }
  img = null;
  
}

function openwindow(url, name, options){
  var tWnd = window.open(url, name, options);
  if(tWnd){tWnd.focus();}
  tWnd = null;
}


function trim(thestring) { 
  return thestring.replace(/^\s+|\s+$/g, ''); 
}

function in_array (needle,haystack){ 
  return new RegExp('(^|\,)'+needle+'(\,|$)','gi').test(haystack);
} 

function get_vars(t_name_in) {
  var t_string = window.location.search.substring(1);
  var t_groups = t_string.split("&");
  var t_length = t_groups.length;
  for (i=0;i<t_length;i++) {
    var t_name = t_groups[i].split("=");
    if (t_name[0] == t_name_in) {
      return t_name[1];
    }
  }
}

//seconds to hours:minutes:seconds
function sec_to_hms(secs)
{
  var t = new Date(1970,0,1);
  t.setSeconds(secs);
  return t.toTimeString().substr(0,8);
}
   
/*
//more than 24 hours
function cheat2(secs)
{
var t = new Date(1970,0,1);
t.setSeconds(secs);
var s = t.toTimeString().substr(0,8);
if(secs > 86399)
s = Math.floor((t - Date.parse("1/1/70")) / 3600000) + s.substr(2);
return s;
}
*/
//different aproach
//function hms(secs){
//time=[0,0,secs];
//for(var i=2;i>0;i--){
//time[i-1]=Math.floor(time[i]/60);
//time[i]=time[i]%/**/60;
//if(time[i]<10)
//time[i]='0'+time[i];
//};
//return time.join(':')
//}


function resizeWindowToFit(w, h)
{
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	else return;
  
	if (frameWidth != w || frameHeight != h)
	{
		newWidth = w-frameWidth;
		newHeight = h-frameHeight;
    parent.window.resizeBy(newWidth,newHeight);
	}
}

function resizeWindowMaximum(pWindow){

  if(!pWindow){pWindow = window;}
  var newWidth = (screen.availWidth)?screen.availWidth:screen.width;
  var newHeight = (screen.availHeight)?screen.availHeight:screen.height;
  pWindow.moveTo(0,0);
  pWindow.resizeTo(newWidth,newHeight);
  delete(newWidth);
  delete(newHeight);
    
	if (self.innerWidth)
	{
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	else return;
  
  var ret = new Array();
  ret['width'] = frameWidth;
  ret['height'] = frameHeight;
  return ret;
}




function check_textarea_length(the_textarea_obj,max_length){
  if(the_textarea_obj.value.length>(max_length-1)){
    alert('You are at '+max_length+' characters');
    remove_extra(the_textarea_obj,max_length);
  }
}
function remove_extra(the_textarea_obj,max_length){
  the_textarea_obj.value = the_textarea_obj.value.substring(0,max_length);
} 

// ON LOAD
addLoadEvent(linkClass);

