//<!--

//*** AcktelInput ***

/*
**********
COPYRIGHT: ACKTEL
    Mantainer: Paolo Patri    Authors: Paolo Patri

CHANGE LOG
    1.6    13102003:    Paolo ha aggiunto uno switch per i messaggi in lingua e implementato un metodo setLanguage()
    1.5    03072003:    Paolo ha cambiato la logica di scansione (check_all()) degli elementi nei singoli form per la ricerca dei tag <input> di tipo ackinput
                        basandosi sul prefisso del parametro id settato per default a "ackinput_" (es. "ackinput_newI3")
    1.4    14032003:    Paolo ha aggiunto il parametro inType per poter utilizzare l'oggetto come campo hidden
    1.3    03042003:    Paolo ha aggiunto un parametro facoltativo (inMsgName) per assegnare all'input un nome idoneo ai messaggi a video
    1.2    24032003:    Paolo ha aggiunto il controllo sulla lunghezza del value nel caso sia presente una maschera (Matching diretto)
                        e ha cambiato il nome al metodo create() in create_ackinput() per motivi di compatibilità con altri oggetti
    1.1    13032003:    Marco ha aggiunto il metodo setLength(Integer)
    1.0    01 2003:     first release

TODO
    -

UNRESOLVED ISSUES
    - 

USAGE
    - Inserire il seguente evento all'interno del tag del Form
      onsubmit="return check_all(this)"
      
    - Dichiarare l'oggetto e inizializzarlo come nei seguenti esempi
    
        NomeOggetto = new AcktelInput(Name, Mask, DefValue, isMandatory, ErrorMessage, NomeOggetto, [inMsgName], [inType])

        
        . Esempio 1 (con maschera esplicita):
        
            var newI0 = new AcktelInput("CODPROD", "Cn/nn-nnn", "E4/07-200", "1", "Errore nella validazione del Codice Prodotto!", "newI0", "Codice Prodotto")
            newI0.create_ackinput()

        . Esempio 2 (con maschera implicita + set della lunghezza):

            var newI3 = new AcktelInput("EMAIL", "§EMAIL", "info@acktel.com", "1", "Errore nella validazione della Email!", "newI3")
            newI6.setLength(20)
            newI3.create_ackinput()

        . Esempio 3 (senza controlli):

            var newI4 = new AcktelInput("JOLLY", "", "", "0", "Errore nella validazione della Email!", "newI4")
            newI4.create_ackinput()
            
        # Legenda per le maschere
            * -> qualunque elemento
            n -> un solo numero
            N -> uno o più numeri
            c -> un solo carattere
            C -> uno o più caratteri

METHOD

    boolean obj.check(void) -> esegue tutte le funzioni di controllo e validazione
    void obj.create_ackinput(void) -> crea il tag INPUT e imposta il default value
    string obj.getValue(void) -> recupera il testo del campo
    boolean obj.isMandy(integer) -> controlla se il campo deve essere obbligatorio
    void obj.setValue(string) = -> imposta il testo nel campo
    boolean obj.Validator(integer) -> controlla la validazione del campo in base ad una maschera
    void obj.setLength(integer) -> imposta la lunghezza del campo input, non dom
    void obj.setLanguage(string) -> imposta la lingua

**********
*/


function AcktelInput(inName, inMask, inDefVal, isMry, inErrMsg, objName, inMsgName, inType) {

this.myName = objName
this.Name = inName
this.defValue = inDefVal
this.codeMry = isMry
this.ErrorMsg = inErrMsg
this.length = 10
this.language = "ITA"
this.onchange = ""

if (inType == "") {
    this.type = "text"
} else {
    this.type = inType
}

if (inMask == "") {
    this.Mask = "*"
} else {
    this.Mask = inMask
}

if (inMsgName == null) {
    this.MsgName = this.Name
} else {
    this.MsgName = inMsgName
}

//WildList
this.WL_SoloTesto = "abcdefghilmnopqrstuvzjkxywàèéìòùABCDEFGHILMNOPQRSTUVZJKXYW'"
this.WL_Telefono = "0123456789/.- "
this.WL_SoloNumeri = "0123456789"
//this.WL_SoloNumeriPositivi = "123456789"
this.WL_Speciali = "+-/\|!?&%$£^§€ç#@[]{}"

//browser sniffing
var brwsType = navigator.userAgent.toLowerCase();
if (brwsType.indexOf("msie")>-1) {
    this.isIE = 1
} else {
    this.isIE = 0
}

//metodi
this.create_ackinput = create_ackinput
this.isMandy = isMandy
this.getValue = getValue
this.check = check
this.setValue = setValue
this.getValue = getValue
this.Validator = Validator
this.setLength = setLenght
this.setOnChange = setOnChange
this.setLanguage = setLanguage

}

function Validator(inMsk, inTxt, inWChar) {
    /* validatore
    - riceve in input la maschera e il carattere da considerare come jolly
    - analizza  sia il testo che la maschera procedendo con un sistema di tagli successivi in parallelo
    */

    var isOK = true
    
    //lettura maschera
    var mskChar = ""
    var i=0
    var tmp_inMsk = inMsk
    var tmp_inTxt = inTxt
    
    //esiste *
    //test di obsoletitudine
    //while (tmp_inMsk.indexOf(inWChar) != -1) {
    while (tmp_inMsk.length > 0) {
        //* non al primo posto
        if ((inMsk.indexOf(inWChar) != 0) && (i == 0)) {
            mskChar = inMsk.slice(0,inMsk.indexOf(inWChar))
            if (inTxt.indexOf(mskChar) != 0) {
                isOK = false
                break
            }
        } else {
            //toglie la * se al primo posto
            if (tmp_inMsk.indexOf(inWChar) == 0) tmp_inMsk = tmp_inMsk.substr(1,tmp_inMsk.length)
            //se non ci sono più * lascia la maschera come la trova
            if (tmp_inMsk.indexOf(inWChar) == -1) {
                mskChar = tmp_inMsk
                //toglie la parte * dal testo e poi lo confronta
                tmp_inTxt = tmp_inTxt.substr(tmp_inTxt.indexOf(mskChar),tmp_inTxt.length)
                //controllo esistenza
                if (tmp_inTxt != mskChar) {
                    isOK = false
                    break
                }
            } else {
                mskChar = tmp_inMsk.slice(0,tmp_inMsk.indexOf(inWChar))
            }
            //controllo esistenza
            if (tmp_inTxt.indexOf(mskChar) == -1) {
                isOK = false
                break
            }                
        }
        //alert(i+" - mskChar: "+mskChar+" tmp_inMsk: "+tmp_inMsk+" tmp_inTxt: "+tmp_inTxt)

        //taglio maschera e testo
        tmp_inMsk_x = tmp_inMsk.substr(mskChar.length,tmp_inMsk.length)
        //alert("tmp_inMsk_x: "+tmp_inMsk_x)
        tmp_inMsk = tmp_inMsk.substr(mskChar.length + 1,tmp_inMsk.length)
        tmp_inTxt = tmp_inTxt.substr(tmp_inTxt.indexOf(mskChar) + mskChar.length,tmp_inTxt.length)

        //se è rimasta solo la * nella maschera_x controllo che ci siano caratteri nel testo rimasto se no esco
        if (tmp_inMsk_x == inWChar) {
            //alert("la * è sola alla fine della maschera")
            //controllo esistenza
            if (tmp_inTxt.length == 0) {
                isOK = false
                break
            }
        }
        //alert(i+"+ - mskChar: "+mskChar+" tmp_inMsk: "+tmp_inMsk+" tmp_inTxt: "+tmp_inTxt)

        /*
        //test di obsoletitudine
        //controllo se finisce con caratteri fissi della maschera (* non è ultimo carattere)
        if (tmp_inMsk.length == mskChar.length) {
            //alert("tmp_inMsk.length = mskChar.length")
            if ((tmp_inTxt.length - tmp_inTxt.indexOf(tmp_inMsk)) != tmp_inMsk.length) {
                //alert("tmp_inMsk_x "+tmp_inMsk_x+" non è alla fine della stringa")
                isOK = false
                break
            }
        }
        */

        i++
    }
    return isOK
}

function setValue(par) {
    document.getElementById("ackinput_"+this.myName).value = par
}

function getValue() {
    return document.getElementById("ackinput_"+this.myName).value
}

function isMandy(par) {
    var isOK = true
    if (par == 1) {
        txt = this.getValue()
        if (txt == "") {
            //messaggio di obbligatorietà
            alert(this.txtIsmand_1+this.MsgName+this.txtIsmand_2)
            isOK = false
        }    
    }
    return isOK
}

function setLenght(length) {
    if (parseInt(length) > 0)    {
        this.length = length
    }
}

function setOnChange(onch) {
	this.onchange = onch;
}

function setLanguage(lang) {
    this.language = lang
    //lingue
    switch (this.language) {
        case "ENG":
            this.txtIsmand_1 = "The  "
            this.txtIsmand_2 = " field cannot be empty!"
        break;
        case "FRA":
            this.txtIsmand_1 = "Le zone "
            this.txtIsmand_2 = " ne peut pas etre vide!"
        break;
        default:
            this.txtIsmand_1 = "Il campo "
            this.txtIsmand_2 = " non può essere vuoto!"
        break;
    }
}

function check() {
    //funzioni di controllo
    var maskItems = ""            //caratteri accettati per la maschera
    var isOK = true
    var txt = document.getElementById("ackinput_"+this.myName).value
    
    //if ((txt.length == this.Mask.length) && (this.Mask.charAt(0) != "§")) {
    //se c'è una maschera e non è un implicito
    if ((this.Mask != "*") && (this.Mask.charAt(0) != "§")) {
        //alert("Diretto "+this.Mask+txt.length)
        //lunghezza testo = lunghezza maschera
        if (txt.length == this.Mask.length) {
            //*** Matching diretto ***
            var i=0
            while ((i<txt.length) && (isOK==true)) {
                //impostra il range di validita' per ogni elemento del testo
                switch (this.Mask.charAt(i)) {
                    case "C" :
                        maskItems = this.WL_SoloTesto
                    break                
                    case "S" :
                        maskItems = this.WL_Speciali
                    break                
                    case "n" :
                        maskItems = this.WL_SoloNumeri
                    break                
                    case "/" :
                        maskItems = "/"
                    break
                    case "-" :
                        maskItems = "-"
                    break
                }
                if (maskItems.indexOf(txt.charAt(i)) != -1) {
                    //alert("ok "+txt.charAt(i))
                } else {
                    isOK = false
                }
                i++
            }
        } else {
            isOK = false
        } 
        return isOK
    } else {
        //*** Matching implicito ***
        //alert("Implicito "+this.Mask)
        if (txt.length > 0) {
            switch (this.Mask) {
                case "§EMAIL" :
                    maskItems = "*@*.*"
                    //check @ singola
                    if (txt.indexOf("@") == txt.lastIndexOf("@")) {
                        isOK = this.Validator(maskItems,txt,"*")
                    } else {
                        isOK = false
                    }
                break
                case "§TELEFONO" :
                    maskItems = this.WL_Telefono+" "
                    for (var i=0; i<txt.length; i++) {
                        if (maskItems.indexOf(txt.charAt(i)) == -1) {
                            isOK = false
                        }
                    }
                break
                case "§SOLOTESTO" :
                    maskItems = this.WL_SoloTesto+" "
                    for (var i=0; i<txt.length; i++) {
                        if (maskItems.indexOf(txt.charAt(i)) == -1) {
                            isOK = false
                        }
                    }
                break
                case "§SOLONUMERI" :
                    //naturali con 0
                    maskItems = this.WL_SoloNumeri
                    for (var i=0; i<txt.length; i++) {
                        if (maskItems.indexOf(txt.charAt(i)) == -1) {
                            isOK = false
                        }
                    }
                break                                    
                case "§SOLONUMERIPOSITIVI" :
                    maskItems = this.WL_SoloNumeri
                    for (var i=0; i<txt.length; i++) {
                        if (maskItems.indexOf(txt.charAt(i)) == -1) {
                            isOK = false
                        }
                    }
                    if (txt <= 0) {
                        isOK = false
                    }
                break
                case "§NUMERIREALI" :
                    if (isNaN(txt)) {
                        isOK = false
                    }
                break
                case "§NUMERIREALIPOSITIVI" :
                    //qualunque reale, ma positivo
                    if (isNaN(txt)) {
                        isOK = false
                    } else {
                        if (txt <= 0) {
                            isOK = false
                        }
                    }
                break
                default :
                    //mascera generica

                    //attivare queste righe al posto delle altre per non fare nessuna validazione
                    //maskItems = null
                    //isOK = true

                    //maschera custom basata su carattere jolly standard (*)
                    maskItems = this.Mask.substr(1)                 //tolgo il carattere § all'inizio
                    isOK = this.Validator(maskItems,txt,"*")
                break
            }  
        }
        return isOK
    }
}

function create_ackinput() {
    document.writeln("<input id='ackinput_"+this.myName+"' type='"+this.type+"' name='"+this.Name+"' size='"+this.length+"' onchange='"+this.onchange+"' value='' class='acktInput'>")

    this.setLanguage(this.language)

    //non fa in tempo a crearlo al volo in Mozilla e da errore
    ipt = document.getElementById("ackinput_"+this.myName)
    ipt.value = this.defValue
}

//*** di supporto ***

function check_all(objFrm) {
    //funzioni di controllo
    
    var nEl = eval("document."+objFrm.name+".elements.length")
    for (var i=0; i<nEl; i++) {

        //alert(eval("document."+objFrm.name+".elements["+i+"].id").substr(0,9) == "ackinput_")

        //if ((eval("document."+objFrm.name+".elements["+i+"].type") == "text") || (eval("document."+objFrm.name+".elements["+i+"].type") == "hidden")) {
        if ((eval("document."+objFrm.name+".elements["+i+"].id").substr(0,9) == "ackinput_")) {
            var nomeIpt = eval("document."+objFrm.name+".elements["+i+"].id")
            //check IsMandatory

            //alert(eval(nomeIpt.substr(9)).codeMry)

            var mryYN = eval(nomeIpt.substr(9)).codeMry
            var check_OK = eval(nomeIpt.substr(9)).isMandy(mryYN)
            if (check_OK == false) {
                break
            }
            //validazione
            var check_OK = eval(nomeIpt.substr(9)).check()
            if (check_OK == false) {
                //stop se un input non e' validato
                alert(eval(nomeIpt.substr(9)).ErrorMsg)
                break
            }
        }
    }

    return check_OK
}
//-->