/**
 * Magento/scicom
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author    Ng Kiat Siong, Celera eShop, kiatsiong.ng@malayakisses.com 
 *   
 */

// tracking AJAX
var Track = Class.create();
Track.prototype = {
    initialize: function(formId, successUrl, saveUrl){
        this.form = $(formId);
        if (this.form) {
            this.form.observe('submit', function(event){this.submitEvent();Event.stop(event);}.bind(this));           
        } else {
            alert('Form ' + formId + ' is null!');
            return;
        }       
        this.successUrl = successUrl;
        this.saveUrl = saveUrl;
        //this.onSave = this.nextStep.bindAsEventListener(this);
        //this.onComplete = this.nextStep.bindAsEventListener(this);
        this.loadWaiting = false;
        this.onSave = this.nextStep.bindAsEventListener(this);
        this.onComplete = this.resetLoadWaiting.bindAsEventListener(this);
    },

  submitEvent: function(){
        if (this.loadWaiting!=false) return;
        var validator = new Validation(this.form);
        Validation.add('validate-awb', 'Please use 9 to 12 numbers in this field.', 
           {minLength : 9,
            maxLength : 12,
            include : ['validate-digits']});
        if (validator.validate()) {
            var request = new Ajax.Request(
                this.saveUrl,
                {
                    method: 'post',
                    onSuccess: this.onSave,
                    onComplete: this.onComplete,
                    parameters: Form.serialize(this.form)
                }
            );
            this.setLoadWaiting('track');
        }
    },

    nextStep: function(transport){
        if (transport && transport.responseText){
            try{
                response = eval('(' + transport.responseText + ')');
            }
            catch (e) {
                response = {};
            }
        }
        /*
        * if there is an error in backend validation, show error message
        */
        if (response.error) {            
            alert(response.field);
        } else {
        window.location=this.successUrl + '?AWBNumber=' + response.field + '&time=' + response.time;
        }       
    },
    
    resetLoadWaiting: function(transport){
        this.setLoadWaiting(false);
    },
    
    _disableEnableAll: function(element, isDisabled) {
        var descendants = element.descendants();
        for (var k in descendants) {
            descendants[k].disabled = isDisabled;
        }
        element.disabled = isDisabled;
    },
    
    setLoadWaiting: function(step, keepDisabled) {
        if (step) {
            if (this.loadWaiting) {
                this.setLoadWaiting(false);
            }
            var container = $(step+'-buttons-container');
            container.setStyle({opacity:.5});
            this._disableEnableAll(container, true);
            Element.show(step+'-please-wait');
        } else {
            if (this.loadWaiting) {
                var container = $(this.loadWaiting+'-buttons-container');
                var isDisabled = (keepDisabled ? true : false);
                if (!isDisabled) {
                    container.setStyle({opacity:1});
                }
                this._disableEnableAll(container, isDisabled);
                Element.hide(this.loadWaiting+'-please-wait');
            }
        }
        this.loadWaiting = step;
    }
} //Track
