//Gestion des dates

//padding des entiers
function padInteger(iValue)
{
    if (iValue < 10)
        return '0' + iValue.toString();
    else
        return iValue.toString();
}
function padInt(iValue)
{
    return padInteger(iValue);
}

//Ecriture d'une date + heure dans la locale utilisateur (prise en compte du fuseau)
//Parametre accepte : date au format UTC : mm/jj/yyyy hh:mm UTC
function toLocaleDateTime(sDate, sLang)
{
    if (sDate == '')
        return;
    var tDate, strDate;
    tDate = new Date(sDate);
    strDate = getLocaleDateTime(tDate, sLang);
    document.write(strDate);
}

//Conversion d'une date dans la locale utilisateur (prise en compte du fuseau)
//Parametre accepte : date au format UTC : mm/jj/yyyy hh:mm UTC
function toLocaleDate(sDate, sLang)
{
    if (sDate == '')
        return;
    var tDate, strDate;
    tDate = new Date(sDate);
    strDate = getLocaleDate(tDate, sLang);
    document.write(strDate);
}

//Conversion d'une date dans la locale utilisateur (prise en compte du fuseau)
//Parametre accepte : date au format UTC : mm/jj/yyyy hh:mm UTC
function getLocaleDateTime(tDate, sLang)
{
    var strDate;
    strDate = getLocaleDate(tDate, sLang);
    strDate = strDate + " " + padInteger(tDate.getHours()) + ':' + padInteger(tDate.getMinutes());
    return strDate;
}

function getUTCDateTime(tDate, sLang)
{
    var strDate;
    strDate = getUTCDate(tDate, sLang);
    strDate = strDate + " " + padInteger(tDate.getUTCHours()) + ':' + padInteger(tDate.getUTCMinutes());
    return strDate;
}

//Format de dates les plus courants
function getLocaleDate(tDate, sLang)
{
    var strDate;
    sLang = sLang.substr(0,2); 
    
    switch (sLang)
    {
        case 'de':
            strDate = padInteger(tDate.getDate()) + '.' + padInteger(tDate.getMonth()+1) + '.' + tDate.getFullYear();
            break;
        case 'el':
            strDate = padInteger(tDate.getDate()) + '/' + padInteger(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'en':
            strDate = padInteger(tDate.getMonth()+1) + '/' + padInteger(tDate.getDate()) + '/' + tDate.getFullYear();
            break;
        case 'es':
            strDate = padInteger(tDate.getDate()) + '/' + padInteger(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'fr':
            strDate = padInteger(tDate.getDate()) + '/' + padInteger(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'it':
            strDate = padInteger(tDate.getDate()) + '/' + padInteger(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'ja':
            strDate =  tDate.getFullYear() + '/' + padInteger(tDate.getMonth()+1) + '/' + padInteger(tDate.getDate());
            break;
        case 'ru':
            strDate = padInteger(tDate.getDate()) + '.' + padInteger(tDate.getMonth()+1) + '.' + tDate.getFullYear();
            break;
        case 'zh':
            strDate =  tDate.getFullYear() + '-' + padInteger(tDate.getMonth()+1) + '-' + padInteger(tDate.getDate());
            break;
        default :
            strDate = padInteger(tDate.getMonth()+1) + '/' + padInteger(tDate.getDate()) + '/' + tDate.getFullYear();
    }

    return strDate;
}

//Format de dates les plus courants
function getUTCDate(tDate, sLang)
{
    var strDate;
    sLang = sLang.substr(0,2); 
    
    switch (sLang)
    {
        case 'de':
            strDate = padInteger(tDate.getUTCDate()) + '.' + padInteger(tDate.getUTCMonth()+1) + '.' + tDate.getUTCFullYear();
            break;
        case 'el':
            strDate = padInteger(tDate.getUTCDate()) + '/' + padInteger(tDate.getUTCMonth()+1) + '/' + tDate.getUTCFullYear();
            break;
        case 'en':
            strDate = padInteger(tDate.getUTCMonth()+1) + '/' + padInteger(tDate.getUTCDate()) + '/' + tDate.getUTCFullYear();
            break;
        case 'es':
            strDate = padInteger(tDate.getUTCDate()) + '/' + padInteger(tDate.getUTCMonth()+1) + '/' + tDate.getUTCFullYear();
            break;
        case 'fr':
            strDate = padInteger(tDate.getUTCDate()) + '/' + padInteger(tDate.getUTCMonth()+1) + '/' + tDate.getUTCFullYear();
            break;
        case 'it':
            strDate = padInteger(tDate.getUTCDate()) + '/' + padInteger(tDate.getUTCMonth()+1) + '/' + tDate.getUTCFullYear();
            break;
        case 'ja':
            strDate =  tDate.getUTCFullYear() + '/' + padInteger(tDate.getUTCMonth()+1) + '/' + padInteger(tDate.getUTCDate());
            break;
        case 'ru':
            strDate = padInteger(tDate.getUTCDate()) + '.' + padInteger(tDate.getUTCMonth()+1) + '.' + tDate.getUTCFullYear();
            break;
        case 'zh':
            strDate =  tDate.getUTCFullYear() + '-' + padInteger(tDate.getUTCMonth()+1) + '-' + padInteger(tDate.getUTCDate());
            break;
        default :
            strDate = padInteger(tDate.getUTCMonth()+1) + '/' + padInteger(tDate.getUTCDate()) + '/' + tDate.getUTCFullYear();
    }

    return strDate;
}

//Converti une date XML en mm/jj/yyyy hh:mm UTC
function fromXMLUTCDate(sXMLDate)
{
    var sDate;
    sDate =  sXMLDate.substr(5,2) + '/' + sXMLDate.substr(8,2) + '/' + sXMLDate.substr(0,4) + ' ' + sXMLDate.substr(11,2) + ':' + sXMLDate.substr(14,2) + ' UTC';   
    return sDate;
}
//Converti une date XML en mm/jj/yyyy hh:mm
function fromXMLDate(sXMLDate)
{
    var sDate;
    sDate =  sXMLDate.substr(5,2) + '/' + sXMLDate.substr(8,2) + '/' + sXMLDate.substr(0,4) + ' ' + sXMLDate.substr(11,2) + ':' + sXMLDate.substr(14,2);   
    return sDate;
}
function getXMLDate(tDate)
{
    var strDate;
    strDate = padInteger(tDate.getFullYear());
    strDate = strDate + '-' + padInteger(tDate.getMonth()+1);
    strDate = strDate + '-' + padInteger(tDate.getDate());
    strDate = strDate + 'T' + padInteger(tDate.getHours()) + ':' + padInteger(tDate.getMinutes()) + ':00';
    return strDate;
}

function getUTCXMLDate(tDate)
{
    var strDate;
    strDate = padInteger(tDate.getUTCFullYear());
    strDate = strDate + '-' + padInteger(tDate.getUTCMonth()+1);
    strDate = strDate + '-' + padInteger(tDate.getUTCDate());
    strDate = strDate + 'T' + padInteger(tDate.getUTCHours()) + ':' + padInteger(tDate.getUTCMinutes()) + ':00';
    return strDate;
}
function fixDisplayDateTime(sField, sLang, bShowTime)
{
    //on recupere le champ form qui contient la date
    //en xml / utc
    var sDate = document.LMForm.elements['_xfer_' + sField].value;
    
    var sUTCDate = sDate.substr(5,2) + '/' + sDate.substr(8,2) + '/' + sDate.substr(0,4);
    sUTCDate = sUTCDate + ' ' + sDate.substr(11,8) + ' UTC';
    
    var tDate = new Date(sUTCDate);
    sLang = sLang.substr(0,2);
    switch (sLang)
    {
        case 'de':
            sDate = padInt(tDate.getDate()) + '.' + padInt(tDate.getMonth()+1) + '.' + tDate.getFullYear();
            break;
        case 'el':
            sDate = padInt(tDate.getDate()) + '/' + padInt(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'en':
            sDate = padInt(tDate.getMonth()+1) + '/' + padInt(tDate.getDate()) + '/' + tDate.getFullYear();
            break;
        case 'es':
            sDate = padInt(tDate.getDate()) + '/' + padInt(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'fr':
            sDate = padInt(tDate.getDate()) + '/' + padInt(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'it':
            sDate = padInt(tDate.getDate()) + '/' + padInt(tDate.getMonth()+1) + '/' + tDate.getFullYear();
            break;
        case 'ja':
            sDate =  tDate.getFullYear() + '/' + padInt(tDate.getMonth()+1) + '/' + padInt(tDate.getDate());
            break;
        case 'ru':
            sDate = padInt(tDate.getDate()) + '.' + padInt(tDate.getMonth()+1) + '.' + tDate.getFullYear();
            break;
        case 'zh':
            sDate =  tDate.getFullYear() + '-' + padInt(tDate.getMonth()+1) + '-' + padInt(tDate.getDate());
            break;
        default :
            sDate = padInt(tDate.getMonth()+1) + '/' + padInt(tDate.getDate()) + '/' + tDate.getFullYear();
    }
    //affichage de l'heure ou pas
    if (bShowTime)
        sDate = sDate + ' ' + padInt(tDate.getHours()) + ':' + padInt(tDate.getMinutes());
    //actualisation de l'affichage
    document.LMForm.elements['_' + sField].value = sDate;
    return;
}
