var GUI = new Object();

GUI.find = function(elementId) {
  return document.getElementById(elementId);
}

GUI.show = function(ids, show) {

	if( ids )
	    for(var i = 0; i < ids.length; i++) {
	    
	    	var id = ids[i];
	    	
	    	if( id ) {
	        
			    var style = document.layers ? document.layers[id] :
			    document.getElementById ?  document.getElementById(id).style :
			    document.all[id].style;
			
			    style.display = show ? 'block' : 'none';
		    }
	    }
}

GUI.propagateDisable = function(disabled, fieldIds, hideableId, formName, radioGroupNames) {

    for(var i = 0; i < fieldIds.length; i++) {
        
        var field = this.find(fieldIds[i]);
        if( field )
            field.disabled = disabled;
    }
    
    if( formName && radioGroupNames ) {

	    for(var i = 0; i < radioGroupNames.length; i++) {

			var radioGroup = this.find(formName)[radioGroupNames[i]];
			
			for(var j = 0; j < this.getRadioGroupLength(radioGroup); j++ )
		   		this.getRadio(radioGroup, j).disabled = disabled;
		}
    }
    
    if( hideableId )
        this.show([hideableId], !disabled)
}

GUI.refreshOpener = function() {

	if( opener && !opener.closed && typeof(opener.refreshPage) != 'undefined' ) {
	
		opener.refreshPage();
		window.focus();
	}		
}

GUI.getSelectedIndex = function(radioGroup) {

    for(var i = 0; i < this.getRadioGroupLength(radioGroup); i++ )
        if( this.getRadio(radioGroup, i).checked )
            return i;
            
    return -1;            
}

GUI.getRadio = function(radioGroup, index) {
	return typeof(radioGroup.length) == 'number' ? radioGroup[index] : index == 0 ? radioGroup : null;
}

GUI.getRadioGroupLength = function(radioGroup) {
	return typeof(radioGroup.length) == 'number' ? radioGroup.length : radioGroup != null ? 1 : 0;
}

GUI.actionSelectorClicked = function(formName, radioGroupName, index) {

	var radioGroup = this.find(formName)[radioGroupName];
	
    for(var i = 0; i < this.getRadioGroupLength(radioGroup); i++ ) {
    
    	var div = this.find(radioGroupName+i);
    	div.className = i == index ? 'selected' : 'unselected';
    	
    	if( i == index )
    		this.getRadio(radioGroup, i).checked = true;
    }
    
	return false;
}

function ActionSelectorPopupWindowRedirector(formName, radioGroupName, redirectors) {

	function getRedirector() {
        
		var index = GUI.getSelectedIndex(GUI.find(formName)[radioGroupName]);
	
		if( 0 <= index && index < redirectors.length )
			return redirectors[index];
	
	    return null;
	}
	
	this.getWindowName = function() {
	
		var redirector = getRedirector();
		return redirector ? redirector.getWindowName() : '';
	}

	this.getWindowFeatures = function() {
	
		var redirector = getRedirector();
		return redirector ? redirector.getWindowFeatures() : '';
	}
	
	this.needsRedirect = function() {
	
		var redirector = getRedirector();
		return redirector ? redirector.needsRedirect() : false;
	}
	
	this.preActivate = function(form) {
	
		var redirector = getRedirector();
		if( redirector )
			redirector.preActivate(form);
	}
	
	this.postActivate = function(form) {
	
		var redirector = getRedirector();
		if( redirector )
			redirector.postActivate(form);
	}
}

GUI.addOnLoad = function(function_) {

	var old = window.onload;
	
	if( typeof old != 'function' )
	    window.onload = function_;
	
	else
	    window.onload = function() {
	        if( old )
	            old();
	        function_();
	    }
}

GUI.addOnKeyDown = function(input, function_) {

	var old = input.onkeydown;
	
	if( typeof old != 'function' )
	    input.onkeydown = function_;
	
	else
	    input.onkeydown = function() {
	        if( old )
	            old();
	        function_();
	    }
}

GUI.addInputBackgroundHintHandler = function(id) {
    
    GUI.addOnLoad(function() {

        var input = Tapestry.find(id);
        
        if( input.value )
            input.style.backgroundImage = 'none';
    });
    
    GUI.addOnKeyDown(Tapestry.find(id), function() {
		this.style.backgroundImage = 'none';
    });
}                


