/**
 * @author Rui Cruz
 */

	function stopTMCE(object)
	{
		try
		{

			tinyMCE.execInstanceCommand(object, 'mceFocus');
			current_value = tinyMCE.getContent();
			
			tinyMCE.execCommand("mceRemoveControl", false, object);
			object.value = current_value;
			
		} 
		catch(e) 
		{
			//alert(e);
		}

	}

	/**
	 * After innerHtml changes it's necessary to start the tinyMCE control
	 * @param {Object} object
	 */
	function startTMCE(object)
	{
		
		tinyMCE.idCounter=0;
    	tinyMCE.execCommand('mceAddControl', true, object);	
		
	}	
	
	/**
	 * Populate a combobox with values (UNFINISHED)
	 * @param {Object} combobox
	 * @param {Object} values
	 */
	function populateCombo(combobox, values)
	{
		
		box = $(combobox);
		
		
		for (x=0; (x<values.ids.length); x++)
		{
		
			box.options[ box.options.length ] = new Option(values.names[x], values.ids[x]);
		
		}
	}
	
	/**
	 * Define the checked state of a bunch of checkboxes from a given form
	 * 
	 * @param {Object} FormName
	 * @param {Object} FieldName
	 * @param {Object} CheckValue
	 */
	function SetAllCheckBoxes(FormName, FieldName, CheckValue) 
	{	
		if(!$(FormName))
			return false;
			
		var objCheckBoxes = document.forms[FormName].elements[FieldName];
		if(!objCheckBoxes) 
			return false;
		var countCheckBoxes = objCheckBoxes.length;
		if(!countCheckBoxes)
			objCheckBoxes.checked = CheckValue;
		else
			// set the check value for all check boxes
			for(var i = 0; i < countCheckBoxes; i++)
				objCheckBoxes[i].checked = CheckValue;
				
		return true;
	}
	
	/**
	 * Get a variable from the querystring
	 * @param string variable
	 */
	function getQueryVariable(variable) 
	{
		
	  var query = window.location.search.substring(1);
	  var vars = query.split("&");
	  for (var i=0;i<vars.length;i++) 
	  {
	    var pair = vars[i].split("=");
	    if (pair[0] == variable) {
	      return pair[1];
	    }
	  } 
	}


	/**
	 * Navigate to a new URL
	 * 
	 * @param string url
	 */
	function navToUrl (url) 
	{	
		window.location.href = url;		
	}
		
	/**
	 * Open link in another browser window with confirmation
	 * @param string link
	 */
	function navToExtUrl (url, message) 
	{
		if (confirm(message) == true) 
		{
			open('http://' + url, 'externallink');
		}	
	}
	
	/**
	 * Open link in the same browser with confirmation
	 * @param string url
	 * @param string message
	 */
	function confirmNavToUrl (url, message) 
	{
		if (confirm(message) == true) 
		{
			navToUrl(Url);
		}			
	}
	
	/**
	 * Submit form with confirmation
	 * @param string form
	 * @param string Message
	 */
	function confirmForm(formname, Message) 
	{	
	  if (confirm(Message)) 
	  {
	    formname = document.getElementById(formname);
		/*formname.Send.value = 'true';*/
	    formname.submit();
		
	  }
	}	
	
	/**
	 * Open link in parent window. Good for popups
	 * @param string url
	 */
	function parentNavToUrl (url) 
	{
		window.opener.location = url;
	}	
	
	
	function highlightRow (element, color) {
		
		while (element.tagName.toUpperCase() != 'TR' && element != null)
    		element = document.all ? element.parentElement : element.parentNode;
  		if (element)
    		element.bgColor = color;
	}
	

	/**
	 * Toggles the visibility of an object
	 * @param {Object} objecto
	 */
	function TogglePanel(objecto) {
	
		var itemtotoggle = $(objecto);
	
		if (itemtotoggle.className == 'visible') {
			itemtotoggle.className = 'hidden';
		} else {
			itemtotoggle.className = 'visible';
		}
	}
	
	/**
	 * Add url to browser favorites. Works not just in IE
	 */
	function AddFav() 
	{		
		if( window.sidebar && window.sidebar.addPanel ) {
			//Gecko (Netscape 6 etc.) - add to Sidebar
			window.sidebar.addPanel( this.title, this.href, '' );
		} else if( window.external && ( navigator.platform == 'Win32' || ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) ) {
			//IE Win32 or iCab - checking for AddFavorite produces errors for no
			//good reason, so I use a platform and browser detect.
			//adds the current page page as a favourite; if this is unwanted,
			//simply write the desired page in here instead of 'location.href'
			window.external.AddFavorite( location.href, document.title );
		} else if( window.opera && window.print ) {
			//Opera 6+ - add as sidebar panel to Hotlist
			return true;
		} else if( document.layers ) {
			//NS4 & Escape - tell them how to add a bookmark quickly (adds current page,
			//not target page)
			window.alert( 'Please click OK then press Ctrl+D to create a bookmark' );
		} else {
			//other browsers - tell them to add a bookmark (adds current page, not target page)
			window.alert( 'Please use your browser\'s bookmarking facility to create a bookmark' );
		}
		return false;			
	}
    
    function colorTables()
    {
    
        $$("table").each(function(table){
          Selector.findChildElements(table, ["tr"])
            .findAll(function(row,i){ return i % 2 == 1; })
            .invoke("addClassName", "odd");
        });    
        
    }
	
	/**
	 * Makes all links in a page with a class popup open in a new window
	 */
	function doPopups() {
	  if (!document.getElementsByTagName) return false;
	  var links = document.getElementsByTagName("a");
	  for (var i=0; i < links.length; i++) {
		if (links[i].className.match("popup")) {
		  links[i].onclick = function() {
			window.open(this.href);
			return false;
		  }
		}
	  }
	}
    
    /**
     * Define the selected item on a combobox given a value
     */
    function selectCombo(combo_name, value)
    {
    
        var combo = document.getElementById(combo_name);

        for(var i=0; i < combo.options.length; i++)
        {
        
            if (combo.options[i].value == value)
            {
            
                combo.options.selectedIndex = i;            
            
            }        
        
        }

    }    