﻿function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("بريد إلكتروني غير صالح")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("بريد إلكتروني غير صالح")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("بريد إلكتروني غير صالح")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("بريد إلكتروني غير صالح")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("بريد إلكتروني غير صالح")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("بريد إلكتروني غير صالح")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("بريد إلكتروني غير صالح")
		    return false
		 }

 		 return true					
	}
	
	function IsNumeric(strString)
		//  check for valid numeric strings	
		{
			var strValidChars = "0123456789.-";
			var strChar;
			var blnResult = true;

			if (strString.length == 0) return false;

			//  test strString consists of valid characters listed above
			for (i = 0; i < strString.length && blnResult == true; i++)
				{
				strChar = strString.charAt(i);
				if (strValidChars.indexOf(strChar) == -1)
					{
					blnResult = false;
					}
				}
			return blnResult;
		}
		
		function IsUsername(strString)
		//  check for valid numeric strings	
		{
			var strValidChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
			var strChar;
			var blnResult = true;

			if (strString.length == 0) return false;

			//  test strString consists of valid characters listed above
			for (i = 0; i < strString.length && blnResult == true; i++)
				{
				strChar = strString.charAt(i);
				if (strValidChars.indexOf(strChar) == -1)
					{
					blnResult = false;
					}
				}
			return blnResult;
		}
		
		function IsWord(strString)
		//  check for valid numeric strings	
		{
			var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
			var strChar;
			var blnResult = true;

			if (strString.length == 0) return false;

			//  test strString consists of valid characters listed above
			for (i = 0; i < strString.length && blnResult == true; i++)
				{
				strChar = strString.charAt(i);
				if (strValidChars.indexOf(strChar) == -1)
					{
					blnResult = false;
					}
				}
			return blnResult;
		}
		
		function IsAddress(strString)
		//  check for valid numeric strings	
		{
			var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\()\r\n ";
			var strChar;
			var blnResult = true;

			if (strString.length == 0) return false;

			//  test strString consists of valid characters listed above
			for (i = 0; i < strString.length && blnResult == true; i++)
				{
				strChar = strString.charAt(i);
				if (strValidChars.indexOf(strChar) == -1)
					{
					blnResult = false;
					}
				}
			return blnResult;
		}
		
		function IsEmpty(strString)
		{	
			var blnResult = false;
			
			if(strString.length == 0)
			{
				blnResult = true;
			}
			
			return blnResult;
		}
		
		function LenghtCheckSix(strString)
		//  check for valid numeric strings	
		{
			var blnResult = true;
			
			if (strString.length < 6)
			{
				blnResult = false;
			}
			return blnResult;
		}
		
		function LenghtCheckTwenty(strString)
		//  check for valid numeric strings	
		{
			var blnResult = true;
			
			if (strString.length > 20)
			{
				blnResult = false;
			}
			return blnResult;
		}
		
		function LenghtCheckTwentyFive(strString)
		//  check for valid numeric strings	
		{
			var blnResult = true;
			
			if (strString.length > 25)
			{
				blnResult = false;
			}
			return blnResult;
		}
		
		function LenghtCheckFifty(strString)
		//  check for valid numeric strings	
		{
			var blnResult = true;
			
			if (strString.length > 50)
			{
				blnResult = false;
			}
			return blnResult;
		}
		
		function LenghtCheckHundred(strString)
		//  check for valid numeric strings	
		{
			var blnResult = true;
			
			if (strString.length > 100)
			{
				blnResult = false;
			}
			return blnResult;
		}
		
		function ClearAll()
		{
			document.getElementById('userName').value = "";
			document.getElementById('txtPassword').value = "";
			document.getElementById('txtRePassword').value = "";
			document.getElementById('txtFName').value = "";
			document.getElementById('txtLName').value = "";
			document.getElementById('txtEmail').value = "";
			document.getElementById('txtContactNumber').value = "";
			document.getElementById('txtAddress').value = "";
			return false;
		}
		
		function Check_Form()
		{
			if (document.getElementById('userName').value == "")
			{
				alert("من فضلك قم بإدخال إسم المستخدم");
				document.getElementById('userName').focus();
				return false;
			}
			else if (IsUsername(document.getElementById('userName').value)!= true)
			{
				alert("يجب أن يتكون إسم المستخدم من الحروف الأبجدية أ-ي و أرقام من0-9  و العلامة _");
				document.getElementById('userName').focus();
				return false;
			}
			else if(LenghtCheckTwentyFive(document.getElementById('userName').value) != true)
			{
				alert("ادخل أقلّ من 25 حرف");
				document.getElementById('userName').focus();
				return false;
			}
			else if (document.getElementById('txtPassword').value == "")
			{
				alert("من فضلك قم بإدخال كلمة السر");
				document.getElementById('txtPassword').focus();
				return false;
			}
			else if (IsWord(document.getElementById('txtPassword').value)!= true)
			{
				alert("يجب أن يكون الحقل هجائيّ عدديّ");
				document.getElementById('txtPassword').focus();
				return false;
			}
			else if(LenghtCheckSix(document.getElementById('txtPassword').value) != true)
			{
				alert("يجب أن تتكون كلمة السر من 6  أحرف  لتكون أكثر أمنًا");
				document.getElementById('txtPassword').focus();
				return false;
			}
			else if(LenghtCheckTwentyFive(document.getElementById('txtPassword').value) != true)
			{
				alert("ادخل أقلّ من 25 حرف");
				document.getElementById('txtPassword').focus();
				return false;
			}
			else if (document.getElementById('txtRePassword').value == "")
			{
				alert("من فضلك قم بإدخال كلمة السر مرة أخرى");
				document.getElementById('txtRePassword').focus();
				return false;
			}
			else if(document.getElementById('txtRePassword').value != document.getElementById('txtPassword').value)
			{
				alert("مباراة دويسنوت كلمة السّرّ");
				document.getElementById('txtRePassword').focus();
				return false;
			}
			else if (document.getElementById('txtFName').value == "")
			{
				alert("من فضلك قم بإدخال الإسم الأول");
				document.getElementById('txtFName').focus();
				return false;
			}
			//else if (IsWord(document.getElementById('txtFName').value)!= true)
			//{
			//	alert("First Name must be alphanumeric");
			//	document.getElementById('txtFName').focus();
			//	return false;
			//}
			else if(LenghtCheckFifty(document.getElementById('txtFName').value) != true)
			{
				alert("ادخل أقلّ من 50 حرف");
				document.getElementById('txtFName').focus();
				return false;
			}
			else if (document.getElementById('txtLName').value == "")
			{
				alert("من فضلك قم بإدخال الإسم الأخير");
				document.getElementById('txtLName').focus();
				return false;
			}
			//else if (IsWord(document.getElementById('txtLName').value)!= true)
			//{
			//	alert("Last Name must be alphanumeric");
			//	document.getElementById('txtLName').focus();
			//	return false;
			//}
			else if(LenghtCheckFifty(document.getElementById('txtLName').value) != true)
			{
				alert("ادخل أقلّ من 50 حرف");
				document.getElementById('txtLName').focus();
				return false;
			}
			else if (document.getElementById('txtEmail').value == "")
			{
				alert("من فضلك قم بإدخال البريد الإلكتروني");
				document.getElementById('txtEmail').focus();
				return false;
			}
			else if(LenghtCheckFifty(document.getElementById('txtEmail').value) != true)
			{
				alert("ادخل أقلّ من 50 حرف");
				document.getElementById('txtEmail').focus();
				return false;
			}
			else if (echeck(document.getElementById('txtEmail').value)==false)
			{
				document.getElementById('txtEmail').value="";
				document.getElementById('txtEmail').focus();
				return false;
			}
			else if ((IsEmpty(document.getElementById('txtContactNumber').value) != true) && (IsNumeric(document.getElementById('txtContactNumber').value)!= true))
			{
				alert("يجب أن يكون الحقل هجائيّ عدديّ");
				document.getElementById('txtContactNumber').focus();
				return false;
			}
			else if ((IsEmpty(document.getElementById('txtContactNumber').value) != true) && (LenghtCheckTwenty(document.getElementById('txtContactNumber').value) != true))
			{
				alert("ادخل أقلّ من 20 حرف");
				document.getElementById('txtContactNumber').focus();
				return false;
			}
			else if (document.getElementById('txtAddress').value == "")
			{
				alert("من فضلك قم بإدخال العنوان");
				document.getElementById('txtAddress').focus();
				return false;
			}
			//else if (IsAddress(document.getElementById('txtAddress').value)!= true)
			//{
			//		alert("Enter alphanumeric and '-_\()' literals for Address");
			//		document.getElementById('txtAddress').focus();
			//		return false;
			//}
			else if(LenghtCheckHundred(document.getElementById('txtAddress').value) != true)
			{
				alert("ادخل أقلّ من 100 حرف");
				document.getElementById('txtAddress').focus();
				return false;
			}
			else
			{
				//document.getElementById('flag1').value = "true";
				return true;
			}
		}
		function userlogin()
		{
			
			if (document.getElementById('txtname').value == "")
			{
				alert("من فضلك أدخل الإسم");
				document.getElementById('txtname').focus();
				return false;
			}
			//else if (IsWord(document.getElementById('txtname').value)!= true)
			//{
			//	alert("Name must be alphanumeric");
			//	document.getElementById('txtname').focus();
			//	return false;
			//}
			/*else if (LenghtCheck(document.getElementById('txtname').value) != true)
			{
				alert("Name must be less than 50 characters");
				document.getElementById('txtname').focus();
				return false;
			}*/
			else if (document.getElementById('txtemails').value == "")
			{
				alert("من فضلك قم بإدخال البريد الإلكتروني");
				document.getElementById('txtemails').focus();
				return false;
			}
			/*else if(LenghtCheck(document.getElementById('txtemails').value) != true)
			{
				alert("Email Address must be less than 50 characters");
				document.getElementById('txtemails').focus();
				return false;
			}*/
			else if (echeck(document.getElementById('txtemails').value)==false)
			{
				document.getElementById('txtemails').value="";
				document.getElementById('txtemails').focus();
				return false;
			}
			else
			{
				return true;
			}
			
		}
