$(document).ready(function() {
    var dlg = UnauthRFQDialog();
    $('img.rfq_button').click(function(event) {
        dlg.show();
    });
});

var loginDlg;

var UnauthRFQDialog = function() {
    var dialog = new YAHOO.widget.SimpleDialog("alert_dialog",
    {
        width: "400px",
        modal:true,
        fixedcenter: true,
        visible: false,
        draggable: false,
        close: false,
        text: "Please select one of the following options",
        icon: YAHOO.widget.SimpleDialog.ICON_HELP,
        constraintoviewport: true,
        buttons: [
        {
            text:"Cancel",
            handler:handleCancel
        },
        {
            text:"Sign Up",
            handler:handleSignUp
        },
        {
            text:"Login In",
            handler:handleLogin
        }
        ]
    });
    dialog.setHeader("You are not authenticated.");
    dialog.render(document.body);
    return dialog;
};

var handleCancel = function() {
    this.hide();
};

var handleSignUp = function() {
    this.hide();
    var returnPath = window.location.href;
    window.location = '../account/register.php?backtrack='+returnPath;
};

var handleLogin = function() {
    this.hide();
    if (loginDlg==null) {
        loginDlg = loginDialog();
    }
    loginDlg.resetForm();
    loginDlg.show();
};

var loginDialog = function() {
    YAHOO.util.Dom.removeClass('login_panel', "hidden");
    var loginDialog = new YAHOO.widget.Dialog('login_panel',{
        close:false,
        fixedcenter: true,
        dragable: true,
        visible: false,
        modal:true,
        hideaftersubmit:false,
        buttons:[
        {
            text:"Login",
            handler: function() {
                this.submit();
            },
            isDefault:true
        },

        {
            text:"Cancel",
            handler: function() {
                loginDialog.hide();
            }
        }]
    });
    loginDialog.render();
    loginDialog.callback.success = function (response) {
        var result = YAHOO.lang.JSON.parse(response.responseText);
        if (result.success) {
            // reload
            loginDialog.hide();
            infoDialog(result.message,function(){
                window.location.reload();
            });
        } else {
            showErrorDialog(result.message);
        }
    };
    loginDialog.callback.failure = function (response) {
        showResponseMessageDialog(response);
    };
    loginDialog.resetForm = function() {
        document.forms['rfq_login_form'].reset();
    };
    return loginDialog;
};


