﻿function CheckForm(){

	if (document.cont_form.fullname.value==''){
		alert("שדה 'שם' אינו יכול להיות ריק")
		document.cont_form.fullname.focus();
		return false;
	}
	if (document.cont_form.email.value==''){
		alert("שדה 'דואל' אינו יכול להיות ריק")
		document.cont_form.email.focus();
		return false;
	}
	if (document.cont_form.email.value!=''){
		var checkEmailResult=checkEmail(document.cont_form.email.value)
		if( checkEmailResult != true){
			alert(checkEmailResult)
			document.cont_form.email.focus();
			return false;
		}
	}
}


function checkEmail(email){
	re = /^([\w\-\.]+)@((\[([0-9]{1,3}\.){3}[0-9]{1,3}\])|(([\w\-]+\.)+)([a-zA-Z]{2,4}))$/
	//if (!re.test(email))
	//	return("The e-mail is illegal")
	//return true
	if (re.test(email) == true)
		return true //alert('Valid email');
	else {
		return (validateEmail(email))//alert(checkEmail(email))
	}
}

function validateEmail(email){
	var atPos = email.indexOf("@",1)
	var invalidChars = " /:;,"
	if(email==""){ //email cannot be empty
		return("אנא הזן ערך בשדה דואר אלקטרוני.");
	}
	
	for(var i=0; i<invalidChars.length; i++){
		badChar = invalidChars.charAt(i)
		if(email.indexOf(badChar,0) != -1){ //email cannot be invalid chars
			return ("קיים תו בלתי חוקי בתוך כתובת הדואר שלך");
		}
	}
	if((email.indexOf("@")) != (email.lastIndexOf("@"))){ //email must be one "@" symbol
		return("כתובת דואר האלקטרוני שלך מכיל יותר מ@ אחד.");
	}
	
	if(email.indexOf("@") == -1){ //check if there is "@" symbol in the email
		return("כתובת דואר אלקטרוני שלך שגוייה אנא הוסף @")
	}
	
	if((email.indexOf("@")) == 0){ //check if the "@" symbol is in the first place
		return("כתובת דואר שלך שגוייה הנקודה נמצאת במקום בלתי חוקי.");
	}
	
	if((email.indexOf(".")) == -1){ //check if there is "." symbol in the email
		return("כתובת דואר האלקטרוני שלך שגוייה אנא הוסף נקודה.")
	}
	
	if((email.indexOf(".")) < atPos){ // check if the "." before the "@" symbol
		return("הנקודה נמצאת במקום בלתי חוקי בכתובת דואר אלקטרוני שלך.");
	}
	
	if((email.indexOf(".")) == atPos+1){ // check if the "." before the "@" symbol
		return("הנקודה נמצאת במקום בלתי חוקי בכתובת דואר אלקטרוני שלך.");
	}
	
	if(email.lastIndexOf(".") == (email.length -1)){ //check if the "." is the last in the email
		return("כתובת דואר האלקטרוני שלך שגוייה אנא הזן ערך לאחר הנקודה");
	}
	
	var dotCount = 0;
	for(var j=0; j<email.length; j++){
		if(email.substring(j,j+1) == ".") dotCount ++ // check if there isn't more then two dots
		if (dotCount  >2)	{
			return("כתובת הדואר האלקטרוני שלך שגוייה היא יכולה להכיל עד שני נקודות");
		}
	}
	
	if(email.indexOf("..") != -1){ // check if the isn't two dots in 
		return("כתובת הדואר אלקטרוני שלך שגוייה אין להזין שתי נקודות ברצף");
	}

	return true;
	
}

//*******************************************************************
// Tests whether the given argument is empty or null
function isnull(arg) {
	arg = arg+'';
	return (arg == '' || arg == 'null' || arg == 'undefined');
}

//*******************************************************************
function numberOk(str){
	var numOk="0123456789"
	for(i=0; i<str.length; i++){
		if(numOk.indexOf(str.substring(i,i+1))==-1){
			return false;
		}
	}
	return true;
}
//*******************************************************************
function trim(str){
	try {
		return str.replace(/^\s+|\s+$/g,'');
	}
	catch (e) {
		return str;
	}
}

function onlyChars(str){
	var re = /\d+/gi;
	if (re.test(str) == false)
		return true
	else 
		return false
}

function charOk(str,badChar){
	var charPlace;
	for(i=0;i<badChar.length; i++){
		charPlace=badChar.charAt(i);
		if(str.indexOf(charPlace,0)!=-1){
			return false;
		}
	}
	return true;
}



