﻿// JavaScript Document

function validateFormOnSubmit(theForm) {
var reason = "";

  
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);
  reason += validateEmpty(theForm.name);
      
  if (reason != "") {
    alert("יש בעייה במספר שדות:\n" + reason);
    return false;
  }

  return true;
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "אנא הקלד/י שם בו ניתן לפנות אליך."
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if  (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "אנא הקלד/י כתובת דוא\"ל חוקית.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "ישנם תווים לא חוקיים בכתובת הדוא\"ל שהקלדת.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "אנא הקלד/י מספר טלפון.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "מספר הטלפון שהקלדת מכיל תווים לא חוקיים.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}

function mypopup(page)
 {
   mywindow = window.open (page, "mywindow","location=0,status=0,scrollbars=0, width=400,height=400");
 } 
