var userArr = Array();
userArr['user_name'] = false;
userArr['user_email'] = false;
userArr['user_passwd'] = false;
userArr['user_repasswd'] = false;
userArr['data'] = false;

function trim( str ) {
        return str.replace(/^\s+|\s+$/g,"");
}

function validEmail( email )
{
        var pattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;
        return pattern.test(email);
}

function validPassword( password )
{
        var pattern = /[^\w\-\,\.\;\:]/;
        if(password.length<6) {
                return false;
        } else if( pattern.test(password) ) {
                return false;
        }
        return true;
}


function saveTransInfo( userId )
{
	var text = $('#trans_info').attr('value');
	if( text.length>3 ) {
		$.post(
				'/ajax/savetransinfo/',
				{
					userid: userId,
					text: text
				},
				afterSaveTransInfo
		);
	} else {
		alert("Empty info");
	}
	
	return false;
}
function afterSaveTransInfo(data)
{
	alert("Info is save");
}

function setStatusRegButton()
{
	if( $('#aligment').attr('checked') && userArr['user_nickname'] && userArr['user_email'] && userArr['user_passwd'] && userArr['user_repasswd'] && userArr['data'] ) {
		$('#regButton').attr("disabled", "" );
	} else {
		$('#regButton').attr("disabled", "disabled" );
	}
}

function setStatusRegButtonEdit()
{
	if(  userArr['user_passwd'] && userArr['user_repasswd'] && trim($('#user_passwd').attr("value")).length>0 ) {
		$('#regButton').attr("disabled", "" );
	} else {
		$('#regButton').attr("disabled", "disabled" );
	}
}

function changeIco(fld, res) {
	userArr[fld] = res;
    if (res == true) {
        $('#'+fld+'_check_img').attr( "src", "/images/true.gif" );
    } else {
        $('#'+fld+'_check_img').attr( "src", "/images/false.gif" );
    }
}
function checkValue( fld )
{
	var curValue = trim($('#'+fld).attr("value"));

	switch( fld ) {
    case 'user_nickname' :
            if(curValue.length>2) {
                    changeIco( fld, true );
                    $('#'+fld+'_mes').html("");
            } else {
                    changeIco( fld, false );
                    $('#'+fld+'_mes').html("Empty Value");
            }
    break;
    case 'user_email' :
        if( validEmail( curValue ) ) {
                $.post(
                                '/ajax/validemail',
                                {
                                        useremail: curValue
                                },
                                ajaxValidEmail
                );
        } else {
                changeIco( fld, false );
                $('#'+fld+'_mes').html("Not valid Email");
        }
    break;
    case 'user_passwd' :
        if( validPassword(curValue) ) {
        	changeIco( fld, true );
        	$('#'+fld+'_mes').html("");
        } else {
        	changeIco( fld, false );
        	$('#'+fld+'_mes').html("Not valid password");
        }
        checkValue( 'user_repasswd' );
        break;
    case 'user_repasswd' :
        var pass = $('#user_passwd').attr("value");
        if( curValue==pass && curValue.length>0) {
        	changeIco( fld, true );
        	$('#'+fld+'_mes').html("");
        } else {
        	changeIco( fld, false );
        	$('#'+fld+'_mes').html("Password mismatch");
        }
    break;    
    }
	setStatusRegButton();
}
function checkValueEdit( fld )
{
	var curValue = trim($('#'+fld).attr("value"));

	switch( fld ) {
    case 'user_passwd' :
        if( curValue.length==0 || validPassword(curValue) ) {
        	changeIco( fld, true );
        	$('#'+fld+'_mes').html("");
        } else {
        	changeIco( fld, false );
        	$('#'+fld+'_mes').html("Not valid password");
        }
        checkValue( 'user_repasswd' );
        break;
    case 'user_repasswd' :
        var pass = $('#user_passwd').attr("value");
        if( curValue==pass && curValue.length>0 || curValue==pass && curValue.length==0) {
        	changeIco( fld, true );
        	$('#'+fld+'_mes').html("");
        } else {
        	changeIco( fld, false );
        	$('#'+fld+'_mes').html("Password mismatch");
        }
    break;    
    }
	setStatusRegButtonEdit();
}
function ajaxValidEmail( data )
{
    if( parseInt(data)>0 ) {
        $('#user_email_mes').html("Email already in use");
        changeIco('user_email', false);
    } else {
        $('#user_email_mes').html("");
        changeIco('user_email', true);
    }
	setStatusRegButton();
}
function checkValueSelect( )
{
	if( $('#user_birthday_day').attr("value")>0 && $('#user_birthday_month').attr("value")>0 && $('#user_birthday_year').attr("value")>0 ) {
		$('#data_check_mes').html("");
		changeIco('data', true);
	} else {
		$('#data_check_mes').html("Select Birthday date");
		changeIco('data', false);
	}
	setStatusRegButton();
}
function startCheckEdit()
{
	checkValueEdit('user_passwd');
	checkValueEdit('user_repasswd');
}
function startCheck()
{
	checkValue("user_nickname");
	checkValue("user_email");
	checkValue('user_passwd');
	checkValue('user_repasswd');
	checkValueSelect();
}
function readAligment()
{
	$('#aligment_text').css('display','block');
}
function closeAligment()
{
	$('#aligment_text').css('display','none');
}
function rateTranslation( tranId, value )
{
	$.post(
            '/ajax/ratetranslation',
            {
                    tranid: tranId,
                    rate: value
            },
            ajaxRateTranslation
			);
	$('input.star').rating('readOnly',true);
}
function ajaxRateTranslation()
{
	alert("You voted!");
}
function checkPassword()
{
	var password = $('#user_passwd').val();
	var repassword = $('#user_repasswd').val();
    var pattern = /[^\w\-\,\.\;\:]/;

	if( password.length<6  ) {
		$('#user_error_message').html("Short password, min of 6 characters");
		return false;
	} else if( pattern.test( password )) {
		$('#user_error_message').html("Password contains illegal characters");
	} else if ( password!=repassword ) {
		$('#user_error_message').html("Passwords do not match");
	}
	return true;
}
function checkForm( type )
{
	var errorText = "";
	
	var nickname = $('#user_nickname').val();
	if( nickname.length<3 ) {
		errorText += "Empty value 'Screenname'<br>";
	}

	var day = $('#user_birthday_day').val();
	var mon = $('#user_birthday_month').val();
	var year = $('#user_birthday_year').val();
	if( day<1 || day>31 || mon<1 || mon>12 || year <1900 || year>2020 ) {
		errorText += "Incorrect value 'Birthday'";
	}
	
	if( errorText != "" ) {
		$('#user_error_message').html( errorText+'&nbsp;' );
		return false;
	}
	return true;
}
function showMoney()
{
	$('#addMoney').css('display','block');
}

