
/**
 * Display an element.
 * @param divId the id of the element
 */
function displayDiv(divId) {
   viewDiv = document.getElementById(divId);
      if (viewDiv) {
         display = viewDiv.style.display;
      if (display == 'none') {
         viewDiv.style.display = 'block';
      } else if (display == 'block') {
         viewDiv.style.display = 'none';
      }
   }
}

/**
 * Hide an element.
 * @param divId the id of the element
 */
function hideDiv(divId) {
   if (document.getElementById) { // DOM3 = IE5, NS6
      document.getElementById(divId).style.visibility = 'hidden';
   } else {
      if (document.layers) { // Netscape 4
         document.hideshow.visibility = 'hidden';
      } else { // IE 4
         document.all.hideshow.style.visibility = 'hidden';
      }
   }
}

/**
 * Show an element.
 * @param divId the id of the element
 */
function showDiv(divId) {
   if (document.getElementById) { // DOM3 = IE5, NS6
      document.getElementById(divId).style.visibility = 'visible';
   } else {
      if (document.layers) { // Netscape 4
         document.hideshow.visibility = 'visible';
      } else { // IE 4
         document.all.hideshow.style.visibility = 'visible';
      }
   }
}

/**
 * Open a popup.
 * @param url the popup url
 */
 //TODO Make options an array and parse all possible option to compute the string option
 //SEE http://www.toutjavascript.com/savoir/savoir15.php3
function popupWin(url, options) {
	if(!options) {
		options = 'resizable=0,scrollbars=1,width=800,height=600,';
	}
  	pw=window.open(url, '_blank' , options);
}

/**
 * Change an element's id.
 * @param id the id to change
 * @param length the beginning index to substring
 */
function changeId(id, length) {
   //TODO Optimize ?
   var sId = new String(id);
   sId = sId.substring(length, sId.length);
   return sId;
}

/**
 * Fade an element.
 * @param id
 * @return
 */
function fade(id, duration, timeout) {
	if(duration == null) {
		duration = 2000;
	}
	if(timeout == null) {
		timeout = 2000;
	}
	setTimeout(function() {
		$('#'+id).fadeOut(duration, function () {
			$('#'+id).remove();
      	});
	}, timeout);
}

/**
 * Confirmation message.
 * @param message
 * @return boolean
 */
function deleteUserConfirmation() {
	var answer = confirm ("\u00CAtes-vous s\u00FBr de vouloir supprimer cet utilisateur ?");
	if (answer) {
		return true;
	} else {
		return false;
	}
}

/**
 * Check all element by element name.
 * @param elementID
 */
function checkAll(elementName) {
	var i;
	var elements = document.getElementsByName(elementName);
	for(i=0; i<elements.length; i++) {
		elements[i].checked = true;
	}
}

/**
 * Uncheck all element by element name.
 * @param elementID
 */
function uncheckAll(elementName) {
	var i;
	var elements = document.getElementsByName(elementName);
	for(i=0; i<elements.length; i++) {
		elements[i].checked = false;
	}
}

/**
 * Confirmation message.
 * @param message
 * @return boolean
 */
function endSubscriptionConfirmation() {
	var answer = confirm ("\u00CAtes-vous s\u00FBr de vouloir stopper l'abonnement de cet utilisateur ?");
	if (answer) {
		return true;
	} else {
		return false;
	}
}

/**
 * Confirmation choice.
 * @return boolean.
 */
function chooseDateSubscription() {
	var text = "merci de choisir le mois de debut de l'abonnement";
	if(!startTwoChoices.subscription[0].checked && !startTwoChoices.subscription[1].checked) {
		return false;
	}
	return true;
}

/**
 * Perform ajax stuff available with dhtmlxAjax.
 * @param id : id of drug.
 * @return
 */
function ajaxLocalAddPrescription(id) {
	var randomTimeValue = Math.random();
	var url = "prescriptionAdd!execute?drugId=" + id + "&time=" + randomTimeValue;
	var loader = dhtmlxAjax.getSync(url);
	$("#divOrdonnanceVirtuelle").html(loader.xmlDoc.responseText);
	var urlBis = "displayPrescriptionCount!execute?drugId=" + id + "&time=" + randomTimeValue;
	var loaderBis = dhtmlxAjax.getSync(urlBis);
	$("#aim").html(loaderBis.xmlDoc.responseText);
	var s=s_gi('cmpmvidalofficine');s.events='scAdd';s.tl(this,'o',id);
}

/**
 * Perform ajax stuff available with dhtmlxAjax.
 * @param id : id of drug.
 * @return
 */
function ajaxLocalRemoveOnePrescription(id) {
	var randomTimeValue = Math.random();
	var url = "prescriptionRemove!execute?drugId=" + id + "&time=" + randomTimeValue;
	var loader = dhtmlxAjax.getSync(url);
	$("#divOrdonnanceVirtuelle").html(loader.xmlDoc.responseText);
	var urlBis = "displayPrescriptionCount!execute?drugId=" + id + "&time=" + randomTimeValue;
	var loaderBis = dhtmlxAjax.getSync(urlBis);
	$("#aim").html(loaderBis.xmlDoc.responseText);
	prescriptionHidePrescriptionList();
}

/**
 * Perform ajax stuff available with dhtmlxAjax.
 * @param id : id of drug.
 * @return
 */
function ajaxLocalRemoveAllPrescription() {
	var randomTimeValue = Math.random();
	var url = "prescriptionRemoveAll!execute?time=" + randomTimeValue;
	var loader = dhtmlxAjax.getSync(url);
	$("#divOrdonnanceVirtuelle").html(loader.xmlDoc.responseText);
	var urlBis = "displayPrescriptionCount!execute?time=" + randomTimeValue;
	var loaderBis = dhtmlxAjax.getSync(urlBis);
	$("#aim").html(loaderBis.xmlDoc.responseText);
	prescriptionHidePrescriptionList();
}

function ajaxLocalResetUnknowInteraction() {
	var randomTimeValue = Math.random();
	var url = "prescriptionAdd!resetUnknowProblem?time=" + randomTimeValue;
	var loader = dhtmlxAjax.getSync(url);
}

/**
 * perform ajax stuff to display news.
 * @param id : id.
 * @return 
 */
function ajaxCallNewsRestService(id, term) {
	if(id == null || id == 0) {
		var url = "displayLastNews!execute";
		var loader = dhtmlxAjax.getSync(url);
		$("#newswrapper").html(loader.xmlDoc.responseText);
	}
	else {
		var url = "displayNumberOfNews!execute?drugId=" + id + "&term=" + term;
		var loader = dhtmlxAjax.getSync(url);
		$("#newswrapper").html(loader.xmlDoc.responseText);
	}
}

/**
 * Get drug number.
 */
function prescriptionGetNbItems() {
	return document.getElementById("nbDrugsInPrescription").value;
}

/**
 * Hide prescription list.
 */
function prescriptionHidePrescriptionList() {
	if(prescriptionGetNbItems() == 0) {
		$("#divOrdonnanceVirtuelle").hide();
	}
}



function disabledCategory( strCategoryName, bAvailable, bHospitalier ) {
    if( bAvailable == false ) {
        disabledAvailableCategory( strCategoryName );
    }
    if( bHospitalier == false ) {
        disabledHospitalierCategory( strCategoryName );
    }
}
function disabledAvailableCategory( strCategoryName ) {
    var strCheckboxName = "available" +strCategoryName +"FilterCheckbox";
    var strLabelName = "available" +strCategoryName +"FilterCheckboxLabel";
    disableFilter( strCheckboxName, strLabelName );
}
function disabledHospitalierCategory( strCategoryName ) {
    var strCheckboxName = "hospital" +strCategoryName +"FilterCheckbox";
    var strLabelName = "hospital" +strCategoryName +"FilterCheckboxLabel";
    disableFilter( strCheckboxName, strLabelName );
}
function disableFilter( strCheckboxName, strLabelName ) {
    objCheckbox = document.getElementById(strCheckboxName);
    objLabel = document.getElementById(strLabelName);
    if( (objCheckbox!=null) && (objLabel!=null) ) {
        objCheckbox.disabled = 'true';
        objLabel.disabled = 'true';
    }
}
