﻿function validRange(startDay, startMonth, startYear, endDay, endMonth, endYear)
{
    var startD = document.getElementById(startDay);
    var startM = document.getElementById(startMonth);
    var startY = document.getElementById(startYear);
    var endD = document.getElementById(endDay);
    var endM = document.getElementById(endMonth);
    var endY = document.getElementById(endYear);   
    
    var start = new Date(startY.value, startM.value, startD.value);
    var end = new Date(endY.value, endM.value, endD.value);
    
    if(end < start)
    {
        endD.value = startD.value;
        endM.value = startM.value;
        endY.value = startY.value;
    }
}

function blgDialog(url, width, height, init, action, actionargs) {

    if (window.showModalDialog) 
    { 
        // here we hope that Mozilla will never support document.all
        var value =
            window.showModalDialog(url, init, 'dialogWidth: ' + width + 'px; dialogHeight: ' + height + 'px;resizable: no; help: no; status: no; scroll: no');
        
        if (action) {
            action(value, actionargs);
        }
    }
    else
    {
		var dlg = window.open(url, "ha_dialog","toolbar=no,menubar=no,personalbar=no," +
										"width=" + width + ",height=" + height + ",scrollbars=no,resizable=no," +
										"dependent=yes,z-lock=yes,modal=yes");
		
		dlg.dialogArguments = init;
		dlg.actionArgs = actionargs;
		// make up a function to be called when the Dialog ends.
		dlg._return = function () {
			
			if (dlg.removeEventListener) {
				dlg.removeEventListener('unload', dlg._return, false);
			}
			else if (dlg.detachEvent) {
				dlg.detachEvent('onunload', dlg._return);
			}
			
			if (action) {
				action(dlg.returnValue, dlg.actionArgs);				
			}
		};
		
		var set_observer = function() {
		
			if (dlg.addEventListener) {
				dlg.addEventListener('unload', dlg._return, false);
			}
			else if (dlg.attachEvent) {
				dlg.attachEvent('onunload', dlg._return);
			}			
			
			if (dlg.removeEventListener) {
				dlg.removeEventListener('load', set_observer, false);
			}
			else if (dlg.detachEvent) {
				dlg.detachEvent('onload', set_observer);
			}
		};		
			
		if (dlg.addEventListener) {
			dlg.addEventListener('load', set_observer, false);
		}
		else if (dlg.attachEvent) {
			dlg.attachEvent('onload', set_observer);
		}
	}
};

function switchAll(setSelected, cbName)
{
    var inputs = document.getElementsByTagName('input');
    
    for(var i=0; i<inputs.length; i++)
    {
        if((inputs[i].type == 'checkbox') && (inputs[i].id.indexOf(cbName) == inputs[i].id.length-cbName.length))
        {
            inputs[i].checked = setSelected;
        }
    }
}

function validateBodyLength(src, args)
{
    args.IsValid = (args.Value.length <= 4000);
}

var m_win, m_field_name;
function fileBrowserCallBack(field_name, url, type, win) {
	m_field_name = field_name;
	m_win = win;	    
    var handler = null;
    switch (type) {
        case "file": //other files
            handler = openFileActionHandler;
            break;
		case "image": // images
			handler = openImageActionHandler;
			fileStoreURL += '&searchValue=image%';						
			break;
		case "flash": // flash objects
			handler = openFlashActionHandler;						
			fileStoreURL += '&searchValue=application%2fx-shockwave-flash';						
			break;
	}
	
	blgDialog(fileStoreURL, 615, 405, window, handler, '');
}

function openFileActionHandler(returnVal, actionArgs) {
    if (returnVal && returnVal.ok) {
        if (returnVal.objectId && m_win && m_field_name) {
            var url = '../' + returnVal.objectId + '/getfile.ashx';
            m_win.document.forms[0].elements[m_field_name].value = url;
        }
    }
};

	
function openImageActionHandler(returnVal, actionArgs)
{
	if(returnVal && returnVal.ok){
		if(returnVal.objectId && m_win && m_field_name) {
			var url = '../' + returnVal.objectId + '/getfile.ashx';
			m_win.document.forms[0].elements[m_field_name].value = url;
			if(m_win.showPreviewImage)
				m_win.showPreviewImage(url); // update preview
		}
	}
};

function loadTinyMCE(language) {
    tinyMCE.init({
        mode: "textareas",
        editor_selector: "wysiwyg",
        theme: "advanced",
        language: language,
        height: "300",
        width: "450",
        button_tile_map: true,
        plugins: "emotions,inlinepopups,advlink,advimage,flash,paste,fullscreen,media",
        theme_advanced_buttons1: "formatselect,fontselect,fontsizeselect,|,bold,italic,underline,strikethrough", //styleselect
        theme_advanced_buttons2: "sub,sup,|,undo,redo,|,cut,copy,paste,pastetext,pasteword,justifyleft,justifycenter,justifyright,justifyfull,|,fullscreen",
        theme_advanced_buttons3: "bullist,numlist,|,outdent,indent,|,forecolor,backcolor,liststyle,|,link,unlink,image,charmap,emotions,blockquote,code,media",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: false,
        paste_use_dialog: false,
        file_browser_callback: "fileBrowserCallBack",
        flash_wmode: "transparent",
        dialog_type: "modal",
        fullscreen_new_window: true,
        template_external_list_url: "example_template_list.js",
        extended_valid_elements: "script[lang|language|type],hr[class|width|size|noshade]",
        setup: function (ed) {
            ed.onInit.add(function (ied) {
                ied.focus();
            });
        }
    });
}

function openFlashActionHandler(returnVal, actionArgs)
{
	if(returnVal && returnVal.ok){
		if(returnVal.objectId && m_win && m_field_name) {
			m_win.document.forms[0].elements[m_field_name].value = '../' + returnVal.objectId + '/getfile.ashx';
		}
	}
};

if (typeof (initTooltip) == 'undefined') {

    var tipHolder;
    var tipTop = 0;
    var tipLeft = 0;
    var tipWidth = 0;
    var tipHeight = 0;
    var scrollCorrected = false;
    var hideTipTimer;
    var showingTip = false;

    function initTooltip() {
        if (!tipHolder) {
            tipHolder = document.createElement('DIV');
            tipHolder.className = 'blg_tooltip';
            document.body.appendChild(tipHolder);

            if (window.document.addEventListener) {
                window.document.addEventListener('mousemove', storeTooltipPos, false);
            }
            else {
                window.document.attachEvent('onmousemove', storeTooltipPos);
            }
        }
    }

    function doTooltip(evt, thetext) {
        initTooltip();

        if ((tipHolder) && (thetext) && (thetext.length > 0)) {
            if (hideTipTimer) {
                clearTimeout(hideTipTimer);
                hideTipTimer = null;
            }

            tipHolder.innerHTML = thetext;
            tipHolder.style.display = 'block';

            tipWidth = tipHolder.offsetWidth;
            tipHeight = tipHolder.offsetHeight;

            showingTip = true;

            updateTipPosition();
        }
    }

    function storeTooltipPos(evt) {
        var e = evt || window.event;

        if ((e.pageX) || (e.pageY)) {
            scrollCorrected = true;
        }
        else {
            scrollCorrected = false;
        }

        var newTop = (scrollCorrected) ? e.pageY : e.clientY;
        var newLeft = (scrollCorrected) ? e.pageX : e.clientX;

        if ((newTop != tipTop) || (newLeft != tipLeft)) {
            tipTop = newTop;
            tipLeft = newLeft;

            updateTipPosition();
        }
    }

    function updateTipPosition() {
        if (showingTip) {
            var lScroll = document.documentElement.scrollLeft;
            var tScroll = document.documentElement.scrollTop;

            var newLeft = tipLeft + (scrollCorrected ? 0 : lScroll) + 5;

            if ((newLeft + tipWidth) > (document.body.scrollWidth)) {
                newLeft -= (tipWidth + 5);
            }
            tipHolder.style.right = '';
            tipHolder.style.left = newLeft + 'px';

            var newTop = tipTop + (scrollCorrected ? 0 : tScroll) + 5;
            if ((newTop + tipHeight) > (document.body.scrollHeight)) {
                newTop -= (tipHeight + 5);
            }
            tipHolder.style.top = newTop + 'px';
            tipHolder.style.bottom = '';
        }
    }

    function hideTip() {
        if (!hideTipTimer) {
            hideTipTimer = setTimeout("closeTip()", 100);
        }
    }

    function closeTip() {
        if (tipHolder) {
            tipHolder.style.display = 'none'; showingTip = false;
        }
    }
}
