/* ------ FUSEDOC ---------------------------------------------------- 
VLJS/JS_ClientPortal_commonInclude.js
Responsibilities ------------------------------------------------------


	

Attributes ------------------------------------------------------------

On This Page ----------------------------------------------------------
Exits -----------------------------------------------------------------
Outstanding -----------------------------------------------------------
-----------------------------------------------------------------------
Date	      Modification Log
2008.08.25    LOS.SIITCS/Created		
-----------------------------------------------------------------------
- END FUSEDOC -------------------------------------------------------- */

/*	This function will create/'make visible' a new tab which will show the drill down details of another tab.
	this tab will be closable.
*/
function opennewTab(grid, rowIndex, cellindex, e, LayoutName, TabName, TabTilte, TabSourceURL,isSelected,isClosable){
	layoutObj = ColdFusion.Layout.getTabLayout(LayoutName);
	//console.log(layoutObj);
	// This is to check the maximum number of tabs opened. Will allow to see only seven tabs at a time.
	if (layoutObj.items.length > 6)
	{
		
		var cnt = 0;
		for(var i=0; i<layoutObj.items.length; i++) {
			var txt = layoutObj.items[i].text;

			if (txt == 'DashBoard View' || txt == 'Job Order History' || txt == 'Contract History' || txt == 'Current Consultants' || txt == 'Profiles - Web' || txt == 'Profiles - Considered' || txt == 'Profiles - Shortlisted')
				var value = txt;
			else
				var value = txt.split('-')[1].replace(/^\s+|\s+$/g, '');
			
			if (TabTilte == 'DashBoard View' || TabTilte == 'Job Order History' || TabTilte == 'Contract History' || TabTilte == 'Current Consultants' || TabTilte == 'Profiles - Web' || TabTilte == 'Profiles - Considered' || TabTilte == 'Profiles - Shortlisted')
				var TabTilteval = TabTilte;
			else
				var TabTilteval = TabTilte.split('-')[1].replace(/^\s+|\s+$/g, '');
			
			if(TabTilteval == value)
			{
				cnt = 0;
				break;				
			}
			else
			{
				cnt = 1;
			}
		}
	}	
	if (cnt > 0)
	{
		alert("The maximum number of tabs has been reached.\nNew tabs may not be opened until one or more tabs have been closed.");
		return 0;
	}
	//console.log(layoutObj.items.length);
	//create a tab if the tab doenst exist.else just make the tab visible and make it selected
	if (!eval(layoutObj.items[TabName])) {
		//below line will create a new closable tab
		ColdFusion.Layout.createTab(LayoutName,TabName,TabTilte,TabSourceURL,{inithide: false,selected:isSelected,closable:isClosable});
		//ColdFusion.navigate(TabSourceURL, TabName); //load this page in the tab
		//ColdFusion.Layout.selectTab(LayoutName, TabName); //make this tab selected
	}
	else {
		layoutObj.items[TabName].setText(TabTilte); //this will change the title of the tab
		ColdFusion.navigate(TabSourceURL, TabName); //load this url to the tab
		ColdFusion.Layout.hideTab(LayoutName, TabName);
		ColdFusion.Layout.showTab(LayoutName, TabName);
		if(isSelected)
			ColdFusion.Layout.selectTab(LayoutName, TabName); //make this tab selected
	}
	return 1;
}

/*this function will disable the the row selection of a grid*/
function changegridsettings(gridName) {
	myGrid = ColdFusion.Grid.getGridObject(gridName);
	
	/*code to format results of each column*/
	//cm = myGrid.getColumnModel();
	//cm.setRenderer(8, Ext.util.Format.usMoney);
	/*end code to format results of each column*/
	selModel=myGrid.getSelectionModel();
	/*code to disable selection of grid*/
	selModel.on('beforerowselect',function(selModel,rowIndex,keepExisting){return false});
	myGrid.removeListener('mouseover',function(myGrid){return false;});
	return;	
}

/*This function will do an ajax call to a remote cfc for sending mail.*/

SendEmail = function(datasource,formName) {
    var proxy = new remoteHandler();
    proxy.setCallbackHandler(callbackHandler);
    //proxy.setErrorHandler(errorHandler);
    //proxy.setForm(formName);
    //proxy.setData(datasource);
	//console.log(proxy);
	proxy.setHTTPMethod ='POST';
	proxy.setForm(formName);
	proxy.cfcsendemail();
}

function callbackHandler(result) {
	if (result.indexOf('success') != -1) {
		redirecturl = JS_StaticURL + 'CCLP.ClientPortal_Sendemail_callbackResult&sendto='+result.split('|',1)
		winObj = ColdFusion.Window.getWindowObject('emailwindow');
		winObj.setContentSize(400,120);
		ColdFusion.navigate(redirecturl, 'emailwindow');
	}
	else {
		redirecturl = JS_StaticURL + 'CCLP.ClientPortal_Sendemail&' + result;
		ColdFusion.navigate(redirecturl, 'emailwindow');
			
	}
}

/*this function will create a cfwindow using javascript on the fly*/

function CreateWindow (windowName,title,url,width,height,iscentered,ismodal,isdragable,isclosable,isinitshow,isresizable) {
	var config = new Object();
	config.width = width;
	config.height = height;
	config.center = iscentered;
	config.modal = true;
	config.draggable = isdragable;
	config.closable = isclosable;
	config.initshow = isinitshow;
	config.resizable = isresizable;
	
	ColdFusion.Window.create(windowName,title,url,config);
	ColdFusion.navigate(url,windowName); //refresh cfwindow
	winObj = ColdFusion.Window.getWindowObject(windowName);
	winObj.setContentSize(480,320);
}

/*this function is called if we need to change the look and feel of a column values of cfgrid as HREF*/
linkstyle = function(data,cellmd,record,row,col,store) {

	if(data != null)
		return "<a href='##' style='color:green;text-decoration:underline'>" + data + "</a>";
	else 
		return data;	
  
}

/*fucntion to change the date formatting of cfgrid*/
ChangeDateFormatting = function(data,cellmd,record,row,col,store) {

	if (data != null) {
		//return dateFormat(data, "mmm dd, yyyy");
		return Ext.util.Format.date(data, 'd.m.Y');
	}
	else 
		return data;	
  
}

/*fucntion to display record range and total count in grid*/
function getRecourdCountDisplay(grid) {
 	var gridFoot = grid.getView().getFooterPanel(true);
 	//console.log(grid.getView());
	var paging = new Ext.PagingToolbar(gridFoot,grid.getDataSource(),{
	  pageSize:10, //number of records displayed in grid
	  displayInfo:true,
	  displayMsg:'Displaying records {0} - {1} of {2}',
	  emptyMsg:"No records to display"
	});
    return;	
}

/*fucntion to display Total Summary of a grid column - Eg: DashBoardview Tab showing total summary in footer*/
function getSummaryToolBar(grid){
 	var gridFoot = grid.getView().getFooterPanel(true);
 	//console.log(grid.getView());
	var paging = new Ext.PagingToolbar(gridFoot,grid.getDataSource(),{
	  pageSize:10, //number of records displayed in grid
	  displayInfo:true,
	  displayMsg:'Displaying records {0} - {1} of {2}',
	  emptyMsg:"No records to display"
	});
	if (document.getElementById('summarygrid').value != '0')
	{
		var tb2container=Ext.DomHelper.append(paging.el,{tag:'div',id:Ext.id()},true);
		var tb = new Ext.Toolbar(tb2container);
	    var tot = tb.addText(document.getElementById('summarygrid').value);
	    tot.getEl().style.padding=0;
    }
    return;
}

