var emailLists = "";
var postLists = "";
var mandatoryEmail = new Array("email","firstName","lastName");
var mandatoryPost = new Array("email","firstName","lastName","address","zipCodeCity","country");

function OnClickCheckbox(checkbox, type){                        
    if (checkbox.checked){
        
        if (type == "email"){
            emailLists = addString(emailLists, checkbox.id);
        }
        else{
            postLists = addString(postLists, checkbox.id);
        }
    }
    else{
    
        if (type == "email"){
            emailLists = removeString(emailLists, checkbox.id);
        }
        else{
            postLists = removeString(postLists, checkbox.id);
        }
    }
    
    SetMandatoryFieldsAll();
}

function SetMandatoryFieldsAll(){
    HideAllMarkers(mandatoryPost);
    
    if (emailLists.length > 0){
        SetMandatoryFieldsSingle(mandatoryEmail);
    }
    
    if (postLists.length > 0){
        SetMandatoryFieldsSingle(mandatoryPost);
    }
}

function HideAllMarkers(fields){
    for (var i = 0; i < fields.length; i++){
        if (fields[i] == "zipCodeCity"){
            document.getElementById("zipCodeCityMarker").style.visibility = "hidden";
        }
        else{
            document.getElementById(fields[i] + "Marker").style.visibility = "hidden";
        }
    }
}

function SetMandatoryFieldsSingle(mandatoryFields){
    for (var i = 0; i < mandatoryFields.length; i++){
        if (mandatoryFields[i] == "zipCodeCity"){
            var textboxZipCode = document.getElementById("zipCode");
            var textboxCity = document.getElementById("city");
        
            if (textboxZipCode.value.length == 0 || textboxCity.value.length == 0){
                document.getElementById("zipCodeCityMarker").style.visibility = "visible";
            }    
        }
        else{
            var textbox = document.getElementById(mandatoryFields[i]);
            
            if (textbox.value.length == 0){
                document.getElementById(mandatoryFields[i] + "Marker").style.visibility = "visible";
            }
        }
    }
}

function validateOnBlur(textboxesIds, markerSpanId){
    var aTextboxesIds = textboxesIds.split("|");
    var validated = true;
    
    for (var i = 0; i < aTextboxesIds.length; i++){
        if (validated){
            if (document.getElementById(aTextboxesIds[i]).value.length == 0){
                validated = false;
            }
        }
    }
    
    markerSpan = document.getElementById(markerSpanId);
    if (validated){
        markerSpan.style.visibility = "hidden";
    }
    else{
        markerSpan.style.visibility = "visible";
    }
}

function validateOnSubmit(){
    var emailNotValid = document.getElementById("dic_emailNotValid").value;
    var nothingSelected = document.getElementById("dic_nothingSelected").value;
    var fillAll = document.getElementById("dic_fillAll").value;
         
    if (emailLists.length > 0 || postLists.length > 0){
        if (emailLists.length > 0){
            if (!CheckMandatoryFieldsSingle(mandatoryEmail)){
                replaceSpanText("validationText", fillAll);
                return false;
            }
        }
        
        if (postLists.length > 0){
            if (!CheckMandatoryFieldsSingle(mandatoryPost)){
                replaceSpanText("validationText", fillAll);
                return false;
            }
        }
        
        if (!validateEmail(document.getElementById("email").value)){
            replaceSpanText("validationText", emailNotValid);
            return false;
        }
    }
    else{
        replaceSpanText("validationText", nothingSelected);
	    return false;
    }
    
    return true;
}

function CheckMandatoryFieldsSingle(mandatoryFields){
    for (var i = 0; i < mandatoryFields.length; i++){
        if (mandatoryFields[i] == "zipCodeCity"){
            var textboxZipCode = document.getElementById("zipCode");
            var textboxCity = document.getElementById("city");
        
            if (textboxZipCode.value.length == 0 || textboxCity.value.length == 0){
                return false;
            }    
        }
        else{
            var textbox = document.getElementById(mandatoryFields[i]);
            
            if (textbox.value.length == 0){
                return false;
            }
        }
    }
    return true;    
}

function validateEmail(email){
    var isEmailValid = false;
    var posAt = email.indexOf("@",0);
    
    if (posAt > -1){
        charsBeforeAt = email.substring(0,posAt);
        
        if (charsBeforeAt.length > 0){
            var posDot = email.indexOf(".",posAt);

            if(posDot > -1){
                
                var charsBetweenAtAndDot = email.substring(posAt+1,posDot);
                if (charsBetweenAtAndDot.length > 0){
                    var charsAfterDot = email.substring(posDot+1);
                    
                    if (charsAfterDot.length > 0){
                        isEmailValid = true;
                    }
                }
            }   
        }        
    }
    return isEmailValid;
}

function replaceSpanText(spanId, text){
    var span = document.getElementById(spanId);
    span.style.visibility = "visible";
    span.innerHTML = text;
}

function removeString(text, stringToRemove){
    text = text.replace(stringToRemove, "");
    text = text.replace(",,",",");
    
    if (text.substring(text.length-1) == ","){
        text = text.substring(0, text.length-1);
    }
    
    return text;
}

function addString(text, stringToAdd){
    if (text.length == 0){
        text = stringToAdd;
    }
    else{
        text += "," + text;
    }
    
    return text;
}