/*
 * Web Genie Magic
 *
 */
var WGM  = {
    required_styles: [
    'http://yui.yahooapis.com/2.7.0/build/container/assets/skins/sam/container.css',
    'http://yui.yahooapis.com/2.7.0/build/button/assets/skins/sam/button.css'
    ],
    required_scripts: [
    'http://yui.yahooapis.com/2.7.0/build/animation/animation-min.js',
    'http://yui.yahooapis.com/2.7.0/build/json/json-min.js',
    'http://yui.yahooapis.com/2.7.0/build/connection/connection-min.js',
    'http://yui.yahooapis.com/2.7.0/build/element/element-min.js',
    'http://yui.yahooapis.com/2.7.0/build/button/button-min.js',
    'http://yui.yahooapis.com/2.7.0/build/dragdrop/dragdrop-min.js',
    'http://yui.yahooapis.com/2.7.0/build/container/container-min.js',
    'http://yui.yahooapis.com/2.7.0/build/selector/selector-min.js',
    'http://yui.yahooapis.com/2.7.0/build/cookie/cookie-min.js'
    ],
    init: function() {
        var i;
        for (i=0;i<WGM.required_styles.length;i++) {
            Webolution.Utils.loadStyleSheet(WGM.required_styles[i]);
        }
        Webolution.Utils.loadScript('http://yui.yahooapis.com/2.7.0/build/yahoo-dom-event/yahoo-dom-event.js');
        

        Webolution.Utils.onClassAvailable(['YAHOO.util.Event','YAHOO.util.Dom'], function() {
            for (i=0;i<WGM.required_scripts.length;i++) {
                Webolution.Utils.loadScript(WGM.required_scripts[i]);
            }
            var body = document.getElementsByTagName('body')[0];
            YAHOO.util.Dom.addClass(body, 'yui-skin-sam');
            Webolution.Utils.onClassAvailable('YAHOO.widget.SimpleDialog',function() {
                // Define various event handlers for Dialog
                var handleOK = function() {
                    this.hide();
                };
                // Instantiate the Dialog
                WGM.okDialog =
                new YAHOO.widget.SimpleDialog("OKDialog",
                {
                    width: "300px",
                    fixedcenter: true,
                    visible: false,
                    draggable: false,
                    modal:true,
                    close: true,
                    text: "Do you want to continue?",
                    icon: YAHOO.widget.SimpleDialog.ICON_HELP,
                    constraintoviewport: true,
                    buttons: [ {
                        text:"OK",
                        handler:handleOK,
                        isDefault:true
                    } ]
                } );
                WGM.okDialog.setHeader('dialog');
                WGM.okDialog.render(document.body);
            });
        });
    },
    sendFormInit: function() {
        var form = document.getElementById('checkFRM');
        var prevfunction = form.onsubmit;
        var callback =
        {
            success: function(o) {
                document.getElementById('contact-form-submit-buttom').disabled = false;
                if (o.responseText=='-1') {
                    var c = document.getElementById('form-cacptcha-image');
                    var ci = document.getElementById('form-cacptcha-input');
                    c.src = c.src;
                    ci.value = '';
                    WGM.okDialog.setHeader('Error!');
                    WGM.okDialog.setBody('Invalid captcha code!');
                    WGM.okDialog.show();
                }
                if (o.responseText=='0') {
                    
                    WGM.okDialog.setHeader('Form sent!');
                    WGM.okDialog.setBody('Your data has been submitted successfully!');
                    WGM.okDialog.show();
                }
            },
            failure: function(o) {
                document.getElementById('contact-form-submit-buttom').disabled = false;
            },
            cache: false
        };
        form.onsubmit = function() {
            if (prevfunction()) {
                YAHOO.util.Connect.setForm(form);
                var cObj = YAHOO.util.Connect.asyncRequest('POST', 'features/form/send-form.php?id=1&PHPSESSID='+encodeURIComponent(YAHOO.util.Cookie.get("PHPSESSID")), callback);
                document.getElementById('contact-form-submit-buttom').disabled = true;
            } else {
            //do nothing
            }
            return false;
        };

    },
    simpleDialog: function(content,title) {
        if (title==null) {
            title = 'Dialog';
        }
        WGM.okDialog.setHeader(title);
        WGM.okDialog.setBody(content);
        WGM.okDialog.show();
    },
    recommendFormInit: function() {
        var form = document.getElementById('tellafriend');
        var prevfunction = form.onsubmit;
        var callback =
        {
            success: function(o) {
                document.getElementById('recommend-submit-buttom').disabled = false;
                var c = document.getElementById('security_image');
                var ci = document.getElementById('security_code');
                c.src = c.src;
                ci.value = '';
                WGM.okDialog.setHeader('Message');
                WGM.okDialog.setBody(o.responseText);
                WGM.okDialog.show();
            },
            failure: function(o) {
                document.getElementById('recommend-submit-buttom').disabled = false;
            },
            cache: false
        };
        form.onsubmit = function() {
            if (prevfunction()) {
                YAHOO.util.Connect.setForm(form);
                var cObj = YAHOO.util.Connect.asyncRequest('POST', 'recommend.php', callback);
                document.getElementById('recommend-submit-buttom').disabled = true;
            } else {
            //do nothing
            }
            return false;
        };

    }
};


function IsIE8Browser() {

    var rv = -1;

    var ua = navigator.userAgent;

    var re = new RegExp("Trident\/([0-9]{1,}[\.0-9]{0,})");

    if (re.exec(ua) != null) {

        rv = parseFloat(RegExp.$1);

    }

    return (rv == 4);

}
if (IsIE8Browser()) {
    window.onload = WGM.init;
} else {
    WGM.init();
}