/* Marc Boronat                                                               */

var regExps = new Array();
regExps["text"] = new RegExp("(.)+");
regExps["number"] = new RegExp("^([0-9]+[- /.]*?)+$");
regExps["date"] = new RegExp("^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}$");
regExps["email"] = new RegExp("^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$");
regExps["compulsory"] = new RegExp("(.)+");
regExps["superficial"] = new RegExp("(.)*?");
regExps["antispam"] = 4;

/* ---------------------------------------------------- */

var contacto_nm = new Array();
contacto_nm[0] = "nombre";
contacto_nm[1] = "email";
contacto_nm[2] = "asunto";
contacto_nm[3] = "antispam";
contacto_nm[4] = "mensaje";

var contacto_rx = new Array();
contacto_rx[0] = "text";
contacto_rx[1] = "email";
contacto_rx[2] = "text";
contacto_rx[3] = "antispam";
contacto_rx[4] = "text";

var tienda_nm = new Array();
tienda_nm[0] = "cantidad";
tienda_nm[1] = "shipping";
tienda_nm[2] = "os1";

var tienda_rx = new Array();
tienda_rx[0] = "number";
tienda_rx[1] = "compulsory";
tienda_rx[2] = "compulsory";

function checkRegex(str, rxp) {
    if(str.search(rxp)==-1){
        return false;
    } else {
        return true;
    }
}

function validate(formName, formArr, regexArr) {

    var validated = true

    for(var i=0; i<formArr.length; i++) {
        var theLmnt = document.getElementById(formArr[i]);
        var theCheck = checkRegex(theLmnt.value, regExps[regexArr[i]]);
        if(theCheck == false) {
            validated = false;
            theLmnt.style.backgroundColor = "#B15653";
        } else {
            theLmnt.style.backgroundColor = "#84bdb6";
        }
    }

    if (validated == false) {
        alert("Please check the fields in red");
    } else if (validated == true) {
        document.forms[formName].submit();
    }

}