﻿// add regex to inputs 
// text and numbers no spaces
textAndNumWithSpace = "^[A-Z1-9א-תa-z][\\s]?[A-Z1-9א-תa-z,'.]";
textAndNumNoSpace = "^[A-Z1-9&=-א-תa-z][\\S]*$";
onlyNumbers = "^\\d{0,9999}$";
emailRegex = "^([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);
    $(".textAndNumNoSpace").attr("regex", textAndNumNoSpace);
    $(".onlyNumbers").attr("regex", onlyNumbers);
    $(".emailRegex").attr("regex", emailRegex);
}


// send mail fron sidebar
function SendMail() {
    if (Validation()) {
        $.post("../handlers/SendMail.ashx",
                            {
                                fullName: $("#fullName").val(),
                                email: $("#email").val(),
                                cell: $("#kidomet").val() + $("#cellPhone").val()
                            },
          function(data) {
              $("#Fields").html("");
              $("#contactHeading").html("<img height='1' width='1' style='border-style:none;' alt='' src='http://www.googleadservices.com/pagead/conversion/998246587/?value=1&amp;label=ehdkCKWZ6gEQu5GA3AM&amp;guid=ON&amp;script=0'/>תודה על יצירת הקשר");
          });
    }
}

// send mail from contact page
function SendMailBig() {
    if (Validation()) {
        $.post("../handlers/SendMail.ashx",
                            {
                                fullName: $("#fullName").val()+" "+$("#lastName").val(),
                                email: $("#email").val(),
                                cell: $("#phone").val(),
                                company: $("#company").val(),
                                url: $("#URL").val(),
                                message: $("#message").val()
                            },
          function(data) {
              $("#Fields").html("");
              $("#contactHeading").html("<img height='1' width='1' style='border-style:none;' alt='' src='http://www.googleadservices.com/pagead/conversion/998246587/?value=1&amp;label=ehdkCKWZ6gEQu5GA3AM&amp;guid=ON&amp;script=0'/><h3>תודה על יצירת הקשר</h3>");
          });
    }
}

// Validate Form
function Validation() {
    addRegexValidation();
    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("ReqError");
                    flag = false;
                }
                else $(this).removeClass("ReqError");
            }
            else // IF IT NOT A REQUIRED FIELD
            {
                if ($(this).val().length > 0) {
                    if (!(regex).test($(this).val())) {
                        $(this).addClass("ReqError");
                        flag = false;
                    }
                    else $(this).removeClass("ReqError");
                }
            }
        }
    })
    if (flag) {
        return true;
    }
}

$(document).ready(function() {
$(".InputNum").keypress(function(e) {
    if (e.whice != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) return false;
})
})
