var url = window.location.host;

function funcRegSubmit()
{
    var error = '';
    var check_captcha = false; 
    var check_unique = false;

    $("#check_captcha").html("<font color='red'>verification...</font>");
    
    if ( $("#login").val() == '' )
    {
        error = error + "<font color='red'>Login can't be empty</font><br />";
    }

    if ( $("#password").val() == '' )
    {
        error = error + "<font color='red'>Password can't be empty</font><br />";
    }
    if ( $("#password2").val() == '' )
    {
        error = error + "<font color='red'>Duplicate Password</font><br />";
    }
    if (( $("#password").val() != '' ) && ( $("#password2").val() != '' ) && ($("#password").val() != $("#password2").val()))
    {
        error = error + "<font color='red'>Passwords don't match</font><br />";   
    }

    if ( $("#name").val() == '' )
    {
        error = error + "<font color='red'>Firstname can't be empty</font><br />";
    }
    
    if ( $("#lastname").val() == '' )
    {
        error = error + "<font color='red'>Lastname can't be empty</font><br />";
    }
    
    if( $("#email").val() == '' ) 
    {
        error = error + "<font color='red'>Email Address can't be empty</font><br />";
    }
    else
    {   
        if (!validateEmail($("#email").val()))
        {
            error = error + "<font color='red'>Incorrect e-mail</font><br />"; 
        }
    }
    
    if ($("#is_affiliate:checked").length == 1)
    {
        switch ($("#earn_payment_type option:selected").val())
        {
            case '':
                error = error + "<font color='red'>Choose payment type</font><br />";     
                break;
            
            case 'paypal':
                if ( $("#paypal_email").val() == '' )
                {
                    error = error + "<font color='red'>Paypal email can't be empty</font><br />";
                }
                else
                {   
                    if (!validateEmail($("#paypal_email").val()))
                    {
                        error = error + "<font color='red'>Incorrect paypal e-mail</font><br />"; 
                    }
                }
                break;
                
            case 'cheque':
                if ( $("#cheque_name").val() == '' )
                {
                    error = error + "<font color='red'>Cheque name can't be empty</font><br />";
                }
                if ( $("#cheque_address").val() == '' )
                {
                    error = error + "<font color='red'>Cheque address can't be empty</font><br />";
                }
                break;
        }
    }
    
    if (!$("#agree").attr('checked'))
    {
        error = error + "<font color='red'>You are not able to continue without agreement</font><br />";         
    }
    
    if( $("#imgcode").val() == '' ) 
    {
        error = error + "<font color='red'>Security code can't be empty</font><br />";
        $("#check_captcha").html("");
    }
    else
    {
        $.post(
            "http://"+url+"/check_captcha.php",
            { 
                code: $("#imgcode").val() 
            },
            function( data )
            {          
                if( data == '0' )
                {
                    $("#check_captcha").html("<font color='red'>Security code is incorect</font>");
                    check_captcha = false;
                }
                if (data == '1')
                {
                    //$("#check_captcha").html("<font color='red'>right</font>");
                    $("#check_captcha").html("");
                    check_captcha = true;
                    if ( error == '' )
                    {
                        $.post(
                            "http://"+url+"/admin/index.php",
                            {
                                s:  "ajax", 
                                ev: "checkUserUnique",
                                login: $("#login").val(),
                                status: $("#email").val()
                            },
                            function( data1 )
                            {
                                if( data1 == 1)
                                {      
                                    check_unique = true;
                                    $("#reg_form").submit();    
                                }
                                else
                                {
                                    error = error + "<font color='red'>Such login is already presented in the system</font><br />"; 
                                    $("#errors").html(error);
                                }
                            }
                        );
                        
                    } 
                }
            }
        );
    }
  
    if (( error == '' ) && (check_captcha == true) && (check_unique == true))
    {
        $("#reg_form").submit();
    }
    else
    {
        $("#errors").html(error);
    }
    return false;   
}

function loadRegAddFields(earn_payment_type)
{
    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "loadRegAddFields",
            earn_payment_type: earn_payment_type
        },
        function( data )
        {
            if( data)
            {      
                $("#add_fields_div").html(data);    
            }
        }
    );    
}

function loadRegAddFieldsProfile(earn_payment_type)
{
    $("div[@id^='add_fields_div']").attr('style','display:none');
    $("#add_fields_div_" + earn_payment_type).attr('style','display:block');
}

function togglePaymentTypeDiv()
{
    $("#earn_payment_type_div").toggle();
}

function form_login_subm()
{
    var error = '';
    if( $("#login").val() == '' )
    {
        error = "<font color='red'>Login can't be empty</font><br />";
    }
    
    if( $("#password").val() == '' )
    {
        error = error + "<font color='red'>Password can't be empty</font><br />";
    }

    if( error == '' )
    {
        $("#form_login").submit();
    }
    else
    {
        $("#errors").html(error);
    }
    return false;
}

function form_forgot_subm()
{
    var error = '';
    if( $("#login").val() == '' )
    {
        error = "<font color='red'>Login can't be empty</font><br />";
    }
    
    if( $("#email").val() == '' )
    {
        error = error + "<font color='red'>Email can't be empty</font><br />";
    }

    if( error == '' )
    {
        $("#form_forgot").submit();
    }
    else
    {
        $("#errors").html(error);
    }
    return false;
}



function form_edit_subm()
{
    var error = '';
    
    if (( $("#login").val() == '' ))
    {
        error = error + "<font color='red'>Login can't be empty</font><br />";
    }
    
    if (( $("#name").val() == '' ))
    {
        error = error + "<font color='red'>Firstname can't be empty</font><br />";
    }
    if (( $("#lastname").val() == '' ))
    {
        error = error + "<font color='red'>Lastname can't be empty</font><br />";
    }
    
    if (($("#password").val() != '') && ($("#password2").val() == ''))
    {
        error = error + "<font color='red'>Duplicate password</font><br />";   
    }
    if (( $("#password").val() != '' ) && ( $("#password2").val() != '' ) && ($("#password").val() != $("#password2").val()))
    {
        error = error + "<font color='red'>Passwords don't match</font><br />";   
    }
    
    if( $("#email").val() == '' ) 
    {
        error = error + "<font color='red'>Email Address can't be empty</font><br />";
    }
    else
    {   
        if (!validateEmail($("#email").val()))
        {
            error = error + "<font color='red'>Incorrect e-mail</font><br />"; 
        }
    }
    
    if ($("#is_affiliate:checked").length == 1)
    {
        switch ($("#earn_payment_type option:selected").val())
        {
            case '':
                error = error + "<font color='red'>Choose payment type</font><br />";     
                break;
            
            case 'paypal':
                if ( $("#paypal_email").val() == '' )
                {
                    error = error + "<font color='red'>Paypal email can't be empty</font><br />";
                }
                else
                {   
                    if (!validateEmail($("#paypal_email").val()))
                    {
                        error = error + "<font color='red'>Incorrect paypal e-mail</font><br />"; 
                    }
                }
                break;
                
            case 'cheque':
                if ( $("#cheque_name").val() == '' )
                {
                    error = error + "<font color='red'>Cheque name can't be empty</font><br />";
                }
                if ( $("#cheque_address").val() == '' )
                {
                    error = error + "<font color='red'>Cheque address can't be empty</font><br />";
                }
                break;
        }
    }

    if( error == '' )
    {
        $("#form_edit").submit();
    }
    else
    {
        $("#errors").html(error);
    }
    return false;
}


function form_add_subm()
{
    var error = '';
    var check_unique = false;
    
    if (( $("#login").val() == '' ))
    {
        error = error + "<font color='red'>Login can't be empty</font><br />";
    }  
    
    if (( $("#name").val() == '' ))
    {
        error = error + "<font color='red'>Firstname can't be empty</font><br />";
    }
    
    if (( $("#lastname").val() == '' ))
    {
        error = error + "<font color='red'>Lastname can't be empty</font><br />";
    }
    
    if (( $("#password").val() == '' ))
    {
        error = error + "<font color='red'>Password can't be empty</font><br />";
    }

    if ($("#password2").val() == '')
    {
        error = error + "<font color='red'>Duplicate password</font><br />";   
    }
    if (( $("#password").val() != '' ) && ( $("#password2").val() != '' ) && ($("#password").val() != $("#password2").val()))
    {
        error = error + "<font color='red'>Passwords don't match</font><br />";   
    }
    
    if( $("#email").val() == '' ) 
    {
        error = error + "<font color='red'>Email Address can't be empty</font><br />";
    }
    else
    {   
        if (!validateEmail($("#email").val()))
        {
            error = error + "<font color='red'>Incorrect e-mail</font><br />"; 
        }
    }
    
    if ($("#is_affiliate:checked").length == 1)
    {
        switch ($("#earn_payment_type option:selected").val())
        {
            case '':
                error = error + "<font color='red'>Choose payment type</font><br />";     
                break;
            
            case 'paypal':
                if ( $("#paypal_email").val() == '' )
                {
                    error = error + "<font color='red'>Paypal email can't be empty</font><br />";
                }
                else
                {   
                    if (!validateEmail($("#paypal_email").val()))
                    {
                        error = error + "<font color='red'>Incorrect paypal e-mail</font><br />"; 
                    }
                }
                break;
                
            case 'cheque':
                if ( $("#cheque_name").val() == '' )
                {
                    error = error + "<font color='red'>Cheque name can't be empty</font><br />";
                }
                if ( $("#cheque_address").val() == '' )
                {
                    error = error + "<font color='red'>Cheque address can't be empty</font><br />";
                }
                break;
        }
    }
    
    if ( error == '' )
    {
        $.post(
            "http://"+url+"/admin/index.php",
            {
                s:  "ajax", 
                ev: "checkUserUnique",
                login: $("#login").val(),
                status: $("#email").val()
            },
            function( data1 )
            {
                if( data1 == 1)
                {      
                    check_unique = true;
                    $("#form_add").submit();    
                }
                else
                {
                    error = error + "<font color='red'>Such login is already presented in the system</font><br />"; 
                    $("#errors").html(error);
                }
            }
        );
        
    }

    if (( error == '' ) && (check_unique == true)) 
    {
        $("#form_add").submit();
    }
    else
    {
        $("#errors").html(error);
    }
    return false;
}

function changeUserStatus(id)
{
    var status = 1 - $('#bull_'+id).attr("name");

    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "changeUserStatus",
            id: id,
            status: status
        },
        function( data )
        {
            if( data )
            {
                if (status == 1)
                {
                    $('#bull_'+id).css('color','#00ff00');    
                }
                else
                {
                    $('#bull_'+id).css('color','#ff0000'); 
                }
                $('#bull_'+id).attr("name",status);
            }
        }
    ); 
    return false; 
}

function form_subscribe_subm(unsubscribe)
{
    $.floatbox({
        content: "<p align='center' id='mess_text'><img src='/tpl/vlm/en/images/ajax-loader.gif' /></p>",
        fade: true
    });
   
    $.post(
        "http://"+url+"/admin/index.php",
        {
            s:  "ajax", 
            ev: "newsSubscribeUser",
            unsubscribe: unsubscribe,
            email: $('#email').val(),
            name: $('#name').val()
        },
        function( data )
        {
            if( data )
            {
                $("#mess_text").html(data);
            }
        }
    ); 
    return false;    
}

function search_form_subm(url, uclass)
{
    if ($("#search_val").val()=='')
    {
        //$("#errors").html("<font color='red'>Input search value</font><br />");
        document.getElementById('search_form').action = 'http://' + url + '/admin/viewUsers/' + uclass + '.htm'; 
    }
    else
    {
        document.getElementById('search_form').action = 'http://' + url + '/admin/viewUsers/search/' + $("#search_name").val() + '/' + $("#search_val").val() + '/' + uclass + '.htm'; 
    }
    document.getElementById('search_form').submit(); 
}
    