/* Trim Function */
function trim(value)
 {
  var temp = value;
  return temp.replace(/^\s*(\b.*\b|)\s*$/, "$1");
 }

/* validContactForm Function */
function validContactForm(form_id, div_id)
 {
  tmp="passForm=document.forms['"+form_id+"'];";
  eval(tmp);

  // Beginning of Regular Expressions Creating
  regular_digits=/^([0-9]{1})$/;
  regular_email_address=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  // End of Regular Expressions Creating

  // Beginning of Full Name Checking
  temp=trim(passForm.full_name.value);
  passForm.full_name.value=temp;
  if (temp=="")
     {
      alert("אנא רשמו שם.");
      passForm.full_name.focus();
      return false;
     }
  // End of Full Name Checking
  
   // Beginning of Phone Number Checking
   temp=trim(passForm.phone_number.value);
   passForm.phone_number.value=temp;

   if (temp.charAt(0)!="0")
	{
	 alert("הספרה הראשונה של מספר הטלפון מוכרחה להיות 0.");
	 passForm.phone_number.focus();
	 return false;
	}

   count_digits=0;
   for (i=0; i<=temp.length; i++)
	 {
	  if (regular_digits.test(temp.charAt(i)))
	     { count_digits++; }
	 }

   if (count_digits<9)
	{
	 alert("אנא רשמו את מספר הטלפון שלכם כולל קידומת ב-9 ספרות לפחות.");
	 passForm.phone_number.focus();
	 return false;
	}
   // End of Phone Number Checking

  // Beginning of E-mail Address Checking
  temp=trim(passForm.email_address.value);
  passForm.email_address.value=temp;
  if (temp!="")
     {
      if (!(regular_email_address.test(temp)))
         {
          alert("אנא רשמו כתובת E-mail תקינה.");
          passForm.email_address.focus();
          return false;
	  }
     }
  // End of E-mail Address Checking

  // Beginning of Message Content Checking
  temp=trim(passForm.message_content.value);
  passForm.message_content.value=temp;
  // End of Message Content Checking

  send_mail_procedure(form_id, div_id);
  return false;
 }

function send_mail_procedure(form_id, div_id)
  {
   tmp="passForm=document.forms['"+form_id+"'];";
   eval(tmp);

   full_name=passForm.full_name.value;
   phone_number=passForm.phone_number.value;
   email_address=passForm.email_address.value;
   message_content=passForm.message_content.value;

   new Ajax.Request('send_mail_procedure.php',
    {
     method: 'post',
     parameters: {full_name: full_name, phone_number: phone_number, email_address: email_address, message_content: message_content, limit: 999}
    });

   str ="document.getElementById('"+div_id+"').innerHTML='<div style=\"font-size: 13px\">פנייתך נשלחה בהצלחה.<br />נציגנו יצרו איתך קשר בתוך 24 שעות.<br />המשך גלישה נעימה.";
   if (div_id=="contact_form_popup") { str+="<p /><a href=\"javascript:parent.parent.GB_hide();\" class=\"greybox_links\">חזרה לאתר</a>"; }
   str+="</div>';";
   eval(str);
  }
