
function onEmailChanged(email, busyTranslation)
{
    if(!email)return;
    
    var onEmailSuccess = function(ajaxObj)
    {
        var response = ajaxObj.parseJSON();
        var text = "";
              
        switch(response.status)
        {
            case "banned": text = "banned"; break;
            case "busy": text = busyTranslation; break; 
        }
        
        $('indicatorWhetherEmailIsBusy').innerHTML = text; 

    }
    
    ajax.clearParams();
    ajax.set("onSuccess", onEmailSuccess.handler(this))
    ajax.addParam("email", email);   
    ajax.post(setting.isEmailAcceptable);

}

function my_alert(msg)
{
    alert(msg);
    return false;
}

function validateAndRegisterWebmaster(invalidEmailMessage,
                                      passwordsNotEquallMessage,
                                      passwordsEmptyMessage,
                                      busyEmailMessage,
                                      successMessage,
                                      notSuccessMessage)
{
    var inscriptionForm = $('webmasterInscriptionForm');
	var loginForm = $('webmasterLoginForm');
    
	var emailPattern = new RegExp("^(([A-Za-z0-9_])|([\-\.]))+\@(([A-Za-z0-9_])|([\-\.]))+\.([A-Za-z]{2,4})$");
	
	// email validation
    if(emailPattern.test(inscriptionForm.email.value) == false)
        return my_alert(invalidEmailMessage);
			
	// passwords
	if(inscriptionForm.password.value != inscriptionForm.repeatPassword.value)
        return my_alert(passwordsNotEquallMessage);
        
    if(inscriptionForm.password.value == '')
        return my_alert(passwordsEmptyMessage);
	
	// if email is free
	if($('indicatorWhetherEmailIsBusy').innerHTML != '')
        return my_alert(busyEmailMessage);
		
    ajax.clearParams();
    
	if(inscriptionForm.privateCode)
	{
		ajax.addParam('privateCode', inscriptionForm.privateCode.value);
		ajax.addParam('publicCode', inscriptionForm.publicCode.value);
	}
	
	ajax.addParam('email', inscriptionForm.email.value);
    ajax.addParam('password', inscriptionForm.password.value);
	
	var onRegisterSuccess = function (ajaxObj)
    {
		var response = ajaxObj.parseJSON(); 
        
		if(response.registered)
		{
			$('webmasterInscriptionBlockHeader').hide();
			$('webmasterInscriptionBlockContent').hide();
			alert(successMessage);
            
			loginForm.email.focus();
			loginForm.email.select();
		}
		else
		{
			if(inscriptionForm.publicCode)
			{
                CaptchaCode.replaceCaptchaImage(inscriptionForm.publicCode.value, inscriptionForm);   
				alert(response.message);
                //alert(notSuccessMessage);
			}
            
			inscriptionForm.privateCode.focus();
			inscriptionForm.privateCode.select();
		} 
    }
    
    ajax.set("onSuccess", onRegisterSuccess.handler(this))        
    ajax.post(setting.registerUrl);
	
	return false;
}
