﻿/******************** VALIDATION FUNCTIONS ********************/
function validateContactForm() {
	if (trim(document.form.name.value)=="") {
		document.form.name.focus();
 	 	alert("נא למלא את השם");
   		return false;
	}

	if (trim(document.form.phone.value)=="" && trim(document.form.email.value)=="") {
		document.form.phone.focus();
 	 	alert("נא למלא טלפון ו/או כתובת דוא\"ל");
   		return false;
	}
	if (trim(document.form.phone.value)!="" && (isNaN(trim(document.form.phone.value)) || trim(document.form.phone.value).length<6)) {
		document.form.phone.focus();
 	 	alert("המספר שהוזן אינו תקין");
   		return false;
	}
	if (trim(document.form.email.value)!="" && !checkEmail(trim(document.form.email.value))) {
		document.form.email.focus();
 	 	alert("כתובת הדוא\"ל שהוזנה אינה תקינה");
   		return false;
	}

	if (trim(document.form.subject.value)=="") {
		document.form.subject.focus();
 	 	alert("נא למלא את נושא הפניה");
   		return false;
	}

	if (trim(document.form.content.value)=="") {
		document.form.content.focus();
 	 	alert("נא למלא את התוכן");
   		return false;
	}

	return true;
}

function validateLoginBox() {
	if (trim(document.form.email.value)=="") {
		document.form.email.focus();
 	 	alert("נא למלא כתובת דוא\"ל");
   		return false;
	}
	if (!checkEmail(trim(document.form.email.value))) {
		document.form.email.focus();
 	 	alert("כתובת הדוא\"ל שהוזנה אינה תקינה");
   		return false;
	}

	if (trim(document.form.password.value)=="") {
		document.form.password.focus();
 	 	alert("נא למלא סיסמה");
   		return false;
	}

	return true;
}

function validateOrderForm() {
	if (trim(document.form.invoiceName.value)=="") {
		document.form.invoiceName.focus();
 	 	alert("נא למלא את השם");
   		return false;
	}

	if (trim(document.form.invoiceAddress.value)=="") {
		document.form.invoiceAddress.focus();
 	 	alert("נא למלא כתובת");
   		return false;
	}

	if (trim(document.form.invoicePhone.value)=="") {
		document.form.invoicePhone.focus();
 	 	alert("נא למלא טלפון");
   		return false;
	}
	if (isNaN(trim(document.form.invoicePhone.value)) || trim(document.form.invoicePhone.value).length<6) {
		document.form.invoicePhone.focus();
 	 	alert("המספר שהוזן אינו תקין");
   		return false;
	}

	return true;
}

function validateRegistrationForm() {
	if (trim(document.form.fname.value)=="") {
		document.form.fname.focus();
 	 	alert("נא למלא שם פרטי");
   		return false;
	}

	if (trim(document.form.lname.value)=="") {
		document.form.lname.focus();
 	 	alert("נא למלא שם משפחה");
   		return false;
	}

	if (trim(document.form.phone.value)=="") {
		document.form.phone.focus();
 	 	alert("נא למלא טלפון");
   		return false;
	}
	if (isNaN(trim(document.form.phone.value)) || trim(document.form.phone.value).length<6) {
		document.form.phone.focus();
 	 	alert("המספר שהוזן אינו תקין");
   		return false;
	}

	if (trim(document.form.email.value)=="") {
		document.form.email.focus();
 	 	alert("נא למלא כתובת דוא\"ל");
   		return false;
	}
	if (!checkEmail(trim(document.form.email.value))) {
		document.form.email.focus();
 	 	alert("כתובת הדוא\"ל שהוזנה אינה תקינה");
   		return false;
	}

	if (trim(document.form.password.value)=="") {
		document.form.password.focus();
 	 	alert("נא למלא סיסמה");
   		return false;
	}
	var regex=/^[0-9A-Za-z]+$/;
	if (!regex.test(trim(document.form.password.value))) {
 		document.form.password.focus();
 	 	alert("הסיסמה יכולה להכיל רק אותיות באנגלית ומספרים");
   		return false;
	}

	if (trim(document.form.password2.value)=="") {
		document.form.password2.focus();
 	 	alert("נא למלא סיסמה בשנית");
   		return false;
	}
	if (trim(document.form.password2.value)!=trim(document.form.password.value)) {
		document.form.password2.focus();
 	 	alert("הסיסמאות אינן זהות");
   		return false;
	}

	if (!document.form.agreement.checked) {
		document.form.agreement.focus();
 	 	alert("יש לקרוא את התקנון ולאשר את הסכמתך עם הכתוב בו");
   		return false;
	}

	return true;
}
function checkEmailUpdate() {
	if (trim(document.form.email.value)!=document.form.oldEmail.value) {
		return confirm(".בחרת לעדכן את כתובת הדוא\"ל שלך\n"+
					   "פעולה זו תחייב אותך לאמת את הכתובת החדשה באמצעות מייל שישלח אליה.\n"+
					   "חשבונך לא יהיה פעיל עד לאימות הכתובת החדשה.\n\n"+
					   "האם ברצונך להמשיך?");
	}

	return true;
}

function validateSendSMS() {
	if (document.form.SMSBank.value==0) {
 	 	if (confirm(".עליך להזמין הודעות על מנת לבצע משלוח\nOK כדי לעבור לדף ההזמנה, לחץ/י"))
			location.href = "order.asp";
		return false;
	}

	if (trim(document.form.sender.value)=="") {
		document.form.sender.focus();
 	 	alert("לאור החוק החדש נגד דואר זבל, על מפרסמים לציין בתוכן ההודעה הנשלחת את שם העסק/חברה");
   		return false;
	}

	if (trim(document.form.message.value)=="") {
		document.form.message.focus();
 	 	alert("לא ניתן לשלוח הודעה ריקה");
   		return false;
	}

	if (document.form.recipientsCount.value==0) {
		document.form.recipientsCount.focus();
 	 	alert("רשימת הנמענים אינה מכילה מספרים תקינים");
   		return false;
	}

	if (document.form.recipientsCount.value > document.form.SMSBank.value) {
		document.form.recipientsCount.focus();
 	 	if (confirm("יתרת ההודעות שלך לא מאפשרת משלוח ליותר מ-" + document.form.SMSBank.value + " נמענים.\nהאם ברצונך להזמין הודעות נוספות?"))
			location.href = "order.asp";
   		return false;
	}

	return true;
}

/******************** UTILITY FUNCTIONS ********************/
//Validates the given e-mail address
function checkEmail(emailstr) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (filter.test(emailstr))
		return true;
	else
		return false;
}

//Creates and returns an XMLHttpRequest object to work with AJAX
function getXmlHttpObject() {
	var xmlHttp = null;

	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			//Internet Explorer 6.0+
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			//Internet Explorer 5.0+
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

//Trims the given string from both sides
function trim(str) {
	//Ltrim
	while (str.charAt(0)==" ")
		str = str.slice(1);

	//Rtrim
	while (str.charAt(str.length-1)==" ")
		str = str.slice(0,-1);

	return str;
}