var errors=false;
var er_name=true;
var txt="";
/***************************************************
 * Check Valid Passwords
 **************************************************/
function checkPasswords()
{
	if( document.getElementById('form_password').value == "" ||
		document.getElementById('form_password').value.length<3 || document.getElementById('form_password').value.length>15)
	{
		errors=true;
         document.getElementById("dpass").style.width="16px";
         document.getElementById("dpass").style.height="16px";
        document.getElementById("dpass").src="/images/register_xicon.gif";
       document.getElementById('check_password').innerHTML='<span style="color:#FF0000;font-weight:bold;">סיסמא צריכה להיות בין 3 ל 15 תווים</span>';
    }
	else
	{
	    errors=false;
        document.getElementById("dpass").style.width="16px";
         document.getElementById("dpass").style.height="16px";
        document.getElementById("dpass").src="/images/register_vicon.gif";
        document.getElementById("dpass").title="תקין";
        document.getElementById('check_password').innerHTML="";
	}
}

/***************************************************
 * Check Valid Email
 **************************************************/
// Block the following email extensions
var invalidaddress = new Array();
//invalidaddress[0] = "hotmail";
//invalidaddress[2] = "yahoo";

function checkEmail()
{
    var invalidcheck = 0;
	var str = document.getElementById("form_email").value;
    if (str!='')
    {
	//var filter = /^(\w+(?:\.\w+)*\w[\w-]*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	var filter = /^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.([a-z]{2,4})$/i

		if (filter.test(str)) {

			var tempstring = str.split("@");
			tempstring = tempstring[1].split(".");

			for ( i=0; i<invalidaddress.length; i++ ) {
				if (tempstring[0] == invalidaddress[i]) {
					invalidcheck = 1;
				}
			}

			if (invalidcheck != 1) {
				handlerFuncMail(str);
			}
			
			} else {
				
			errors=true;
			document.getElementById("dmail").style.width="16px";
			document.getElementById("dmail").style.height="16px";
			document.getElementById("dmail").src="http://www.kefpo.co.il/images/register_xicon.gif";
			document.getElementById("dmail").title="כתובת דואר אלקטרוני לא תקינה";
			document.getElementById("mail_err").innerHTML='<span style="color:#FF0000;font-weight:bold;">כתובת דואר אלקטרוני לא תקינה</span>';
			}
		
    }
    else
    {
        errors=true;
        document.getElementById("dmail").style.width="16px";
         document.getElementById("dmail").style.height="16px";
        document.getElementById("dmail").src="http://www.kefpo.co.il/images/register_xicon.gif";
        document.getElementById("dmail").title="כתובת דואר אלקטרוני לא תקינה";
        document.getElementById("mail_err").innerHTML='<span style="color:#FF0000;font-weight:bold;">חסרה כתובת דואר אלקטרוני</span>';
    }

}


/***************************************************
 * Check Valid User Name
 **************************************************/
function handlerFuncMail (t)
{
	$.post("/ajax.php", { func: "checkEmail", email: t},
	  function(data){
		var status = data;
				
			if (status == "inUse")
			{
				errors=true;
				document.getElementById("dmail").style.width="16px";
				document.getElementById("dmail").style.height="16px";
				document.getElementById("dmail").src="http://www.kefpo.co.il/images/register_xicon.gif";
				document.getElementById("dmail").title="קיים חשבון לאי מייל הזה, אנא בחר אחר";
				document.getElementById("mail_err").innerHTML='<span style="color:#FF0000;font-weight:bold;">קיים חשבון לאי מייל הזה, אנא בחר אחר</span>';
			}
			else
			{
				errors=false;
				document.getElementById("dmail").style.width="16px";
				document.getElementById("dmail").style.height="16px";
				document.getElementById("dmail").src="http://www.kefpo.co.il/images/register_vicon.gif";
				document.getElementById("dmail").title="תקין";
				document.getElementById("mail_err").innerHTML="";
			}
	
	  }, "text");
	  
}

function handlerFunc (t)
{

	$.post("/ajax.php", { func: "checkUsername", username: t},
	  function(data){
		var status = data;
		
	if (status != 'e')
		{
			
			if (status=='a')
				txt= "שם משתמש מכיל תווים אסורים לשימוש (! & @)";
			if (status=='b')
				txt= "שם המשתמש שבחרת ארוך מדי";
			if (status=='c')
				txt= "שם המשתמש שבחרת קצר מדי ";
			if (status=='d')
				txt= "שם המשתמש שבחרת כבר בשימוש";
			if (status=='e')
				txt= "שם משתמש תקין";
			document.getElementById("check_username").innerHTML="<span style='font-weight:bold;color:#FF0000;'>"+txt+"</span>";
			errors=true;
			er_name=true;
			document.getElementById("dusername").style.width="16px";
			 document.getElementById("dusername").style.height="16px";
			document.getElementById("dusername").src="http://www.kefpo.co.il/images/register_xicon.gif";
		}
		else
		{
			errors=false;
			er_name=false;
			document.getElementById("dusername").style.width="16px";
			 document.getElementById("dusername").style.height="16px";
			document.getElementById("dusername").src="http://www.kefpo.co.il/images/register_vicon.gif";
			document.getElementById("dusername").title="תקין";
			document.getElementById("check_username").innerHTML="";
		}
		document.getElementById("dusername").title=txt;
		
	  }, "text");

	

}

function updateUsernameStatus()
{
	self.username = document.getElementById("form_username").value;
	if(self.username != "")
	{
        checkVal=self.username;
        if (checkVal.indexOf('!') != -1 || checkVal.indexOf('@') != -1 || checkVal.indexOf('#') != -1 || checkVal.indexOf('$') != -1 || checkVal.indexOf('%') != -1 || checkVal.indexOf('^') != -1 || checkVal.indexOf('&') != -1 || checkVal.indexOf('*') != -1 || checkVal.indexOf('(') != -1 || checkVal.indexOf(')') != -1 || checkVal.indexOf('-') != -1 || checkVal.indexOf('_') != -1 || checkVal.indexOf('=') != -1 || checkVal.indexOf('+') != -1 || checkVal.indexOf('`') != -1 || checkVal.indexOf('~') != -1 || checkVal.indexOf(':') != -1 || checkVal.indexOf('"') != -1 || checkVal.indexOf("'") != -1 )
         {
               errors=true;
        er_name=true;
        document.getElementById("dusername").style.width="16px";
         document.getElementById("dusername").style.height="16px";
        document.getElementById("check_username").innerHTML="<span style='font-weight:bold;color:#FF0000;'>שם משתמש מכיל תווים אסורים לשימוש (! & @)</span>"
        document.getElementById("dusername").src="http://www.kefpo.co.il/images/register_xicon.gif";
         }
         else
            {

		handlerFunc(self.username);
	}

    }
	else
	{
        errors=true;
        er_name=true;
        document.getElementById("dusername").style.width="16px";
        document.getElementById("dusername").style.height="16px";
		document.getElementById("dusername").src="http://www.kefpo.co.il/images/register_xicon.gif";
        document.getElementById("check_username").innerHTML="<span style='font-weight:bold;color:#FF0000;'>חסר שם משתמש</span>"
        
	}
}

function firstcheck()
{
    updateUsernameStatus();
    checkEmail();
    checkPasswords();
	
    if (!errors)
	{
        documemt.getElementById('form').submit();
	}
	else 
	{
		alert('הטופס לא מולא כהלכה');		
	}
}

function formSub()
{ 
			if(firstcheck())
			{
				document.getElementById('form').submit();
			}
			else
			{
				alert('הטופס לא מלא כהלכה');
			}

}
