(function($){
 $.fn.getObj = function(path) { return path ? $(path, this) : this; }
 $.fn.getText = function(path) { return this.getObj(path).text(); }
 $.fn.getAttr = function(attr, path) { return this.getObj(path).attr(attr); }
})(jQuery);

function InitCalender(cal, onSelect, date){
    return cal.datepicker({    
      showOn: "both",
      dateFormat: "M d, yy",
      defaultDate: "today", 
      buttonImage: ApplicationUrl + "App_Themes/Default/Calendar/datePickerPopup.gif",
      buttonImageOnly: true,    
      firstDay: 1,
      onSelect: onSelect 
    })
    .datepicker("disable").datepicker("setDate",typeof date == 'undefined' ? "today" : new Date(date)).datepicker("enable");    
    //.datepicker('setDate', new Date(2009, 0, 4)).datepicker('hide');            
}

function DateFormat(d) {  //format('MMM dd, yyyy')
    var AbbreviatedMonthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ""];
    return AbbreviatedMonthNames[d.getMonth()] + " " + d.getDate() + ", " + d.getFullYear();
}

function RequiredValidator(obj) {

    isValid = true;
    if (obj != null) {
        var p = $(obj).parent().parent().parent();
        if ($(obj).val() != "") {
            
            var _class = $(p).attr('class');
            var ind = _class.indexOf('FeError');
            if (ind != -1) {

                $(p).removeClass(_class);
                _class = _class.substring(0, ind - 1);
                $(p).addClass(_class);
                $('label', p).css('color', 'White');
                isValid = false;
            }            
        }
        
        return isValid;
    }
    for (i = 0; i < Page_Validators.length; i++) {

        val = Page_Validators[i];
        var ctrl = $('#' + val.controltovalidate);
        var p = ctrl.parent().parent().parent();

        if ($('input', p).val() == "") {
            $('label', p).css('color', 'Red');
            var _class = $(p).attr('class');
            $(p).removeClass(_class);
            $(p).addClass(_class + ' FeError');
            isValid = false;
        }
        if ($('textarea', p).val() == "") {
            $('label', p).css('color', 'Red');
            var _class = $(p).attr('class');
            $(p).removeClass(_class);
            $(p).addClass(_class + ' FeError');
            isValid = false;
        }
    }    
    return isValid;
}

function EmailValidator() {
    isValid = true;
    for (i = 0; i < Page_Validators.length; i++) {
        
        val = Page_Validators[i];
        if (val.validationexpression != null) {
            var ctrl = $('#' + val.controltovalidate);            
            if (ctrl.val().match(val.validationexpression)==null) {
                
                isValid = false;
            }
        }
    }
    return isValid;
}

//function onValidatorError(o) {
//    $(o).addClass(o.validSet.errorCSS + " nopad_top");
//    if ($('#err_' + o.id).length > 0)
//        $('#err_' + o.id).html(o.validSet.lastMSG)
//    else
//        $(o).prev().addClass("nopad_top")
//      .before($('<div id="err_' + o.id + '" class="error">').html(o.validSet.lastMSG));
//}
//function onValidatorSuccess(o) {
//    $(o).removeClass(o.validSet.errorCSS).removeClass("nopad_top").prev().removeClass("nopad_top");
//    $('#err_' + o.id).remove();
//}


var hideMsgInterval;

function showMessage(msgText, isError, hideTimeout) {
    var msgContainer = $('#msgContainer');
    if (isError == undefined || isError == false) {
        msgContainer.removeClass("msgError");
    }
    else {
        msgContainer.addClass("msgError");
    }
    $('#msgDiv', msgContainer).html(msgText);
    msgContainer.show();
    if (hideTimeout != undefined) {
        hideMsgInterval = setInterval('hideMessage()', parseInt(hideTimeout));
    }
}

function hideMessage() {
    $('#msgContainer').hide();
    clearInterval(hideMsgInterval);
}
function megaFormStart(message, divID, fadeOut, isAbsolute) {
    //document.getElementById('info').innerHTML = message;
    $('.contentForm', $('#' + divID)).html(message);

    var user_agent = navigator.userAgent.toLowerCase()
    var pos_x = 0;
    var pos_y = 0;

    if (!fadeOut) fadeOut = 'fadeOutFon';
    var fadeOutFon = document.getElementById(fadeOut);
    var objBody = document.getElementsByTagName("body").item(0);

    fadeOutFon.style.zIndex = '9999';
    fadeOutFon.style.position = 'absolute';
    fadeOutFon.style.top = 0;
    fadeOutFon.style.left = 0;
    fadeOutFon.style.background = 'black'; //'#d0e7e7';
    fadeOutFon.style.width = '100%'//getPageSize()[0] + 'px';
    fadeOutFon.style.height = $(document).height() + 'px';
    fadeOutFon.style.display = '';
    //fadeOutFon.style.marginLeft = '-17px';
    fadeOutFon.style.opacity = 0.5;
    if ($.browser.msie) {
        fadeOutFon.style.marginLeft = '0';
        setOpacity(fadeOutFon, 0.5)
    }



    //     new Effect.Opacity(fadeOutFon, { duration:0.3, from:0, to:0.5 });
    addForm = document.getElementById(divID);
    addForm.style.zIndex = '10000';
    addForm.style.position = isAbsolute ? 'absolute' : 'fixed';
    if ($.browser.msie && parseInt($.browser.version) == 6) addForm.style.position = 'absolute';
    addForm.style.opacity = 0;
    if ($.browser.msie) setOpacity(addForm, 0.0)
    addForm.style.display = '';
    $('#' + divID).animate({ opacity: '1' }, 500);

    //document.body.style.overflowX = 'hidden';

    //   new Effect.Opacity(addForm, { duration:1, from:0, to:1});



    //SCROLL HEIGHT
    var y;
    if (self.pageYOffset) { y = self.pageYOffset; }
    else if (document.documentElement && document.documentElement.scrollTop) { y = document.documentElement.scrollTop; }
    else if (document.body) { y = document.body.scrollTop; }

    pos_y = y;

    //PAGE HEIGHT  & WIDTH
    
    if (self.innerHeight) { x = self.innerWidth; }
    else if (document.documentElement && document.documentElement.clientHeight) { x = document.documentElement.clientWidth; }
    else if (document.body) { x = document.body.clientWidth; }

    pos_x = x;
    
    if (!addForm.style.width) {
        addForm.style.width = pos_x - 60 + 'px';
    }

    var width=getElementSizeFromStyle(addForm);
    addForm.style.left = addForm.style.width != pos_x - 60 + 'px' ? ((pos_x / 2) - (width / 2)) + "px" : "30px";    
    addForm.style.top = "250px"; //pos_y + 250 + "px";

}

function megaFormFinish(divID, fadeOut) {
    if (!fadeOut) fadeOut = 'fadeOutFon';
    var fadeOutFon = document.getElementById(fadeOut);
    addForm = document.getElementById(divID);
    fadeOutFon.style.display = 'none';
    addForm.style.display = 'none';

    document.body.style.overflowX = 'visible';

}

function getElementSizeFromStyle(elem) {
    var widthString = elem.style.width;
    //var heightString = elem.style.height;
    
    var widthValueString = widthString.substr(0, widthString.length - 2);
    var width = parseInt(widthValueString);
    
    return width;
}

function getPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }             // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }

    var test1 = document.body.scrollHeight;
    var test2 = document.body.offsetHeight
    if (test1 > test2) // all but Explorer Mac
    {
        x = document.body.scrollWidth;
        y = document.body.scrollHeight;
    }
    else // Explorer Mac;
    //would also work in Explorer 6 Strict, Mozilla and Safari
    {
        x = document.body.offsetWidth;
        y = document.body.offsetHeight;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    return arrayPageSize;
}

function setOpacity(element, alpha) {
    var style = element.style;
    if (style.MozOpacity != undefined) { //Moz and older
        style.MozOpacity = alpha;
    }
    else if (style.filter != undefined) { //IE
        style.filter = "alpha(opacity=0)";
        element.filters.alpha.opacity = (alpha * 100);
    }
    else if (style.opacity != undefined) { //Opera
        style.opacity = alpha;
    }
}

function fixPNG(element) {
    if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
        var src;

        if (element.tagName == 'IMG') {
            if (/\.png$/.test(element.src)) {
                src = element.src;
                element.src = "assets/images/width.gif";

            }
        }
        else {
            src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
            if (src) {
                src = src[1];
                element.runtimeStyle.backgroundImage = "none";
            }
        }
        if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
    }
}

function ShowHide(id) {
    if (ge(id).style.display == 'none') {
        ge(id).style.display = '';
    }

    else {
        ge(id).style.display = 'none';
    }
}
function ge(id) {
    return document.getElementById(id);
}

//function isBrowserIe() {
//    return (! +"\v1");
//}

function getWindowScrollX() {
    return (window.scrollX) ? window.scrollX : document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
}

function getWindowScrollY() {
    return (window.scrollY) ? window.scrollY : document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
}

function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function getFromUrlHash(paramName) {
    if (window.location.hash.length > 0) {
        var params = window.location.hash.substr(1).split('=');
        if (params.length > 1 && params[0] == paramName) {
            return params[1];
        }
    }
}

function flyUP(URL, w, h) {
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=true,width='+w+',height='+h+',left = 0,top = 0');");
}
