// login to forum's system (home.php)
function doLogin(){
	if (document.frmEnter.email.value!=""){
		if (document.frmEnter.password.value!=""){
			document.frmEnter.submit();
		}else{
			alert("יש להזין סיסמא");
			document.frmEnter.password.focus();
		}
	}else{
		alert("יש להזין כתובת אימייל");
		document.frmEnter.email.focus();
	}
}

//in case that user forgot his password (home.php)
function doLoginForgot(){
	var email = document.frmEnter.email.value;
	if (document.frmEnter.email.value!=""){
		if (confirm("מייל לשינוי הסיסמא יישלח לכתובתך.\nהאם להמשיך?")){
			location.href = "home.php?act=remind&email="+email;
		}
	}else{
		alert("יש להזין כתובת אימייל.\nאימייל לשינוי סיסמא יישלח אליך.");
		document.frmEnter.email.focus();
	}
}

// add new message (addmsg.php)
function doSend(){
	if (document.frmSend.title.value!=""){
		if ((!document.frmSend.rndchk) || (document.frmSend.rndchk.value!="")){
			try{
				syncTextarea();
			}catch(e){
				
			}
			document.frmSend.submit();
		}else{
			alert("יש להזין את קוד האבטחה בעל 5 ספרות");
			document.frmSend.rndchk.focus();
		}
	}else{
		alert("יש להזין נושא ההודעה");
		document.frmSend.title.focus();
	}
}

// display message content (forum.php)
function displayMessage(id){
	var obj = document.getElementById(id);
	obj.style.display=(obj.style.display==""?"none":"");
}

function displayTitle(id){
	var obj = document.getElementById(id);
	obj.style.display=(obj.style.display==""?"none":"");
}

function displayTitleOpen(id){
	var obj = document.getElementById(id);
	obj.style.display=(obj.style.display==""?"none":"");
}

// go to page by user's choice (forum.php)
function goToPage(id, total, forumid, max){
	var obj = document.getElementById(id);
	var topage = obj.value;
	if (topage<=total && topage>0)
	document.location.href = "forum?&forum_loc=forum&forumid="+forumid+"&p="+topage+"&from="+((topage-1)*max);
}

// send mail, by request, from one user to another (forum.php)
function sendMail(forumid,userid, currentuser){
	if (currentuser!='') {
		url = "mailto.php?forumid="+forumid+"&userid="+userid;
		window.open(url,null, "height=365,width=500,status=no,toolbar=no,menubar=no,location=no");
	}else location.href = "home.php?msg=7";
	
}

// put message on top (forum.php)
function stickMsg(id,forumid,isstick){
	var msg = "";
	if (isstick == 'yes') msg = "הודעה זו תוקפץ לתחילת הפורום.\nהאם הינך בטוח?";
	else msg = "ההודעה תוחזר למקומה בפורום בהתאם לתאריך.\nהאם הינך בטוח?";
	if (confirm(msg))
	location.href = "forum?forum_loc=forum&act=stick&id="+id+"&isstick="+isstick+"&forumid="+forumid;
}

// delete message (can be done only by manager) (forum.php)
function doDel(id,forumid){
	if (confirm("הנך עומד למחוק ההודעה,\nההודעה וכל תגובותיה ימחקו גם כן.\nהאם הינך בטוח?"))
	document.location = "forum?forum_loc=forum&act=del&id=" + id+"&forumid="+forumid;
}

// send mail to user (mailto.php)
function doSendMail(){
	if (document.frmSend.subject.value!=""){
		if (document.frmSend.content.value!=""){
			document.frmSend.submit();
		}else{
			alert("יש להזין תוכן להודעה");
			document.frmSend.content.focus();
		}
	}else{
		alert("יש להזין נושא להודעה");
		document.frmSend.subject.focus();
	}
}

//check email validity (register.php)
function IsEmail(sText) {
	var at="@"
	var dot="."
	var lat=sText.indexOf(at)
	var lstr=sText.length
	var ldot=sText.indexOf(dot)
	if (sText=="" || sText==null){ return false }
	if (sText.indexOf(at)==-1 || sText.indexOf(at)==0 || sText.indexOf(at)==lstr){ return false }
	if (sText.indexOf(dot)==-1 || sText.indexOf(dot)==0 || sText.indexOf(dot)==lstr){ return false }
	if (sText.indexOf(at,(lat+1))!=-1){ return false }
	if (sText.substring(lat-1,lat)==dot || sText.substring(lat+1,lat+2)==dot){ return false }
	if (sText.indexOf(dot,(lat+2))==-1){ return false }
	if (sText.indexOf(" ")!=-1){ return false }
	return true
}

//check if all fields are filled correctly (register.php)
function doRegister(){
	if (document.frmSend.fullname.value!=""){
		if (document.frmSend.nickname.value!=""){
			if (document.frmSend.password1.value!="" && document.frmSend.password1.value.length>=6 && document.frmSend.password1.value.length<=8){
				if (document.frmSend.password2.value!="" && document.frmSend.password2.value.length>=6 && document.frmSend.password2.value.length<=8){
					if (document.frmSend.password1.value==document.frmSend.password2.value){
						if (IsEmail(document.frmSend.email.value)){
							document.frmSend.submit();
						}else{
							alert("יש להזין אימייל תקין");
							document.frmSend.email.focus();
						}
					}else{
						alert("הסיסמא החוזרת חייבת להיות זהה לסיסמא שהקולדה");
						document.frmSend.password1.select();
					}
				}else{
					alert("יש להזין סיסמא לאישור");
					document.frmSend.password2.select();
				}
			}else{
				alert("יש להזין סיסמא בת 6 - 8 ספרות");
				document.frmSend.password1.select();
			}
		}else{
			alert("יש להזין כינוי");
			document.frmSend.nickname.focus();
		}
	}else{
		alert("יש להזין שם מלא");
		document.frmSend.fullname.focus();
	}
}
