//Administration JavaScript functions

// Preload Icons
image4 = new Image();
image4.src = "http://crag.columbusnova.com/images/folders/ln.gif";
image5 = new Image();
image5.src = "http://crag.columbusnova.com/images/folders/lph.gif";
image2 = new Image();
image2.src = "http://crag.columbusnova.com/images/folders/lm.gif";
image3 = new Image();
image3.src = "http://crag.columbusnova.com/images/folders/lmh.gif";
image6 = new Image();
image6.src = "http://crag.columbusnova.com/images/folders/icon_pdf.gif";
image7 = new Image();
image7.src = "http://crag.columbusnova.com/images/folders/icon_excel.gif";
image1 = new Image();
image1.src = "http://crag.columbusnova.com/images/folders/directory.gif";	

var btnCreateStatus = false;
var btnEditStatus = false;
var btnDeleteStatus = false;
var btnAddClientStatus = false;
var btnSearchStatus = false;
var globalCurrentAction = '';	

var tdOpen = '<td width="78" valign="bottom" background="http://crag.columbusnova.com/images/adm/background.gif">'; 
var	tdClose = '</td>';	
var	createDisabled = '<img src="../images/adm/btnblue_createnew_disable.gif" width="78" height="22" border="0" id="btnCreate">';
var createOff = '<a href="javascript:selectFundTabs(\'create\');"><img src="../images/adm/btnblue_createnew_off.gif" width="78" height="22" border="0" onmouseover="if(!btnCreateStatus) this.src=\'../images/adm/btnblue_createnew_on.gif\';" onmouseout="if(!btnCreateStatus) this.src=\'../images/adm/btnblue_createnew_off.gif\';" id="btnCreate" name="btnCreate"/></a>';
var createOn = '<a href="javascript:selectFundTabs(\'create\');"><img src="../images/adm/btnblue_createnew_on.gif" width="78" height="22" border="0" onmouseover="if(!btnCreateStatus) this.src=\'../images/adm/btnblue_createnew_off.gif\';" onmouseout="if(!btnCreateStatus) this.src=\'../images/adm/btnblue_createnew_on.gif\';" id="btnCreate" name="btnCreate"/></a>';
var	editDisabled = '<img src="../images/adm/btnblue_edit_disable.gif" width="78" height="22" border="0" id="btnEdit">';
var editOff ='<a href="javascript:selectFundTabs(\'edit\');"><img src="../images/adm/btnblue_edit_off.gif" width="78" height="22" border="0" onmouseover="if(!btnEditStatus) this.src=\'../images/adm/btnblue_edit_on.gif\';" onmouseout="if(!btnEditStatus) this.src=\'../images/adm/btnblue_edit_off.gif\';" id="btnEdit" name="btnEdit"/></a>';
var editOn = '<a href="javascript:selectFundTabs(\'edit\');"><img src="../images/adm/btnblue_edit_on.gif" width="78" height="22" border="0" onmouseover="if(!btnEditStatus) this.src=\'../images/adm/btnblue_edit_off.gif\';" onmouseout="if(!btnEditStatus) this.src=\'../images/adm/btnblue_edit_on.gif\';" id="btnEdit" name="btnEdit"/></a>';
var	deleteDisabled = '<img src="../images/adm/btnblue_delete_disable.gif" width="78" height="22" border="0" id="btnDelete">';
var deleteOff ='<a href="javascript:selectFundTabs(\'delete\');"><img src="../images/adm/btnblue_delete_off.gif" width="78" height="22" border="0" onmouseover="if(!btnDeleteStatus) this.src=\'../images/adm/btnblue_delete_on.gif\';" onmouseout="if(!btnDeleteStatus) this.src=\'../images/adm/btnblue_delete_off.gif\';" id="btnDelete" name="btnDelete"/></a>';
var deleteOn = '<a href="javascript:selectFundTabs(\'delete\');"><img src="../images/adm/btnblue_delete_on.gif" width="78" height="22" border="0" onmouseover="if(!btnDeleteStatus) this.src=\'../images/adm/btnblue_delete_off.gif\';" onmouseout="if(!btnDeleteStatus) this.src=\'../images/adm/btnblue_delete_on.gif\';" id="btnDelete" name="btnDelete"/></a>';
var	addRemoveDisabled = '<img src="../images/adm/btnblue_addremove_disable.gif" width="150" height="22" border="0" id="btnAddClient">';
var addRemoveOff = '<a href="javascript:selectFundTabs(\'addClients\');"><img src="../images/adm/btnblue_addremove_off.gif" width="150" height="22" border="0" onmouseover="if(!btnAddClientStatus) this.src=\'../images/adm/btnblue_addremove_on.gif\';" onmouseout="if(!btnAddClientStatus) this.src=\'../images/adm/btnblue_addremove_off.gif\';" id="btnAddClient" name="btnAddClient"/></a>';
var addRemoveOn = '<a href="javascript:selectFundTabs(\'addClients\');"><img src="../images/adm/btnblue_addremove_on.gif" width="150" height="22" border="0" onmouseover="if(!btnAddClientStatus) this.src=\'../images/adm/btnblue_addremove_off.gif\';" onmouseout="if(!btnAddClientStatus) this.src=\'../images/adm/btnblue_addremove_on.gif\';" id="btnAddClient" name="btnAddClient"/></a>';
var	searchOff  = '<a href="javascript:selectFundTabs(\'search\');"><img src="../images/adm/btnblue_search_off.gif" width="84" height="22" border="0" onmouseover="if(!btnSearchStatus) this.src=\'../images/adm/btnblue_search_on.gif\';" onmouseout="if(!btnSearchStatus) this.src=\'../images/adm/btnblue_search_off.gif\';" id="btnSearch" name="btnSearch"/></a>';
var	searchOn  = '<a href="javascript:selectFundTabs(\'search\');"><img src="../images/adm/btnblue_search_on.gif" width="84" height="22" border="0" onmouseover="if(!btnSearchStatus) this.src=\'../images/adm/btnblue_search_off.gif\';" onmouseout="if(!btnSearchStatus) this.src=\'../images/adm/btnblue_search_on.gif\';" id="btnSearch" name="btnSearch"/></a>';

/**
 * write out the html of each sub option. These are based on the current action such us "create, edit, delete, addClients".
 * @param localAction "create, edit, delete, addClients".
 */
function writeOptionsBasedOnGlobalNodeTypeAndAction(localCurrentAction)
{	
	if(localCurrentAction != "")
	{	
		localProcessPage = 'http://crag.columbusnova.com/create_editNodes.php';
		urlParameters = '&action='+localCurrentAction+'&nodeID='+globalNodeID+'&nodeParentID='+globalNodeParentID+'&nodeType='+globalNodeType;
		if(globalNodeName != '')
		{	
			if(localCurrentAction == 'delete'  || localCurrentAction == 'edit' || localCurrentAction == 'addClients' || localCurrentAction == 'search') htmlOutput = '';		
			else	
				if(globalNodeType == 0) // Funds
					htmlOutput = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'+localProcessPage+'?creationType=Fund'+urlParameters+'" target="iFrameContent">Fund</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Directory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|';		
				else // Directories or Files
					htmlOutput = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp Fund &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'+localProcessPage+'?creationType=Directory'+urlParameters+'" target="iFrameContent">Directory</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="'+localProcessPage+'?creationType=File'+urlParameters+'" target="iFrameContent">File</a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|';									
		}		
		else // Any tree node clicked
			htmlOutput = 'Please, select an element from the list of Funds';
		getDivRef('layerSubOptions').innerHTML = htmlOutput;
		// Register current action as global
		globalCurrentAction = localCurrentAction;
	}
}	

/**
 * reset the options and suboptions.
 */
function setTabOptions(action)
{	
	document.getElementById('btnCreate').src = 'http://crag.columbusnova.com/images/adm/btnblue_createnew_off.gif';
	document.getElementById('btnEdit').src = 'http://crag.columbusnova.com/images/adm/btnblue_edit_off.gif';	
	document.getElementById('btnDelete').src = 'http://crag.columbusnova.com/images/adm/btnblue_delete_off.gif';		
	document.getElementById('btnAddClient').src = 'http://crag.columbusnova.com/images/adm/btnblue_addremove_off.gif';			
	document.getElementById('btnSearch').src = 'http://crag.columbusnova.com/images/adm/btnblue_search_off.gif';				
	btnCreateStatus = false;
	btnEditStatus = false;
	btnDeleteStatus = false;
	btnAddClientStatus = false;		
	btnSearchStatus = false;	
	document.getElementById("contentHtmlTabs").innerHTML = '';
	document.getElementById("layerSubOptions").innerHTML = '';
	htmlTabs = '';	
	// Default
	htmlTabs = '<table cellpadding="0" cellspacing="0"><tr>';
	htmlTabs+= tdOpen+createOff+tdClose;
	htmlTabs+= tdOpen+editOff+tdClose;
	htmlTabs+= tdOpen+deleteOff+tdClose;
	htmlTabs+= tdOpen+addRemoveDisabled+tdClose;
    if(action == 'search') htmlTabs+= tdOpen+searchOn+tdClose; else htmlTabs+= tdOpen+searchOff+tdClose;			
	htmlTabs+= '</tr></table>';
	// Any node selected
	if(globalNodeID == 0 && globalNodeParentID == 0 && globalNodeType == 0)
	{ 
		htmlTabs = '<table cellpadding="0" cellspacing="0"><tr>';
		htmlTabs+= tdOpen+createDisabled+tdClose; 		
		htmlTabs+= tdOpen+editDisabled+tdClose;
		htmlTabs+= tdOpen+deleteDisabled+tdClose;
		htmlTabs+= tdOpen+addRemoveDisabled+tdClose;
		if(action == 'search') htmlTabs+= tdOpen+searchOn+tdClose; else htmlTabs+= tdOpen+searchOff+tdClose;				
		htmlTabs+= '</tr></table>';
	}	
	// Root selected
	if(globalNodeID == 1 && globalNodeParentID == 0 && globalNodeType == 0)
	{ 
		htmlTabs = '<table cellpadding="0" cellspacing="0"><tr>';
		if(action == 'create') htmlTabs+= tdOpen+createOn+tdClose; else htmlTabs+= tdOpen+createOff+tdClose;		
		htmlTabs+= tdOpen+editDisabled+tdClose;
		htmlTabs+= tdOpen+deleteDisabled+tdClose;
		htmlTabs+= tdOpen+addRemoveDisabled+tdClose;
		if(action == 'search') htmlTabs+= tdOpen+searchOn+tdClose; else htmlTabs+= tdOpen+searchOff+tdClose;				
		htmlTabs+= '</tr></table>';
	}	
	// Any fund selected
	if(globalNodeID > 1 && globalNodeParentID == 1 && globalNodeType == 1)
	{ 
		htmlTabs = '<table cellpadding="0" cellspacing="0"><tr>';
		if(action == 'create') htmlTabs+= tdOpen+createOn+tdClose; else htmlTabs+= tdOpen+createOff+tdClose;
		if(action == 'edit') htmlTabs+= tdOpen+editOn+tdClose; else htmlTabs+= tdOpen+editOff+tdClose;
		htmlTabs+= tdOpen+deleteOff+tdClose;	
		if(action == 'addClients') htmlTabs+= tdOpen+addRemoveOn+tdClose; else htmlTabs+= tdOpen+addRemoveOff+tdClose;
		if(action == 'search') htmlTabs+= tdOpen+searchOn+tdClose; else htmlTabs+= tdOpen+searchOff+tdClose;			
		htmlTabs+= '</tr></table>';
	}	
	document.getElementById("contentHtmlTabs").innerHTML = htmlTabs;
	localProcessPage = 'http://crag.columbusnova.com/contentIframeDefault.php'; urlParameters = '';
	if(globalIsSearch == false) top.iFrameContent.location=localProcessPage+'?'+urlParameters;	
}	

/**
 * load the html of each process page.
 * @param localCurrentAction "create, edit, delete, addClients".
 */
function showPage(localCurrentAction)
{	
	urlParameters = '';
	switch (localCurrentAction)
	{
		
		case "create": 	localProcessPage = 'http://crag.columbusnova.com/contentIframeDefault.php'; urlParameters = ''; break;	
		case "edit": 	
			if(globalNodeID == 1)
			{
				alert("Sorry, the \"Columbus Nova Funds\" Root Directory can not edited.");
				localProcessPage = 'http://crag.columbusnova.com/contentIframeDefault.php'; urlParameters = '';
			}	
			else
				localProcessPage = 'http://crag.columbusnova.com/create_editNodes.php'; urlParameters = '&action='+localCurrentAction+'&nodeID='+globalNodeID+'&nodeParentID='+globalNodeParentID+'&nodeType='+globalNodeType;	 break;	
		case "delete": 	
			if(globalNodeID == 1)
			{
				alert("Sorry, the \"Columbus Nova Funds\" Root Directory can not deleted.");
				localProcessPage = 'http://crag.columbusnova.com/contentIframeDefault.php'; urlParameters = '';
			}		
			else {localProcessPage = 'http://crag.columbusnova.com/delete_nodes.php'; urlParameters = '&action='+localCurrentAction+'&nodeID='+globalNodeID+'&nodeParentID='+globalNodeParentID+'&nodeType='+globalNodeType;}
			break; 	
		case "addClients": 	
			if(globalNodeID == 1)
			{
				alert("Please select any fund on the tree");
				localProcessPage = 'http://crag.columbusnova.com/contentIframeDefault.php'; urlParameters = '';
			}		
			else {localProcessPage = 'http://crag.columbusnova.com/add_removeClients.php'; urlParameters = '&action='+localCurrentAction+'&nodeID='+globalNodeID+'&nodeParentID='+globalNodeParentID+'&nodeType='+globalNodeType;}
			break; 	
		case "search": 	
			localProcessPage = 'http://crag.columbusnova.com/search.php'; urlParameters = '&action='+localCurrentAction+'&nodeID='+globalNodeID+'&nodeParentID='+globalNodeParentID+'&nodeType='+globalNodeType;
			break; 				
	};	
	if(globalNodeID == 0 && globalNodeParentID == 0 && globalNodeType == 0 && localCurrentAction != 'search')
	{
		alert("Please select any content on the tree.");
		localProcessPage = 'http://crag.columbusnova.com/contentIframeDefault.php'; urlParameters = '';
	}	
	if(localProcessPage != '')  top.iFrameContent.location=localProcessPage+'?'+urlParameters;
}	

/**
 * Select the tabs of "Create New, Edti, Delete and Add / Remove Clients".
 * @param action string with the action that represents the current option.
 */
function selectFundTabs(localCurrentAction)
{

	btnCreateStatus = false;
	btnEditStatus = false;
	btnDeleteStatus = false;
	btnAddClientStatus = false;	
	btnSearchStatus = false;

	switch (localCurrentAction)
	{
		case "create": btnCreateStatus = true;
		break;
		case "edit": btnEditStatus = true;
		break;	
		case "delete":
		btnDeleteStatus = true;
		break;	
		case "addClients":
		btnAddClientStatus = true;
		break;		
		case "search":
		document.btnSearch.src = 'http://crag.columbusnova.com/images/adm/btnblue_search_on.gif';				
		btnSearchStatus = true;
		break;			
	};	
	setTabOptions(localCurrentAction);
	writeOptionsBasedOnGlobalNodeTypeAndAction(localCurrentAction);
	showPage(localCurrentAction);	
}

function validateFileForm(form)
{ 
	if(form.name.value == ""){alert("\"Name \" is required."); form.name.focus(); return false; } 
	if(form.description.value == ""){alert("\"Description \" is required."); form.description.focus(); return false; } 	
	return true;
}

function validateDirectoryForm(form)
{ 
	if(form.name.value == ""){alert("\"Name \" is required."); form.name.focus(); return false; } 
	if(form.description.value == ""){alert("\"Description \" is required."); form.description.focus(); return false; } 	
	return true;
}


