/*
 * Function for login ajax
 */

function loginAjax(formName, from){

    if(formName == null)
        formName = 'login'

    $("#loginMsg").dialog({
        height: 140,
        modal: true,
        autoOpen: false,
        buttons: {
            'Chiudi': function() {

                $("#loginMsg").dialog('close');
            }
        }
    }
    );
    $.ajax({
        type: "POST",
        url: "/Login.jsp",
        data: "loginmode=ajax&" + $("#" + formName).serialize(),
        dataType: "json",
        success: function(itemRes){

            if(itemRes.logged){
              if(from == null)
                document.location.replace('/it/home.jsp?idrub=9086');
              else if(from == 'cart')
                document.location.replace('/it/home.jsp?proc=spedSel&couponId1=' + $('#couponId1').val() + '&SpedType=' + $("input[name=\'SpedType\']:checked").val());
              else if(from == 'cartNoSped')
                 document.location.replace('/it/home.jsp?proc=orderSummary&cartKey=' + $('#cartKey').val() +'&couponId1=' + $("#couponId1").val());
            }else{

                $("#loginMsg").dialog('open');
                $("#loginMsgContent").html( itemRes.msg );
            }
        },
        error: function(msg){
            $("#loginMsg").html("<span id=\"errMsg\">" + msg + "</span>");
        }
    });
}



function loginForm(from){

    var loginF = "<form id=\"loginForm\"><div>Per poter acquistare e' necessario essere registrati. Se non sei registrato clicca su non ho un account.</div><br/><table><tr style=\"padding:3px 3px 3px 3px;\"><td style=\"padding:3px 3px 3px 3px;\">User</td><td style=\"padding:3px 3px 3px 3px;\"><input name=\"username\" id=\"username\" type=\"text\" /></td></tr><tr><td style=\"padding:3px 3px 3px 3px;\">Password</td><td style=\"padding:3px 3px 3px 3px;\"><input name=\"password\" id=\"password\" type=\"password\" /></td></tr></table></form>";
    $("#dialogCartContent").html(loginF);
    $("#dialogCart").dialog('option','buttons',{        
        "NON HO UN ACCOUNT!": function() {
            $(this).dialog("close");
            document.location.replace('home.jsp?proc=registerCustomer');
        },
        "OK": function() {
            loginAjax('loginForm',from);
        }
    } );
    $('#dialogCart').dialog('option', 'height', 200);
    $("#dialogCart").dialog('open');

}



