/*
 * Validate form functions assembly 
 */
 
// ! -- Properties --
var iNumCar = 6;
var bWarning = true;
var aWarning = {};

// ! -- ValidatePass function --
function ValidatePass(oForm, sWarnId) {
    var iLength = oForm.value.length;
    // If text field is empty, the function is returned
    if(iLength == 0){
        return;
    }
    
    if(iLength < iNumCar){
        ActivateWarning(oForm, sWarnId);
    } else {
        DeactivateWarning(sWarnId);
    }
} // End of ValidatePass function

// ! -- ValidateEmail function --
function ValidateEmail(oForm, sWarnId) {
/*
    i = oForm.value.indexOf("@");
    j = oForm.value.indexOf(".");
    k = oForm.value.indexOf(",");
    kk = oForm.value.indexOf("   ");
    jj = oForm.value.lastIndexOf(".") + 1;
    iLength = oForm.value.length;
    var bValid;
    
    // If text field is empty, the function is returned
    if(iLength == 0){
        return;
    }
*/
    // if((i > 0) && (j > 2) && (k == -1) && (iLength-jj >= 2) && (iLength-jj <= 3)) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(oForm.value)){
        bValid = true;
    } else {
        bValid = false;
    }
    if(!bValid) {
        ActivateWarning(oForm, sWarnId);
    } else {
        DeactivateWarning(sWarnId);
    }
    return  bValid;
} // End of ValidateEmail function

// ! -- ValidatePhone function --
function ValidatePhone(oForm, sWarnId){
    var bValid;
    if(oForm.value != "") {
        for(i = 0; i < oForm.value.length; i++){
            if(ValidateNumber(oForm.value.charAt(i)) == false){
                bValid = false;
                break;
            } else {
                bValid = true;
            }
        }
        if(!bValid){
            ActivateWarning(oForm, sWarnId);
        } else {
            DeactivateWarning(sWarnId);
        }
    }
} // End of ValidatePhone funtion

// ! -- ValidateNumber function --
function ValidateNumber(iNumber){
    if((iNumber >= '0' && iNumber <= '9') || iNumber == ',' || iNumber == '-' || iNumber == " ") {
        return true;
    } else {
        return false;
    }
} // End of ValidateNumber function

// ! -- ValidateEmpty function --
function ValidateEmpty(oForm, sWarnId) {
    if(oForm.value.length == 0){   
        ActivateWarning(oForm, sWarnId);
        return false;
    } else {
        DeactivateWarning(sWarnId);
        return true;
    }
} // End of ValidateEmpty function

// ! -- CompareInputs function --
function CompareInputs(oForm1, oForm2, sWarnId) {
    if(oForm1.value != oForm2.value){
        ActivateWarning(oForm2, sWarnId);
    } else {
        DeactivateWarning(sWarnId);
    }
} // End of CompareInputs funcion

// ! -- ValidateOnSubmit function --
function ValidateOnSubmit(sAgreeId) {   
    var oCheckBox = document.getElementById(sAgreeId);
    if(!sAgreeId || oCheckBox.checked) {         
        return ValidateWarnings();
    } else {
        return false;
    }
} // End of ValidateOnSubmit function

function ValidateWarnings(){
    var sum=0;    
    for (i in aWarning){      
        sum+=aWarning[i]; 
    }
    if (sum>0) {
        bWarning=true;
    }
    else{
        bWarning=false;                 
    }
    if(bWarning){
        alert("Existen campos erróneos");
        return false;
    } else {
        return true;
    }
}

function CheckEmptyFields() {
    oInputName = document.getElementById("IName");
    oInputLName = document.getElementById("ILastName");
    oInputEmail = document.getElementById("IEmail");
    oInputRepEmail = document.getElementById("RepEmail");
    oInputPassword = document.getElementById("IPassword");
    oInputRepPassword = document.getElementById("RepPassword");
    oInputYearBirth = document.getElementById("IYearBirth");
    oInputCountry = document.getElementById("ICountry");
    oInputProvince = document.getElementById("IProvince");
    oInputLocality = document.getElementById("ILocality");
    oInputNacionality = document.getElementById("INacionality");
    oInputPhone = document.getElementById("IPhone");
    oInputSubmit = document.getElementById("create-account");
    if((oInputName.value != "") && (oInputLName.value != "") && (oInputEmail.value != "")&& (oInputRepEmail.value != "") && (oInputPassword.value != "")&& (oInputRepPassword.value != "")&& (oInputYearBirth.value != "")&& (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputNacionality.value != "")&& (oInputPhone.value != "")) {
        var checkboxes = document.getElementsByName("IActivityId[]");
        var cont = 0;
        for (var x=0; x < checkboxes.length; x++) {
            if (checkboxes[x].checked) {
                cont = cont + 1;
            }
        }
        if(cont>0){
            if(cont>15){
                alert("Como máximo puede inscribirse en 15 secciones.");
                document.getElementById("agree2").checked = false; 
                oInputSubmit.disabled = true;
            }
            else{
                oInputSubmit.disabled = false;
            }
        }
        else{
            alert("Es obligatorio anunciarse al menos en una sección.");
            document.getElementById("agree2").checked = false;
            oInputSubmit.disabled = true;
        }
    } else {
        alert("Hay campos obligatorios sin cubrir.");
        document.getElementById("agree2").checked = false;
        oInputSubmit.disabled = true;
    }
} // End of CheckEmptyFields function



function CheckEmptyFieldsInd() {
    oInputName = document.getElementById("UName");
    oInputLName = document.getElementById("ULastName");
    oInputEmail = document.getElementById("UEmail");
    oInputRepEmail = document.getElementById("RepEmail");
    oInputPassword = document.getElementById("UPassword");
    oInputRepPassword = document.getElementById("RepPassword");
    oInputYearBirth = document.getElementById("UYearBirth");
    oInputCountry = document.getElementById("UCountry");
    oInputProvince = document.getElementById("UProvince");
    oInputLocality = document.getElementById("ULocality");
    oInputNacionality = document.getElementById("UNacionality");
    oInputPhone = document.getElementById("UPhone");
   // oInputSubmit = document.getElementById("save-changes");
    // if((oInputName.value != "") && (oInputLName.value != "") && (oInputEmail.value != "")&& (oInputRepEmail.value != "") && (oInputPassword.value != "")&& (oInputRepPassword.value != "")&& (oInputYearBirth.value != "")&& (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputNacionality.value != "")&& (oInputPhone.value != "")) {
    if((oInputName.value != "") && (oInputLName.value != "") && (oInputEmail.value != "")&& (oInputRepEmail.value != "") && (oInputYearBirth.value != "")&& (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputNacionality.value != "")&& (oInputPhone.value != "")) {
        var checkboxes = document.getElementsByName("UActivityId[]");
        var cont = 0;
        for (var x=0; x < checkboxes.length; x++) {
            if (checkboxes[x].checked) {
                cont = cont + 1;
            }
        }
        if(cont>0){
            if(cont>15){
                alert("Como máximo puede inscribirse en 15 secciones."); 
                return false;
                //oInputSubmit.disabled = true;
            }
            else{
                //oInputSubmit.disabled = false;
            }
        }
        else{
            alert("Es obligatorio anunciarse al menos en una sección.");
        return false;
           // oInputSubmit.disabled = true;
        }
    } else {
        alert("Hay campos obligatorios sin cubrir.");
        return false;
        //oInputSubmit.disabled = true;
    }
    return true;
} // End of CheckEmptyFields function


// company
function CheckEmptyFields2() {
    oInputName = document.getElementById("IName");
    oInputCountry = document.getElementById("ICountry");
    oInputProvince = document.getElementById("IProvince");
    oInputLocality = document.getElementById("ILocality");
    oInputAddress = document.getElementById("IAddress");
    oInputPostalCode = document.getElementById("IPostalCode");
    oInputPhone = document.getElementById("IPhone");
    oInputContName = document.getElementById("IContName");
    oInputContLastName = document.getElementById("IContLastName");
    oInputEmail = document.getElementById("IContEmail");
    oInputRepEmail = document.getElementById("RepEmail");
    oInputPassword = document.getElementById("IContPassword");
    oInputRepPassword = document.getElementById("RepPassword");
    oInputSubmit = document.getElementById("create-account");
    // if((oInputName.value != "") && (oInputEmail.value != "") && (oInputPassword.value != "")&& (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputAddress.value != "")&& (oInputPostalCode.value != "")&& (oInputPhone.value != "")&& (oInputContName.value != "")&& (oInputContLastName.value != "")&& (oInputRepEmail.value != "")&& (oInputRepPassword.value != "")) {
    if((oInputName.value != "") && (oInputEmail.value != "") && (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputAddress.value != "")&& (oInputPostalCode.value != "")&& (oInputPhone.value != "")&& (oInputContName.value != "")&& (oInputContLastName.value != "")&& (oInputRepEmail.value != "")) {
        var checkboxes = document.getElementsByName("IActivityId[]");
        var cont = 0;
        for (var x=0; x < checkboxes.length; x++) {
            if (checkboxes[x].checked) {
                cont = cont + 1;
            }
        }
        if(cont>0){
            oInputSubmit.disabled = false;
        }
        else{
            alert("Es obligatorio anunciarse al menos en una sección.");   
            document.getElementById("agree2").checked = false;
            oInputSubmit.disabled = true;
        }
    } else {
        alert("Hay campos obligatorios sin cubrir."); 
        document.getElementById("agree2").checked = false;
    }
} // End of CheckEmptyFields2 function


function CheckEmptyFieldsCom() {
    oInputName = document.getElementById("UName");
    oInputCountry = document.getElementById("UCountry");
    oInputProvince = document.getElementById("UProvince");
    oInputLocality = document.getElementById("ULocality");
    oInputAddress = document.getElementById("UAddress");
    oInputPostalCode = document.getElementById("UPostalCode");
    oInputPhone = document.getElementById("UPhone");
    oInputContName = document.getElementById("UContName");
    oInputContLastName = document.getElementById("UContLastName");
    oInputEmail = document.getElementById("UContEmail");
    oInputRepEmail = document.getElementById("RepEmail");
    oInputPassword = document.getElementById("UContPassword");
    oInputRepPassword = document.getElementById("RepPassword");
    //oInputSubmit = document.getElementById("create-account");
    // if((oInputName.value != "") && (oInputEmail.value != "") && (oInputPassword.value != "")&& (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputAddress.value != "")&& (oInputPostalCode.value != "")&& (oInputPhone.value != "")&& (oInputContName.value != "")&& (oInputContLastName.value != "")&& (oInputRepEmail.value != "")&& (oInputRepPassword.value != "")) {
    if((oInputName.value != "") && (oInputEmail.value != "") && (oInputCountry.value != "")&& (oInputProvince.value != "")&& (oInputLocality.value != "")&& (oInputAddress.value != "")&& (oInputPostalCode.value != "")&& (oInputPhone.value != "")&& (oInputContName.value != "")&& (oInputContLastName.value != "")&& (oInputRepEmail.value != "")) {
        var checkboxes = document.getElementsByName("IActivityId[]");
        var cont = 0;
        for (var x=0; x < checkboxes.length; x++) {
            if (checkboxes[x].checked) {
                cont = cont + 1;
            }
        }
        if(cont>0){
            oInputSubmit.disabled = false;
        }
        else{
            alert("Es obligatorio anunciarse al menos en una sección.");   
            //oInputSubmit.disabled = true;
            return false
        }
    } else {
        alert("Hay campos obligatorios sin cubrir.");
        return false
    }
    return true;
} // End of CheckEmptyFields2 function



// ! -- ActivateWarning function --
function ActivateWarning(oForm, sWarnId){   
    bWarning = true;
    oForm.style.display = "none";
    document.getElementById(sWarnId).style.display = "inline";
    aWarning[sWarnId]=1;
} // End of ActivateWarning function

// ! -- DeactivateWarning function --
function DeactivateWarning(sWarnId){
    bWarning = false;
    aWarning[sWarnId] = 0;
} // End of DeactivateWarning function

// ! -- ResetInput --
function ResetInput(oForm, sWarnId){
    oForm.style.display = "none";
    document.getElementById(sWarnId).style.display = "inline";
    document.getElementById(sWarnId).focus();
} // End of ResetInput function
