﻿//   Nom ................... : Funcions Java
//   Formulari ............. : slw_FuncionsJavaScript
//   Inici tasca ........... : Daniel Vila (28 Abr 2010)
//   Canvis ................ : Daniel Vila (02 Nov 2011)
//   Canvis ................ : 
//   Canvis ................ : 
//   Canvis ................ : 
//   Canvis ................ :
//   Comentari ............. :

//
// var browserName = navigator.appName;
// if (browserName == "Netscape") 

//document.bgColor = '#dcdcba'

windowsverticalcenter(625);

attachEventHandler(window, "onresize", function() { windowsverticalcenter(625); })

function finestra(mode) {

    var formulari = document.forms[0]

    if (mode == undefined) { mode = "_self" }

    formulari.target = mode;

}

function finestranova(mode, reduida, fullscreen) {

    var formulari = document.forms[0]

    if (mode == undefined) { mode = "_self" }

    formulari.target = mode;

    if (reduida != undefined) { formulari.menubar = 0; formulari.toolbar = 0; formulari.statusbar = 0; formulari.url = 0 }

    if (fullscreen != undefined) { formulari.fullscreen = true }

}

function maximize() { window.moveTo(0, 0); window.resizeTo(screen.availWidth, screen.availHeight); }

function obrirfinestra(pagina) { window.open(pagina, '', 'fullscreen=yes,width=' + screen.availWidth + ',height=' + screen.availHeight + ',left=0,top=0,location=no,menubar=no,status=no,toolbar=no,scrollbars=no,resizable=yes'); }

function tancarfinestra() { window.opener = self; window.close(); }

function attachEventHandler(element, eventToHandle, eventHandler) {

    if (element.attachEvent) { element.attachEvent(eventToHandle, eventHandler); }

    else if (element.addEventListener) { element.addEventListener(eventToHandle.replace("on", ""), eventHandler, false); }

    else { element[eventToHandle] = eventHandler; }

}

function GetFocus(control, color, statusbar, statusbartext) { control.style.backgroundColor = color; statusbar.innerHTML = statusbartext; }

function LostFocus(control, color, statusbar) { control.style.backgroundColor = color; statusbar.text = '' }

function MouseOver(tr, backcolor, forecolor) { tr.style.backgroundColor = backcolor; tr.style.color = forecolor; }

function MouseOut(tr, backcolor, forecolor) { tr.style.backgroundColor = backcolor; tr.style.color = forecolor; }

function Change(control, label) { if (label != null) { label.style.fontWeight = 'bold'; label.style.color = 'blue' } }

function TeclesNavegacio(control) {

    var nom = control.id;
    var dummy = nom.split('_');
    var sControl = dummy[0];
    var nControl = parseInt(dummy[1]);
    var tControls = parseInt(dummy[2]);
    var key = window.event.keyCode;

    if (!IsNumeric(nControl)) {

        if (window.event.shiftKey && (key == 9)) { return true; }

        if (window.event && (key == 9)) { return true; }

        if ((key == 13) || (key == 40)) { window.event.keyCode = 9; return true; }

        if (key == 38) { return false; }

    }

    else {

        if (window.event.shiftKey && (key == 9)) { GetFocusItem(control, nControl - 1); return false; }

        if (((window.event && (key == 9)) || (key == 13) || (key == 40))) { GetFocusItem(control, nControl + 1); return false; }

        if (key == 38) { GetFocusItem(control, nControl - 1); return false; }

    }

}

function Tecles(control, itipusdada, iuppercase, icaractersvalids, separadordecimal, separadordata, formatdata, label) {

    var nom = control.id;
    var dummy = nom.split('_');
    var sControl = dummy[0];
    var nControl = parseInt(dummy[1]);
    var tControls = parseInt(dummy[2]);
    var key = window.event.keyCode;
    var strkey = String.fromCharCode(key);
    var valor = control.value;
    var lbl = document.getElementById(label);

    if (lbl != null) { lbl.style.fontWeight = 'bold'; }

    if (iuppercase == 1)    // -> Majúscules

    { if ((key >= 97) && (key <= 122)) { key = key - 32; window.event.keyCode = key; strkey = String.fromCharCode(key); } }

    if (itipusdada == 1)    // Binary

    { if ("01".indexOf(strkey) >= 0) { return true; } else { return false; } }

    if (itipusdada == 2)    // Enter
    {

        if (("-".indexOf(strkey) >= 0) && (valor != "")) { return false; }
        if ("0123456789-".indexOf(strkey) >= 0) { return true; } else { return false; }

    }

    if ((itipusdada == 3) || (itipusdada == 18))    //  Real ó Resultat
    {

        if (("-".indexOf(strkey) >= 0) && (valor != "")) { return false; }
        if (".".indexOf(strkey) >= 0) { key = 44; strkey = String.fromCharCode(key); window.event.keyCode = key; }
        if ((",".indexOf(strkey) >= 0) && (valor == "")) { return false; }
        if ((",".indexOf(strkey) >= 0) && (valor.indexOf(",") >= 0)) { return false; }
        if ("0123456789-,".indexOf(strkey) >= 0) { return true; } else { return false; }

    }

    if (itipusdada == 5)   //  Hora

    { if (("0123456789:" + icaractersvalids).indexOf(strkey) >= 0) { return true; } else { return false; } }

    if (itipusdada == 6)   //  Data

    { if (("0123456789/NOW-" + icaractersvalids).indexOf(strkey) >= 0) { return true; } else { return false; } }

    if (itipusdada == 7)   //  Data Time

    { if (("0123456789:" + icaractersvalids).indexOf(strkey) >= 0) { return true; } else { return false; } }

    if (itipusdada == 12)   //  Dob
    {

        if ((icaractersvalids.indexOf(strkey) >= 0) && ((valor == "") || (valor.indexOf(strkey) >= 0))) { return false; }
        if (("0123456789/NOW-" + icaractersvalids).indexOf(strkey) >= 0) { return true; } else { return false; }

    }

    if (itipusdada == 13)   //  Edat
    {

        if ((icaractersvalids.indexOf(strkey) >= 0) && ((valor == "") || (valor.indexOf(strkey) >= 0))) { return false; }
        if (("0123456789" + icaractersvalids).indexOf(strkey) >= 0) { return true; } else { return false; }

    }

}

function TractarText(control, itipusdada, separadordecimal, separadordata, formatdata) {

    var valor = control.value;
    var dummy = valor.split(separadordata);
    var data = new Date();
    var day = data.getDate();
    var month = data.getMonth() + 1;
    var year = data.getFullYear();

    if ((itipusdada == 6) || (itipusdada == 12))   //  Data, Dob
    {

        if (valor == '') { return true; }

        if ((valor == "NOW") || (valor == "now") || (valor == "N") || (valor == "n")) { control.value = DiaMesAny(control, day + separadordata + month + separadordata + year, separadordata); return true; }

        if (valor.slice(-1) > "9") { return valor; }

        if (dummy.length == 1) {

            if (dummy[0].length <= 2) { control.value = DiaMesAny(control, valor.substr(0, 2) + separadordata + month + separadordata + year, separadordata); return true; }

            if (dummy[0].length <= 4) { control.value = DiaMesAny(control, dummy[0].substr(0, 2) + separadordata + dummy[0].substr(2, 2) + separadordata + year, separadordata); return true; }

            { control.value = DiaMesAny(control, valor.substr(0, 2) + separadordata + valor.substr(2, 2) + separadordata + valor.substr(4), separadordata); return true; }

        }

        if (dummy.length == 2) { control.value = DiaMesAny(control, dummy[0] + separadordata + dummy[1] + separadordata + year, separadordata); return true; }

        if (dummy.length == 3) { control.value = DiaMesAny(control, dummy[0] + separadordata + dummy[1] + separadordata + dummy[2], separadordata); return true; }

    }

}

function DiaMesAny(control, valor, separadordata) {

    var dummy = valor.split(separadordata);
    var day = dummy[0];
    var month = dummy[1];
    var year = dummy[2];

    day = ("00" + day).slice(-2);
    month = ("00" + month).slice(-2);
    year = "2000".substr(0, 4 - year.length) + year;

    valor = day + separadordata + month + separadordata + year

    if (!isDate(valor, separadordata)) { alert(valor + " ... Is not a Date !"); valor = control.defaultValue; }

    control.defaultValue = valor

    return valor

}

function isDate(valor, separadordata) {

    var dummy = valor.split(separadordata);
    var day = dummy[0];
    var month = dummy[1];
    var year = dummy[2];
    var data = new Date(month + separadordata + day + separadordata + year);

    return (data.getDate() == day) && (data.getMonth() + 1 == month) && (data.getFullYear() == year);

}

function IsNumeric(input) {

    if (isNaN(input)) { return false; }

    return (input - 0) == input;

}

//enmTiDaBinary = 1  - Fet
//enmTiDaEnter = 2 - Fet
//enmTiDaReal = 3 - Fet
//enmTiDaHexadecimal = 4
//enmTiDaHora = 5 - Fet
//enmTiDaData = 6 - Fet
//enmTiDaDataTime = 7 - Fet
//enmTiDaDataTimeDesde = 8
//enmTiDaDataTimeFins = 9
//enmTiDaCaracters = 10
//enmTiDaNumeroPeticio = 11
//enmTiDaDob = 12 - Fet
//enmTiDaEdat = 13 - Fet
//enmTiDaDataAny = 14
//enmTiDaDataMes = 15
//enmTiDaDataDia = 16
//enmTiDaDataMesAny = 17
//enmTiDaResultat = 18
//enmTiDaDataSemestre = 19
//enmTiDaDataTrimestre = 20
//enmTiDaDataSemestreAny = 21
//enmTiDaDataTrimestreAny = 22
//enmTiDaImatge = 24
//enmTiDaPanellPrograma = 24

function performclick(control) {

    var nom = control.id;
    var key = window.event.keyCode;
    var boto = document.getElementById(control.id);

    if (key == 13) { boto.click(); }

}

function campsobligatoris(strcamps, texte) {

    var dummy;
    var dummy1;
    var n;
    var stroutput = "";
    var control;

    dummy = strcamps.split(",");

    for (n = 0; n <= dummy.length - 1; n++) {

        dummy1 = dummy[n].split(",");

        control = document.getElementById(dummy[n]);

        if (control.name.substr(0, 5) == "tbox_") { if (control.value == "") { stroutput = stroutput + dummy[n] + " / "; } }

        if (control.name.substr(0, 7) == "ddlist_") { if (control.value == "0") { stroutput = stroutput + dummy[n] + " / "; } }

    }


    if (stroutput != "") { alert(texte); return false; } else { return true; }

}

function ChkBoldChecked(control) {

    //var nom = control.id;
    //var chk = document.getElementById(nom);

    //if (chk.checked == true) { chk.style.backgroundColor = 'blue'; chk.style.color = 'blue'; chk.style.fontWeight = 'bold'; } else { chk.style.backgroundColor = 'white'; chk.style.color = 'black'; chk.style.fontWeight = 'normal'; }

    if (control.checked == true) { control.style.backgroundColor = 'blue'; control.style.color = 'blue'; control.style.fontWeight = 'bold'; } else { control.style.backgroundColor = 'white'; control.style.color = 'black'; control.style.fontWeight = 'normal'; }

}

function GetFocusItem(control, item) {

    var nom = control.id;
    var dummy = nom.split('_');
    var sControl = dummy[0];
    var nControl = parseInt(dummy[1]);
    var tControls = parseInt(dummy[2]);

    var nomseguent;
    var controlseguent;

    if (item < 1) { item = tControls }
    if (item > tControls) { item = 1 }

    nomseguent = "tbox_" + item + "_" + tControls; controlseguent = document.getElementById(nomseguent); if (controlseguent != null) { controlseguent.focus(); return true; }
    nomseguent = "ddlist_" + item + "_" + tControls; controlseguent = document.getElementById(nomseguent); if (controlseguent != null) { controlseguent.focus(); return true; }
    nomseguent = "chk_" + item + "_" + tControls; controlseguent = document.getElementById(nomseguent); if (controlseguent != null) { controlseguent.focus(); return true; }

    return false;

}

function windowsverticalcenter(h) {

    var topmargin = ((document.documentElement.clientHeight - h) / 2) - 25;

    document.documentElement.style.marginTop = topmargin + 'px'

    window.focus

}

function ocultarurl() {

    document.URL = ""

    window.focus

}

