
function ValidateForm(formId)
{
//    מערך של כל האוביקטים שבטופס
//      OrderFormDetails
//      וריצה עליו
  var flag=true; 
  var el = document.getElementById(formId).elements
  for (var i = 0; i < el.length; i++)
  {               
//  מכניסים למשתנה את המחרוזת
//  spn
//  ואחריו את שם האוביקט מתוך המערך
     var mandatory = document.getElementById("spn"+el[i].name);
//  בדיקה האם קים אוביקט כזה
     if (mandatory != null)
//  אם קים אז בודקים את הערך של אוביקט במערך
       if (el[i].value == "" || parseInt(el[i].value, 10) == 0)  
       {
//  אם הוא ריק אז מציגים את
//  span
//  ומשנים את ערך הדגל שיש שגיעה
         mandatory.style.display = "";
         flag = false;
       }
       else         
//  אם הוא לא ריק אז מעלימים את
//  span
         mandatory.style.display = "none";              
    }   
//  מחזירים את הדגל האם היתה שגיעה או לא        
    return flag;
}

function ValidateControl(strControlName, strErrorMessage)
{
   var control = document.getElementById(strControlName)
   if (control.value == null || control.value.length == 0)
   {
     document.getElementById("error").innerHTML = strErrorMessage
     control.focus()
     return false
   }
   return true 
}

function ValidateHiddenControl(strControlName, strErrorMessage)
{
   var control = document.getElementById(strControlName)
   if (control.value == null || control.value.length == 0)
   {
     document.getElementById("error").innerHTML = strErrorMessage   
     return false
   }
   return true 
}

function ValidateListControl(strControlName, strErrorMessage)
{ 
   var control = document.getElementById(strControlName)
   if (control.selectedIndex == 0)
   {
     document.getElementById("error").innerHTML = strErrorMessage
     control.focus()
     return false
   }
   return true  
}

function ValidateSelectBoxControl(strControlName, strErrorMessage)
{ 
   var control = document.getElementById(strControlName)
   if (control.options.length == 0)
   {
     document.getElementById("error").innerHTML = strErrorMessage
     control.focus()
     return false
   }
   return true  
}

function ValidateDateControl(strControlName, strErrorMessage)
{
   if (ValidateControl(strControlName, strErrorMessage))   
     return checkDate(document.getElementById(strControlName))
}

function ValidatePhoneControl(strControlName, strErrorMessage)
{ 
  if (ValidateControl(strControlName, strErrorMessage))   
     return checkPhone(document.getElementById(strControlName))
}

function ValidateCellphoneControl(strControlName, strErrorMessage)
{
  if (ValidateControl(strControlName, strErrorMessage))   
     return checkCellular(document.getElementById(strControlName))
}

function ValidateEmailControl(strControlName, strErrorMessage)
{
  if (ValidateControl(strControlName, strErrorMessage))   
     return checkEmail(document.getElementById(strControlName))
}

//הפונקצייה אינה מחייבת להכניס תאריך לשדה
// אלא רק בודקת שהתאריך שהוכנס הוא תקין - אם הכניסו אותו
// על-מנת לחייב משתמש להכניס תאריך יש להשתמש בפונקצייה אחרת
function checkDate(input)
{
  var strDate = input.value
  
  if (strDate == "") 
      return true;

  
  //The dot is not allowed (not supported in db insert queries)
  badDotExp = /\./
  if (badDotExp.test(strDate)) 
     return invalidDate(input);
 
  // Non-Leap year Month days..
  var monthDays =  [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  // Leap year Month days..
  var leapMonthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];  
      
  var year = parseInt(strDate.substring(6), 10);
  var month = parseInt(strDate.substring(3,5), 10);
  var day = parseInt(strDate.substring(0, 2), 10);
  
  if (isNaN(year)) year = 0; 
  //alert(strDate.substring(0, 2))
 // alert("year="+year+";month="+month+";day="+day)
  //Check for leap year ..
  //Years evenly divisible by four are normally leap years, except for...
  //Years also evenly divisible by 100 are not leap years, except for...
  //Years also evenly divisible by 400 are leap years.
   
  if (day < 1 || month > 12 || month < 1) 
     return invalidDate(input);
  
  if ((year % 4) == 0) 
  {
    if ((year % 100) == 0 && (year % 400) != 0) //not leap year
      if (monthDays[month - 1] < day)
        return invalidDate(input);
      else
          return true      
    else
      if (leapMonthDays[month - 1] < day)      
        return invalidDate(input);
      else
          return true       
  } 
  else
     if (monthDays[month - 1] < day)      
       return invalidDate(input)
      else
          return true  
}

function invalidDate(input)
{
   alert("התאריך שהזנת הינו שגוי. יש לחזור על ההקלדה שנית")
   input.value = ""
   input.focus()   
   return false;
}

function checkEmail(input)
{
  if (input.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/))
    return true;
  alert("כתובת האימייל שהוזנה שגויה. יש להזין כתובת חוקית!"); 
  input.focus(); 
  return false;  
}

// בדיקה האם דואל  קיים
function checkEmailExist(strEmail)
{
    var xmlhttp = getXmlHttpObj();	   	
    var xmlDoc = getXmlDocObj();

    var sUrl = "RegisterChkEmail.asp?Email=" + strEmail;
    xmlDoc = sendXmlHttpRequest(xmlhttp,sUrl);
    var Contact = xmlDoc.getElementsByTagName("Contact");
    return Contact.item(0).text;
}

function checkIsraeliPhone(input)
{
  if (input.value.length == 10)
  {
    return true;
  }
  else
  {
    alert("מספר הטלפון שהוזן שגוי. יש להזין מספר בעל 9 ספרות!");  
    input.focus();   
    return false;  
  }
}

function checkIsraeliCellular(input)
{
  if (input.value.length == 11)
  {
    return true;
  }
  else
  {
    alert("מספר הטלפון הנייד שהוזן שגוי. יש להזין מספר בעל 10 ספרות!");
    input.focus();     
    return false;  
  }
}

function checkZip(input)
{
  if (input.value.length <= 5)
  {
    return true;
  }
  else
  {
    alert("המיקוד שהוזן שגוי. יש להזין מספר בעל 5 ספרות לכל היותר!");
   input.focus();   
   return false;  
  }
}
