////////////////
var tree;
//load tree on page



function loadTree(id){
	tree = new dhtmlXTreeObject("treeBook","100%","100%",0);
	tree.setImagePath("imgs/");
	tree.loadXML("treebook.php");
	tree.preventIECaching(false);
}


var layout_1, layout_2, layout_3;
var tree1, tree2, tree3;
var load1 = false;
var load2 = false;
var load3 = false;
var tabbar_1;
function loadTree2(thisTree, treediv, treephp, isLoad, dhxLayout){
	if (!isLoad) {
		thisTree = dhxLayout.cells("b").attachTree();
		thisTree.setImagePath("imgs/");
		///////////////
		//thisTree.enableCheckBoxes(1);
		///////////////
		thisTree.loadXML(treephp);
		thisTree.preventIECaching(false);
		var ret = new Array();
		ret[0] = thisTree;
		ret[1] = false; 
		return ret;
	}
}


function reloadTreeCheck () {
	var ret = loadTree2_check(tree1, "tree_1", "treebook.php", load1, layout_1);
	load1 = ret[1]; tree1 = ret[0];
	Set_Cookie2( 'goto_tab', '1',getCurrentdir2(),'1' );
	if (  Get_Cookie('cookie_eln_tree_view_option[hide_show_archived_projects_tree1]') == 'show' ) {
		if (Get_Cookie('cell_mb_a') != 0 && Get_Cookie('ref_mb_a') != null && Get_Cookie('refId_mb_a') != null && Get_Cookie('ref_mb_a') != '' && Get_Cookie('refId_mb_a') != '' ) {
			restore( Get_Cookie('ref_mb_a'), Get_Cookie('refId_mb_a') );
		} 
	} else { 
		if (Get_Cookie('cell_mb_n') != 0 && Get_Cookie('ref_mb_n') != null && Get_Cookie('ref_mb_n') != '' && Get_Cookie('refId_mb_n') != null && Get_Cookie('refId_mb_n') != '') {
			restore( Get_Cookie('ref_mb_n'), Get_Cookie('refId_mb_n') ); 
		}
	}
}
function loadTree2_check(thisTree, treediv, treephp, isLoad, dhxLayout){
	if (!isLoad) {
		thisTree = dhxLayout.cells("b").attachTree();
		thisTree.setImagePath("imgs/");
		///////////////
		thisTree.enableCheckBoxes(1);
		///////////////
		thisTree.loadXML(treephp);
		thisTree.preventIECaching(false);
		var ret = new Array();
		ret[0] = thisTree;
		ret[1] = false; 
		return ret;
	}
}

function getIdOfCheckedPrint(tree){
	var data = '';
	var tab = tree.getAllChecked().split(',') ;
	for (var i in tab) {
		var txt = tree.getItemText(tab[i]);
		if (txt.length > 2) {
			var find_string = txt.match(/ELN-P-\d*-/g);
			var find_id = find_string.join("").match(/\d*/g);
			//alert( find_id.join("") );
			data = data + 'page'+i+"="+ find_id.join("")+'&';
		}
	}
//	alert(data);
	var t = new Date().getTime();
	window.open( "eln_print_merge.php?"+data+"t="+t );
}
function getIdOfCheckedPdf(tree){
	var data = '';
	var tab = tree.getAllChecked().split(',') ;
	for (var i in tab) {
		var txt = tree.getItemText(tab[i]);
		if (txt.length > 2) {
			var find_string = txt.match(/ELN-P-\d*-/g);
			var find_id = find_string.join("").match(/\d*/g);
			data = data + 'page'+i+"="+ find_id.join("")+'&';
		}
	}
//	alert(data);
	var t = new Date().getTime();
	window.open( "eln_pdf_generator.php?"+data+"t="+t );
}

//var layout_1, layout_2, layout_3;
//var tree1, tree2, tree3;
//
//function loadTree2(thisTree, treediv, treephp, isLoad){
//	if (!isLoad) {
//		thisTree = new dhtmlXTreeObject(treediv,"100%","100%",0);
//		thisTree.setImagePath("imgs/");
//		thisTree.loadXML(treephp);
//		thisTree.preventIECaching(false);
//		return false;
//	}
//}



//show status of request on page
function bookStatus(fl)
{
	var d = document.getElementById("showproc");
	if(fl)
		d.style.display = "";
	else
		d.style.display = "none";
}

//show status of request on page
function collStatus(fl)
{
	var d = document.getElementById("showproc");
	if(fl)
		d.style.display = "";
	else
		d.style.display = "none";
}
// end of new tree integration fns

/////////////

var globalId = 0;
function addnewexperiment(ref,refId)
{
	document.location.href="index.php?page=newexperiment.php&ref="+ref+"&refId="+refId;
}
function addnewpage(ref,refId,todo)
{
	document.location.href="index.php?page=newpage.php&ref="+ref+"&refId="+refId+"&todo="+todo;
}

getCurrentdir = function()
{
	var path = document.location.pathname;
	var dir = "/"+path.substr(1, path.lastIndexOf('/'));
	return dir;

}


function printpage(ref,refId)
{
//	var disp_setting="toolbar=yes, location=no, directories=yes, menubar=yes,"; 
//	disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";
//	window.open( "print.php?ref="+ref+"&refId="+refId );
	var t = new Date().getTime();
	window.open( "eln_print.php?ref="+ref+"&refId="+refId+"&t="+t );
}
function exportpdf(ref,refId)
{
//	var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
//	disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25";
//	window.open( "pdf.php?ref="+ref+"&refId="+refId );
	var t = new Date().getTime();
	window.open( "eln_pdf_generator.php?ref="+ref+"&refId="+refId+"&t="+t );
}
		
//function getHTTPObject()
//{
//	var xmlhttp;
//
//	if(window.XMLHttpRequest)
//	{
//		xmlhttp = new XMLHttpRequest();
//	}
//	else if (window.ActiveXObject)
//	{
//		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
//	    if (!xmlhttp)
//		{
//			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
//		}
//	}	
//	return xmlhttp; 
//}
//var http = getHTTPObject(); // We create the HTTP Object 
////////////////////////////////////////////////////////
vision=new Array();

function hidebook(id,ref){
		
	try{
		for(i=1;i< id+1;i++){
			var obj = document.getElementById("book"+i);
			if(i == id && (vision[i]=='none' || !vision[i])){
				obj.style.display = 'block';
				vision[i] = 'block';
				if (ref == 0)
				{
					Set_Cookie( 'book', id, getCurrentdir(),1 );
				}
				else if ( ref == 1)
				{
					Set_Cookie( 'expr', id, getCurrentdir(),1 );				
				}
			}
			else if(i == id && vision[i]=='block'){
				obj.style.display = 'none';
				vision[i] = 'none';
				if (ref == 0)
				{
					Set_Cookie( 'book', '-1', getCurrentdir(),1 );
				}
				else if ( ref == 1)
				{
					Set_Cookie( 'expr', '-1', getCurrentdir(),1 );				
				}
			
			}

		}
	}catch(e){
		return;
	}
}
function Set_Cookie( name, value, path,expires ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" );
}
restore = function(ref , refId)
{
	//////////////////////////////////////////////////////////////
	var tmp = window.location.search.split('&');
	var tab_get = [];
	for (var i in tmp) {
		if (tmp[i].indexOf("=")>0) { tab_get[decodeURI(tmp[i].substring(0, tmp[i].indexOf("=")))] = decodeURI(tmp[i].substring(tmp[i].indexOf("=")+1)); }
		else                       { tab_get[decodeURI(tmp[i])]=''; }
	} // for
	var open_image_panel       = "false";
	var open_spreadsheet_panel = "false";
	var open_template          = ""; 
	var template_page          = "";
	if (tab_get["sow_image"] == refId)   { open_image_panel       = "true"; }
	if (tab_get["template_p"] == refId)  { open_spreadsheet_panel = "true"; template_page =  tab_get["template_p"]; }
	if (tab_get["template"] != "" )      { open_template          = tab_get["template"]; } 
	//////////////////////////////////////////////////////////////
	
	$.ajax({ // ajax  request
		type: "GET", // send in GET
		url: "utf8_ajax.php", // url cible du script PHP
		async: true, // asynchrone mode
		data: "ajaxpage=content.php&ref="+ref+"&refId="+refId+"&template="+open_template+"&template_p="+template_page+"", //data send	
		dataType: "html",
		success: function(data){ // when we've got php response
			//$("#content").html(data);
			document.getElementById("content").innerHTML = "<div id='content_innter_html_protect_"+Math.floor(Math.random() * 10000)+1+"'>"+data+"</div>";
			//
			////////////////////////////////////////////////////////////////////////////////////////
			if (ref==0) {
				$.ajax({ // ajax  request
					type: "GET", // send in GET
					url: "hasblob_book.php?refId="+refId, // url cible du script PHP
					async: true, // asynchrone mode
					dataType: "script"
				});
			}
			///////////
			if (ref==1) {
				$.ajax({ // ajax  request
					type: "GET", // send in GET
					url: "hasblob_expr.php?refId="+refId, // url cible du script PHP
					async: true, // asynchrone mode
					dataType: "script"
				});
			}
			if (ref==2) {
				$.ajax({ // ajax  request
					type: "GET", // send in GET
					url: "hasBlob.php?refId="+refId+"&open_image_panel="+open_image_panel+"&open_spreadsheet_panel="+open_spreadsheet_panel, // url cible du script PHP
					async: true, // asynchrone mode
					dataType: "script"
				});
			}
			//setTimeout("setLayoutSize\(\);", 1000);
			//setTimeout("setLayoutSize\(\);", 3000);
		}
	});
}

var etat_applet='none';
function showhideLC(id){
if (etat_applet == 'block') { 
etat_applet = 'none'; 
} 
else { 
etat_applet = 'block'; 
} 
var objdiv = document.getElementById(id);
objdiv.style.display = etat_applet;

}

var etat_coll = 'none';
function showhideCOLL(id){
	if (etat_coll == 'block') { 
		etat_coll = 'none'; 
		//document.getElementById('hidden_space_bar').style.paddingTop = '8px'; 
	} else { 
		etat_coll = 'block';
		//document.getElementById('hidden_space_bar').style.paddingTop = '4px'; 
	} 
	var objdiv = document.getElementById(id).style.display = etat_coll;
}


//New Tree Integration Functions
// AJAX Function Calls for each feature//
function editbook(ref,refId)
{	 
	$.ajax({ // ajax  request
		type: "GET", // send in GET
		url: "index.php", // url cible du script PHP
		async: true, // asynchrone mode
		data: "page=editbook.php&ref="+ref+"&refId="+refId, //data send	
		dataType: "html",
		success: function(data){ // when we've got php response
			//$("#content").html(data);
			document.getElementById("content").innerHTML = data;
		}
	});
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function openContent(ref, refId, cell, arch) {
	
	var divApplet = document.getElementById("divApplet");
	if (divApplet) {
		divApplet.innerHTML = "";
	    //document.body.removeChild(divApplet);
	}
	
	Set_Cookie('eln_normal_tree_level_selected', ref,   getCurrentdir(), 1);
	Set_Cookie('eln_normal_tree_item_selected',  refId, getCurrentdir(), 1);
	
	Set_Cookie( 'ref',   ref,   getCurrentdir(),1 );
	Set_Cookie( 'refId', refId, getCurrentdir(),1 );
	Set_Cookie( 'cell',  cell,  getCurrentdir(),1 );
	
	//////////////////////////////////////////////////////////////
	var tmp = window.location.search.split('&');
	var tab_get = [];
	for (var i in tmp) {
		if (tmp[i].indexOf("=")>0) { tab_get[decodeURI(tmp[i].substring(0, tmp[i].indexOf("=")))] = decodeURI(tmp[i].substring(tmp[i].indexOf("=")+1)); }
		else                       { tab_get[decodeURI(tmp[i])]=''; }
	} // for
	var open_image_panel       = "false";
	var open_spreadsheet_panel = "false";
	var open_template          = ""; 
	var template_page          = "";
	if (tab_get["sow_image"] == refId)   { open_image_panel       = "true"; }
	if (tab_get["template_p"] == refId)  { open_spreadsheet_panel = "true"; template_page =  tab_get["template_p"]; }
	if (tab_get["template"] != "" )      { open_template          = tab_get["template"]; } 
	//////////////////////////////////////////////////////////////
	
	//if (arch) { 
		if (arch == 0) {
			
			Set_Cookie( 'ref_mb_n',   ref,   getCurrentdir(), 1 );
			Set_Cookie( 'refId_mb_n', refId, getCurrentdir(), 1 );
			Set_Cookie( 'cell_mb_n',  cell,  getCurrentdir(), 1 );

		} else if (arch == 1) {

			Set_Cookie( 'ref_mb_a',   ref,   getCurrentdir(), 1 );
			Set_Cookie( 'refId_mb_a', refId, getCurrentdir(), 1 );
			Set_Cookie( 'cell_mb_a',  cell,  getCurrentdir(), 1 );

		}
	//}
	
	$.ajax({ // ajax  request
		type: "GET", // send in GET
		url: "utf8_ajax.php", // url cible du script PHP
		async: true, // asynchrone mode
		data: "ajaxpage=content.php&ref="+ref+"&refId="+refId, //data send
		dataType: "html",
		success: function(data){ // when we've got php response
			document.getElementById("content").innerHTML = "<div id='content_innter_html_protect_"+Math.floor(Math.random() * 10000)+1+"'>"+data+"</div>";
			///////////////////
//			var tmp = window.location.search.split('&');
//			var tab_get = [];
//			for (var i in tmp) {
//				if (tmp[i].indexOf("=")>0) { tab_get[decodeURI(tmp[i].substring(0, tmp[i].indexOf("=")))] = decodeURI(tmp[i].substring(tmp[i].indexOf("=")+1)); }
//			 	else                       { tab_get[decodeURI(tmp[i])]=''; }
//			} // for
//			var open_image_panel       = "false";
//			var open_spreadsheet_panel = "false";
//			if (tab_get["sow_image"]   == refId) { open_image_panel       = "true";}
//			if (tab_get["template_p"]  == refId) { open_spreadsheet_panel = "true";}
			
//			document.location.href = "#contentpanel";
			///////////////////
			if (ref==0) {
				$.ajax({ // ajax  request
					type: "GET", // send in GET
					url: "hasblob_book.php?refId="+refId, // url cible du script PHP
					async: true, // asynchrone mode
					dataType: "script"
				});
			}
			////////////////////////////////////////////////////////////////////////////////////////
			if (ref==1) {
				$.ajax({ // ajax  request
					type: "GET", // send in GET
					url: "hasblob_expr.php?refId="+refId, // url cible du script PHP
					async: true, // asynchrone mode
					dataType: "script"
				});
			}
			////////////////////////////////////////////////////////////////////////////////////////
			if (ref==2) {
				$.ajax({ // ajax  request
					type: "GET", // send in GET
					url: "hasBlob.php?refId="+refId+"&open_image_panel="+open_image_panel+"&open_spreadsheet_panel="+open_spreadsheet_panel, // url cible du script PHP
					async: true, // asynchrone mode
					//data: "refId="+refId, //data send	
					dataType: "script"
				});
			}
			//setTimeout("setLayoutSize\(\);", 1000);
			//setTimeout("setLayoutSize\(\);", 3000);
		} // success
	}); // ajax 1
}	

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function  move_expr( refid, bookid) {
	//alert(refid + "_" + bookid);
	$.ajax({ 
		type: "GET",
		url: "translocation_expr.php",
		async: true, 
		data: "refid="+refid+"&bookid="+bookid,
		dataType: "html",
		success: function(data){ 
			location.reload();
		}
	});
}

var isTreeCollapsed = false;
function treeHideShow() {
//	try {
//		if (isTreeCollapsed ) { dhxLayout.cells("a").expand();   isTreeCollapsed = false; document.getElementById('hideshow').value = document.getElementById('id_txt_hide_tree').value; }
//		else                  { dhxLayout.cells("a").collapse(); isTreeCollapsed = true;  document.getElementById('hideshow').value = document.getElementById('id_txt_show_tree').value; }
//	} catch(e) {}
	try {
		if (isTreeCollapsed ) { 
			dhxLayout.cells("a").expand();
			isTreeCollapsed = false; 
			document.getElementById('hideshow').title = document.getElementById('id_txt_hide_tree').value;
			document.getElementById('hideshow').alt   = document.getElementById('id_txt_hide_tree').value;
		} else { 
			dhxLayout.cells("a").collapse();
			isTreeCollapsed = true;
			document.getElementById('hideshow').title = document.getElementById('id_txt_show_tree').value;
			document.getElementById('hideshow').alt   = document.getElementById('id_txt_show_tree').value;
		}
	} catch(e) {}
}


function openContent2(ref,refId, cell, arch, tag) {
	
	var divApplet = document.getElementById("divApplet");
	if (divApplet) {
		divApplet.innerHTML = "";
	}
	var tmp = window.location.search.split('&');
	var tab_get = [];
	for (var i in tmp) {
		if (tmp[i].indexOf("=")>0) { tab_get[decodeURI(tmp[i].substring(0, tmp[i].indexOf("=")))] = decodeURI(tmp[i].substring(tmp[i].indexOf("=")+1)); }
		else                       { tab_get[decodeURI(tmp[i])]=''; }
	} // for
	var open_image_panel       = "false";
	var open_spreadsheet_panel = "false";
	var open_template          = ""; 
	var template_page          = "";
	if (tab_get["sow_image"] == refId)   { open_image_panel       = "true"; }
	if (tab_get["template_p"] == refId)  { open_spreadsheet_panel = "true"; template_page =  tab_get["template_p"]; }
	if (tab_get["template"] != "" )      { open_template          = tab_get["template"]; } 
	Set_Cookie('eln_normal_tree_level_selected', ref,   getCurrentdir(), 1);
	Set_Cookie('eln_normal_tree_item_selected',  refId, getCurrentdir(), 1);
	Set_Cookie( 'ref',   ref,   getCurrentdir(),1 );
	Set_Cookie( 'refId', refId, getCurrentdir(),1 );
	Set_Cookie( 'cell',  cell,  getCurrentdir(),1 );
	if (arch == 0) {
		Set_Cookie( 'ref_'+tag+'_n',   ref,   getCurrentdir(), 1 );
		Set_Cookie( 'refId_'+tag+'_n', refId, getCurrentdir(), 1 );
		Set_Cookie( 'cell_'+tag+'_n',  cell,  getCurrentdir(), 1 );
	} else if (arch == 1) {
		Set_Cookie( 'ref_'+tag+'_a',   ref,   getCurrentdir(), 1 );
		Set_Cookie( 'refId_'+tag+'_a', refId, getCurrentdir(), 1 );
		Set_Cookie( 'cell_'+tag+'_a',  cell,  getCurrentdir(), 1 );
	}
	$.ajax({
		type: "GET", 
		url: "utf8_ajax.php", 
		async: true, 
		data: "ajaxpage=content.php&ref="+ref+"&refId="+refId, 
		dataType: "html",
		success: function(data){ 
			//$("#content").html(data);
			document.getElementById("content").innerHTML = "<div id='content_innter_html_protect_"+Math.floor(Math.random() * 10000)+1+"'>"+data+"</div>";
			var tmp = window.location.search.split('&');
			var tab_get = [];
			for (var i in tmp) {
				if (tmp[i].indexOf("=")>0) { tab_get[decodeURI(tmp[i].substring(0, tmp[i].indexOf("=")))] = decodeURI(tmp[i].substring(tmp[i].indexOf("=")+1)); }
			 	else                       { tab_get[decodeURI(tmp[i])]=''; }
			} //
			var open_image_panel       = "false";
			var open_spreadsheet_panel = "false";
			if (tab_get["sow_image"]   == refId) { open_image_panel       = "true";}
			if (tab_get["template_p"]  == refId) { open_spreadsheet_panel = "true";}
			//document.location.href = "#contentpanel";
			if (ref==0) {
				$.ajax({ 
					type: "GET", 
					url: "hasblob_book.php?refId="+refId, 
					async: true,
					dataType: "script"
				});
			}
			if (ref==1) {
				$.ajax({ 
					type: "GET",
					url: "hasblob_expr.php?refId="+refId, 
					async: true, 
					dataType: "script"
				});
			}
			if (ref==2) {
				$.ajax({ 
					type: "GET",
					url: "hasBlob.php?refId="+refId+"&open_image_panel="+open_image_panel+"&open_spreadsheet_panel="+open_spreadsheet_panel, // url cible du script PHP
					async: true, 
					data: "refId="+refId, 	
					dataType: "script"
				});
			}
		}
	});
}	
