// JavaScript Document

function checkForm1() {

    missinginfo = "";

    for (i = 0; i < document.getElementsByName('ans_skin_type').length; i++) {
        if (document.getElementsByName('ans_skin_type')[i].checked) {
            var ans_skin_type = true;
        }
    }
    if (ans_skin_type) {

    } else {
        missinginfo += "\n- Δεν έχετε επιλέξει τύπο επιδερμίδας";
    }

    for (i = 0; i < document.getElementsByName('ans_skin_look').length; i++) {
        if (document.getElementsByName('ans_skin_look')[i].checked) {
            var ans_skin_look = true;
        }
    }
    if (ans_skin_look) {

    } else {
        missinginfo += "\n- Δεν έχετε επιλέξει όψη επιδερμίδας";
    }

    if (!document.getElementById('ans_c1c').checked
								  && !document.getElementById('ans_c2c').checked
								  && !document.getElementById('ans_c3c').checked
								  && !document.getElementById('ans_c4c').checked
                                  && !document.getElementById('ans_c5c').checked
        ) {
        missinginfo += "\n- Δεν έχετε συμπληρώσει τι παρουσιάζει η επιδερμίδα σας";
    }


    for (i = 0; i < document.getElementsByName('ans_skin_tone').length; i++) {
        if (document.getElementsByName('ans_skin_tone')[i].checked) {
            var ans_skin_tone = true;
        }
    }
    if (ans_skin_tone) {

    } else {
        missinginfo += "\n- Δεν έχετε συμπληρώσει απόχρωση επιδερμίδας";
    }

    if (missinginfo != "") {
        missinginfo = "_____________________________________________________\n" +
         "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
          missinginfo;

        alert(missinginfo);
        return false;
    } else {
        return true;
    }
}


function checkForm2() {

    missinginfo = "";

    for (i = 0; i < document.getElementsByName('ans_cover').length; i++) {
        if (document.getElementsByName('ans_cover')[i].checked) {
            var ans_cover = true;
        }
    }
    if (ans_cover) {

    } else {
        missinginfo += "\n- Δεν έχετε επιλέξει τύπο επιδερμίδας";
    }

    if (
        !document.getElementById('ans_f1c').checked
            && !document.getElementById('ans_f2c').checked
            && !document.getElementById('ans_f3c').checked
            && !document.getElementById('ans_f4c').checked
        ) {
        missinginfo += "\n- Δεν έχετε συμπληρώσει τι αποτέλεσμα ψάχνετε ";
    }


    if (missinginfo != "") {
        missinginfo = "_____________________________________________________\n" +
         "Δεν έχετε συμπληρώσει όλα τα απαραίτητα στοιχεία:\n" +
          missinginfo;

        alert(missinginfo);
        return false;
    } else {
        return true;
    }
}


function checkForm3() {

    missinginfo = "";

    for (i = 0; i < document.getElementsByName('used_vichy').length; i++) {
        if (document.getElementsByName('used_vichy')[i].checked) {
            var used_vichy = true;
        }
    }
    
    if (used_vichy) {
        if (document.getElementById('ans_f1c').checked == true) {
           for (i = 0; i < document.getElementsByName('used_vichy_yes').length; i++) {
                if (document.getElementsByName('used_vichy_yes')[i].checked) {
                    var used_vichy_yes = true;
                }
            }
            if (!used_vichy_yes) {
                missinginfo += "\n- Δεν έχετε επιλέξει ποιο makeup χρησιμοποιείτε.";
            }
        } else {
            for (i = 0; i < document.getElementsByName('used_vichy_no').length; i++) {
                if (document.getElementsByName('used_vichy_no')[i].checked) {
                    var used_vichy_no = true;
                }
            }
            if (!used_vichy_no) {
                missinginfo += "\n- Δεν έχετε επιλέξει ποια μάρκα makeup χρησιμοποιείτε.";
            }
        }
    } else {
        missinginfo += "\n- Δεν έχετε επιλέξει αν έχετε χρησιμοποιήσει make-up της Vichy";
    }
    
    for (i = 0; i < document.getElementsByName('buy_makeup').length; i++) {
        if (document.getElementsByName('buy_makeup')[i].checked) {
            var buy_makeup = true;
        }
    }
    if (buy_makeup) {

    } else {
        missinginfo += "\n- Δεν έχετε επιλέξει από πού αγοράζετε συνήθως make-up.";
    }



    if (!document.getElementById('ans_i1c').checked
    	  && !document.getElementById('ans_i2c').checked
								  && !document.getElementById('ans_i3c').checked
								  && !document.getElementById('ans_i4c').checked
								  && !document.getElementById('ans_i5c').checked
								  && !document.getElementById('ans_i6c').checked
								  && !document.getElementById('ans_i7c').checked
        ) {
        missinginfo += "\n- Δεν έχετε συμπληρώσει τι σας επηρεάζει στην επιλογή make-up.";
    }

    for (i = 0; i < document.getElementsByName('use_other_vichy').length; i++) {
        if (document.getElementsByName('use_other_vichy')[i].checked) {
            var use_other_vichy = true;
        }
    }
    if (!use_other_vichy) {
        missinginfo += "\n- Δεν έχετε επιλέξει αν χρησιμοποιείτε άλλα προϊόντα Vichy.";
    }

    if (document.getElementsByName('face_clean_brand').value == "") {
        missinginfo += "\n- Δεν έχετε συμπληρώσει Όνομα";
    }

    for (i = 0; i < document.getElementsByName('satisfaction_texture').length; i++) {
        if (document.getElementsByName('satisfaction_texture')[i].checked) {
            var satisfaction_texture = true;
        }
    }
    if (!satisfaction_texture) {
        missinginfo += "\n- Δεν έχετε επιλέξει πόσο ικανοποιημένη είστε από την υφή.";
    }


    for (i = 0; i < document.getElementsByName('satisfaction_perfume').length; i++) {
        if (document.getElementsByName('satisfaction_perfume')[i].checked) {
            var satisfaction_perfume = true;
        }
    }
    if (!satisfaction_perfume) {
        missinginfo += "\n- Δεν έχετε επιλέξει πόσο ικανοποιημένη είστε από το άρωμα.";
    }

    for (i = 0; i < document.getElementsByName('satisfaction_result').length; i++) {
        if (document.getElementsByName('satisfaction_result')[i].checked) {
            var satisfaction_result = true;
        }
    }
    if (!satisfaction_result) {
        missinginfo += "\n- Δεν έχετε επιλέξει πόσο ικανοποιημένη είστε από το αποτέλεσμα.";
    }

    if (missinginfo != "") {
        missinginfo = "_____________________________________________________\n" +
         "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
          missinginfo;

        alert(missinginfo);
        return false;
    } else {
        return true;
    }
    
}

function checkForm4() {

    missinginfo = "";


    if (document.getElementById('gender').options[document.getElementById('gender').selectedIndex].value == "0") {
        missinginfo += "\n- Δεν έχετε επιλέξει τίτλο";
    }
    if (document.getElementById('fname').value == "") {
        missinginfo += "\n- Δεν έχετε συμπληρώσει Όνομα";
    } else {
        if (!valid_name(document.getElementById('fname').value)) {
            missinginfo += "\n- Το Όνομά που έχετε συμπληρώσει δεν είναι αποδεκτό";
        }
    }
    if (document.getElementById('sname').value == "") {
        missinginfo += "\n- Δεν έχετε συμπληρώσει Επώνυμο";
    } else {
        if (!valid_name(document.getElementById('sname').value)) {
            missinginfo += "\n- Το Επίθετο που έχετε συμπληρώσει δεν είναι αποδεκτό";
        }
    }
    if ((document.getElementById('bday_dd').options[document.getElementById('bday_dd').selectedIndex].value == 0) || (document.getElementById('bday_mm').options[document.getElementById('bday_mm').selectedIndex].value == 0) || (document.getElementById('bday_yyyy').options[document.getElementById('bday_yyyy').selectedIndex].value == 0)) {
        missinginfo += "\n- Δεν έχετε συμπληρώσει ημερομηνία γεννήσεως";
    }
    if (document.getElementById('email').value == "") {
        missinginfo += "\n- Δεν έχετε συμπληρώσει Email";
    } else {
        if (!isEmail(document.getElementById('email').value)) {
            missinginfo += "\n- Το Email που έχετε συμπληρώσει δεν είναι αποδεκτό";
        }
    }
    if ((!valid_tel(document.getElementById('phone').value)) || (!(len(document.getElementById('phone').value) > 9))) {
        missinginfo += "\n- Το τηλέφωνο που έχετε συμπληρώσει δεν είναι αποδεκτό";
    }
    if (document.getElementById('optin').checked == true) {

        if (document.getElementById('address').value == "") {
            missinginfo += "\n- Δεν έχετε συμπληρώσει Διεύθυνση";
        } else {
            if (!valid_addressGR(document.getElementById('address').value)) {
                missinginfo += "\n- H Διεύθυνση που έχετε συμπληρώσει δεν είναι αποδεκτή";
            }
        }
        if (document.getElementById('postcode').value == "") {
            missinginfo += "\n- Δεν έχετε συμπληρώσει ΤΚ";
        }

        if (!valid_postcodeGR(document.getElementById('postcode').value)) {
            missinginfo += "\n- Ο ΤΚ που έχετε συμπληρώσει δεν είναι αποδεκτός.";
        }
    }

    if (document.getElementById('accept').checked == false) {
        missinginfo += "\n- Δεν έχετε αποδεχτεί τους όρους του διαγωνισμού";
    }


    if (missinginfo != "") {
        missinginfo = "_____________________________________________________\n" +
                "Δεν έχετε συμπληρώσει σωστά όλα τα απαραίτητα στοιχεία:\n" +
                missinginfo;

        alert(missinginfo);
        return false;
    } else {
        return true;
    }
}


function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function DaysArray(n) {
    for (var i = 1; i <= n; i++) {
        this[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { this[i] = 30 }
        if (i == 2) { this[i] = 29 }
    }
    return this
}

function isDate(dtStr) {
    var britedate = ""
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strMonth = dtStr.substring(pos1 + 1, pos2)
    var strDay = dtStr.substring(0, pos1)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear

    britedate = strYr + "/" + strMonth + "/" + strDay

    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        //        alert("The date format should be : dd/mm/yyyy")
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        //        alert("Please enter a valid month")
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        //        alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        //        alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isNumber(stripCharsInBag(dtStr, dtCh)) == false) {
        //        alert("Please enter a valid date")
        return false
    }

    // document.Register.i_birthdate.value = britedate
    return true
}

function isEmail(str) {
    var supported = 0;
    if (window.RegExp) {
        var tempStr = "a";
        var tempReg = new RegExp(tempStr);
        if (tempReg.test(tempStr)) supported = 1;
    }
    if (!supported)
        return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
    var r2 = new RegExp("^[a-zA-Z0-9\\-\\.\\_]+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
    return (!r1.test(str) && r2.test(str));
}

function valid_chars(valid, str) {
    var temp;
    for (var i = 0; i < str.length; i++) {
        temp = "" + str.substring(i, i + 1);
        flag = valid.indexOf(temp) + 1;
        if (!flag) return false;
    }
    return true;
}

function valid_name(str) {
    return valid_chars(" -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZαβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέίύόήώϊϋΐΰ’ΈΊΎΏΉΈ", str);
}
function valid_addressGR(str) {
    return valid_chars(", -1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZαβγδεζηθικλμνξοπρστυφχψωςΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩάέίύόήώϊϋΐΰ’ΈΊΎΏΉΈ ", str);
}

function valid_tel(str) {
    return valid_chars("1234567890-+", str);
}

function valid_postcodeGR(str) {
    return valid_chars("1234567890", str);
}

function valid_postcodeINT(str) {
    return valid_chars("1234567890abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ", str);
}

function isNumber(str) {
    return valid_chars("1234567890", str);
}

function len(str) {
    return str.length;
}
function isUnique(str) {
    if (valid_chars("1234567890abcdefghijklmopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", str)) {
        var re = /^[a-zA-Z]{2}[0-9]{7}$/;
        if (str.match(re)) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}



