//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//                   CLASS VERIFICATION OF FORM                     //
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////


/* CONSTRUCTOR */
//////////////////////////////////////////////////////////////////////
function VerifyForm(){
    this.message = "";
    this.tab_verif = new Array();
}


/* SET VERIFY */
//////////////////////////////////////////////////////////////////////
//set list of the texts & register their old colors
VerifyForm.prototype.setVerifyText = function(tab){
    for(var i=0;i<tab.length;i++){
        var color = document.getElementsByName(tab[i]).item(0).style.borderColor;
        if(color){
            this.tab_verif.push(new Array("text",tab[i],color));
        }else{
            this.tab_verif.push(new Array("text",tab[i],'#000000'));
        }
    }
}

//set list of the textarea & register their old colors
VerifyForm.prototype.setVerifyTextarea = function(tab){
    for(var i=0;i<tab.length;i++){
        var color = document.getElementsByName(tab[i]).item(0).style.borderColor;
        if(color){
            this.tab_verif.push(new Array("text",tab[i],color));
        }else{
            this.tab_verif.push(new Array("text",tab[i],'#000000'));
        }
    }
}

//set list of the textarea & register their old colors
VerifyForm.prototype.setVerifySelect = function(tab){
    for(var i=0;i<tab.length;i++){
        var color = document.getElementsByName(tab[i]).item(0).style.borderColor;
        if(color){
            this.tab_verif.push(new Array("select",tab[i],color));
        }else{
            this.tab_verif.push(new Array("select",tab[i],'#000000'));
        }
    }
}

//set list of the checkbox
VerifyForm.prototype.setVerifyCheckbox = function(tab, min, max, div_error, error){
    this.tab_verif.push(new Array("checkbox", tab, min, max, div_error, error));
}

//set list of the radio
VerifyForm.prototype.setVerifyRadio = function(tab, div_error, error){
    for(var i=0;i<tab.length;i++){
        this.tab_verif.push(new Array("radio", tab[i], div_error, error));
    }
}


/* METHOD */
//////////////////////////////////////////////////////////////////////
VerifyForm.prototype.setMessage = function(message){
    this.message = message;
}


/* VERIFY */
//////////////////////////////////////////////////////////////////////
//verify the form
VerifyForm.prototype.verify = function(){
    //init
    var result = true;
    
    for(var i=0;i<this.tab_verif.length;i++){
        switch (this.tab_verif[i][0]){
        
            case "select":
            case "textarea":
            case "text":
                var input = document.getElementsByName(this.tab_verif[i][1]).item(0);
                //delete space
                var value_string = removeSpaces(input.value);
                if(value_string == ""){
                    input.style.borderColor = "#ff0000";
                    input.value = "";
                    result = false;
                }else{
                    input.style.borderColor = this.tab_verif[i][2];
                }
                break;
                
            case "radio":
                var result_radio = false;
                var tab_radio = document.getElementsByName(this.tab_verif[i][1]);
                for(var j=0;j<tab_radio.length;j++){
                    if(tab_radio.item(j).checked){
                        result_radio = true;
                    }
                }
                if(!result_radio){
                    result = false;
                }
                break;
                
            case "checkbox":
                var result_checkbox = 0;
                var tab_checkbox = this.tab_verif[i][1];
                for(var j=0;j<tab_checkbox.length;j++){
                    if(document.getElementsByName(tab_checkbox[j]).item(0).checked){
                        result_checkbox++;
                    }
                }
                if(this.tab_verif[i][2] > result_checkbox || this.tab_verif[i][3] < result_checkbox) result = false;
                break;
        }
    }
    
    //message
    if(!result) alert(this.message);
    //result
    return result;
}


/* FONCTIONS */
//remove the space of a string
function removeSpaces(string) {
	var result = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++) result += splitstring[i];
	return result;
}

/* */