/*jslint browser: true, cap: false, glovar: true, passfail: false, undef: false, white: false */
/*global SONGHAY YAHOO */

(function(){

    //Module-level members:
    var client = SONGHAY.ui.Client;
    var dom = SONGHAY.ui.Dom;

    SONGHAY.ui.Client.dataForm =
    {
        alert:function(type,args)
        {
            if(args.length){} else { return; }

            if(args.length > 1)
            {
                client.alertFocus = args[1];

                dom.alert(client.applicationTitle,
                    args[0], self.alertProcedure);
            }
            else
            {
                dom.alert(client.applicationTitle,
                    args[0], self.alertProcedure);
            }
        },

        alertFocus:null,

        alertProcedure:function()
        {
            if(this.hide) { this.hide(); }

            if(client.alertFocus && client.alertFocus.focus)
            {
                client.alertFocus.focus();
            }
        },

        loadAppSettings:function()
        {
            var vSuccess = false;

            client.appSettings = YAHOO.util.Dom.get('AppProperties');
            if(client.appSettings)
            {
                vSuccess = true;
            }

            return vSuccess;
        },

        validate:function(e)
        {
            var oUI = client.ui;

            var o = YAHOO.util.Event.getTarget(e);

            var oForm = o.form ? o.form : null;

            if(oForm && (oForm.name == 'frmEmail'))
            {
                if (oUI.DataValidation.isNotComplete(oForm.realname, 'Name')) { return; }
                if (oUI.DataValidation.isEmailValid(oForm.name,window.document.forms[oForm.name].email) === false) { return; }

                var vType = (o.id && (o.id == 'SendMail'))? 'php' : 'html';

                switch(vType){
                    case'html':
                        oForm.redirect.value += '?' + window.document.referrer;
                    break;

                    case'php':
                        oForm.redirect.value += '=' + window.document.referrer;
                    break;
                }

                oForm.submit();
                return;
            }

            if(oForm && (oForm.name == 'frmSearch'))
            {
                if (oUI.DataValidation.isNotComplete(oForm.txtSearchPhrase, 'Search Phrase')) { return; }
                oForm.submit();
                return;
            }
        }
    };

    //Module-level "this":
    var self = client.dataForm;

    client.ui.DataValidation.onMessage.subscribe(self.alert);

})();
