Moda.Account = {
    
    step1:function(){
        //Ouvrir la modal
        $j.facebox({
            ajax: '/style/creation/'
        });
        
        Moda.Analytics.Track("/login/step1")
    },
    
    step2:function(){
        
        Moda.Analytics.Track("/login/step2")
        
        //Get Data
        email =     $j("input[name=email]").val();
        password =  $j("input[name=password]").val();
        password2 = $j("input[name=confirmation]").val();
        firstname = $j("input[name=firstname]").val();
        lastname =  $j("input[name=lastname]").val();
        
        //Data Validation
        if(!$j("#check_terms").attr('checked')){Moda.Account.error("You have to accept the terms and conditions");return;}
        if(firstname == ""){Moda.Account.error("Please include your firstname");return;}
        if(lastname == ""){Moda.Account.error("Please include your lastname");return;}
        if(email == ""){Moda.Account.error("Please include a valid email address");return;}
        if(password == ""){Moda.Account.error("Please include your password");return;}
        if(password2 == ""){Moda.Account.error("Please confirm your password");return;}

        //post ajax
        Moda.Account.loading.show();
        $j.ajax({
            context:this,
            type:"POST",
            data:{
                email:email,
                password:password,
                password2:password2,
                firstname:firstname,
                lastname:lastname
            },
            dataType:"json",
            success:function(data,status){
                if(data.status == 200){
                    $j("#style_creation_step1").fadeOut(300, function(){
                        $j("#style_creation_step2").fadeIn(300);
                        Moda.Account.loading.hide();
                    })
                }else if(data.status == 504){
                    $j("#creationErrorMsg").html(data.message);
                    $j("#creationErrorMsg").show();
                    Moda.Account.loading.hide();
                }else{
                    alert(data.message)
                    Moda.Account.loading.hide();
                }
            },
            url:"/style/creation/step1post/"
        });  
    },
    
    step3:function(){
        
        Moda.Analytics.Track("/login/step3")
        
        //Get Data
        street1 =       $j("input[name=street1]").val();
        street2 =       ""; //$j("input[name=password]").val();
        city =          $j("input[name=city]").val();  
        country_id =    $j("select[name=country_id]").val();
        region_id =     country_id == "CA" ? $j("select[name=states_id_ca]").val() : $j("select[name=states_id_us]").val();
        postcode =      $j("input[name=postcode]").val();
        telephone =     $j("input[name=telephone]").val();
        
        //Data Validation
        if(street1 == ""){Moda.Account.error("street1");return;}
        if(city == ""){Moda.Account.error("city");return;}
        if(country_id == ""){Moda.Account.error("country_id");return;}
        if(region_id == ""){Moda.Account.error("region_id");return;}
        if(postcode == ""){Moda.Account.error("postcode");return;}

        //post ajax
        Moda.Account.loading.show();
        $j.ajax({
            context:this,
            type:"POST",
            data:{
                street1:street1,
                street2:street2,
                city:city,
                region_id:region_id,
                postcode:postcode,
                country_id:country_id,
                telephone:telephone
            },
            dataType:"json",
            success:function(data,status){
                if(data.status == 200){
                    /*$j("#style_creation_step2").fadeOut(300, function(){
                        $j("#style_creation_step3").fadeIn(300);
                        //Show Confirmation msg!
                    })*/
                    document.location = "/style/intro?n=1";
                    Moda.Account.loading.changeText("Redirecting you to your custom store...");
                }else if(data.status == 504){
                    Moda.Account.error(data.message);
                }else{
                    alert(data.message);
                    Moda.Account.loading.hide();
                }
            },
            url:"/style/creation/step2post/"
        }); 
    },
    
    step3Fast:function(){
        
        Moda.Analytics.Track("/login/step3-skip")
        
        Moda.Account.loading.show();
        Moda.Account.loading.changeText("Redirecting you to your custom store...");
        document.location = "/style/intro?n=1";
    /*$j("#style_creation_step2").fadeOut(300, function(){
            $j("#style_creation_step3").fadeIn(300);
        })*/
    },
    
    loading:{
        _container:"account_creation_loadingmsg",
        show:function(){
            $j("#"+this._container).show();
            $j("#creationErrorMsg").hide();
        },
        hide:function(){
            $j("#"+this._container).hide();
        },
        changeText:function(text){
            $j("#"+this._container).html(text);
        }
    },
    
    error:function(text){
        $j("#creationErrorMsg").html(text);
        $j("#creationErrorMsg").show();
        Moda.Account.loading.hide();
    }
};
