// Universal script

// Tree
function blockManager(class_name, show, hide, block_name, show_name, hide_name)
{
        if (document.getElementById) 
        {
                document.write("<span><a class='" + class_name + "' href='javascript:blockAct('" + block_name + "', '" + show_name + "', '" + hide_name + "');'>" +
                        "<span id='" + show_name + "'>" + show + "</span>" +
                        "<span id='" + hide_name + "' style='display: none;'>" + hide + "</span>"
                        + "</a></span>");
        }
}
                                
function blockAct(block_name, show_name, hide_name)
{
        var block = document.getElementById(block_name);
        var showlink = document.getElementById(show_name);
        var hidelink = document.getElementById(hide_name);
        if (block.style.display == 'none')
        {
                block.style.display = '';
                hidelink.style.display = '';
                showlink.style.display = 'none';
        }
        else
        {
                block.style.display = 'none';
                hidelink.style.display = 'none';
                showlink.style.display = '';
        }
}

// Features
function whoVisible()
{
        arg = whoVisible.arguments;

        for (i = 0; i < arg.length; i++)
        {
                var item = document.getElementById(arg[i]);
                if (item.style.display == '')
                {
                        return arg[i];
                }
        }
}

function changeBlocks(block1, block2)
{
        var first_block = document.getElementById(block1);
        var second_block = document.getElementById(block2);
        if (first_block.style.display == 'none')
        {
                first_block.style.display = '';
                second_block.style.display = 'none';
        }
        else
        {
                first_block.style.display = 'none';
                second_block.style.display = '';
        }
}

function checkDate(orig_m, orig_y, new_m, new_y, block1, msg_orig, msg_new)
{
        var block = document.getElementById(block1);
        var xs = document.getElementById('XS');
        if (xs.value == "")
        {
		xs.value = msg_orig;
        }
        if ((orig_m != new_m) || (orig_y != new_y))
        {
        	block.options[block.options.selectedIndex].text = msg_new;
                block.disabled = true;
        }
        else
        {
        	block.options[block.options.selectedIndex].text = xs.value;
                block.disabled = false;
                xs.value = "";
        }
}

function openUrl()
{
        arg = openUrl.arguments;
        url = arg[0];
        msg = arg[1];
        if (msg)
        {
                if (confirm(msg))
                {
                        document.location = url;
                }
        }
        else
                document.location = url;
}

function openWindow()
{
        arg = openWindow.arguments;
        link = arg[0];
        wid = (arg[1]) ? arg[1] + 20 : 200;
        hei = (arg[2]) ? arg[2] + 30 : 200;
        window.open(link, '', 'toolbar=no,menubar=no,scrollbars=no,width=' + wid + ',height=' + hei);
}

function openWindow2()
{
        arg = openWindow2.arguments;
        link = arg[0];
        wid = (arg[1]) ? arg[1] + 20 : 200;
        hei = (arg[2]) ? arg[2] + 30 : 200;
        window.open(link, '', 'toolbar=no,menubar=no,scrollbars=no,resizable=yes,width=' + wid + ',height=' + hei);
}

function allCheckbox(form_name, mark)
{
        bForm = form_name;
        cnt = bForm.elements.length;
        var item;
        arg = allCheckbox.arguments;
        prefix = (arg[2]) ? arg[2] : "";

        for (i = 0; i < cnt; i++)
        {
                item = bForm.elements[i];
                if (item.type == "checkbox" && item.name.substring(0, prefix.length) == prefix)
                {
                        item.checked = mark;
                }
        }
}

function writeTo()
{
        arg = writeTo.arguments;

	bName = navigator.appName;
	bVer = parseInt(navigator.appVersion);

	if (bName == "Microsoft Internet Explorer" && bVer >= 4)
	{
	        // bForm = arg[0];
		bForm = document.forms[arg[0]];
	}
	else
	{
		bForm = document.forms[arg[0]];
	}             
        
        bForm[arg[1]].value = arg[2];
}

// ***************************************************************

function checkForm()
{
        arg = checkForm.arguments;

	bName = navigator.appName;
	bVer = parseInt(navigator.appVersion);

	if (bName == "Microsoft Internet Explorer" && bVer >= 4)
	{
	        // bForm = arg[0];
		bForm = document.forms[arg[0]];
	}
	else
	{
		bForm = document.forms[arg[0]];
	}             

        for (i = 1; i < arg.length; i++)
        {
                if (bForm[arg[i]].value == "")
                {
                        bForm[arg[i]].focus();
                        alert('Please enter all rquired fields!');
                        return false;
                }
        }
                
        return true;
}
        
function submitForm(form_name)
{
	bName = navigator.appName;
	bVer = parseInt(navigator.appVersion);

	if (bName == "Microsoft Internet Explorer" && bVer >= 4)
	{
		bForm = document.forms[form_name];
	}
	else
	{
		bForm = document.forms[form_name];
	}

        bForm.submit();
}


// Cool buttons
function formatStr(v) 
{
	if (!document.selection) return;
	var str = document.selection.createRange().text;
	if (!str) return;
	document.selection.createRange().text = '[' + v + ']' + str + '[/' + v + ']';
	//document.selection.empty();	
}

function insertLink() 
{
	if (!document.selection) return;
	var str = document.selection.createRange().text;
	if (!str) return;
	var my_link = prompt('Please Enter URL:', 'http://');
	var my_title = prompt('Please Enter Title:', '');
	if (my_link != null)
		document.selection.createRange().text = '[a href=[' + my_link + '] title=['  + my_title + ']]' + str + '[/a]';
	//document.selection.empty();	
}

function getSelectStart(s) 
{
        return s.selectionStart;
}

function getSelectEnd(s) 
{
        return s.selectionEnd;
}

function getTextLength(s) 
{
        return s.textLength;
}

function getMozSelection(s) 
{
        return (s.value).substring(getSelectStart(s), getSelectEnd(s))
}

function setMozSelection(s, a, z) 
{
        s.selectionStart = a;
        s.selectionEnd = z;
}

function wrapSelection(v) 
{
	var x = document.forms.length;
	var y, s, s1, s2;
	for (i = 0; i < x; i++)
	{
	        y = document.forms[i].elements.length;
		for (j = 0; j < y; j++)
		{
		        s = document.forms[i].elements[j];
		        if (s.type != "textarea") continue;
		        s1 = (s.value).substring(0, getSelectStart(s));
		        s2 = (s.value).substring(getSelectEnd(s), getTextLength(s));
		        if (getMozSelection(s) == "") continue;
		        s.value = s1 + '[' + v + ']' + getMozSelection(s) + '[/' + v + ']' + s2;
		        setMozSelection(s, 0, 0);
		}
	}                
}

function insertMozLink() 
{
	var x = document.forms.length;
	var y, s, s1, s2, my_link, my_title;
	for (i = 0; i < x; i++)
	{
	        y = document.forms[i].elements.length;
		for (j = 0; j < y; j++)
		{
		        s = document.forms[i].elements[j];
		        if (s.type != "textarea") continue;
		        s1 = (s.value).substring(0, getSelectStart(s));
		        s2 = (s.value).substring(getSelectEnd(s), getTextLength(s));
		        if (getMozSelection(s) == "") continue;
		        my_link = prompt('Enter URL:', 'http://');
		        my_title = prompt('Enter Title:', '');
		        if (my_link != null)
		                s.value = s1 + '[a href=[' + my_link + '] title=['  + my_title + ']]' + getMozSelection(s) + '[/a]' + s2;
		        setMozSelection(s, 0, 0);
		}
	}
}

function coolButtons()
{
        arg = coolButtons.arguments;

        for (i = 0; i < arg.length; i++)
        {
                switch (arg[i])
                {
                        case 'b':
                                if (document.selection) 
                                        document.write('<a href="javascript:formatStr(\'b\')"><img src="' + arg[i + 1] + '" height="17" alt="Bold"></a>&nbsp;');
                                else
                                        document.write('<a href="javascript:wrapSelection(\'strong\')"><img src="' + arg[i + 1] + '" height="17" alt="Bold"></a>&nbsp;');
                        break;
                        case 'i':
                                if (document.selection) 
                                        document.write('<a href="javascript:formatStr(\'i\')"><img src="' + arg[i + 1] + '" height="17" alt="Italic"></a>&nbsp;');
                                else
                                        document.write('<a href="javascript:wrapSelection(\'em\')"><img src="' + arg[i + 1] + '" height="17" alt="Italic"></a>&nbsp;');
                        break;
                        case 'u':
                                if (document.selection) 
                                        document.write('<a href="javascript:formatStr(\'u\')"><img src="' + arg[i + 1] + '" height="17" alt="Underline"></a>&nbsp;');
                                else
                                        document.write('<a href="javascript:wrapSelection(\'u\')"><img src="' + arg[i + 1] + '" height="17" alt="Underline"></a>&nbsp;');
                        break;
                        case 'h1':
                                if (document.selection) 
                                        document.write('<a href="javascript:formatStr(\'h1\')"><img src="' + arg[i + 1] + '" height="17" alt="Header"></a>&nbsp;');
                                else
                                        document.write('<a href="javascript:wrapSelection(\'h1\')"><img src="' + arg[i + 1] + '" height="17" alt="Header"></a>&nbsp;');
                        break;
                        case 'link':
                                if (document.selection) 
                                        document.write('<a href="javascript:insertLink()"><img src="' + arg[i + 1] + '" height="17" alt="Insert link"></a>&nbsp;');
                                else
                                        document.write('<a href="javascript:insertMozLink();"><img src="' + arg[i + 1] + '" height="17" alt="Insert Link"></a>&nbsp;');
                        break;
                        default:
                        break;
                }
                i++;
        }
}