// JavaScript Document
window.addEvent('domready', function () {


    var aryInputValues = Array();
    $$('input.focus').each(function (item, index) {
        aryInputValues[item.get('id')] = item.get('value');
    })


    $$('input.focus').addEvent('focus', function () {
        if (this.get('id') && this.get('value') == aryInputValues[this.get('id')]) {
            key = this.get('id').toLowerCase();
            if (key.indexOf("zip") == -1) {
                this.set('value', '');
            }
        }
    });

    $$('input.focus').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 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");
                var mooLbl = mooLI.getElement('label');
                mooLbl.setProperty('id', 'rfv' + el.getProperty('id'));
                el.addEvent('blur', function () { validate(this, cls) });
            }
        })

    });

    if (Cookie.read('site_font_size')) {
        fontsz = Cookie.read('site_font_size');
        document.body.style.fontSize = fontsz;
    }

})


function AlertText(html) {
    if (!$('alerts')) {
        document.id(document.body).adopt($$([overlay = new Element("div", { id: "alerts" })]).setStyle("display", "none"));
    }
    var mooAlert = $('alerts');
    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(5000).chain(function () { overlay.start(0) }).chain(function () { $('alerts').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
    }
}

function resizeText(multiplier) {
    var fontsz = "1.0em"; //Default to 1em
    if (multiplier != 0) {
        if (Cookie.read('site_font_size')) {
            // Set fontsz to the cookie value if it exists
            fontsz = Cookie.read('site_font_size');
        }
        fontsz = parseFloat(fontsz) + (multiplier * 0.1);
        if (fontsz > 1.5) {
            fontsz = 1.5;
        }
        if (fontsz <.6) {
            fontsz = .6
        }

        fontsz += "em";
        // Change body text size
    }
    else {
        fontsz = "0.8em";
    }
    document.body.style.fontSize = fontsz;
    //Set a new cookie 
    var myCookie = Cookie.write('site_font_size', fontsz, {
        duration: 365 //Save for 1 year
    });
}
    
