var login = {
    window : null,
    action : null,
    open : function(action){
        if(!login.window){
            login.window = new YAHOO.widget.Panel(
                'w_login',  
                {
                    width:"290px",
                    y: 300,
                    fixedcenter:false, 
                    close:false, 
                    draggable:false, 
                    zindex:101,
                    modal:true,
                    visible:false,
                    underlay:'none',
                    constraintoviewport: true
                }
            );
        }
        login.action = action;
        YAHOO.util.Connect.asyncRequest('GET', loginUrl + login.action, login.callback, '');
        return false; /* Don't follow link */
    },
    
    callback : {
        success: function(o) {
            hide_selects();
            var win = login.window;
            var content = o.responseText.split("<SPLIT>")[1];
            win.setBody(content);
            win.render(document.body);
            win.show();
            
            /* Underlay/Mask closes when it's clicked */
            var underlay = document.getElementById('w_login_mask');
            if(underlay)
                YAHOO.util.Event.addListener(underlay, "click", function(){win.setBody('');show_selects();login.window.hide();}); 
            else
                alert("Mask doesn't exit");  
        },
        failure: function (o) {
            login.window.hide();
        }
    },
    
    close : function(){
        login.window.setBody('');
        show_selects();
        login.window.hide();
        return false;
    },
    
    submit : function (form){
        var elements = form.elements;
        var len = elements.length;
        var postData = '';
        if(login.action == "register")
            var phone = elements['billing[phone_area]'].value+'-'+elements['billing[phone_prefix]'].value+'-'+elements['billing[phone_suffix]'].value;
        var reg = new RegExp("phone");
        for(var i = 0; i < len; i++)
            if(!reg.test(elements[i].name))
                postData += "&"+elements[i].name+"="+elements[i].value;
        if(login.action == "register")
            postData += "&billing[phone]="+phone;
        YAHOO.util.Connect.asyncRequest('POST',"/account/"+login.action,login.formCallback, postData);
        return false; /* Don't follow link */
    },
    
    formCallback : {
        success: function(o) {
            var win = login.window;
            var responses = o.responseText.split('<SPLIT>');
            if(responses[0] == "granted" && login.action == "signin"){
                login.window.hide();
                document.getElementById('link_signin').innerHTML = responses[1];
                window.location.replace( unescape(window.location.pathname) ); //refresh
                return false;
            }            
            var body = win.setBody(responses[1]);
            
            // if we have errors, show them
            // if (responses.length == 3) {
            //     body = responses[2] + responses[1];
            // };
            // win.setBody(body);
            win.render(document.body);
            win.show();

            /* Underlay/Mask closes when it's clicked */
            var underlay = document.getElementById('w_login_mask');
            if(underlay)
                YAHOO.util.Event.addListener(underlay, "click", function(){login.window.hide();}); 
            else
                alert("Mask doesn't exit");  
        },
        failure: function (o) {
            login.window.hide();
        }
    },
    
    doyoureallywanttoquit : function() {
        var postData = "";
        other_this = this;
        var callback = {
            success: function(o) {
                if (o.responseText.match('false') == null) {
                    other_this.logout();
                }else{
                    other_this.showExitWarning();
                }
            },
            failure: function (o) {}
        };

        YAHOO.util.Connect.asyncRequest(
            'POST',
            '/account/prepareLogout',
            callback,
            postData);
    },
    
    showExitWarning: function(){
        // while we get the txt from the ltd
        this.logout();
        return;
        // **
        if (typeof warning != 'undefined'){warning.show();return;}
        
        var msg     = '<div>Are you really want to quit? Your cart will be erased.';
        var buttons = '<a href="#" onclick="warning.hide();">I want to save my cart</a> |';
        buttons    += '<a href="#" onclick="login.logout();">Log me out</a></div>';
        warning = new YAHOO.widget.Panel("warning", {
            width:"400px", 
            fixedcenter: true, 
            constraintoviewport: true, 
            underlay:"none", 
            close:false, 
            visible:false, 
            draggable:false,
            modal: true 
        });
        
        var body = msg + '<br>' + buttons;
        
        warning.setBody(body);
        warning.render(document.body);
        warning.show();
        
        var underlay = document.getElementById('warning_mask');
        if(underlay)
            YAHOO.util.Event.addListener(underlay, "click", function(){warning.hide();});
    }, 
    
    logout: function(){
        window.location = '/account/logout';
    } 
};

function prepareData(iForm){
    var elem = iForm.elements;
    var iPh = document.createElement("INPUT");
    iPh.type = "hidden";
    iPh.name = "billing[phone]";
    iPh.value = elem['billing[phone_area]'].value+'-'+elem['billing[phone_prefix]'].value+'-'+elem['billing[phone_suffix]'].value;
    iForm.appendChild(iPh);
    return true;
}

function clean_phone(obj){
    return;
}

function next_if_full(){
    return;
}
