// JavaScript Document
window.addEvent('domready', function () {
    if ($('frmOptions')) {
        buildData();
    }
    //Add ajax data gathering to form as its filled out in form builder
    if ($('frmBuilder')) {
        var aryFormFields = $('frmBuilder').getElements('input').combine($('frmBuilder').getElements('select').combine($('frmBuilder').getElements('textarea'))).include($('frmBuilder'));
        aryFormFields.each(function (el) {
            el.addEvent('blur', function () {
                var myRequest = new Request({ method: 'post', url: '/form/partialresponse/' + $('FormID').get('value') });
                myRequest.send('formresponse=' + $('FormResponse').get('value') + '&field=' + el.get('name') + '&value=' + el.get('value'));
            })
        })
    }
    $$('.validate').each(function (el, i) {
        var aryClass = el.getProperty("class").split(" ");
        aryClass.each(function (cls, j) {
            if (cls.indexOf("regex") == 0) {
                var mooLI = el.getParent("li");
                if (mooLI) {
                    var mooLbl = mooLI.getElement('label');
                    mooLbl.setProperty('id', 'rfv' + el.getProperty('id'));
                    el.addEvent('blur', function () { validate(this, cls) });
                }
            }
        })

    });
})
function AlertText(html) {
    if (!$('alert')) {
        document.id(document.body).adopt($$([overlay = new Element("div", { id: "alert" })]).setStyle("display", "none"));
    }
    var mooAlert = $('alert');
    if (mooAlert) {
        //$('alert').setStyle('display', 'block'); 
        //	$('alert').set('html',txt);
        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(2800).chain(function () { overlay.start(0) }).chain(function () { $('alert').destroy(); });
    }
}
function OpenLargerNoChrome(src, width, height) {
    window.open(src, 'lg', 'width=' + width + ',height=' + height + ',resizable=1,scrollbars=0,location=0,status=0');
}
function ImgResize(img, container, imgH, imgW, containerH, containerW) {
    return;
    var imgContainer = document.getElementById(container);
    if (imgContainer) {
        imgContainer.style.display = 'inline-block';
        imgContainer.style.textAlign = 'center';
    }
    img.style.verticalAlign = 'middle';
    img.style.display = 'inline-block';
    containerH = (containerH ? containerH : imgContainer.clientHeight);
    containerW = (containerW ? containerW : imgContainer.clientWidth);
    //alert(containerH);
    var scaleH = containerH / imgH;
    var scaleW = containerW / imgW;
    if (scaleH < scaleW) {
        img.style.height = containerH + "px";
        img.style.width = Math.round(imgW * scaleH) + "px";
        //img.style.height = imgH + "px";
        //img.style.width = imgW + "px";
    } else {
        img.style.width = containerW + "px";
        img.style.height = Math.round(imgH * scaleW) + "px";
        //img.style.height = imgH + "px";
        //img.style.width = imgW + "px";
    }
}
function numbersonly(e) {
    var unicode = e.charCode ? e.charCode : e.keyCode;
    if (unicode != 8) { //if the key isn't the backspace key (which we should allow)
        if (unicode < 48 || unicode > 57) //if not a number return false 
        //disable key press
            return false
    }
}
