/*
    Document   : script.js
    Created on : 13 septembre 2011, 20:30
    Author     : Pat
*/

function preSubmit(event)
{
    sCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

    if(sCode == 13)
    {
	document._Action.oDeconnexion.disabled = 'disabled';
	document._Action.confirme.click();
    }
}

function doSubmitUpload(psSection, psMethode)
{
    document._Action.encoding = "multipart/form-data";
    doSubmit(psSection, psMethode);
}
	
function doSubmit(psSection, psMethode)
{
    document._Action.methode.value = psMethode;
    document._Action.section.value = psSection;
    document._Action.submit();
}

function doDelete(psSection, psCount)
{
    if(psCount != null && psCount != 0)
    {
	var bDelete = confirm("Voulez-vous vraiment supprimer les éléments (" + psCount + ") selectionnés?");

	if(bDelete)
	{
	    doSubmit(psSection, '');
	}
    }
}

function doModifier(psSection, psCount)
{
    if(psCount == 1)
    {
	doSubmit(psSection, '');
    }
    else
    {
	alert('Seulement un enregistrement peut être modifié à la fois.');
    }
}


//Fonction de creation de la requete pour ajax
function getXmlHttpObject()
{
    var xmlhttp = null;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
    }
    else
    { // code for IE6, IE5
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    return xmlhttp;
}

//Action ajax
function doAjaxAction(sQuery, sDivName)
{
    var xmlhttp = getXmlHttpObject();

    xmlhttp.onreadystatechange=function()
    {
	if (xmlhttp.readyState==4)
	{
	    if( xmlhttp.status==200 )
	    {
		document.getElementById(sDivName).innerHTML=xmlhttp.responseText;
		xmlhttp = "";
	    }
	}
    }

    xmlhttp.open("GET", sQuery, true);
    xmlhttp.send();
}

function checkSubmit(oChecked)
{
    var sText = ""
    var iCount = 0;

    if( document._Action.bIdChecked != null &&
	document._Action.bIdChecked.length != null )
    {
	sText = "~";

	var iLength = document._Action.bIdChecked.length;
	for(var i=0; iLength > i; i++)
	{
	    var oCheck = document._Action.bIdChecked[i];

	    if(oCheck.checked)
	    {
		sText += oCheck.value + '~';
		iCount++;
	    }
	}
    }
    else if(document._Action.bIdChecked != null  && 
	    document._Action.bIdChecked.checked == true)
    {
	sText = "~" + oChecked.value + "~";
	iCount++;
    }
    document._Action.toDelete.value = sText;
    document._Action.toDeleteCount.value = iCount;
}
