$(function(){
    init_page();
})

function init_page()
{
    $('#email_friend_link').click(function() {
        $('#email_to_friend').dialog('open');
        $('.email_friend_detail button').addClass('custom_button').removeClass('ui-corner-all').removeClass('ui-state-default').css('width', '90px').css('height', '21px').css('font-family', 'Arial');
    })

    // dialog
    $("#email_to_friend").dialog({
        bgiframe: true,
        width: 600,
        height: 300,
        modal: true,
        autoOpen: false,
        dialogClass : 'email_friend_detail',
        buttons: {
            'Send': function() {
                var error = false;
                var string_content = '';
                var data = {};

                $('.required_2').each(function(){
                    value = $.trim($(this).val());
                    if(value)
                    {
                        $(this).next().html('');
                        //string_content+= $(this).attr('name')+'='+value+'&';
                        data[$(this).attr('name')] = value;
                    }else{
                        $(this).next().html(' <font color="red">required</font>');
                        error = true;
                    }
                });

                if(error)	return false;

                $('.email').each(function(){
                    value = $.trim($(this).val());
                    if(check_email(value))
                    {
                        $(this).next().html('');
                    }else{
                        $(this).next().html(' <font color="red">*invalid</font>');
                        error = true;
                    }
                });

                if(error)	return false;

                element = $(this);

                $.post( index()+'callback/management.send_email', data,function(html){
                    alert(html);
                    element.dialog('close');
                });
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            $('.required_2').removeClass('ui-state-error');
        }
    });

}

function balance()
{

}


function check_email( email )
{
    if(!/^([a-zA-Z0-9_\.])+\@([a-zA-Z0-9\-])+\.([a-zA-Z0-9]{2,4})(\.[a-zA-Z0-9]{2,4})?$/.test(email))
    {
        return false;
    }
    return true;
}

function link_to(link)
{
    window.location = index()+link;
}