﻿// JavaScript Document
window.addEvent('domready', function () {


    var aryInputValues = Array();
    $$('input').each(function (item, index) {
        aryInputValues[item.get('id')] = item.get('value');
    })


    $$('input').addEvent('focus', function () {
        if (this.get('value') == aryInputValues[this.get('id')]) {
            //this.set('value', '');
        }
    });

    $$('input').addEvent('blur', function () {
        if (this.get('value') == "") {
            //this.set('value', aryInputValues[this.get('id')]);
            if ($('rfv' + this.get('id'))) {
                validate(this, '');
            }
        }
    });



    if ($('frmOptions')) {
        buildData();
    }

    //Add ajax data gathering to form as its filled out in form builder
    if ($('frmBuilder')) {
        var formresponse = $('FormResponse').get('value');
        var formid = $('frmBuilder').get('name').replace("frm", "");
        $('frmBuilder').getElements('input').each(function (el) {
            /*el.addEvent('blur', function () {
                var myRequest = new Request({ method: 'post', url: '/form/process/' + formid });
                var formfield = el.get('name');
                myRequest.send('partial=true&formresponse=' + formresponse + '&' + formfield + '=' + el.get('value'));
            })*/
        })
    }

    if ($('frmSubscribe')) {
        $('frmSubscribe').set('send', {
            onSuccess: function (html, xml) {
                AlertText(html);
            }
        });

        $('frmSubscribe').addEvent('submit', function (e) {
            if (validate(document.getElementById('frmSubscribe'), '')) {
                new Event(e).stop();
                this.send();
            }
        });
    }


})


function AlertText(html) {
    //if ($('alert')) {
       //$('alert').setStyle('display', 'block');
        // $('alert').set('html', txt);
        var mooAlert = $('alert');
        if (mooAlert) {
            
            var top = window.getScrollTop() + (window.getHeight() / 2)-200;
            var left = window.getScrollLeft() + (window.getWidth() / 2)-225;
            mooAlert.setStyles({ top: top, left: left, display: "" });
            mooAlert.set('html', html);
            var overlay = new Fx.Tween(mooAlert, { property: "opacity", duration: 360 }).set(0)
            overlay.start(1).wait(2000).chain(function () { overlay.start(0) }).chain(function () { $('alert').destroy(); });
           // overlay.cancel().set(0)
            //mooAlert.set('tween', { duration: 3500 });
            //mooAlert.fade('in');
            //mooAlert.fade('out');
        }
   // }
    }


    function NextResults(a,target) {
        var mooA = $(a);
        var div = $(target);

        if (mooA) {
            var quickView = new Request.HTML({
                url: a.getProperty('href'),
                onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
                    div.set("html",div.get('html')+responseHTML);
                }
            }).send();

            mooA.destroy();
        }

    }
