/**************************************************************************************************************
** This is intended to contain general utilities that could be used throughout myRSNA as well as any 
** functions that are used to manage the top level of tabs or intereact with the widgets within the
** myRSNA tab.
**************************************************************************************************************/
var commonspot_headerheight = 110;
var commonspot_footerheight = 80;
var tabsHeight = 0;




/************************************************************************************************************
	GENERAL UTILITIES
************************************************************************************************************/

function detectHeight() {
        var wh = 0;
        if (typeof(window.innerHeight)=="number") {
                wh = window.innerHeight;
        }else{	if(document.documentElement && document.documentElement.clientHeight) {wh = document.documentElement.clientHeight;}
			else if (document.body && document.body.clientHeight) {wh = document.body.clientHeight;}
        }
        return wh;
} 


function detectWidth() {
        var wh = 0;
        if (typeof(window.innerWidth)=="number") {
                wh = window.innerWidth;
        }
        else {
                if (document.documentElement && document.documentElement.clientWidth) {
                        wh = document.documentElement.clientWidth;
                }
                else {
                        if (document.body && document.body.clientWidth) {
                                wh = document.body.clientWidth;
                        }
                }
        }
        return wh;
} 


function getHeight() {
        return detectHeight();
} 

function getWidth() {
        return detectWidth();
}


function setTabsHeight(){
	//return detectHeight() - commonspot_headerheight - commonspot_footerheight; //USE WHEN COMMOMSPOT FOOTER IS USED.
	return detectHeight() - commonspot_headerheight;
}


function writejs(jsfile){
	var hasScript = false;
					    
	for(var i=0;i < document.getElementsByTagName('script').length;i++){
		if(document.getElementsByTagName('script')[i].src.split(jsfile).length >1){
			hasScript = true;
		}
	}// end for
	
	if(!hasScript){
		try{
			var script = document.createElement('script');
			script.src = jsfile;
			script.type = 'text/javascript';
			script.defer = true;
			var head = document.getElementsByTagName('head').item(0);
			head.appendChild(script);
		}catch(e){
			// inserting via DOM fails in Safari 2.0, so brute force approach
			document.write('<script type="text/javascript" src="' + jsfile +'">' + String.fromCharCode(60) + '/script' + String.fromCharCode(62) );
		}							
	}// end if		
}	
	

function writecss(cssfile){
	var hasCss = false;
					    
	for(var i=0;i < document.getElementsByTagName('link').length;i++){
		if(document.getElementsByTagName('link')[i].href.split(cssfile).length >1){
			hasCss = true;
		}
	}// end for	
	
	if(!hasCss){
		try{
			var css = document.createElement('link');
			css.href = cssfile
			css.type = 'text/css';
			css.rel = 'stylesheet';	
			var head = document.getElementsByTagName('head').item(0);
			head.appendChild(css);
		}catch(e){
			// inserting via DOM fails in Safari 2.0, so brute force approach
			document.write('<link rel="stylesheet" type="text/css" href="' + cssfile +'">' + String.fromCharCode(60) + '/link' + String.fromCharCode(62) );
			
		}	
	}// end if	
}	


function executeJavascript(divid){
	//execute any javascript
	var d=document.getElementById(divid).getElementsByTagName("script");

	for(var x=0;x<d.length;x++) {
		try{eval(d[x].text)}catch(e){};
	}// end for		
}


function handleActionFailure(msg){
	var msg = '<div align="center">' + msg + '</div>';
	Ext.Msg.show({
			title: 'Failure!'
			,msg: msg
			,minWidth:300
			,buttons: Ext.MessageBox.OK
	});
}

	
function launchExtWindow(url,title,h,w,iconCls,bookmark){
// Bookmark is a boolean variable that defaults to false.  When set to true, a button will be 
// created at the footer of the window to bookmark the URL that was sent.
	
	var browser_h = getHeight();	
	var browser_w = getWidth();	
	
	if(h == undefined || null){
		var h = browser_h - 50;
	}
	
	if(w == undefined || null){
		var w = browser_w - 50;
	}	
	
	var randomnumber=Math.floor(Math.random()*1001)
	var winid = 'win' + randomnumber;	
	var iframeid = 'if' + randomnumber;	
	bookmark = (bookmark == undefined || bookmark != true ? false : true);
	
	//if the height of the window is greater than that of the browser, resize the window and maintain the proportions.
	if(h >= browser_h){
		var ratio = (browser_h-50)/h;
		h = browser_h - 50;
		w = Math.round(ratio*w)-1;
	}
	
	if(iconCls == undefined || null){
		var iconCls = '';	
	}
	
	if(Ext.value(bookmark,false)){
		var new_bbar = new Ext.Toolbar({
						id:'window_bbar'+winid,
						autoWidth : true,
						cls:'x-panel-header',
						items:[ 		
							new Ext.Toolbar.Button({iconCls:'bm-add-url'
											   ,id:'bm_add_url'+winid
											   ,text:'Add to myBookmarks'
											   ,listeners:{'click': function(buttone,e){
																	   	bookmark_popup('URL','myRSNA',title,url);
																	   }
											   			}
											})
							]								
						});
	}else{
		var new_bbar = new Ext.Toolbar({
						id:'window_bbar'+winid,
						autoWidth : true,
						cls:'x-panel-header',
						hidden:true
					});	
	}
	
	var active_win = Ext.WindowMgr.getActive();
	var zindex = (active_win != null ? active_win.el.dom.style.zIndex : 0);

	launchwin = new Ext.Window(
		{
			id:winid,
			title:title,
			width:w,
			height:h,
			randnum:randomnumber,
			modal:true,
			autoScroll:true,
			iconCls:iconCls,
			bbar: new_bbar,
//			items:[new Ext.ux.ManagedIframePanel({id:iframeid,cls:'x-window-body',width:'100%',height:'100%',defaultSrc:url,loadMask:'Loading...'})]

			items:[
				  new Ext.ux.ManagedIframePanel({
						id:iframeid,cls:'x-window-body'
						,width:'100%'
						,height:'100%'
						,defaultSrc:url
						,loadMask:'Loading...'
					})						  
				]
			,listeners:{'show':function(win){if(active_win != null){active_win.el.dom.style.zIndex = '8000';}}
					,'close':function(win){if(active_win != null){active_win.el.dom.style.zIndex = zindex;}}
			}
		}
	).show();
	
	var thewindow = Ext.WindowMgr.get(winid);
	var ipanel = thewindow.findById(iframeid);
	ipanel.setHeight(thewindow.getInnerHeight());
	
	var bbar = thewindow.getBottomToolbar();
	if(bbar.items.lenght > 0 && bbar.items.items[bbar.items.length-1].id == 'bm_add_url'+winid){
		bbar.el.dom.firstChild.width = '100%';
		bbar.items.items[bbar.items.length-1].el.dom.parentNode.align = 'right';
		bbar.items.items[bbar.items.length-1].el.dom.parentNode.width = bbar.el.dom.clientWidth-150;		
	}
}


function launchHWWindow(args){
	Ext.Ajax.request({
			url:'/myfiles/update_customer_file.cfm'
			,method:'POST'
			,params:{action:'GETHWTOKEN'}
			,success:function(responseObj,optionsObj){
					var json = eval(responseObj.responseText)[0];
					if(json.success && Ext.value(json.hwtoken,'') != '' && Ext.value(json.memno,'') != ''){
						var h = getHeight()-50;
						var w = getWidth()-50;
						var URL = 'http://publications.rsna.org/cgi/externalLogin?url='+args.article_link+'&key='+json.hwtoken+'&memno='+json.memno
						launchExtWindow(URL,args.title,h,w,'tree-URL',true);
					}else{
						handleActionFailure('Sorry, there was an error while retrieving the article');
					}
				}
		});			
}



function launchJournalWindow(url,title,h,w){
	Ext.Ajax.request({
		url:url
		,method:'GET'
		,success:function(responseObj,optionsObj){
				launchwin = new Ext.Window(
					{	title:title,
						width:w,
						autoHeight:true,
						autoScroll:true,
						html: responseObj.responseText
					}
				).show();
			}
			
		,failure:function(responseObj,optionsObj){
			}
	});	
}



/************************************************************************************************************
	MESSAGE CENTER
************************************************************************************************************/

function launchMessageCenterWindow(h,w,modal,optout){
	var browser_h = getHeight();	
	var browser_w = getWidth();	
	
	if(h >= browser_h){
		var ratio = (browser_h-50)/h;
		h = browser_h - 50;
		w = Math.round(ratio*w)-1;
	}	

	modal = ((modal == undefined || modal == '') ? false : true);
	optout = ((optout == undefined || optout != false) ? true : false);
	
	var bbar = new Array();
	bbar.push(
		new Ext.Toolbar.Button(
			{	id:'first_message_button',
				text:'&nbsp;&nbsp;&nbsp;',
				listeners:{'click': handleChangeMessage},
				icon:myRSNApath + 'images/page-first.gif',
				tooltip:'View the first message'						
			})
		
		,new Ext.Toolbar.Button(
			{	id:'previous_message_button',
				text:'&nbsp;&nbsp;&nbsp;',
				listeners:{'click': handleChangeMessage},
				icon:myRSNApath + 'images/page-prev.gif',
				tooltip:'View the previous message'						
			})
		
		,new Ext.Toolbar.TextItem(
			{	id:'message_counter_text',
				text:'of'
			})
		
		,new Ext.Toolbar.Button(
			{	id:'next_message_button',
				text:'&nbsp;&nbsp;&nbsp;',
				listeners:{'click': handleChangeMessage},
				icon:myRSNApath + 'images/page-next.gif',
				tooltip:'View the next message'						
			})
		
		,new Ext.Toolbar.Button(
			{	id:'last_message_button',
				text:'&nbsp;&nbsp;&nbsp;',
				listeners:{'click': handleChangeMessage},
				icon:myRSNApath + 'images/page-last.gif',
				tooltip:'View the last message'						
			})	
		);
	
	if(optout){
		bbar.push(						

			new Ext.Toolbar.Button(
				{	id:'message_center_optout',
					text:'Don\'t show me Messages',
					listeners:{'click': handleMessageOptout},
					icon:'/images/delete.png',
					cls:"x-btn-text-icon",
					tooltip:'Choose to opt-out of future messages'
				})	
			);
	}

	launchwin = new Ext.Window(
		{
			id:'message_center_window'
			,title:'myRSNA Tips'
			,width:w
			,height:h
			,modal:modal
			,autoScroll:true
			,json_id:0
			,header:true	
			,items:[new Ext.ux.ManagedIframePanel({id:'message_center_panel',width:'100%',height:'100%'})]
			,bbar: bbar
		}
	);

	jstore = new Ext.data.JsonStore({
						id:'message_center_jstore'			
						,url: myRSNApath + 'plugins/message_center/messages.json.cfm'
						, root:'messages'
						,fields:['mc_id','mc_title','mc_url','mc_pubdate','mc_urgent'] 
						,autoLoad:true
						,listeners: {load: handleMessageStoreLoad}
					});	

}

function handleMessageStoreLoad(thebutton, theevent){
	var thepanel = launchwin.findById('message_center_panel');	
	var therecord = jstore.getAt(launchwin.json_id);
	url = therecord.data.mc_url;

	launchwin.show();
	thepanel.setSrc(url);	
	thepanel.setHeight(launchwin.getInnerHeight());	
	thepanel.setWidth(launchwin.getInnerWidth());
	
	launchwin.setTitle('myRSNA Tips:  ' + therecord.data.mc_title);
	var bbar = launchwin.getBottomToolbar();
	
	var optout_index = bbar.items.indexOfKey('message_center_optout');
	if(optout_index > -1){
		bbar.el.dom.firstChild.width='100%';
		bbar.items.items[optout_index].el.dom.align = 'right';
		bbar.items.items[optout_index].el.dom.parentNode.width = bbar.el.dom.clientWidth-125;
	}

	bbar.items.items[bbar.items.indexOfKey('previous_message_button')].disable();
	bbar.items.items[bbar.items.indexOfKey('first_message_button')].disable();	
	bbar.items.items[2].destroy();
	bbar.insertButton(2,new Ext.Toolbar.TextItem(
						{	id:'message_counter_text',
							text:launchwin.json_id + 1 +' of ' + jstore.getTotalCount()
						})
					 );	
}

function handleChangeMessage(thebutton, theevent){
	var thewindow = Ext.WindowMgr.get('message_center_window');
	var thepanel = thewindow.findById('message_center_panel');	
	var bbar = thewindow.getBottomToolbar();
	
	if(thebutton != undefined && thebutton.id != 'message_center_jstore'){
		if(thebutton.id == 'next_message_button'){
			thewindow.json_id = thewindow.json_id + 1;		
		}else if(thebutton.id == 'previous_message_button'){
			thewindow.json_id = thewindow.json_id - 1;
		}else if(thebutton.id == 'first_message_button'){
			thewindow.json_id = 0;
		}else if(thebutton.id == 'last_message_button'){
			thewindow.json_id = jstore.getTotalCount()-1;
		}
	}

	var therecord = jstore.getAt(thewindow.json_id);
	url = therecord.data.mc_url;
	thepanel.setSrc(url);
	
	thewindow.setTitle('myRSNA Tips:  ' + therecord.data.mc_title);
	bbar.items.items[2].destroy();
	bbar.insertButton(2,new Ext.Toolbar.TextItem(
						{	id:'message_counter_text',
							text:thewindow.json_id + 1 +' of ' + jstore.getTotalCount()
						})
					 );
	if(thewindow.json_id == 0){
		bbar.items.items[bbar.items.indexOfKey('previous_message_button')].disable();
		bbar.items.items[bbar.items.indexOfKey('first_message_button')].disable();
		bbar.items.items[bbar.items.indexOfKey('next_message_button')].enable();
		bbar.items.items[bbar.items.indexOfKey('last_message_button')].enable();
	} else if(thewindow.json_id == jstore.getTotalCount()-1){
		bbar.items.items[bbar.items.indexOfKey('previous_message_button')].enable();
		bbar.items.items[bbar.items.indexOfKey('first_message_button')].enable();
		bbar.items.items[bbar.items.indexOfKey('next_message_button')].disable();
		bbar.items.items[bbar.items.indexOfKey('last_message_button')].disable();
	}else if(jstore.getTotalCount() == 1){
		bbar.items.items[bbar.items.indexOfKey('previous_message_button')].disable();
		bbar.items.items[bbar.items.indexOfKey('first_message_button')].disable();
		bbar.items.items[bbar.items.indexOfKey('next_message_button')].disable();
		bbar.items.items[bbar.items.indexOfKey('last_message_button')].disable();
	}else{
		bbar.items.items[bbar.items.indexOfKey('previous_message_button')].enable();
		bbar.items.items[bbar.items.indexOfKey('first_message_button')].enable();
		bbar.items.items[bbar.items.indexOfKey('next_message_button')].enable();
		bbar.items.items[bbar.items.indexOfKey('last_message_button')].enable();
	}
	
	thewindow.doLayout();	
}

function handleMessageOptout(thebutton,theevent){	
	Ext.Ajax.request({
		url:'/plugins/message_center/message_center_action.cfm'
		,method:'GET'
		,params:{action:'OPTOUT'}
		,success:function(responseObj,optionsObj){
				var msgtext = "You have successfully removed yourself from any future Message Center alerts.";
				Ext.Msg.show({
					title: 'Message Center Opt-out',
					msg: msgtext,
					minWidth:300,
					buttons: Ext.MessageBox.OK
					,fn: function(btn){
						thewindow = Ext.WindowMgr.get('message_center_window');
						thewindow.close();							
						}
				});	//Ext.Msg.show	
			}
		,failure:function(responseObj,optionsObj){
			}
	});	
}



/************************************************************************************************************
	myRSNA TAB FUNCTIONS
************************************************************************************************************/

function ValidateAddTab(form){
	if(form.tab_name.value.length < 1){
		alert('Please enter a name for your tab.');
		return false;
	}

	for(var i = 0; i<form.tab_type.length;i++){
		if(form.tab_type[i].checked){
			var temp_type = form.tab_type[i].value;
		}
	}

	if(temp_type == 'URL'){
		if(form.tab_url.value.length < 1){
			alert('Please Enter a valid url for this tab');	
			return false;
		}
	}

	Ext.Ajax.request({
		url:'/update_customer_tab.cfm'
		,params:{ACTION:'INSERT',tab_name:form.tab_name.value,tab_type:temp_type,ap_id:form.ap_id.value,tab_url:form.tab_url.value}
		,method:'GET'
		,success:function(responseObj,optionsObj){
				var json = eval(responseObj.responseText)[0];

				if(json.tab_js != '')
					writejs(json.tab_js);
					
				if(json.tab_css != '')
					writecss(json.tab_css);
//debugger;				
				if(json.tab_type == 'LAYOUT'){
					var newtab = myrsnatabs.add({
									title:json.tab_name
									,id:'tab_'+json.tab_id
									,closable:true
									,tab_type:json.tab_type
									,tab_id:json.tab_id
									,ct_id:json.ct_id
									,autoLoad:{url:json.tab_url + '&height=' + myrsnatabs.getInnerHeight(), scripts:true}
							}).show();
					var msg = 'You have created a new DASHBOARD tab. You can add items by clicking on the "ADD STUFF" button.';
				}else if(json.tab_type == 'WIDGET' && json.tab_url.search(/plugins/i) >= 0){
					var newtab = myrsnatabs.add({
									title:json.tab_name
									,id:'tab_'+json.tab_id
									,closable:true
									,tab_type:json.tab_type
									,tab_id:json.tab_id
									,ct_id:json.ct_id
									,autoScroll: true
									,tabTip:json.ap_description
									,autoLoad:{url:json.tab_url, scripts:true}
							}).show();
					var msg = 'You have created a new WIDGET tab.';
				}else if(json.tab_type == 'URL' || json.tab_url.search(/plugins/i) < 0){
					var newtab = myrsnatabs.add({
									title:json.tab_name
									,id:'tab_'+json.tab_id
									,closable:true
									,tab_type:json.tab_type
									,tab_id:json.tab_id
									,ct_id:json.ct_id
									,autoScroll: true
									,tabTip:json.ap_description
									,xtype:'iframepanel'
									,defaultSrc:json.tab_url
							}).show();
					var msg = 'You have created a new URL tab. Please note, the RSNA cannot be responsible for problems caused by this external website. Some sites do not allow their content to be allowed within tabs.';
				}
				
					if(Ext.util.Format.trim(Ext.util.Format.undef(json.post_func)) != ''){ eval(json.post_func); }
					Ext.WindowMgr.getActive().close();
					Ext.Msg.show({
							title: 'Tab Created',
							msg: msg,
							buttons: Ext.MessageBox.OK
								 });
			}
			
		,failure:function(responseObj,optionsObj){
			}
	});	
	
	return false;
}


function ValidateRefreshmyFilesTree(){
	var myFilestab = myrsnatabs.find('title','myFiles');
	if(myFilestab.length > 0 && myFilestab[0].tab_type == 'FILES' && myFilestab[0].rendered){
		refreshmyFilesTree();
	}
}
	
	
function ValidateUpdateTab(form){
	if(form.tab_name.value.length < 1){
		alert('Please enter a name for your tab.');
		return false;
	}
}
	


function handleTabChange(tabpanel,tab){
	if(tab.tab_type == 'LAYOUT'){
		
		tbadd.show();
		if(tab.TabContainer != ''){
			try{
				if( Ext.get(tab.TabContainer).getHeight() > tabsHeight){
					myrsnatabs.setHeight(Ext.get(tab.TabContainer).getHeight());
				}
			} catch(e) {//tab not created yet
			}
		}
		
	}
	else
	{
		tbadd.hide();
//		myrsnatabs.setHeight(tabsHeight);
		this.setHeight(tabsHeight);
	}
}


function handleTabEdit(tab_id){
	var selected_node = editTabsTree.getNodeById(tab_id);
	
	var the_menu = new Ext.menu.Menu(
	{						
		id:'edit-tab-menu',
		cls:'edit-bm-menu',
		selected_node: tab_id,
		width:75,
		items: [
				
		{
			id:'the_menu_rename',
			text:'Rename',
			scope:this,
			icon:myRSNApath + 'images/tab_edit.png'
			,listeners:{'click':handleTabRename}
		},					
				
		{
			id:'the_menu_delete',
			text:'Delete',
			scope:this,
			icon: myRSNApath + 'images/tab_delete.png'
			,listeners:{'click':handleTabDelete}
		},	
		
		{
			id:'the_menu_cancel',
			text:'Cancel',
			scope:this,
			icon:myRSNApath + 'images/cancel.png'
		}]
	});	

	the_menu.show($('edit_tab_'+tab_id));
}



function handleTabMove(the_tree,the_node,old_parent,new_parent,new_index) {
	Ext.Ajax.request({
		url:myRSNApath+'update_customer_tab.cfm'
		,method:'GET'
		,params:{action:'REORDER',ct_id:the_node.id,new_order:new_index + 1}
		,success:function(responseObj,optionsObj){
			eval(responseObj.responseText);
			}
		,failure:function(responseObj,optionsObj){
		}
	});
}


function handleTabDelete(theitem,theevent){
	//alert(theitem.parentMenu.selected_node);
	var ct_id = editTabsTree.getNodeById(theitem.parentMenu.selected_node).id;
	theitem.parentMenu.hide();
	//alert(ct_id);
	var tab = eval('myrsnatabs.find("ct_id",' + ct_id + ')[0]');
	//alert(tab);
	myrsnatabs.remove(tab);
}


function handleRemove(tabpanel, tab){ 		
	var temp_ct_id = tab.ct_id;

	var message = '<div align="center">Are you sure you want to delete this tab?</div>';
	if(tab.tab_type == 'LAYOUT'){
		message = message + '<div align="center">Any widgets you have added to this tab will be deleted as well.</div>';
	}
	
	Ext.Msg.show({
		title: 'Confirm',
		msg: message,
		minWidth:350,
		buttons: Ext.MessageBox.YESNO,
		fn: function(btn){
				if(btn=='yes'){
					if(myrsnatabs.getActiveTab().getId() != myrsnatabs.find('title','myRSNA')[0].id){
						myrsnatabs.setActiveTab(myrsnatabs.getItem(myrsnatabs.find('title','myRSNA')[0].id));
					}

					myrsnatabs.removeListener('beforeremove',handleRemove);
					myrsnatabs.remove(tab.id);
					myrsnatabs.addListener('beforeremove',handleRemove);

					Ext.Ajax.request({
							url:myRSNApath+'update_customer_tab.cfm'
							,method:'GET'
							,params:{action:'DELETE',ct_id:temp_ct_id}
							,success:function(responseObj,optionsObj){
									if(Ext.getCmp('editTabsWin') != undefined && !Ext.getCmp('editTabsWin').hidden){
										editTabsTree.getNodeById(temp_ct_id).remove();
									}
								}
							,failure:function(responseObj,optionsObj){
							}
						});	
				    
				    
					return true;						
				} // btn=='yes'
			}
	});
	return false;	
	

 }


function handleTabRename(theitem,theevent){
	
	theitem.parentMenu.hide();
	
	var selected_node = editTabsTree.getNodeById(theitem.parentMenu.selected_node);

	Ext.Msg.show({
		title: 'Rename Tab'
		,msg: 'Rename your TAB in the field below and press "OK" to commit.'
		,minWidth:250
		,buttons: Ext.MessageBox.OKCANCEL
		,multiline:true
		,value:selected_node.attributes.main_text
		,fn: function(btn,text){
				if(btn=='ok'){
					Ext.Ajax.request({
						url:myRSNApath + 'update_customer_tab.cfm'
						,method:'GET'
						,params:{action:'RENAME',ct_id:selected_node.attributes.id,ct_name:text.replace(/&/g, "")}
						,success:function(responseObj,optionsObj){
								eval(responseObj.responseText);
								selected_node.setText(text);
							}
						,failure:function(responseObj,optionsObj){
						}
					});	
			
				} // btn=='yes'
			}			
	});		
	return false;	
}
	
function handleTabMove(the_tree,the_node,old_parent,new_parent,new_index) {
	Ext.Ajax.request({
		url:myRSNApath + 'update_customer_tab.cfm'
		,method:'GET'
		,params:{action:'REORDER',ct_id:the_node.id,new_order:new_index+1}
		,success:function(responseObj,optionsObj){
				eval(responseObj.responseText);
			}
		,failure:function(responseObj,optionsObj){
		}
	});
}



/************************************************************************************************************
	WIDGET/MISC FUNCTIONS
************************************************************************************************************/


function insertWidget(ap_id,tab_id){
	Ext.Ajax.request({
		url:myRSNApath + 'insert_customer_plugin.cfm'
		,method:'POST'
		,params:{ap_id:ap_id,tab_id:tab_id}
		,success:function(responseObj,optionsObj){
			if(Ext.isSafari){
				myrsnatabs.getActiveTab().getUpdater().refresh();
			}else{
					var json = eval(responseObj.responseText);

					var newwidget = new Widget({
								ap_id:json[0].ap_id
								,colapsed:json[0].colapsed
								,content_url:json[0].content_url
								,container:json[0].container
								,cp_id:json[0].cp_id
								,debug:json[0].debug
								,drag:json[0].drag
								,go_text:json[0].go_text
								,go_url:json[0].go_url
								,height:json[0].height
								,help_url:json[0].help_url
								,id:json[0].id
								,mydelete:json[0].mydelete
								,parent:json[0].parent
								,sortorder:json[0].sortorder
								,tabset:json[0].tabset
								,title:json[0].title
								,width:json[0].width						
							});	
					
					if(json[0].ap_js_file != ''){
						//alert('writejs');
						writejs(json[0].ap_js_file);
					}
					if(json[0].css_url != ''){
						writecss(json[0].css_url);	
					}
					json[0].parent.adjustHeight();
				}
		}
	});		  
}


function getActiveFileTree(){
	var tree = null;
	try{
		var tab = myrsnatabs.getActiveTab();
		if(tab.title == 'myFiles'){
			tree = Ext.getCmp('editFilesTree');
		}else if(tab.title == 'myBookmarks'){
			tree = Ext.getCmp('my_bookmarks');
		}else if(tab.title == 'mySearch'){
			var subtab = Ext.getCmp('dpstabs').getActiveTab();
			if(subtab.id == 'mSmF_tab'){
				tree = Ext.getCmp('mSmFGrid');
			}else if(subtab.id == 'mSmB_tab'){
				tree = Ext.getCmp('mSmBGrid');
			}else if(subtab.id == 'mSJournals_tab'){
				tree = Ext.getCmp('mSJournalsGrid');
			}
		}
	}catch(e){}
	
	return tree;
}


function getActiveFileTreePrefix(){
	var prefix = '';
	
	try{
		var tab = myrsnatabs.getActiveTab();
		if(tab.title == 'myBookmarks'){
			prefix = 'mb_';	
		}else if(tab.title == 'mySearch'){
			var subtab = Ext.getCmp('dpstabs').getActiveTab();
			if(subtab.id == 'mSmF_tab'){
				prefix = 'mSmF_';
			}else if(subtab.id == 'mSmB_tab'){
				prefix = 'mSmB_';	
			}else if(subtab.id == 'mSJournals_tab'){
				prefix = 'mSJournals_';
			}
		}
	}catch(e){}
	
	return prefix;
}


function getSelectedFileTreeNode(){
	var node = null;
	var tree = getActiveFileTree();

	if(tree != null){
		var selModel = tree.getSelectionModel();
		if(tree.getXType()=="grid"){
			var record = selModel.getSelected();
			if(record != undefined){
				node = convertRecordToTreeNode(record);	
			}
		}else{
			if(selModel != undefined){
				node = selModel.getSelectedNode();
				node = (node == undefined ? null : node);
			}
		}
	}
	
	return node;
}


function getMyRSNATab(title){
	var tabs = myrsnatabs.find('title',title);
	if(tabs.length > 0 ){
		return tabs[0];	
	}else{
		return null;
	}
}


function timeconvert(time, type){
	var conversion = false;

	if(Ext.value(type,'seconds') == 'seconds'){
		conversion = (time.substr(time.length-2)=='AM' || (time.substr(0,2)=='12' && time.substr(time.length-2)=='PM') ? 0 : 43200);
		conversion = conversion + time.substr(0,(time.length==7 ? 1: 2))*3600;
		conversion = conversion + time.substr((time.length==7 ? 2: 3),2)*60;
	}else if(type == '24hr'){
		var seconds = timeconvert(time,'seconds');
		var hours = Math.floor(seconds/3600);
		var minutes = Math.floor((seconds-Math.floor(hours*3600))/60);
		conversion = hours + ':' + String.leftPad(minutes,2,'0');
	}
	return conversion;
}