var loadTabContentRequest = null;
var activeTab = null;

function changeActiveTab( tabcontroller, tabid, url, callback, forceReload, iframe ) {
	if( document.getElementsByName( tabcontroller ) != null ) {
		var tabs = document.getElementsByName( tabcontroller );

		//If the current tab is already visible - then hide while updating contents.
		$(tabid).hide();
		//Show the loading dialog		
		$(tabid + '_loading').show();
		
		for( i = 0 ; i < (tabs.length) ; i++ ) {
			try {
				if( tabs[i].value != tabid && document.getElementById('TAB_' + tabs[i].value ) != null ) {
					document.getElementById('TAB_' + tabs[i].value ).className = '';
					$(tabs[i].value).hide();
				}
			} catch( e ) {
				alert( e.message );
			}
		}
		
		//Show the current tab
		document.getElementById('TAB_' + tabid).className = 'current';
	
		activeTab = document.getElementById( tabid );
		
		var fReload = false;
		
		if( typeof forceReload != "undefined") {
			try {
				eval("fReload = " + forceReload + "();");
			} catch(e) {}
		}
		
		var displayInIFrame = false;
		
		if( typeof iframe != "undefined") {
			displayInIFrame = iframe;
		}		

		if( displayInIFrame ) {
			document.getElementById( tabid + '_iframe' ).src = url;		
		} else if( activeTab.innerHTML == '' || fReload ) {
			if( url != null && url != '' && url.length > 0 ) {
				if( callback != null  ) {
					eval("loadTabContentRequest = new Ajax.Request('"+url+"', {method: 'get', parameters: '&responseId=" + tabid + + '&iframe=' + displayInIFrame + "&callBack="+callback+"', onComplete: handleLoadTabContentAJAXResponse} )");							
				} else {
					loadTabContentRequest = new Ajax.Request( url, {method: 'get', parameters: '&responseId=' + tabid + '&iframe=' + displayInIFrame, onComplete: handleLoadTabContentAJAXResponse} );				
				}
			} 
		} else {
			//Show the tab content
			$(tabid).setStyle({'visibility':'visible'});
			$(tabid).show();
			//Hide the loading dialog
			$(tabid + '_loading').hide();
		}
	}
}

var responseObjects = new Array();

function handleLoadTabContentAJAXResponse( res ) {	
	if( res.status == 200 ) {
		$(res.request.parameters.responseId).setStyle({'visibility':'visible'});	
		$(res.request.parameters.responseId).show();
		$(res.request.parameters.responseId + '_loading').hide();
		
		var index = responseObjects.length;
		responseObjects[responseObjects.length] = res;
		
		//If a specific callback has been provided, then call that method.
		if( typeof res.request.parameters.callBack != "undefined" ) {
				eval( res.request.parameters.callBack + "("+index+")" );
		} else {
			document.getElementById(res.request.parameters.responseId).innerHTML = res.responseText;		
		}
		
		onTabContentChanged.fire();
	}
}

function handleIFrameContentLoaded( frame, tabId, minHeight ) {
	if( tabId == activeTab.id ) {
		$(tabId).setStyle({'visibility':'visible'});	
		$(tabId).show();
		$(tabId + '_loading').hide();	
		if( document.getElementById( frame.id ).scrolling == 'no' ) {
			var height = adjustIFrameHeight( frame.id, minHeight );		
		}
	}
}	

function adjustIFrameHeight(id, minHeight) {
    var h = 0;    
    var browser = navigator.userAgent;
    if( typeof minHeight == 'undefined' ) {
    	minHeight = 0;
    }
    if (browser.toLowerCase().indexOf('safari') > 0) {
        document.getElementById(id).height = h;
        h = document.getElementById(id).contentDocument.height;
        if( h < parseInt(minHeight) ) {
        	h = parseInt(minHeight);
        }
        document.getElementById(id).height = h + 60 + 'px';
    } else {
        if (document.getElementById && !(document.all)) {
            h = document.getElementById(id).contentDocument.body.scrollHeight;

	        if( h < parseInt(minHeight) ) {
	        	h = parseInt(minHeight);
	        }
            document.getElementById(id).height = h + 60 + 'px';
        } else if(document.all) {
            h = document.frames[id].document.body.scrollHeight;
	        if( h < parseInt(minHeight) ) {
	        	h = parseInt(minHeight);
	        }
            document.getElementById(id).style.height = h + 20 + 'px';
        }
    }
    return h + 60 + 'px';
} 

