// function showCaption(idDiv)


// Test browser
var ie = (document.all)? true:false;
var ie6 = (document.documentElement && document.documentElement.clientWidth)? true:false;
var ns4 = (document.layers)? true:false;
var ns6 = (document.getElementById)? true:false;
// End Test browser

// Define tab list for each module
// May be deported to php page
TabOrderAdmin = new Array("OrderAdmin");
TabProject = new Array("OpenedPoint","ClosedPoint","ProjectEdit","ProjectShare","ProjectExport","FileList");
TabProjectList = new Array("UserOpenedProjects","UserClosedProjects","OrgOpenedProjects","OrgClosedProjects");
TabMemo = new Array("MemoView","MemoEdit");
TabForm = new Array("FormData","FormProperties","FormPageSetup","FormAnswers","FormOperations");
TabAlbum = new Array("PhotoGallery","VideoGallery","EditGallery","VideoSetup","AlbumProperties","AlbumShare");
TabHerbarium = new Array("Flowers","Properties","Share");
TabFlower = new Array("Properties","Pictures");
TabOrganisation = new Array("Properties","Member","Module","HomeInfo","WelcomeMsg");

var currentTab = 'NoTab';

// Set the Display of a requested div
function displayDiv(DivId,Setting) {
	var disp = "";
	var visi = "";
	if (Setting == 'show') {
		$('#'+DivId).fadeIn(200);
		disp = 'block';
		visi = 'visible';
	}
	else {
		disp = 'none';
		visi = 'hidden';
	}
	if (ns6) { // DOM3 = IE5, NS6
		document.getElementById(DivId).style.display = disp;
		document.getElementById(DivId).style.visibility = visi;
	}	
	else { 
		if (ns4) {	
				document.DivId.display = disp;
				document.DivId.visibility = visi;
		} else {
				document.all.DivId.style.display = disp;
				document.all.DivId.style.visibility = visi;
			}
	}
}

// Invert display of a div
function ShowHideDiv(DivId) {
	var visi = "";
	var Setting = "";
	
	if (ns6) { // DOM3 = IE5, NS6
		visi = document.getElementById(DivId).style.visibility;
	}	
	else { 
		if (ns4) {	
				visi = document.DivId.visibility;
		} else {
				visi = document.all.DivId.style.visibility;
			}
	}
	
	if (visi == 'visible') {
		Setting = 'hide';
	}
	else {
		Setting = 'show';
	}

	displayDiv(DivId,Setting);

	if (document.getElementById(currentTab)) {
		// reset fixed height to avoid overflow when displat pararaph
		document.getElementById(currentTab).style.height = "auto";
		setMinHeight(currentTab,340);
	}
	SetFooterPosition();

}

// Invert display of a div by sliding 
function SlideDiv(DivId) {
	var visi = "";
	var Setting = "";
	visi = document.getElementById(DivId+'_Display').value;
	
	if (visi == 'hidden') {
		$('#'+DivId).slideDown(100);
		SetField(DivId+'_Display','visible')
		displayDiv(DivId,'show');
	}
	else {
		$('#'+DivId).slideUp(100);
		SetField(DivId+'_Display','hidden')
		//displayDiv(DivId,'hide');
	}

	if (document.getElementById(currentTab)) {
		// reset fixed height to avoid overflow when displat pararaph
		document.getElementById(currentTab).style.height = "auto";
		setMinHeight(currentTab,340);
	}
	SetFooterPosition();

}

// Display InfoBull autoclose
function ShowAutoCloseInfo(text,DivId) {
	return overlib(text,OFFSETX,-20,OFFSETY,20,DIVID,DivId); // ,SHADOW,SHADOWOPACITY,60
}

// Display InfoBull autoclose
function ShowTimeoutCloseInfo(text,timeout) {
	return overlib(text,TIMEOUT,timeout,OFFSETY,-40); // ,SHADOW,SHADOWOPACITY,60
}

// Display InfoBull autoclose
function ShowManualCloseInfo(text) {
	return overlib(text,STICKY,OFFSETX,-20,OFFSETY,20); // ,SHADOW,SHADOWOPACITY,60
}

// Display InfoBull autoclose
function ShowManualCloseDiv(text,divclass,offsetx,offsety) {
	return overlib(text,STICKY,OFFSETX,offsetx,OFFSETY,offsety,DIVCLASS,divclass); // ,SHADOW,SHADOWOPACITY,60
}

// Call calendar to set a date in a form
function ShowCalendar(field, custPath) {
    overlib('',IFRAME,custPath+'/phps/calendarV2.php?field='+field,STICKY,OFFSETX,-20,OFFSETY,20); // ,SHADOW,SHADOWOPACITY,60
}    

//Display the requested menu
function showMenu(idMenu) { 

	//Must display the requested DIV, also the parent Div in case of subsubmenu
	// masterMenu contains the parent div Id
	var tabMenu=idMenu.split("_");
	var masterMenu = tabMenu[0];
	
	// Fix IE behaviour onmouseover on body: 
	// IE run the event at same time as div: display and hide the div at the same time
	// not good
	if (ie && (idMenu == 'FromContent')) {
		return true;
	}
	else {
		// each module, display or not the relevant DIV
		for (var id=0; id < ListMenu.length; ++id) {
			theMenu = 'menu'+ListMenu[id];
			if (theMenu == masterMenu) {
				$('#'+theMenu).fadeIn(200);
				$("#"+theMenu).fadeTo(0, 0.9);
				displayDiv(theMenu,'show');
			}
			else {
				$('#'+theMenu).fadeOut(100);
				//displayDiv(theMenu,'hide');
			}
			
			// Get the max number of subsubmenu
			maxDivId = 'MaxSubMenu'+ListMenu[id];
			if (ns6) { // DOM3 = IE5, NS6
				maxDivValue = document.getElementById(maxDivId).value;
			}	
			else { 
				if (ns4) {
					maxDivValue = document.maxDivId.value;
				} else {
					maxDivValue = document.all.maxDivId.value;
				}
			}

			divId = 1;
			// do for each subsubmenu			
			while (divId < maxDivValue) {
				theSubMenu = theMenu+'_'+eval(divId);
				// Check that subsubmenu exists
				if (document.getElementById(theSubMenu)) {
					if (theSubMenu == idMenu) {
						$('#'+theSubMenu).fadeIn(200);
						$("#"+theSubMenu).fadeTo(0, 0.9);
						displayDiv(theSubMenu,'show');
					}
					else {
						displayDiv(theSubMenu,'hide');
					}
				}
				divId++;
			}
		}
	}
}

//Display the requested Tab and hide the other tab from a table
function showTab(IdTab,ListTab) { 

	// each module, display or not the relevant DIV
	for (var id=0; id < ListTab.length; ++id) {
		theTab = ListTab[id];
		if (theTab == IdTab) {
			$('#'+theTab).fadeIn(400);
			displayDiv(theTab,'show');
			setMinHeight(theTab,340);
			currentTab = theTab;
		}
		else {
			//displayDiv(theTab,'hide');
			$('#'+theTab).fadeOut(0);
		}
	}
	if (document.getElementById("footer")) {
		SetFooterPosition();
	}
}

//Set min height to list of div if not enough
function setMinHeight(IdDiv,minHeight) {
		if(document.getElementById(IdDiv).offsetHeight <= minHeight){
			document.getElementById(IdDiv).style.height = minHeight+"px";
		}
}

//Set margin-top to hav at least footer at bottom of page
function SetFooterPosition(){
	if (self.innerHeight) // all except ie
	{
		TopFooter = self.innerHeight - 150;
	}
	else if (ie6)
	{
		TopFooter = document.documentElement.clientHeight - 150;
	}
	else if (ie)
	{
		TopFooter = document.body.clientHeight - 150;
	}
	
	TopFooter = TopFooter - document.getElementById("content").offsetHeight;

	if (TopFooter > 20) {
		document.getElementById("footer").style.margin = TopFooter+"px 0px 0px 0px";
	}
	else {
		document.getElementById("footer").style.margin = "20px 0px 0px 0px";
	}
}

// Ask confirmation to user for delete something 
function ConfirmFormSubmit(TxtConfirm,IdField,FormId) {
	if (FormId.elements[IdField].value == 'Delete' || FormId.elements[IdField].value == 'MultiDelete') {
	    return confirm(TxtConfirm);
    }
    else {
	    return true;
    }
}

// Ask confirmation to user to delete something, redirect to delete php script if confirm 
function ConfirmDelete(TxtConfirm,Url) {
    var DeleteConfirm=confirm(TxtConfirm);
    if (DeleteConfirm){document.location.href=Url;}
}

// Save Position setting
function SavePageSetup(FormName) {
    // Set Act Value
    SetField('MultiAct','MultiPosition');
    // Submit Form
    //document.getElementById(FormName).submit();
}

// Go to page
function gotoPage(url) { 
	window.location = url;
}
// End Go to page

// Set Checkbox for a list of Id
function SetAllCheckbox(IdList,Prefix,Value) { 
	var TheBoxId='';
	for (var id=1; id < IdList.length; id++) {
		TheBoxId = Prefix+IdList[id];
		if (ns6) { // DOM3 = IE5, NS6
			document.getElementById(TheBoxId).checked = Value;
		}	
		else { 
			if (ns4) {
				document.TheBoxId.checked = Value;
			} else {
				document.all.TheBoxId.checked = Value;
			}
		}
	}
}
// End Set field value

// Set field value
function SetField(id,val) { 
	if (ns6) { // DOM3 = IE5, NS6
		document.getElementById(id).value = val;
	}	
	else { 
		if (ns4) {
			document.id.value = val;
		} else {
			document.all.id.value = val;
		}
	}
}
// End Set field value

// Get field value
function GetField(id) { 
	if (ns6) { // DOM3 = IE5, NS6
		return document.getElementById(id).value;
	}	
	else { 
		if (ns4) {
			return document.id.value;
		} else {
			return document.all.id.value;
		}
	}
}
// End Set field value

// Get field value
function GetCheckboxField(id) { 
	if (ns6) { // DOM3 = IE5, NS6
		return document.getElementById(id).checked;
	}	
	else { 
		if (ns4) {
			return document.id.checked;
		} else {
			return document.all.id.checked;
		}
	}
}
// End Set field value

// Set field value
function SetCheckboxField(id,val) { 
	if (ns6) { // DOM3 = IE5, NS6
		document.getElementById(id).checked = val;
	}	
	else { 
		if (ns4) {
			document.id.checked = val;
		} else {
			document.all.id.checked = val;
		}
	}
}
// End Set field value

// Set field value + 1
function FieldPlusVal(id,val) { 
	if (ns6) { // DOM3 = IE5, NS6
		document.getElementById(id).value = parseInt(document.getElementById(id).value) + parseInt(val);
		return document.getElementById(id).value;
	}	
	else { 
		if (ns4) {
			document.id.value = parseInt(document.id.value) + parseInt(val);
			return document.id.value; 
		} else {
			document.all.id.value = parseInt(document.all.id.value) + parseInt(val)
			return document.all.id.value;
		}
	}
}
// End Display caption under mouse

// Set style value
function SetHeight(id,val) { 
	if (ns6) { // DOM3 = IE5, NS6
		document.getElementById(id).style.height = val;
	}	
	else { 
		if (ns4) {
			document.id.style.height = val;
		} else {
			document.all.id.style.height = val;
		}
	}
}
// End Set field value

// Add space and comma to number to be frenchi
function FormatNumber(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
}
// End FormatNumber

// Check all checkbox of FormId for input with name prefix inputPrefix
function SetAll(FormId,InputPrefix,Val) {
	var listInput = '';
	if (ns6) { // DOM3 = IE5, NS6
		theForm=document.getElementById(FormId);
		// Once again an IE patch
		if (ie) {theForm=document.forms[FormId];}
	}	
	else { 
		if (ns4) {
			theForm=document.FormId;
		} else {
			theForm=document.all.FormId;
		}
	}
	InputExpr=new RegExp("^"+InputPrefix);
	
	for (i=0; i<theForm.elements.length; i++) {

		var InputName = theForm.elements[i].name;
		if (InputExpr.test(InputName)) {
			if (ns6) { // DOM3 = IE5, NS6
				document.getElementById(InputName).checked=Val;
			}	
			else { 
				if (ns4) {
					document.InputName.checked=Val;
				} else {
					document.all.InputName.checked=Val;
				}
			}
		}
	}	
}
// End CheckAll

// Move Picture Div
function ChangePictureDivPosition(MovedPictureId,MoveToValue){
	// Init variable
	var Category = GetField('Category_'+MovedPictureId);
	var MaxCategory = GetField('MaxCategory_'+Category);
	var MovedPicturePosition = GetField('Position_'+MovedPictureId);
	var ReplacedPicturePosition = parseInt(MovedPicturePosition) + parseInt(MoveToValue);
	var ReplacedPictureId = GetField('IdPicture_'+Category+'_'+ReplacedPicturePosition);
	
	// Save Div content
	var MovedPictureDiv = document.getElementById('Div_'+Category+'_'+MovedPicturePosition).innerHTML;
	var ReplacedPictureDiv = document.getElementById('Div_'+Category+'_'+ReplacedPicturePosition).innerHTML;
	
	
	// Save Input Value
	MovedSelect = GetCheckboxField('Picture_Select_'+MovedPictureId);
	ReplacedSelect = GetCheckboxField('Picture_Select_'+ReplacedPictureId);
	
	// Swap div content
	document.getElementById('Div_'+Category+'_'+MovedPicturePosition).innerHTML = ReplacedPictureDiv;
	document.getElementById('Div_'+Category+'_'+ReplacedPicturePosition).innerHTML = MovedPictureDiv;
	
	// Set new position values
	SetField('Position_'+MovedPictureId,ReplacedPicturePosition);
	SetField('Position_'+ReplacedPictureId,parseInt(MovedPicturePosition));
	
	SetField('IdPicture_'+Category+'_'+MovedPicturePosition,ReplacedPictureId);
	SetField('IdPicture_'+Category+'_'+ReplacedPicturePosition,MovedPictureId);
	
	SetCheckboxField('Picture_Select_'+MovedPictureId,MovedSelect);
	SetCheckboxField('Picture_Select_'+ReplacedPictureId,ReplacedSelect);
	
	// Show or Hide Arrow div
	displayDiv('DivMoveUp_'+MovedPictureId,'show');
	displayDiv('DivMoveDown_'+MovedPictureId,'show');
	displayDiv('DivMoveUp_'+ReplacedPictureId,'show');
	displayDiv('DivMoveDown_'+ReplacedPictureId,'show');
	displayDiv('DivMoveUpDisable_'+MovedPictureId,'hide');
	displayDiv('DivMoveDownDisable_'+MovedPictureId,'hide');
	displayDiv('DivMoveUpDisable_'+ReplacedPictureId,'hide');
	displayDiv('DivMoveDownDisable_'+ReplacedPictureId,'hide');

	if(parseInt(ReplacedPicturePosition) == 1) {
		displayDiv('DivMoveUp_'+MovedPictureId,'hide');	
		displayDiv('DivMoveUpDisable_'+MovedPictureId,'show');	
	}
	if(parseInt(ReplacedPicturePosition) == parseInt(MaxCategory)) {
		displayDiv('DivMoveDown_'+MovedPictureId,'hide');
		displayDiv('DivMoveDownDisable_'+MovedPictureId,'show');
	}
	if(parseInt(MovedPicturePosition) == 1) {
		displayDiv('DivMoveUp_'+ReplacedPictureId,'hide');	
		displayDiv('DivMoveUpDisable_'+ReplacedPictureId,'show');	
	}
	if(parseInt(MovedPicturePosition) == parseInt(MaxCategory)) {
		displayDiv('DivMoveDown_'+ReplacedPictureId,'hide');
		displayDiv('DivMoveDownDisable_'+ReplacedPictureId,'show');
	}
}
// End Move Picture Div

// Move File Div
function ChangeFileDivPosition(MovedFileId,MoveToValue){
	// Init variable
	var Category = GetField('FileCategory_'+MovedFileId);
	var MaxCategory = GetField('MaxFileCategory_'+Category);
	var MovedFilePosition = GetField('Position_'+MovedFileId);
	var ReplacedFilePosition = parseInt(MovedFilePosition) + parseInt(MoveToValue);
	var ReplacedFileId = GetField('IdFile_'+Category+'_'+ReplacedFilePosition);
	
	// Save Div content
	var MovedFileDiv = document.getElementById('Div_'+Category+'_'+MovedFilePosition).innerHTML;
	var ReplacedFileDiv = document.getElementById('Div_'+Category+'_'+ReplacedFilePosition).innerHTML;
	
	
	// Save Input Value
	MovedSelect = GetCheckboxField('File_Select_'+MovedFileId);
	ReplacedSelect = GetCheckboxField('File_Select_'+ReplacedFileId);
	
	// Swap div content
	document.getElementById('Div_'+Category+'_'+MovedFilePosition).innerHTML = ReplacedFileDiv;
	document.getElementById('Div_'+Category+'_'+ReplacedFilePosition).innerHTML = MovedFileDiv;
	
	// Set new position values
	SetField('Position_'+MovedFileId,ReplacedFilePosition);
	SetField('Position_'+ReplacedFileId,parseInt(MovedFilePosition));
	
	SetField('IdFile_'+Category+'_'+MovedFilePosition,ReplacedFileId);
	SetField('IdFile_'+Category+'_'+ReplacedFilePosition,MovedFileId);
	
	SetCheckboxField('File_Select_'+MovedFileId,MovedSelect);
	SetCheckboxField('File_Select_'+ReplacedFileId,ReplacedSelect);
	
	// Show or Hide Arrow div
	displayDiv('DivMoveUp_'+MovedFileId,'show');
	displayDiv('DivMoveDown_'+MovedFileId,'show');
	displayDiv('DivMoveUp_'+ReplacedFileId,'show');
	displayDiv('DivMoveDown_'+ReplacedFileId,'show');
	displayDiv('DivMoveUpDisable_'+MovedFileId,'hide');
	displayDiv('DivMoveDownDisable_'+MovedFileId,'hide');
	displayDiv('DivMoveUpDisable_'+ReplacedFileId,'hide');
	displayDiv('DivMoveDownDisable_'+ReplacedFileId,'hide');

	if(parseInt(ReplacedFilePosition) == 1) {
		displayDiv('DivMoveUp_'+MovedFileId,'hide');	
		displayDiv('DivMoveUpDisable_'+MovedFileId,'show');	
	}
	if(parseInt(ReplacedFilePosition) == parseInt(MaxCategory)) {
		displayDiv('DivMoveDown_'+MovedFileId,'hide');
		displayDiv('DivMoveDownDisable_'+MovedFileId,'show');
	}
	if(parseInt(MovedFilePosition) == 1) {
		displayDiv('DivMoveUp_'+ReplacedFileId,'hide');	
		displayDiv('DivMoveUpDisable_'+ReplacedFileId,'show');	
	}
	if(parseInt(MovedFilePosition) == parseInt(MaxCategory)) {
		displayDiv('DivMoveDown_'+ReplacedFileId,'hide');
		displayDiv('DivMoveDownDisable_'+ReplacedFileId,'show');
	}
}
// End Move File Div

function callfunction(func) {
	var bb = typeof document.getElementsByName(func);
alert(bb);
}

function getScrollX() {
	if (ie || ie6) {
		return document.documentElement.scrollLeft;
	}
	else {
		return window.pageXOffset;
	}
}

function getScrollY() {
	if (ie || ie6) {
		return document.documentElement.scrollTop;
	}
	else {
		return window.pageYOffset;
	}
}


// Start SendGetRequest to send GET request and return the result
function SendGetRequest(url,myFunction) {
	var xhr_object = null; 
	 
	if(window.XMLHttpRequest) // Firefox 
	   xhr_object = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // Internet Explorer 
	   xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	else { // XMLHttpRequest non supporté par le navigateur 
	   alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   return; 
	} 

	xhr_object.onreadystatechange = function() { 
	   if(xhr_object.readyState == 4){
		   //alert(xhr_object.responseText);
	   }
	} 
	
	xhr_object.open("GET", url, true); 
	xhr_object.send(null);
	return xhr_object
}
// end SendGetRequest
