﻿/// <reference path="scripts.htm" />

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Copyright (c) 2008-2009 OrderBot Software, Inc. All rights reserved.
// orderbot.myaccount.register.js
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.createNamespace('orderbot.myaccount.register');
Object.extend(orderbot.myaccount.register, {
    div: null,
    info: null,
    loadingImg: { name: 'ajax-loading-bg-black.gif', w: 16, h: 16 },
    validateAddress: false,
    addressDialog: null,

    init: function() {
        this.div = $('account-update');
        this.info = this.div.down('span#info');

        var context = this;

        var btn = this.div.down('input[id$=btnSubmit]');
        btn.observe('click', function(e) {
            if (Page_ClientValidate('Register')) { orderbot.toogleAjaxButton(e.element(), true, context.loadingImg); }
        });

        orderbot.goToNext('txtPhoneArea', 'txtPhone1', 3, this.div);
        orderbot.goToNext('txtPhone1', 'txtPhone2', 3, this.div);
        orderbot.goToNext('txtPhone2', 'txtPhoneExt', 4, this.div);

        var ddl = this.div.down('SELECT[id$=ddlCountries]');
        var hasStates = orderbot.countryHasStates($F(ddl));

                ddl.observe('change', function(e) {
                    var element = e.element();
                   var container = element.up('div#account-update');
                    if (!orderbot.countryHasStates(element.value)) {
                        orderbot.togglePhoneState(container, false);
                    } else {
                        orderbot.togglePhoneState(container, true);
                        orderbot.populateStates(container, element.value);
                    }
               });

//        var context = this;

//        ddl.observe('change', function(e) {
//            var countryId = e.element().value;
//            var hasStates = orderbot.countryHasStates(countryId);
//            if (!hasStates) { orderbot.togglePhoneState(context.div, false); }
//            else { orderbot.togglePhoneState(context.div, true); orderbot.populateStates(context.div, countryId); }
//        });

        orderbot.togglePhoneState(this.div, hasStates);
    }
});

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Copyright (c) 2008-2009 OrderBot Software, Inc. All rights reserved.
// orderbot.common.js
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Object.createNamespace('orderbot.myaccount.changepassword');
Object.extend(orderbot.myaccount.changepassword, {

    div: null,
    info: null,
    btnSubmit: null,
    loadingImg: { name: 'ajax-loading-bg-black.gif', w: 16, h: 16 },

    init: function() {
        this.div = $('account-update');
        this.info = this.div.down('span#info');
        this.btnSubmit = this.div.down('input[id$=btnSubmit]');
        //set submit observer
        this.btnSubmit.observe('click', function(e) {
            e.stop(); 
            if (Page_ClientValidate('ChangePassword')) { this.changePassword(); }
        } .bind(this));
    },
    
    changePassword: function() {
        var args = {
            currentPassword: this.div.down('input[id$=txtCurrentPassword]').value.strip().escapeHTML(),
            newPassword: this.div.down('input[id$=txtNewPassword]').value.strip().escapeHTML()
        };
        var context = this;
        orderbot.toogleAjaxButton(this.btnSubmit, true, this.loadingImg);
        
        CustomersHandler.ChangePassword(args, function(response) {
            orderbot.toogleAjaxButton(context.btnSubmit);
            if (response.error) { alert(response.error.Message); return; }
            orderbot.messages.showMessage('Password Changed.');
        })
    }
});
