/*
Add an onload event. This is to allow multiple add load events
*/
function addLoadEvent(func) {
		var oldonload = window.onload;
  		if (typeof window.onload != 'function') {
  		  window.onload = func;
	  } else {
	    window.onload = function() {
	      oldonload();
	      func();
	    }
	  }  
	}

function addUnloadEvent(func){
	var oldunload = window.onunload;
	if (typeof window.onunload != 'function'){
		window.onunload = func;
	}else{
		window.onunload = function(){
			var $val1 = oldunload();
			var $val2 = func();
			return $val1 && $val2;
		}
	}
}

/*
Show a popup window
Title must NOT have any spaces in it
*/
function popup(url,title,height,width){
	
	if (! height){ height = 350; }		

	if (! width){ width = 450; }

	var str_options = 'width=' + width + ',height=' + height + ',resizable,scrollbars';

	str_options = str_options + ',left=300,top=300';

	var imgWindow = window.open(url,title,str_options);
	imgWindow.focus();
}

var previousStyle;
var handType;

function tableruler(){
	
	//set the hand type
 	if (navigator.appName.indexOf("Microsoft")!=-1)
 		handType = 'hand';
 	else
 		handType = 'pointer';
	
	if (document.getElementById && document.createTextNode){
	   var tables=document.getElementsByTagName('table');
	   for (var i=0;i<tables.length;i++){
		   if(tables[i].className.indexOf('ruler') != -1){
				var trs=tables[i].getElementsByTagName('tr');
				for(var j=0;j<trs.length;j++){
					if(trs[j].parentNode.nodeName=='TBODY' && trs[j].parentNode.nodeName!='TFOOT' && trs[j].className.indexOf('norule') == -1){
						
						 if (trs[j].onclick){
						 	var tds=trs[j].getElementsByTagName('td');
							for(var k=0;k<tds.length;k++){
								if (!tds[k].onmouseover)
									tds[k].onmouseover=function(){this.style.cursor=handType;};
							}
							
						 }
						
						trs[j].onmouseover=function(){previousStyle=this.className;this.className='ruled ' + this.className;return false}
						trs[j].onmouseout=function(){this.className=previousStyle;return false}
					}
				}
			}
			if(tables[i].className.indexOf('stripe') != -1)
				stripe(tables[i]); //call the strip function
		}
	}
}

  // this function is needed to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
	  //if (obj.getAttribute("class") != null) {
     //    result = obj.getAttribute("class");
     //}

	  return (obj.className != '');
  }   

 function stripe(table) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenStyle = arguments[1] ? arguments[1] : "altOne";
    var oddStyle = arguments[2] ? arguments[2] : "altTwo";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    //var table = document.getElementById(id);
    //if (! table) { return; }
	 
	 hasClass(table);
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");
	 
    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
	 
	     
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {
		
		  // avoid rows that have a class attribute
        // or backgroundColor style
        //if (! hasClass(trs[i]) &&
        //   ! trs[i].style.backgroundColor) {
        if (! trs[i].style.backgroundColor){
			var $new_class = even ? evenStyle : oddStyle;
			if (hasClass(trs[i])){
				$new_class = trs[i].className + ' ' + $new_class;
			}
			trs[i].className = $new_class;
 		   /*
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
          			 
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }*/
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }

function viewexisting(cbo_id,path){

	var cbo = document.getElementById(cbo_id);
	var option = cbo.options[cbo.selectedIndex];
	var filename = option.value;

	if (! filename){
		filename = option.innerHTML;
	}
	
	if(path.charAt(path.length - 1) != '/'){
		path += '/';
	}

	popup(path + filename,'image',100,300);

	
}

function setexisting(id){
	var cbo = document.getElementById(id + '-existing');
	var option = cbo.options[cbo.selectedIndex];

	var new_val = option.value;

	if (! new_val){
		new_val = option.innerHTML;
	}

	document.getElementById(id + '-old').value = new_val;

	var image = document.getElementById(id + '-image');

	if (image){
		var src = image.src;
		src = src.substr(0,src.lastIndexOf('/'));
		src += '/' + new_val;
		image.src = src;
		
	}
	var link = document.getElementById(id + '-link');
	if (link){
		link.innerHTML = new_val;
		var href = link.href;
		href = href.substr(0,href.lastIndexOf('/'));
		href += '/' + new_val;
		link.href = href;
	}


	id = id.substr(4);
}

function setexistingfile($id,$image,$path){
	var $cbo = document.getElementById($id + '-existing');
	var $option = $cbo.options[$cbo.selectedIndex];

	var $new_val = $option.value;

	if (! $new_val){
		$new_val = $option.innerHTML;
	}

	document.getElementById($id + '-old').value = $new_val;

	if ($image){
		var $image_element = document.getElementById($id + '-image');
		if (! $image_element){
			//The image element does not exist, create it
			$image_element = document.createElement('img');
			$image_element.id = $id + '-image';
			var $container = document.getElementById($id).parentNode;
			$container.insertBefore($image_element,$container.firstChild);
			$container.insertBefore(document.createElement('br'),$image_element.nextSibling);
		}

		$image_element.src = $path + $new_val;
	}
}

function getElementsByClass($classname,$node,$tag_type){

	if (! $node){
		$node = document;
	}

	if (! $tag_type){
		$tag_type = '*';
	}

	var $alltags=$node.all? $node.all : $node.getElementsByTagName($tag_type)
		
	var $inc=0;
	var $array = Array();
	var $num_items = 0;
	for (var $i=0; $i < $alltags.length; $i++){
		
		var $css_class = $alltags[$i].className;

		if ($css_class){
			var $css_tags = $css_class.split(' ');

			for (var $x=0; $x < $css_tags.length; $x++){
				if ($css_tags[$x] == $classname){
					$array[$num_items++] = $alltags[$i];
					break;
				}
			}
		}
	}

	return $array;
}

function empty_select($select){
	while ($select.childNodes.length > 0){
		$select.removeChild($select.firstChild);
	}
}

function selectRadioButton( id ) {
    var r = document.getElementById(id);
    r.checked = true;
}


