﻿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)+encodeURIComponent(' '+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) {
	if (sbars == "") sbars = "no";
	if (resize == "") resize = "yes";

	if (isIE()) {
		//h += 10;
		if (sbars == "yes") w += 17;
	}

	window.open(url,"_blank","toolbar=0,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 057 072 073 074 076 077 1700 1800 ';
		var minLength;

		if (phoneCode.value != '' && phoneCodes.indexOf(' '+phoneCode.value+' ') > -1) {
			minLength = (phoneCode.value.length == 4 ? 6 : 7)
		} else {
			phoneCode.focus();
		 	alert('נא לבחור קידומת');
			return false;
		}

	  	if (trim(phone.value).length < minLength) {
			phone.focus();
		 	alert('חסרות ספרות למספר הטלפון');
		  	return false;
	 	}

		if (isNaN(trim(phone.value))) {
	 		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);
	}

	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 (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() {
	//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;
		}
		var regex=/^[0-9A-Za-zא-ת]+$/; //^[a-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 (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) {
   		document.myform.Details.focus();
  		alert('					   .התיאור שהוזן קצר מידי. אנא כתבו לפחות 20 תווים\nהצעות מומלצות לפירוט: סוג החפץ, חברה ודגם, כמות, מדוע נמסר ,את מי הוא עשוי לשמש');
		return false;
  	}

	if (document.myform.condition[0].checked==false && document.myform.condition[1].checked==false && document.myform.condition[2].checked==false && document.myform.condition[3].checked==false) {
		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 (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;
	}
	/*var regex=/^[0-9A-Za-z]+$/;
	if (!regex.test(document.forms[formName].password.value)) {
 		document.forms[formName].password.focus();
 	 	alert('הסיסמה יכולה להכיל רק אותיות באנגלית ומספרים');
   		return false;
	}
	if (trim(document.forms[formName].password.value).length < 6) {
 		document.forms[formName].password.focus();
 	 	alert('הסיסמה צריכה להיות באורך 6 תווים לפחות');
   		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א-ת]+$/; //^[a-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;
	}

	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;
		}
	}

	document.myform.agreeToTerms.checked = true;
	return true;
}

//Validation for add Delivery
function addDeliveryOK() {
  	if (trim(document.myform.takeCity.value)=='') {
		document.myform.takeCity.focus();
 	 	alert('נא למלא את נקודת האיסוף');
   		return false;
	}

	if (trim(document.myform.takeAddress.value)=='') {
		document.myform.takeAddress.focus();
 	 	alert('נא למלא את נקודת את כתובת האיסוף');
   		return false;
	}
	
	if (trim(document.myform.takeName.value)=='') {
		document.myform.takeName.focus();
 	 	alert('נא למלא את שם מוסר החפץ');
   		return false;
	}
	
	if (trim(document.myform.takePhone.value)=='') {
		document.myform.takePhone.focus();
 	 	alert('נא למלא את טלפון מוסר החפץ');
   		return false;
	}
	
	if (trim(document.myform.takeName.value)=='') {
		document.myform.takeName.focus();
 	 	alert('נא למלא את שם מוסר החפץ');
   		return false;
	}
	
	if (trim(document.myform.deliveryCity.value)=='') {
		document.myform.deliveryCity.focus();
 	 	alert('נא למלא את נקודת היעד');
   		return false;
	}
	
	if (trim(document.myform.deliveryAddress.value)=='') {
		document.myform.deliveryAddress.focus();
 	 	alert('נא למלא את נקודת את כתובת היעד');
   		return false;
	}
	
	if (trim(document.myform.deliveryName.value)=='') {
		document.myform.deliveryName.focus();
 	 	alert('נא למלא את שם מקבל החפץ');
   		return false;
	}
	
	if (trim(document.myform.deliveryPhone.value)=='') {
		document.myform.deliveryPhone.focus();
 	 	alert('נא למלא את טלפון מקבל החפץ');
   		return false;
	}
	
	if (trim(document.myform.Month.value)=='') {
		document.myform.Month.focus();
 	 	alert('נא למלא תאריך מבוקש להובלה');
   		return false;
	}

	if (trim(document.myform.Day.value)=='') {
		document.myform.Day.focus();
 	 	alert('נא למלא תאריך מבוקש להובלה');
   		return false;
	}
	
	if (trim(document.myform.time1.value)=='') {
		document.myform.time1.focus();
 	 	alert('לבחור טווח שעות בו אהיה זמין לתת/לקבל את ההובלה');
   		return false;
	}
	
	if (trim(document.myform.time2.value)=='') {
		document.myform.time2.focus();
 	 	alert('לבחור טווח שעות בו אהיה זמין לתת/לקבל את ההובלה');
   		return false;
	}
	
	if (trim(document.myform.time1.value)>trim(document.myform.time2.value)) {
		document.myform.time2.focus();
 	 	alert('שעת הגג אינה יכולה להיות נמוכה משעת המינימום');
   		return false;
	}
	
	if (Number(document.myform.time2.value.charAt(0)+document.myform.time2.value.charAt(1))-Number(document.myform.time1.value.charAt(0)+document.myform.time1.value.charAt(1))<3) {
		document.myform.time2.focus();
 	 	alert('עליכם להגדיר טווח של מינימום שלוש שעות');
   		return false;
	}
	
	if (trim(document.myform.details.value)=='') {
		document.myform.details.focus();
 	 	alert('עליכם לכתוב איזה חפץ ברצונכם להוביל');
   		return false;
	}
	
	if (trim(document.myform.email.value)=='') {
		document.myform.email.focus();
 	 	alert('עליכם להזין כתובת מייל לקבלת הצעת המחיר');
   		return false;
	}
	
	if (!checkEmailTextBox(document.myform.email)) return false;

	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;
			}
		}
	}
}