﻿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 loadTinyMCE(language)
{
	tinyMCE.init({
	    mode: "textareas",
	    editor_selector : "wysiwyg",
		theme : "advanced",
		language: language,
		height : "300",
		width : "450",
		button_tile_map : true,
		plugins : "safari,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",
		template_external_list_url : "example_template_list.js",
		extended_valid_elements : "script[lang|language|type],hr[class|width|size|noshade]"
	});	
}

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 "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 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 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';
		}
	}
};	