﻿// add regex to inputs 
// text and numbers no spaces
textAndNumWithSpace = "^[A-Zא-תa-z][\\s]?[A-Zא-תa-z,'.]";
onlyNumbers = "^[0-9]{1,9}-? ?[0-9]{1,9}$";
email = "^([0-9a-zA-Z]([-\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

function addRegexValidation() {
    $(".textAndNumWithSpace").attr("regex", textAndNumWithSpace);
    $(".onlyNumbers").attr("regex", onlyNumbers);
    $(".email").attr("regex", email);
}

$(document).ready(function() {
    // Allow only numbers function -- add the class InputNum to the field 
    $(".InputNum").keypress(function(e) {
        if (e.whice != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) return false;
    })
    $(".MainMenu").hover(function() {
        $(this).addClass("navSign");
    }, function() {
        $(this).removeClass("navSign");
    })
})
// validate form
function Validation() {
    var flag = true;
    $(".CheckField").each(function(i) {
        if (typeof $(this).attr("regex") != 'undefined') {
            var regex = new RegExp($(this).attr("regex"));
            if ($(this).attr("class").indexOf("required") != -1) {
                if (!(regex).test($(this).val())) {
                    $(this).addClass("RedBorder");
                    flag = false;
                }
                else $(this).removeClass("RedBorder");
            }
            else // IF IT NOT A REQUIRED FIELD
            {
                if ($(this).val().length > 0) {
                    if (!(regex).test($(this).val())) {
                        $(this).addClass("RedBorder");
                        flag = false;
                    }
                    else $(this).removeClass("RedBorder");
                }
            }
        }
    })
    return flag;
}
// contact us page
function contactUs() {
    if (Validation()) {
        $.post("../handlers/SendMail.ashx",
                                {
                                    fullName: $("#fullName").val(),
                                    phone: $("#phone").val(),
                                    email: $("#email").val(),
                                    comments: $("#comments").val()
                                },
              function(data) {
                  if (data == "Error") {
                      $("#contactHeading").html("ארעה תקלה, אנא נסה שוב")
                  }
                  else {
                      $("#contactFields").css("visibility", "hidden");
                      $("#contactHeading").html("תודה על יצירת הקשר, פנייתכם תענה בהקדם")
                  }
              });
    }

}
// wristbands order
function WristsOrder() {
    if (Validation()) {
        $.post("../handlers/order.ashx",
                                {
                                    eventDate: $("#eventDate").val(),
                                    numberOfPeople: $("#numberOfPeople").val(),
                                    wristType: $("#wristType").val(),
                                    wristAmount: $("#wristAmount").val(),
                                    wristColor: $("#wristColor").val(),
                                    color: $("#color").val(),
                                    dateToGet: $("#dateToGet").val(),
                                    fullName: $("#fullName").val(),
                                    phone: $("#phone").val(),
                                    email: $("#email").val(),
                                    comments: $("#comments").val()
                                },
              function(data) {
                  if (data == "Error") {
                      $("#contactHeading").html("ארעה תקלה, אנא נסה שוב")
                  }
                  else {
                      $("#contactFields").css("visibility", "hidden");
                      $("#contactHeading").html("תודה על יצירת הקשר, פנייתכם תענה בהקדם")
                  }
              });
    }
}