function hideCompany(company){
    if (company == 'p') {
        document.getElementById('company').value = '';
        document.getElementById('company').disabled = true;
        document.getElementById('company').style.border = '1px solid #D3D3D3';
    }
    else {
        document.getElementById('company').disabled = false;
    }
}

function toggle_visibility(id, baseurl, imgId, spanId){
    if (document.getElementById(id).style.display == "block") {
        document.getElementById(id).style.display = "none";
        document.getElementById(imgId).src = baseurl + "arrow_down.gif";
        document.getElementById(spanId).innerHTML = i18n.expand;
    }
    else {
        document.getElementById(id).style.display = "block";
        document.getElementById(imgId).src = baseurl + "arrow_up.gif";
        document.getElementById(spanId).innerHTML = i18n.collapse;
    }
}

function isEmail(email) // private function
{
    var emailRegex = new RegExp("^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z_-]+\\.)+[a-z]{2,4}$");
    if (emailRegex.test($.trim(email))) {
        return true;
    }
    else {
        return false;
    }
}

function checkEmailField(field){
	if (!isEmail($(field).val())) {
		$(field).addClass('error');
		return false;
	}
	else {
		$(field).removeClass('error');
		return true;
	}
}
	
function checkRequiredField(field) {
    if ($.trim($(field).val()) === "") {
		$(field).addClass('error');
        return false;
    } else {
		$(field).removeClass('error');
		return true;
	}	
}

function formSubmit(){

 	var validated = checkRequiredField('#first_name');
	validated = checkRequiredField('#last_name') && validated;
	validated = checkRequiredField('#email_address') && validated;
	validated = checkEmailField('#email_address') && validated;
	
    if (!validated) {
        $('#reg_error').html(i18n.registererror);
    }
	return validated;
}

/////////////////function for make readonly textfield///////////////////////////////
function changeReadText(){
    if (document.getElementById('bitte_geben_sie_ihren').value == "privat") {
        document.getElementById('bitte_geben_sie').readOnly = true;
        document.getElementById('bitte_geben_sie').style.background = '#d3d3d3';
    }
    else {
        document.getElementById('bitte_geben_sie').readOnly = false;
        document.getElementById('bitte_geben_sie').style.background = '';
    }
}

//////////////////////////function for validate login form//////////////////////////

function validateLogin(){
    var validated = checkRequiredField('#login');
	validated = checkEmailField('#login') && validated;
	validated = checkRequiredField('#password') && validated;

	if(!validated) {
		$('#message').html(i18n.loginfailed);
		$('#message').css('color','red');
	}
	return validated;
}

//////////////////////////function for validate contact form//////////////////////////

function validateContact(){
	var validated = checkRequiredField('#email');
	validated = checkEmailField('#email') && validated;
	validated = checkRequiredField('#first_name') && validated;
	validated = checkRequiredField('#last_name') && validated;
	validated = checkRequiredField('#textmessage') && validated;
    
	if(!validated) {
		$('#contact_error').html(i18n.contacterror);
	}
	return validated;
}


function deleteUser()
{
    if(!confirm(confirmdeleteakk)) return false;

    $.ajax({
        type: "GET",
        url: "/auth/delete",
        data: 'act=first',
        success: function(result)
        {
            $("#msg1").css('display', 'inline');
            $("#msg2").css('display', 'inline');
            if(result == 'ok') $('#message').html('<p style="color:white;">'+deletemailsent+'</p>');
            else $('#message').html('<p style="color:red;">'+dberror+'</p>');
        }
    });
    return true;
}

function forgot()
{
    var email = $('#forgot_email').val();
    if(!isEmail(email))
    {
        $('#message').html('<p style="color:red;">'+emailinvalid+'</p>');
        return false;
    }
    $.getJSON(
        '/auth/checkemailajax',
        { 
            email: email
        },
        function(result)
        {
            if (result == 'avaliable')
            {
                $('#message').html('<p style="color:red;">'+emailisntfound+'</p>');
            }
            else
            {
                $.ajax({
                    type: "POST",
                    url: "/auth/forgot",
                    data: 'login=' + email,
                    success: function(result)
                    {
                        if(result == 'ok')
                        {
                            $('#message').html('<p style="color:white;">'+forgotemailsent+'</p>');
                            setTimeout("window.location.href = baseurl", 2000);
                        }
                        else $('#message').html('<p style="color:red;">'+emailisntfound+'</p>');
                    }
                });                
            }
        }
        );
    return true;
}
