/***** input helper functions *****/
function getobj(n, d){ return util.obj(n, d); }

util = {
    obj: function(n, d){
        var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length){
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
        if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
        for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
        if(!x && d.getElementById) x=d.getElementById(n); return x;
    },
    clear: function(obj, txt){
        if( obj && obj.value && obj.value.toLowerCase() == txt.toLowerCase() )
            obj.value = '';
    },
    reset: function(obj, txt){
        if( obj && obj.value.length == 0 )
            obj.value = txt;
    },
    prevEle: '',
    toggleClass: function(id, classA, classB){
        var ele = getobj(id);
        if(!ele) return;
        if(ele.className == classA){
            ele.className = classB;
            if(util.prevEle != '' && ele != util.prevEle) util.prevEle.className = classA;
        }else{
            ele.className = classA;
            if(util.prevEle != '' && ele != util.prevEle) util.prevEle.className = classB;
        }
        util.prevEle = ele;
    },
    setClass: function(id, className){
        var ele = getobj(id);
        if(!ele) return;
        ele.className = className;
    },
    show: function(eleId){
        var obj = getobj(eleId);
	    if (!obj) return;
	    obj.style.display = 'block';
    },
    hide: function(eleId){
        var obj = getobj(eleId);
	    if (!obj) return;
	    obj.style.display = 'none';
    },
    prevShown: '',
    showUnique: function(eleId){
        if(util.prevShown != '')
            util.hide(util.prevShown);
        if(util.prevShown != eleId){
            util.prevShown = eleId;
            util.show(eleId);
        }else{
            util.prevShown = '';
        }
    }
}

lightbox = {
    expandHeightFullScreen: function(eleId){
        obj = getobj(eleId);
	    if(!obj) return;
	    
	    var lightBoxOverlayHeight;
	    if (window.innerHeight && window.scrollMaxY)
		    lightBoxOverlayHeight = window.innerHeight + window.scrollMaxY;
	    else
		    lightBoxOverlayHeight = document.body.scrollHeight + document.body.clientHeight;
		
	    obj.style.height = lightBoxOverlayHeight + 'px';
    },
    show: function (eleId){
	    util.show(eleId);

	    var obj = getobj(eleId);
	    var whatsTheHeight = (obj.offsetHeight)/2;
	    obj.style.marginTop = '-' + whatsTheHeight + 'px';
    	
	    util.show('lightBox');
	    util.show('lightBoxOverlay');
	    lightbox.expandHeightFullScreen('lightBoxOverlay');
    },
    hide: function (eleId){
	    util.hide(eleId);
	    util.hide('lightBox');
	    util.hide('lightBoxOverlay');
    }
    ,
    hideAll: function (){
	    util.hide('tellAFriendPopup');
	    util.hide('mediaLoginPopup');
	    util.hide('orderAProgrammePopup');
	    util.hide('lightBox');
	    util.hide('lightBoxOverlay');
    }
}

dotNet = {
    postbackOnEnter: function(event, objId){
        if( event && event.keyCode == 13 )
            __doPostBack(objId,'');
    }
}

$(document).ready(function() {
    $('a.help').click(function(event) {
        if ($(this).next('div.helptextContainer').attr('style') != undefined &&
            $(this).next('div.helptextContainer').attr('style').indexOf('visible') != -1)
            $(this).next('div.helptextContainer').attr('style', 'visibility:hidden');
        else
            $(this).next('div.helptextContainer').attr('style', 'visibility:visible');
    });
});
function resetText(obj, text, isClick) {
    if (obj.val() == text && isClick)
        obj.val('');
    else if (obj.val() == '' && !isClick)
        obj.val(text);
}
function registerDatePicker(inputObj) {
    inputObj.DatePicker({
        format: 'a d B Y',
        date: ['24/4/2010', '15/5/2010'],
        current: Date.parse('1 May 2010'),
        starts: 1,
        position: 'r',
        calendars: 2,
        mode: 'multiple',
        onBeforeShow: function() {
            $(this).DatePickerClear();
            //$(this).DatePickerSetDate($(this).val(), true);
            //$(this).DatePickerSetDate('1/5/2010', true);
        },
        onRender: function(date) {
            return {
                //disabled: (date.valueOf() < now.valueOf())
                disabled: (date.valueOf() < Date.parse('24 April 2010').valueOf()),
                disabled2: (date.valueOf() > Date.parse('15 May 2010').valueOf())
            }
        },
        onChange: function(formated, dates) {
        inputObj.val(formated);
        }
    });
}
function registerDatePickerSingle(inputObj) {
    var timeNow = new Date();
    inputObj.DatePicker({
        format: 'a d B Y',
        date: [timeNow],
        current: timeNow,
        starts: 1,
        position: 'r',
        mode: 'single',
        onBeforeShow: function() {
            $(this).DatePickerClear();
        },
        onChange: function(formated, dates) {
            inputObj.val(formated);
            inputObj.DatePickerHide();
        }
    });
}
function countWords(str) {
    var fullStr = str + " ";
    var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
    var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, '');
    //var non_alphanumerics_rExp = /[^A-Za-z0-9]+/gi;
    var non_alphanumerics_rExp = /[\s]+/gi;
    var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, ' ');
    var splitString = cleanedStr.split(' ');
    var word_count = splitString.length - 1;
    return word_count;
}
function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function isFloat(str) {
    str = trim(str);
    return /^[-+]?\d+(\.\d+)?$/.test(str);
}

//new lightbox functions
$(document).ready(function() {
    $('a.buttonSaveAndSubmit').click(function() {
        _buildLightbox($('div#lightboxSubmit'));
        positionLightbox($('div#lightboxSubmit'));
        return false;
    });
    $('a.buttonSaveAndContinueWorking').click(function() {
        _buildLightbox($('div#lightboxSave'));
        positionLightbox($('div#lightboxSave'));
        return false;
    });
});
function positionLightbox(divContentWrapObj) {
    var windowHeight = $(window).height();
    var lightboxHeight = divContentWrapObj.children('div.lightboxContent').height();
    divContentWrapObj.css('top', (windowHeight / 2) - (lightboxHeight / 2) - 100);
}
function _buildLightbox(divContentWrapObj) {
    $('body').append('<div id="lightboxOverlay"></div>');
    divContentWrapObj.attr('style', 'display:block');
    //move content wrap to first child of body
    $('div.lightboxContentWrap').insertBefore($('body form'));

    $('#lightboxOverlay, div.lightboxContentWrap, a.buttonCancel').click(function() {
        _destroyLightbox();
    });
    $('div.lightboxContent').click(function() {
        return false;
    });
};
function _destroyLightbox() {
    $('#lightboxOverlay').remove();
    $('div.lightboxContentWrap').attr('style', 'display:none');
};

//iframe lightbox
function ifrBuildLightbox() {
    $('body').append('<div id="lightboxOverlay"></div>');
    $('div.lightboxContentWrap').attr('style', 'display:block');

    $('div.lightboxContent').attr('style', 'background:none;width:390px;height:276px;');

    //move content wrap to first child of body
    $('div.lightboxContentWrap').insertBefore($('body form'));
    
    $('#lightboxOverlay, div.lightboxContentWrap').click(function() {
        _destroyLightbox();
    });
    $('div.lightboxContent').click(function() {
        return false;
    });
};
// ------------------------------------------------------------------------------------
function _loadContent(url) {
    $('div.lightboxContent').html('<iframe src="' + url + '" border="0" frameborder="0" width="390px" height="276px" scrolling="no" />');
};