var Wizard = {
    /**
     * @var object to hold the json made object with xmk data send from the index
     */
    selectionData : {},

    /**
     * set all required but empty fields to red
     * all valid -> submit
     */
    startContact: function()
    {
        $$("table.evenodd tr").each(function(elem,idx){
            if (idx%2) elem.setStyle('background-color','#EEE');
        });
        if($("edit-submit"))
        {
            $("edit-submit").addEvent('click',Wizard.save);
        }
        
        /*$$("form-text").each(
            function(el,i){
                el.addEvent('click',Wizard.checkFields);
                el.addEvent('change',Wizard.checkFields);
                el.addEvent('keypress',Wizard.checkFields);
                el.addEvent('keyup',Wizard.checkFields);
                el.addEvent('keydown',Wizard.checkFields);
            }
        );*/
    },
    
    save:function(e)
    {
        if(Wizard.checkFields(e)) {
            (function(){
                $('contactform').submit();
            }).delay(450);
        }
    },
    
    checkFields: function(e)
    {
        var valid = 1;

        $('firstname').removeClass('error');
        $('lastname').removeClass('error');
        $('country').removeClass('error');
        $('phone').removeClass('error');
        $('companyaddress').removeClass('error');
        $('companyname').removeClass('error');
        $('email').removeClass('error');

        if($('firstname').getProperty("value") == '')
        {
            valid = 0;
            $('firstname').addClass('error');
            $('consoleFirstNameError').setStyle('display', 'block');
        }

        if ($('lastname').getProperty("value") == '')
        {
            valid = 0;
            $('lastname').addClass('error');
            $('consoleLastNameError').setStyle('display', 'block');
        }

        if ($('country').getProperty("value") == '--')
        {
            valid = 0;
            $('country').addClass('error');
            $('consoleCountryError').setStyle('display', 'block');
        }

        if ($('phone').getProperty("value") == '')
        {
            valid = 0;
            $('phone').addClass('error');
            $('consolePhoneError').setStyle('display', 'block');
        }

        if ($('companyaddress').getProperty("value") == '')
        {
            valid = 0;
            $('companyaddress').addClass('error');
            $('consoleCompanyAddressError').setStyle('display', 'block');
        }

        if ($('companyname').getProperty("value") == '')
        {
            valid = 0;
            $('companyname').addClass('error');
            $('consoleCompanyNameError').setStyle('display', 'block');
        }

        if(!$('email').getProperty("value").test("^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$",'i'))
        {
            valid = 0;
            $('email').addClass('error');
            $('consoleEmailError').setStyle('display', 'block');
        }
        
        if(!valid) {
            $('consoleUl').setStyle('display', 'block');
             window.scrollTo(0, 0);
        }

        return valid
    },

    /**
     * calculate the string length in the target field
     * cut it if needed
     * write the current length
     * @var object pField
     * @var integer maximal text length
     */
    checkLen: function(pField, textLength)
    {
        if($(pField).getProperty('value').length > textLength) {
            $(pField).setProperty('value', $(pField).getProperty('value').substr(0, textLength));
        }

        $(pField + 'anzeige').set('html', $(pField).getProperty('value').length);
    },
    
    resetCounter: function(pField)
    {
        $(pField + 'anzeige').set('html', 0);
    },

    /**
     * catch the target objects id to figure out the used section
     * call change picture
     * @var object pTargetField
     * @return false
     */
    getSelection: function(pTargetField)
    {
        var aTmpSelection = pTargetField.id.split('_');
        this.selectionData[aTmpSelection[0]] = aTmpSelection[1];
        this.changePicture();
        return false;
    },

    /**
     * walk the selectionData object
     * walk the chosen values
     * fade all in
     * if not ok, fade targeted out
     */
    changePicture: function()
    {
        this.closeOverlay();
        for(var prxName in this.selectionData.devices) {
            $(prxName).fade(1);
            var accepted = false;
            for(var selectionSection in this.selectionData.devices[prxName]) {
                if(this.selectionData[selectionSection] == null) {
                    continue;
                }
                var xValue = this.selectionData.devices[prxName][selectionSection].value;
                if(typeof(xValue) == 'object') {
                    for(var i=0; i < xValue.length; ++i) {
                        if(xValue[i] == this.selectionData[selectionSection] || this.selectionData[selectionSection] > 0 && this.selectionData[selectionSection] <= xValue[i]) {
                            accepted = true;
                        }
                    }
                } else {
                    if(xValue == this.selectionData[selectionSection] || this.selectionData[selectionSection] > 0 && this.selectionData[selectionSection] <= xValue[i]) {
                        accepted = true;
                    }
                }
                if(!accepted) {
                    $(prxName).fade(0.2);
                    break;
                } else {
                    accepted = false;
                }
            }
        }
    },

    /**
     * create a mocha object to show the description
     * @var object pTargetObect
     * @var string sLayoutPath
     */
    showDescription: function(pTargetObect, sLayoutPath)
    {
        this.closeOverlay();
        new MUI.Modal({
            id             : "changeMapModal",
            title          : "Module: "+pTargetObect.name.toUpperCase(),
            contentURL     : '/index/showPrx/context/html/name/'+pTargetObect.name+'/layoutPath/'+sLayoutPath,
            loadMethod     : 'xhr',
            type           : "modal",
            icon           : sLayoutPath+'icons/constraining-fixed.png',
            width          : 600,
            height         : 490,
            scrollbars     : true,
            closable       : true,
            draggable      : true,
            restrict: false,
            bodyBgColor    : ['255','255','255'],
            onContentLoaded: function() {
                MochaUI.dynamicResize($("changeMapModal"));
            }
        });
    },

    /**
     * catch the only window and close it
     */
    closeOverlay: function()
    {
        if($("changeMapModal")) {
            $("changeMapModal").parentNode.removeChild($("changeMapModal"));
        }
    },

    /**
     * close the overlay
     * set result with the sended value
     * submit the form
     * @param string sResult
     * @return false
     */
    loadContacts: function(sResult)
    {
        this.closeOverlay();
        $('moduleResult').value = sResult;
        $('preSelectForm').submit();
        return false;
    },

    /**
     * catch all inputs that seems to be from use
     * make your selection on that inputs if they are checked.
     */
    preloadSelection: function()
    {
        $$('input').each(
            function(el,i){
                if(el.getAttribute('class') == 'selection' && el.getAttribute('checked')) {
                    Wizard.getSelection(el);
                }
            }
        );

    }
};

