/* GENERAL FUNCTIONS */

// getElementById Special to handle quirky browsers
// most will use getElementById()
function getElementById_s(id) {
	var obj = null;
	if(document.getElementById){
		/* Prefer the widely supported W3C DOM method, ifavailable:-*/
		obj = document.getElementById(id);
	} else if(document.all){
		/* Branch to use document.all on document.all only
		browsers. Requires that IDs are unique to the page
		and do not coincide with NAME attributes on other
		elements:-*/
		obj = document.all[id];
	}

	/* If no appropriate element retrieval mechanism exists on
	this browser this function always returns null:-*/
	return obj;
}

/* FORM FUNCTIONS */

function setFocus(field) {
	field.focus();
}

function attachEvt(func) {
	if (document.all&&window.attachEvent) { // IE-Win
	     window.attachEvent('onload', func);
	} else if (window.addEventListener) { // Others
	     window.addEventListener('load', func, false);
	}
}

function updateField(field, defaultValue, hasFocus) {
	if (hasFocus && (field.value==defaultValue)) {
		field.value='';
	} else if (field.value == '') {
		field.value=defaultValue;
	}
}

function setDefaultValue(field, defaultValue) {
	field.value=defaultValue;
	var onfocus = function f() { updateField(field, defaultValue, true); };
	var onblur = function f() { updateField(field, defaultValue, false); };
	
	if (field.attachEvent) { // IE-Win
	     field.attachEvent('onfocus', onfocus);
	     field.attachEvent('onblur', onblur);
	} else { // Others
	     field.addEventListener('focus', onfocus, false);
	     field.addEventListener('blur', onblur, false);
	}
}

/* MENU FUNCTIONS */
function aff(objectId)
{
	document.getElementById(objectId).style.display = "block";
}

function cache(objectId)
{
	document.getElementById(objectId).style.display = "none";
}

function toggleMenu(divId)
{	
	pageDiv = document.getElementsByTagName('DIV');
	nombreMenu = 0;
	
	for (var i=0; i<pageDiv.length; i++)
	{
		if (pageDiv[i].id.substring(0, 5) == "sMenu")
		{
			nombreMenu++;
		}
	}
	
	for (var i=1; i<=nombreMenu; i++)
	{
		if(document.getElementById('sMenu'+i).style.display == 'block' && (('sMenu'+i) != divId))
		{
			document.getElementById('sMenu'+i).style.display = 'none';
		}
	}
	var oObj = document.getElementById(divId);
	if(oObj.style.display == 'block')
		oObj.style.display = 'none';
	else
		oObj.style.display = 'block';
}

function openPopupCG(numArbitrage, numCompte, popup) 
{
	showOverlay();
	document.getElementById('arbitrageChoisi').value = numArbitrage;
	document.getElementById('numCompte').value = numCompte;	
	document.getElementById(popup).style.visibility="visible";
}

function openPopupCGDetail() 
{
	showOverlay();
	document.getElementById('popupCG').style.visibility="visible";
}

function openPopupValidation()
{
	showOverlay();
	document.getElementById('popup').style.visibility="visible";
}

function closePopup(popupId)
{
	document.getElementById(popupId).style.visibility="hidden";
	hideOverlay();
}

function checkToActivateButton(innerContenerName, checkboxName)
{
	if (document.getElementById(checkboxName).checked == true)
	{
		document.getElementById(innerContenerName).innerHTML = "<a class='linkBox linkBoxImprimer' href='#' onclick='window.open(\"/static/pages/CG_ACL.pdf\",\"CG_ACL\",\"width=800;height=600;\");'>imprimer</a>&nbsp;<a class='linkBox linkBoxValider' href='./detailArbitrage.do?numArbitrage="+ document.getElementById('arbitrageChoisi').value +"&cpt="+ document.getElementById('numCompte').value +"'>valider</a>";
	}
	else
	{
		document.getElementById(innerContenerName).innerHTML = "<a class='linkBox linkBoxImprimer' href='#' onclick='window.open(\"/static/pages/CG_ACL.pdf\",\"CG_ACL\",\"width=800;height=600;\");'>imprimer</a>&nbsp;<a class='linkBoxOff linkBoxValiderOff'>valider</a>";
	}
}

function checkValidationToActivateButton(checkboxIds, typeContrat)
{
	
	var allChecked = true;
	
	for (i=0; i<checkboxIds.length; i++)
	{
		if (document.getElementById(checkboxIds[i]).checked == false)
			allChecked = false;
	}
	
	if (allChecked == true)
	{
		if (typeContrat == 'pea')
		{
			document.getElementById('confirmProp').innerHTML = "<a href='./arbitrageValidationClient.do?dispatch=validateArbitragePeaConsulte' class='linkBox linkBoxFleche'>confirmer</a>";
		}
		else
		{
			document.getElementById('confirmProp').innerHTML = "<a href='#' class='linkBox linkBoxFleche' onClick='openPopupValidation();'>confirmer</a>";
		}
	}
	else
	{
		document.getElementById('confirmProp').innerHTML = "<a class='linkBoxOff linkBoxFlecheOff'>confirmer</a>";
	} 
}

function checkValidationToActivateButtonLSF()
{
	var lsf = document.getElementById('lsf');
	
	if (lsf.checked == true)
	{
		document.getElementById('confirmProp').innerHTML = "<a href='./arbitrageValidationClient.do?dispatch=validateArbitragePeaConsulte' class='linkBox linkBoxFleche'>confirmer</a>";
	}
	else
	{
		document.getElementById('confirmProp').innerHTML = "<a class='linkBoxOff linkBoxFlecheOff'>confirmer</a>";
	} 
}

function checkAndMotifToActivateButton(checkBoxId, isMotifRequired, selectId)
{
		if (isMotifRequired == true && document.getElementById(checkBoxId).checked == true && document.getElementById(selectId).value != 0 ||
			isMotifRequired == false && document.getElementById(checkBoxId).checked == true)
		
		{
			document.getElementById('submitButton').innerHTML = "<a class='linkBox linkBoxValider' href='#' onclick='arbitrageActionsWFForm.submit();'>valider</a>";
		}
		else
		{
			document.getElementById('submitButton').innerHTML = "<a class='linkBoxOff linkBoxValiderOff'>valider</a>";
		}
}

function toggleSelect(selectId)
{
	selects = document.getElementsByTagName('SELECT');
	for (var i=0; i<selects.length; i++)
	{
		selects[i].value = 0;
		
		if (selects[i].id == selectId)
			selects[i].disabled = false;
		else
			selects[i].disabled = true;
	}
}

function fillMotif(selectId)
{
	if (selectId == null)
		document.getElementById('motifAnnulation').value = '';
	else
		document.getElementById('motifAnnulation').value = document.getElementById(selectId).value;
}

 function afficherDivAideRech(){
 	showOverlay();
 	document.getElementById("divAideRech").style.visibility='visible'; 	
 }
 
 function fermerDivAideRech(){
 	document.getElementById("divAideRech").style.visibility='hidden'
	hideOverlay();
 }
 
 function clearText(){
 	document.getElementById("champRecherche").value="";
 } 