﻿function shareOnFacebook(url,title) {
	window.open("http://www.facebook.com/sharer.php?u="+encodeURIComponent(url)+"&t="+encodeURIComponent(title), "sharer", "toolbar=0,status=0,width=626,height=436");
}
function shareOnTwitter(url,title) {
	window.open("http://twitter.com/home?status="+encodeURIComponent(url+' '+title+' '), "twit_sharer");
}

//Causes an element to appear/disappear while changing display type between block to none
function show_details(elmnt) {
	document.getElementById(elmnt).style.display = (document.getElementById(elmnt).style.display=="none" ? "block" : "none");
}
//Causes an element to appear/disappear while changing display type between inline to none
function show_details_inline(elmnt) {
	document.getElementById(elmnt).style.display = (document.getElementById(elmnt).style.display=="none" ? "inline" : "none");
}

//Opens a file in a new window. parameters: url - file name, w - window width, h - window height
function open_win(url, w, h, sbars, resize, toolbar) {
	if (sbars == null) sbars = 0;
	if (resize == null) resize = 1;
	if (toolbar == null) toolbar = 0;

	if (isIE() && sbars == 1) w += 17;

	window.open(url,"_blank","toolbar="+toolbar+",location=0,directories=0,status=0,menubar=0,scrollbars="+sbars+",resizable="+resize+",width="+w+",height="+h);
}

//Returns a string containing a copy of a specified string with no leading or trailing spaces.
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;
}

//Verifies the user's request to delete messages/objects.
function confirmDelete(formName) {
	if (somethingChecked(formName))
		return confirmDeleteObject();
	else
		return false;
}

//Verifies the user's request to delete an object from objectsTable.
function confirmDeleteObject(obj) {
	if (obj == "deal")
		return confirm("?האם ברצונך להסיר את החפץ מהמאגר");
	else
		return confirm("?האם ברצונך להסיר מהרשימה את הפריטים המסומנים");
}

//Checkes if at least one of the checkboxes is checked.
function somethingChecked(formName) {
	var element;

	for (var i=0; i<document.forms[formName].elements.length; i++) {
		element = document.forms[formName].elements[i];

		//Check if at least one item is checked
		if (element.type=="checkbox" && element.checked) return true;
	}

	alert("לא נבחרו פריטים");
	return false;
}

//Returns true if the user uses IE, or false otherwise.
function isIE() {
	return (navigator.appName.indexOf("Microsoft") != -1);
}

//Validates the e-mail address in the specified form's textbox.
function checkEmailAdress(formName) {
	return checkEmailTextBox(document.forms[formName].email);
}
//Validates the e-mail address in the specified textbox.
function checkEmailTextBox(txt, showAlert) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (!filter.test(trim(txt.value))) {
		if (showAlert != 'no') alert('כתובת הדוא"ל אינה חוקית');
		txt.focus();
		return false;
	}

	return true;
}
//בדוק שגיאות נפוצות
function checkEmailCommonErrors(item) {
	//בדוק שגיאות נפוצות
	var email = document.getElementById(item).value;
	var i = checkErrorList(email);
	var user;

	while (i > -1) {
		user = email.substring(0, email.indexOf("@") + 1);
		email = prompt('.יתכן שכתובת הדוא"ל שהוזנה שגויה\n.להלן מוצע תיקון לכתובת שהוזנה\n\n\n,כדי להשתמש בתיקון OK לחצו על\n.כדי לבטל את הפעולה Cancel או', user + email_corrections[i]);
	
		if (email == null) {
			return false;
		} else {
			document.getElementById(item).value = email;
			i = checkErrorList(email);
		}
	}
}

//Validates the given phone number with the given phone code.
function checkPhoneNumber(phone, phoneCode) {
	if (trim(phone.value) != "") {
		var phoneCodes = " 02 03 04 08 09 050 052 054 055 057 072 073 074 076 077 1700 1800 ";
		var reqLength = (trim(phoneCode.value).length == 4 ? 6 : 7);

		if (phoneCodes.indexOf(" "+phoneCode.value+" ") == -1) {
			phoneCode.focus();
		 	alert('נא לבחור קידומת');
			return false;
		}

	  	if (trim(phone.value).length < reqLength) {
			phone.focus();
		 	alert('חסרות ספרות למספר הטלפון');
		  	return false;
	 	}

		if (isNaN(trim(phone.value)) || trim(phone.value).length != reqLength) {
	 		phone.focus();
	 	 	alert('מספר הטלפון שהוזן אינו חוקי');
	   		return false;
		}
	}

	return true;
}

//Validates the specified date
function validateDate(day, month, year) {
	var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

	//If it's a leap year add a day to February
	if (year%400==0 || (year%4==0 && year%100!=0)) monthDays[1]++;

	return (day <= monthDays[month-1]);
}

//פונקציה שמבצעת שליחת טופס (בשביל שיהיה אפשר לשים תמונה בכפתור של אינטרנט אקספלורר)
function submitThis(loc, formName, nameOfFile) {
	var check = false;

	switch (nameOfFile) {
	case "signUp":
		if (signUpOK()) check=true;
		break;

	case "signIn":
		if (signInOK(formName)) check=true;
		break;

	case "sendPassword":
	  	if (trim(document.forms[formName].email.value)=="") {
	 		document.forms[formName].email.focus();
	 	 	alert('נא למלא כתובת דוא"ל');
	   		break;
		}

		if (checkEmailAdress(formName)) check=true;
		break;

	case "userActivation":
		if (userActivationOK()) check=true;
		break;

	case "contactUs":
		if (contactUsOK()) check=true;

		break;

	case "send2Friend":
		if (send2FriendOK()) check=true;
		break;

	case "personalDetails":
		if (personalDetailsOK()) check=1;
		break;

	case "sendMessage":
		if (loc=='שלח' && trim(document.forms[formName].messegeTitle.value)=='' && trim(document.forms[formName].messegeBody.value)=='') {
	 		document.forms[formName].messegeTitle.focus();
	 	 	alert('לא ניתן לשלוח הודעה ריקה');
		}
		else
			check=true;
		break;

	case "shareWithFriend":
		if (trim(document.forms[formName].name.value)=='') {
	 		document.forms[formName].name.focus();
	 	 	alert('יש למלא את שם החבר/ה');
		}
		else if (trim(document.forms[formName].email.value)=='') {
	 		document.forms[formName].email.focus();
	 	 	alert('נא למלא כתובת דוא"ל');
		}
		else if (checkEmailTextBox(document.forms[formName].email)) {
			check=true;
		}
		break;

	case "signUpFinish":
		if (signUpFinishOK()) check=true;
		break;

	default:
		check=true;
	}

	if (check) {
		document.forms[formName].submitValue.value=loc;
		document.forms[formName].submit();
	}
	return false;
}
//פונקציה ששולחת טופס אם נלחץ ENTER
function submitMe(loc,formName,nameOfFile) {
	if (event.keyCode==13) {
		submitThis(loc,formName,nameOfFile);
		return false;
	}
}
//פונקציה שמבטלת אפשרות לחיצה על כפתור עכבר ימני באובייקט
function noRightButton(event,variant) {
	if(event.button==2) {
		event.returnValue = false;
		alert("נא לאשר את ה" + variant + " באופן ידני");
	}

	if (document.layers) {
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown=clickNS4;
	}
	else if (document.all && !document.getElementById) {
		document.onmousedown=clickIE4;
	}

	return true;
}
//פונקציה המבטלת את האפשרות לצירוף המקשים CTRL+V לגבי אובייקט מסוים
function noPaste(e,variant) {
	if (window.event) { // IE
		if ((e.ctrlKey == true && e.keyCode == 86) || // Ctrl+V
		   (e.shiftKey == true && e.keyCode == 45)) { // Shift+Insert
			alert("נא לאשר את " + variant + " באופן ידני");
			return false;
		}
	}
	else if (e.which) { // Netscape/Firefox/Opera
		if ((e.ctrlKey == true) ||				  // Ctrl only
		   (e.shiftKey == true && e.which == 45)) { // Shift+Insert
			alert("נא לאשר את " + variant + " באופן ידני");
			return false;
		}
	}

	return true;
}

//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;
}

//פונקציות לטיפול בעוגיות
function getCookie(c_name) {
	var nameEQ = c_name + "=";
	var c, ca = document.cookie.split(";");

	for(var i = 0; i < ca.length; i++) {
		c = ca[i];

		while (c.charAt(0)==" ") c = c.substring(1, c.length);

		if (c.indexOf(nameEQ) == 0)
			return c.substring(nameEQ.length, c.length).replace(/%2E/g, ".");
	}

	return null;
}

//Redirects the browser to a mailto link
function sendMail(user, domain) {
	location.href = "mailto:" + user + "@" + domain;
}

//Validation for contact us
function contactUsOK() {
  	if (trim(document.myform.fname.value)=='') {
		document.myform.fname.focus();
 	 	alert('נא למלא את השם');
   		return false;
	}

  	if (trim(document.myform.email.value)=='') {
 		document.myform.email.focus();
 	 	alert('נא למלא כתובת דוא"ל');
   		return false;
	}

	if (!checkEmailTextBox(document.myform.email)) return false;

  	if (trim(document.myform.subject.value)=='') {
 		document.myform.subject.focus();
 	 	alert('נא למלא את נושא הפניה');
   		return false;
	}

  	if (trim(document.myform.messegeBody.value)=='') {
 		document.myform.messegeBody.focus();
 	 	alert('נא למלא את תוכן ההודעה');
   		return false;
	}

	return true;
}

//Validation for sign up
function signUpOK() {
	if (document.myform.signUpCellular) {
		if (trim(document.myform.fullname.value)=='' || trim(document.myform.fullname.value).indexOf(' ')==-1) {
	 		document.myform.fullname.focus();
	 	 	alert('נא למלא שם מלא');
	   		return false;
		}

		var regex=/^[0-9-]+$/;
	  	if (trim(document.myform.phone.value)=='' || !regex.test(trim(document.myform.phone.value)) || trim(document.myform.phone.value).length<10) {
	 		document.myform.phone.focus();
	 	 	alert('נא למלא מספר טלפון תקין');
	   		return false;
		}

		if (trim(document.myform.Password.value)=='') {
	 		document.myform.Password.focus();
	 	 	alert('נא להזין סיסמה');
	   		return false;
		}
		regex=/^[0-9A-Za-z]+$/;
		if (!regex.test(document.myform.Password.value)) {
	 		document.myform.Password.focus();
	 	 	alert('הסיסמה יכולה להכיל רק אותיות באנגלית ומספרים');
	   		return false;
		}
		if (trim(document.myform.Password.value).length < 6) {
	 		document.myform.Password.focus();
	 	 	alert('הסיסמה צריכה להיות באורך 6 תווים לפחות');
	   		return false;
		}
	}
	else {
		if (trim(document.myform.fname.value)=='') {
	 		document.myform.fname.focus();
	 	 	alert('נא למלא את השם הפרטי');
	   		return false;
		}

		if (trim(document.myform.lname.value)=='') {
	 		document.myform.lname.focus();
	 	 	alert('נא למלא את שם המשפחה');
	   		return false;
		}

		if (!checkPhoneNumber(document.myform.phone1, document.myform.phoneCode1)) return false;

		if (trim(document.myform.city.value)=='') {
	 		document.myform.city.focus();
	 	 	alert('נא לבחור את ישוב/עיר המגורים');
	   		return false;
		}

		if (trim(document.myform.Email.value)=='') {
	 		document.myform.Email.focus();
	 	 	alert('נא למלא כתובת דוא"ל');
	   		return false;
		} else {
			//בדוק שהכתובת חוקית
			if (!checkEmailTextBox(document.myform.Email)) return false;

			//בדוק שגיאות נפוצות
			var email = document.myform.Email.value;
			var i = checkErrorList(email);
			var user;

			while (i > -1) {
				user = email.substring(0, email.indexOf("@") + 1);
				email = prompt('.יתכן שכתובת הדוא"ל שהוזנה שגויה\n.להלן מוצע תיקון לכתובת שהוזנה\n\n\n,כדי להשתמש בתיקון OK לחצו על\n.כדי לבטל את הפעולה Cancel או', user + email_corrections[i]);

				if (email == null) {
					return false;
				} else {
					document.myform.Email.value = email;
					document.myform.emailChk.value = email;
					i = checkErrorList(email);
				}
			}
		}

		if (trim(document.myform.emailChk.value)==''){
	 		document.myform.emailChk.focus();
	 	 	alert('נא לאשר את כתובת הדוא"ל');
	   		return false;
		}
		if (document.myform.emailChk.value!=document.myform.Email.value){
	 		document.myform.emailChk.focus();
	 	 	alert('כתובות הדוא"ל אינן זהות. ודאו שרשמתם את כתובתכם ללא שגיאות');
	   		return false;
		}

		if (trim(document.myform.Password.value)=='') {
	 		document.myform.Password.focus();
	 	 	alert('נא להזין סיסמה');
	   		return false;
		}
		var regex=/^[0-9A-Za-z]+$/;
		if (!regex.test(document.myform.Password.value)) {
	 		document.myform.Password.focus();
	 	 	alert('הסיסמה יכולה להכיל רק אותיות באנגלית ומספרים');
	   		return false;
		}
		if (trim(document.myform.Password.value).length < 6) {
	 		document.myform.Password.focus();
	 	 	alert('הסיסמה צריכה להיות באורך 6 תווים לפחות');
	   		return false;
		}
		/*if (trim(document.myform.passwordchk.value)=='') {
	 		document.myform.passwordchk.focus();
	 	 	alert('נא לאשר את הסיסמה');
	   		return false;
		}
		if (document.myform.passwordchk.value!=document.myform.Password.value){
	 		document.myform.passwordchk.focus();
	 	 	alert('הסיסמאות אינן זהות');
	   		return false;
		}*/

		if (!document.myform.agreement.checked) {
	 		document.myform.agreement.focus();
	 	 	alert('יש לקרוא את התקנון, ולאשר שקראת אותו ע"י סימון התיבה');
	   		return false;
		}
	}

	return true;
}

//Validation for add object
function addObjectOK() {
	var regex;

	//Validate the signup form if it exists
	if (document.myform.signedIn.value == 'False') {
	  	if (trim(document.myform.s_fname.value)=='') {
	 		document.myform.s_fname.focus();
	 	 	alert('נא למלא את השם הפרטי');
	   		return false;
		}

		if (trim(document.myform.s_lname.value)=='') {
	 		document.myform.s_lname.focus();
	 	 	alert('נא למלא את שם המשפחה');
	   		return false;
		}

		if (trim(document.myform.s_email.value)=='') {
	 		document.myform.s_email.focus();
	 	 	alert('נא למלא כתובת דוא"ל');
	   		return false;
		} else {
			//בדוק שהכתובת חוקית
			if (!checkEmailTextBox(document.myform.s_email)) return false;

			//בדוק שגיאות נפוצות
			var email = document.myform.s_email.value;
			var i = checkErrorList(email);
			var user;

			while (i > -1) {
				user = email.substring(0, email.indexOf("@") + 1);
				email = prompt('.יתכן שכתובת הדוא"ל שהוזנה שגויה\n.להלן מוצע תיקון לכתובת שהוזנה\n\n\n,כדי להשתמש בתיקון OK לחצו על\n.כדי לבטל את הפעולה Cancel או', user + email_corrections[i]);

				if (email == null) {
					return false;
				} else {
					document.myform.s_email.value = email;
					document.myform.s_emailChk.value = email;
					i = checkErrorList(email);
				}
			}
		}

		if (trim(document.myform.s_emailChk.value)=='') {
	 		document.myform.s_emailChk.focus();
	 	 	alert('נא לאשר את כתובת הדוא"ל');
	   		return false;
		}
		if (document.myform.s_emailChk.value!=document.myform.s_email.value){
	 		document.myform.s_emailChk.focus();
	 	 	alert('כתובות הדוא"ל אינן זהות. ודאו שרשמתם את כתובתכם ללא שגיאות');
	   		return false;
		}

		if (trim(document.myform.s_password.value)=='') {
	 		document.myform.s_password.focus();
	 	 	alert('נא להזין סיסמה חדשה');
	   		return false;
		}
		if (trim(document.myform.s_password.value).length < 6) {
	 		document.myform.s_password.focus();
	 	 	alert('הסיסמה צריכה להיות באורך 6 תווים לפחות');
	   		return false;
		}
		regex=/^[0-9A-Za-zא-ת]+$/;
		if (!regex.test(document.myform.s_password.value)) {
	 		document.myform.s_password.focus();
	 	 	alert('הסיסמה יכולה להכיל רק אותיות ומספרים');
	   		return false;
		}
	}

	if (trim(document.myform.title.value)=='') {
 		document.myform.title.focus();
 	 	alert('נא לכתוב כותרת למודעה');
   		return false;
	}
	if (trim(document.myform.title.value)=='למסירה' || trim(document.myform.title.value)=='יד שניה') {
 		document.myform.title.focus();
 	 	alert('אנא פרטו יותר את הכותרת כדי לעזור למשתמשים האחרים להבין ביתר קלות על מה מדובר');
   		return false;
	}
	//regex = new RegExp("[0-9- ]{9,}");
	regex = new RegExp("[01][- ]*[2345789][0-9]?[- ]*[0-9- ]{7,}");
	if (regex.test(trim(document.myform.title.value))) {
 		document.myform.title.focus();
 	 	alert('אנא מלאו את הטלפון רק בשדה המיועד לכך');
   		return false;
	}
	regex = new RegExp("[a-z0-9- \.]+@([a-z0-9- ]+\.){1,2}[a-z0-9- ]{2,8}","i");
	if (regex.test(trim(document.myform.title.value))) {
 		document.myform.title.focus();
 	 	alert('אנא מלאו את כתובת הדוא"ל רק בשדה המיועד לכך');
   		return false;
	}

	if (document.myform.subCategory.value=='') {
 	 	alert('נא לבחור תת קטגוריה');
   		return false;
	}

	if (trim(document.myform.Details.value)=='') {
   		document.myform.Details.focus();
  		alert('נא לכתוב תיאור לחפץ');
   		return false;
  	}
	if (trim(document.myform.Details.value).length<20 && trim(document.myform.FILE1.value)=='' && document.myform.hasImage.value!='true') {
   		document.myform.Details.focus();
  		alert("					   .התיאור שהוזן קצר מידי. אנא כתבו לפחות 20 תווים\nהצעות מומלצות לפירוט: סוג החפץ, חברה ודגם, כמות, מדוע נמסר, את מי הוא עשוי לשמש וכו'");
		return false;
  	}
	regex = new RegExp("[01][- ]*[2345789][0-9]?[- ]*[0-9- ]{7,}");
	if (regex.test(trim(document.myform.Details.value))) {
 		document.myform.Details.focus();
 	 	alert('אנא מלאו את הטלפון רק בשדה המיועד לכך');
   		return false;
	}
	regex = new RegExp("[a-z0-9- \.]+@([a-z0-9- ]+\.){1,2}[a-z0-9- ]{2,8}","i");
	if (regex.test(trim(document.myform.Details.value))) {
 		document.myform.Details.focus();
 	 	alert('אנא מלאו את כתובת הדוא"ל רק בשדה המיועד לכך');
   		return false;
	}

	if (!document.myform.condition[0].checked && !document.myform.condition[1].checked && !document.myform.condition[2].checked && !document.myform.condition[3].checked) {
		document.myform.condition[0].focus();
 		alert('נא לסמן את מצבו של החפץ');
 		return false;
	}

	if (trim(document.myform.MyName.value)=='') {
  		document.myform.MyName.focus();
 		alert('נא למלא שם פרטי או כינוי');
 		return false;
	}

	if (!checkPhoneNumber(document.myform.phone1, document.myform.phoneCode1)) return false;

	if (!checkPhoneNumber(document.myform.phone2, document.myform.phoneCode2)) return false;

	if (document.myform.signedIn.value == 'True' && document.myform.showEmail.checked) {
		if (trim(document.myform.Email.value)=='') {
	 		document.myform.Email.focus();
	 	 	alert('נא למלא כתובת דוא"ל');
	   		return false;
		} else {
			//בדוק שהכתובת חוקית
			if (!checkEmailTextBox(document.myform.Email)) return false;

			//בדוק שגיאות נפוצות
			var email = document.myform.Email.value;
			var i = checkErrorList(email);
			var user;

			while (i > -1) {
				user = email.substring(0, email.indexOf("@") + 1);
				email = prompt('.יתכן שכתובת הדוא"ל שהוזנה שגויה\n.להלן מוצע תיקון לכתובת שהוזנה\n\n\n,כדי להשתמש בתיקון OK לחצו על\n.כדי לבטל את הפעולה Cancel או', user + email_corrections[i]);

				if (email == null) {
					return false;
				} else {
					document.myform.Email.value = email;
					i = checkErrorList(email);
				}
			}
		}
	}

  	if (trim(document.myform.city.value)=='') {
		document.myform.city.focus();
   		alert('נא לבחור ישוב/עיר');
   		return false;
  	}

	if (trim(document.myform.FILE1.value) != '') { //במידה שהועלתה תמונה
		var path = document.myform.FILE1.value.toLowerCase();

		if (path.substring(path.length-4) != ".gif" && path.substring(path.length-4) != ".jpg" && path.substring(path.length-4) != ".png") {
			document.myform.FILE1.focus();
			alert('נתיב הקובץ לא תקין או שהקובץ לא נתמך באתר');
			return false;
		}
	}

	return true;
}

//Validates send 2 friend
function send2FriendOK() {
	//friend no. 1
  	if (trim(document.myform.email1.value)=='') {
 		document.myform.email1.focus();
 	 	alert('נא למלא את כתובת הדוא"ל של החבר');
   		return false;
	}

	if (!checkEmailTextBox(document.myform.email1)) return false;
	checkEmailCommonErrors('email1');

	if (trim(document.myform.friendName1.value)=='') {
		document.myform.friendName1.focus();
 	 	alert('נא למלא את שם החבר');
   		return false;
	}

	//friend no. 2
	if (trim(document.myform.email2.value)!='' || trim(document.myform.friendName2.value)!='') {
		if (!checkEmailTextBox(document.myform.email2)) return false;
		checkEmailCommonErrors('email2');

		if (trim(document.myform.friendName2.value)=='') {
			document.myform.friendName2.focus();
	 	 	alert('נא למלא את שם החבר');
	   		return false;
		}
	}

	//friend no. 3
	if (trim(document.myform.email3.value)!='' || trim(document.myform.friendName3.value)!='') {
		if (!checkEmailTextBox(document.myform.email3)) return false;
		checkEmailCommonErrors('email3');
		
		if (trim(document.myform.friendName3.value)=='') {
			document.myform.friendName3.focus();
	 	 	alert('נא למלא את שם החבר');
	   		return false;
		}
	}

	//sender
  	if (trim(document.myform.myEmail.value)=='') {
 		document.myform.myEmail.focus();
 	 	alert('נא למלא את כתובת הדוא"ל שלכם');
   		return false;
	}

	if (!checkEmailTextBox(document.myform.myEmail)) return false;

	if (trim(document.myform.myName.value)=='') {
		document.myform.myName.focus();
 	 	alert('נא למלא את שמכם');
   		return false;
	}

	return true;
}

//Validates sign in
function signInOK(formName) {
	if (document.forms[formName].signInCellular) {
		var regex=/^[0-9-]+$/;
	  	if (trim(document.forms[formName].phone.value)=='' || !regex.test(trim(document.forms[formName].phone.value)) || trim(document.forms[formName].phone.value).length<10) {
	 		document.forms[formName].phone.focus();
	 	 	alert('נא למלא מספר טלפון תקין');
	   		return false;
		}
	}
	else {
	  	if (trim(document.forms[formName].email.value)=='') {
	 		document.forms[formName].email.focus();
	 	 	alert('נא למלא כתובת דוא"ל');
	   		return false;
		}

		if (!checkEmailTextBox(document.forms[formName].email)) return false;
	}

  	if (trim(document.forms[formName].password.value)=='') {
 		document.forms[formName].password.focus();
 	 	alert('נא למלא סיסמה');
   		return false;
	}

	return true;
}

//Validates personal details
function personalDetailsOK() {
	if (trim(document.myForm.email.value)=='') {
 		document.myForm.email.focus();
 	 	alert('נא למלא כתובת דוא"ל');
   		return false;
	} else {
		//בדוק שהכתובת חוקית
		if (!checkEmailTextBox(document.myForm.email)) return false;

		//בדוק שגיאות נפוצות
		var email = document.myForm.email.value;
		var i = checkErrorList(email);
		var user;

		while (i > -1) {
			user = email.substring(0, email.indexOf("@") + 1);
			email = prompt('.יתכן שכתובת הדוא"ל שהוזנה שגויה\n.להלן מוצע תיקון לכתובת שהוזנה\n\n\n,כדי להשתמש בתיקון OK לחצו על\n.כדי לבטל את הפעולה Cancel או', user + email_corrections[i]);

			if (email == null) {
				return false;
			} else {
				document.myForm.email.value = email;
				i = checkErrorList(email);
			}
		}
	}

	if (trim(document.myForm.password.value)=='') {
 		document.myForm.password.focus();
 	 	alert('נא להזין סיסמה');
   		return false;
	}
	if (trim(document.myForm.password.value).length < 6) {
 		document.myForm.password.focus();
 	 	alert('הסיסמה צריכה להיות באורך 6 תווים לפחות');
   		return false;
	}
	var regex=/^[0-9A-Za-zא-ת]+$/;
	if (!regex.test(document.myForm.password.value)) {
 		document.myForm.password.focus();
 	 	alert('הסיסמה יכולה להכיל רק אותיות באנגלית ומספרים');
   		return false;
	}
	if (trim(document.myForm.passwordchk.value)=='') {
 		document.myForm.passwordchk.focus();
 	 	alert('נא לאשר את הסיסמה');
   		return false;
	}
	if (document.myForm.passwordchk.value!=document.myForm.password.value) {
 		document.myForm.passwordchk.focus();
 	 	alert('הסיסמאות אינן זהות');
   		return false;
	}

	if (trim(document.myForm.Fname.value)=='') {
 		document.myForm.Fname.focus();
 	 	alert('נא למלא את השם הפרטי');
   		return false;
	}

	if (trim(document.myForm.Lname.value)=='') {
 		document.myForm.Lname.focus();
 	 	alert('נא למלא את שם המשפחה');
   		return false;
	}

	if (!checkPhoneNumber(document.myForm.Phone1, document.myForm.phoneCode1)) return false;

	if (!checkPhoneNumber(document.myForm.Phone2, document.myForm.phoneCode2)) return false;

	if (!validateDate(document.myForm.birthDay.value, document.myForm.birthMonth.value, document.myForm.birthYear.value)) {
   		document.myForm.birthDay.focus();
 	 	alert('היום בתאריך הלידה לא חוקי');
		return false;
	}

	if (document.myForm.city.value != document.myForm.oldCity.value) {
		if (confirm("ראינו ששיניתי את עיר המגורים שלך.\nהאם ברצונך לשנות גם את ההגדרה של הסקירה היומית?"))
			document.myForm.updateDigestLocation.value = 1;
	}

	return true;
}

//Validates signUpFinish
function signUpFinishOK() {
	if (!checkPhoneNumber(document.myform.phone2, document.myform.phoneCode2)) return false;

	if (document.myform.birthDay.value!="" && document.myform.birthMonth.value!="" && document.myform.birthYear.value!="") {
		if (!validateDate(document.myform.birthDay.value, document.myform.birthMonth.value, document.myform.birthYear.value)) {
			document.myform.birthDay.focus();
			alert('היום בתאריך הלידה לא חוקי');
			return false;
		}
	}

	return true;
}

//Validates the user activation form
function userActivationOK() {
  	if (trim(document.activationForm.email.value)=='') {
 		document.activationForm.email.focus();
 	 	alert('נא למלא כתובת דוא"ל');
   		return false;
	}

	if (!checkEmailTextBox(document.activationForm.email)) return false;

  	if (trim(document.activationForm.comp.value)=='') {
 		document.activationForm.comp.focus();
 	 	alert('נא למלא קוד הפעלה');
   		return false;
	}

	return true;
}

//Validates the add agent
function addAgentOK() {
	//בדיקה שלא הוגדר סוכן כללי מדי
	if (document.myform.category.value=='' && document.myform.location.value=='' && document.myform.tags.value=='') {
  		document.myform.category.focus();
 		alert(".הסוכן שהגדרת כללי מדי, וכתוצאה מכך יתכן שישלח לך מס' גדול של התראות\n.עליך להגדיר לפחות אחד מבין הקריטריונים: קטגוריה, אזור/ישוב, טקסט חופשי");
 		return false;
	}

	if (trim(document.myform.name.value)=='') {
  		document.myform.name.focus();
 		alert('נא למלא שם לסוכן');
 		return false;
	}

	if (document.myform.deliveryMethod.value != 2) {
		if (trim(document.myform.phone.value)=='') {
	  		document.myform.phone.focus();
	 		alert('נא למלא מספר סלולרי');
	 		return false;
		}


		if (!checkPhoneNumber(document.myform.phone, document.myform.phoneCode)) return false;

		if (!document.myform.agreeToTerms.checked) {
	  		document.myform.agreeToTerms.focus();
	 		alert('עליך לאשר שאתה מסכים לתנאי השירות (במסגרת הצהובה)');
	 		return false;
		}
	}

	//בדיקה שלמשתמש אין כרגע סוכן פעיל עם קריטריונים זהים
	var textSearchMethod = 0;
	if (document.myform.textSearchMethod[1].checked) textSearchMethod = 1;
	else if (document.myform.textSearchMethod[2].checked) textSearchMethod = 2;
	else if (document.myform.textSearchMethod[3].checked) textSearchMethod = 3;

	var xmlHttp = getXmlHttpObject();
	xmlHttp.open("GET", "models/addAgentM.asp?action=checkAgentExists"+
						"&deliveryMethod="+document.myform.deliveryMethod.value+
						"&onlyWithImage="+(document.myform.onlyWithImage.checked ? "on" : "")+
						"&onlyWithNumber="+(document.myform.onlyWithNumber.checked ? "on" : "")+
						"&notOnSat="+(document.myform.notOnSat.checked ? "on" : "")+
						"&notAtNight="+(document.myform.notAtNight.checked ? "on" : "")+
						"&dealType="+document.myform.dealType.value+
						"&category="+document.myform.category.value+
						"&subcategory="+document.myform.subcategory.value+
						"&condition="+document.myform.condition.value+
						"&location="+encodeURIComponent(document.myform.location.value)+
						"&tags="+encodeURIComponent(document.myform.tags.value)+
						"&textSearchMethod="+textSearchMethod, false);
	xmlHttp.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
	xmlHttp.setRequestHeader("Content-Language", "he");
	xmlHttp.send(null);

	if (xmlHttp.responseText != "") {
		var arr = xmlHttp.responseText.split("|");
		if (arr[0]=="1") {
	 		alert("נראה שכבר הגדרת סוכן חכם עם קריטריונים זהים בשם '"+arr[1]+"'.\nלא ניתן להגדיר שני סוכנים עם אותם קריטריונים");
	 		return false;
		}
		else if (arr[0]=="4") {
			if (confirm("נראה שכבר הגדרת סוכן חכם עם קריטריונים זהים בשם '"+arr[1]+"', אך הוא נמצא במצב הקפאה.\n?האם ברצונך לערוך את פרטיו"))
				location.href = "addAgent.asp?edit="+arr[2];
			return false;
		}
	}

	document.myform.agreeToTerms.checked = true;
	return true;
}

//Validation for Info And Notifications
function infoAndNotificationsOK() {
	var locations = document.form.locations;

	//שמירת הישובים המסומנים ברשימה
	document.form.formattedLocations.value = "";
	while (locations.selectedIndex != -1) {
		if (locations[locations.selectedIndex].value != "") {
			document.form.formattedLocations.value += locations[locations.selectedIndex].value+"|";
			locations[locations.selectedIndex].selected = false;
		}
	}

	if (document.form.dailyDigest.checked && document.form.formattedLocations.value=="") {
		document.form.locations.focus();
		alert("אם אתם מעוניינים לקבל סקירה יומית, עליכם לבחור לפחות אזור או ישוב אחד");
		return false;
	}

	return true;
}

function ajaxFunction(elmnt, page, writeType) {
  	var xmlHttp = getXmlHttpObject();
	xmlHttp.open("GET", page, true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange = function() {
	  	if (xmlHttp.readyState == 4) {
	  		switch (writeType) {
			case 1:
			  	document.getElementById(elmnt).innerHTML = xmlHttp.responseText;
			  	break;

			case 2:
			  	document.getElementById(elmnt).value = xmlHttp.responseText;
			  	break;
			}
		}
	}
}

