var DHTML = (document.getElementById || document.all || document.layers);

function getObj(name) 
{
    if (document.getElementById) {
        this.obj = document.getElementById(name);
        this.style = document.getElementById(name).style;
    } else if (document.all) {
        this.obj = document.all[name];
        this.style = document.all[name].style;
    } else if (document.layers) {
        this.obj = document.layers[name];
        this.style = document.layers[name];
    }
}

function invi(id, flag) 
{
    if (!DHTML) return;
    var x = new getObj(id);
    x.style.display = (flag) ? 'block' : 'none'
}

function showHide(showLayer, hideLayer) 
{
    invi(showLayer, 1);
    invi(hideLayer, 0);
}

function enableOther(thisBox, otherBox)
{
    thisCheckBox = document.getElementById(thisBox);
    otherCheckBox = document.getElementById(otherBox);
    if (!thisCheckBox.checked)
    {
        otherCheckBox.checked = false;
        disableCheckBox(otherCheckBox);
    }
    else
    {
        enableCheckBox(otherCheckBox);
    }
}

function disableOther(thisBox, otherBox)
{
    thisCheckBox = document.getElementById(thisBox);
    otherCheckBox = document.getElementById(otherBox);
    if (thisCheckBox.checked)
    {
        otherCheckBox.checked = false;
        disableCheckBox(otherCheckBox);
    }
    else
    {
        enableCheckBox(otherCheckBox);
    }
}


function setSlaveCheckBox(master, slave)
{
  masterCheckBox = document.getElementById(master);
  slaveCheckBox = document.getElementById(slave);

   slaveCheckBox.checked = masterCheckBox.checked;
   if (masterCheckBox.checked) {
      disableCheckBox(slaveCheckBox);
   } else {
      enableCheckBox(slaveCheckBox);
   }
}


function disableCheckBox(checkBox)
{
    if (!checkBox.disabled)
    {
        checkBox.disabled = true;
        if (!document.all && !document.getElementById)
        {
            checkBox.storeChecked = checkBox.checked;
            checkBox.oldOnClick = checkBox.onclick;
            checkBox.onclick = preserve;
        }
    }
}

function enableCheckBox(checkBox)
{
    if (checkBox.disabled)
    {
        checkBox.disabled = false;
        if (!document.all && !document.getElementById)
        {
            checkBox.onclick = checkBox.oldOnClick;
        }
    }
}

function disableInput(id)
{
    input = document.getElementById(id);
    if (!input.disabled)
    {
        input.disabled = true;
    }
}

function enableInput(id)
{
    input = document.getElementById(id);
    if (input.disabled)
    {
        input.disabled = false;
    }
}

// Used on administration.html to control the display of a sample email address
function clearSampleEmail(id)
{
    defaultVal = 'username@domain.com';
    input = document.getElementById(id);
    newVal = input.value;
    if (newVal == defaultVal)
    {
        input.value = '';
    }
}

// Used on administration.html to control the display of a sample email address
function replaceSampleEmail(id)
{
    defaultVal = 'username@domain.com';
    input = document.getElementById(id);
    newVal = input.value;
    if ((newVal == defaultVal) || (newVal == ''))
    {
        input.value = defaultVal;
    }
}

// Used on post_message.html to check if any attachment input fields are shown
function checkAttachmentDisplay(max)
{
    for (var i = 0; i < max; i++)
    {
        id = 'newattach' + i + 'block';
        block = document.getElementById(id);
        if (block.style.display == 'block')
        {
            return true;
        }
    }
}

// If any attachment input fields are shown, don't show initial "Attach a file" link
function showInitAttachLink(max) {
    var attachmentDisplay = checkAttachmentDisplay(max);
    if (attachmentDisplay != true)
    {
        invi('newattach0block',1);
    }
}

// Used on post_message.html to check if any subsequent attachment input fields are shown
function checkNextAttachmentDisplay(start, max)
{
	var i= eval(start);
	var maxVal = eval(max);
    for (; i < maxVal; i++)
    {
	    id = 'newattach' + i + 'block';
        block = document.getElementById(id);	
        if (block.style.display == 'block')
        {
            return true;
        }
    }
	return false;
}
// Used on post_message.html to get the id of the previous most active attachment
function getPrevActiveAttachmentDisplay(start)
{
    for (var i = start; i > 0; i--)
    {
        id = 'newattach' + i + 'block';
        block = document.getElementById(id);
        if (block.style.display == 'block')
        {
            return i;
        }
    }
}

// If any attachment subsequent input fields are shown, don't show previous block's "Attach another file" link
function showPreviousAttachLink(start, max) {
    var attachmentDisplay = checkNextAttachmentDisplay(start, max);
    if (attachmentDisplay != true)
    {
        var prev = getPrevActiveAttachmentDisplay(start)
        if(prev)
        {   
            var prevId = 'attachanother' + prev;
            invi(prevId,1);
        } 
    }
}

function clearAttachmentValue(AttachNo)
{
	AttachNo = AttachNo-1;
	var AllAttach = document.getElementsByName("attachments"); 
	for (i = 0; i < AllAttach.length; i++)
        {
		if(i==AttachNo)
		{
			AllAttach[i].setAttribute("type", "text");
			AllAttach[i].value = "";
			AllAttach[i].setAttribute("type", "file");
		}
	}
}
function selectDSAll(formName,thisInput,inputToSet)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (dml.elements[i].name == thisInput)
        {
            if (dml.elements[i].checked)
            {
                setSelected(formName,inputToSet,1);
                setTitleValue(formName,thisInput,'Deselect all');
            }
            else
            {
                setSelected(formName,inputToSet,0);
                setTitleValue(formName,thisInput,'Select all');
            }
        }
    }
}

function setSelected(formName,inputName,val)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (dml.elements[i].name == inputName)
        {
            dml.elements[i].checked = val;
        }
    }
}

function setTitleValue(formName,inputName,val)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (dml.elements[i].name == inputName)
        {
            dml.elements[i].title = val;
        }
    }
}

function selectDSCheckAll(formName,thisInput,inputToSet)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var trueCount = 0;
    var inputCount = 0;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if ((dml.elements[i].name == thisInput) && (dml.elements[i].checked == false))
        {
            setSelected(formName,inputToSet,0);
            setTitleValue(formName,inputToSet,'Select all');
        }
        else
        {
            trueCount++;
        }
        inputCount++;
    }
    if (trueCount == inputCount)
    {
            setSelected(formName,inputToSet,1);
            setTitleValue(formName,inputToSet,'Deselect all');
    }
}


// Used on subscriptions.html to control menu associated with subscribe checkbox
function controlMenu(originalCheckbox, checkBox, previousMenu, menu)
{
    // Check subscription state (0 = not subscribed, 1 = subscribed)
    originalCheckboxVal = document.getElementById(originalCheckbox).value;
    checkBox = document.getElementById(checkBox);
    menu = document.getElementById(menu);
    // Save previous selection in menu
    previousMenuVal = document.getElementById(previousMenu).value;
    len = menu.options.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (menu.options[i].selected)
        {
            previousMenuVal = i;
        }
    }

    if (checkBox.checked)
    {
        menu.disabled = false;
        // Select Message-by-Message if not subscribed
        if (originalCheckboxVal == 0)
        {
            menu.options[0].selected = true;
        }
        else
        {
            menu.options[previousMenuVal].selected = true;
        }
    }
    else
    {
        // Re-select My start page only if not subscribed
        if (originalCheckboxVal == 0)
        {
            menu.options[2].selected = true;
        }
        else
        {
            menu.options[previousMenuVal].selected = true;
        }
        menu.disabled = true;
    }
}

function selectDSSubscriberCheckAll(formName,thisInput,inputToSet,type)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var trueCount = 0;
    var inputCount = 0;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if ((dml.elements[i].name == thisInput) && (dml.elements[i].id.indexOf(type) == 0) && (dml.elements[i].checked == false))
        {
            setSelected(formName,inputToSet,0);
            setTitleValue(formName,inputToSet,'Select all');
        }
        else
        {
            trueCount++;
        }
        inputCount++;
    }
    if (trueCount == inputCount)
    {
            setSelected(formName,inputToSet,1);
            setTitleValue(formName,inputToSet,'Deselect all');
    }
}

function selectDSSubscriberAll(formName,thisInput,inputToSet,type)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (dml.elements[i].name == thisInput )
        {
            if (dml.elements[i].checked)
            {
                setSelectedWithType(formName,inputToSet,1,type);
                setTitleValue(formName,thisInput,'Deselect all');
            }
            else
            {
                setSelectedWithType(formName,inputToSet,0,type);
                setTitleValue(formName,thisInput,'Select all');
            }
        }
    }
}

function setSelectedWithType(formName,inputName,val,type)
{
    dml = document.forms[formName];
    len = dml.elements.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (dml.elements[i].name == inputName && dml.elements[i].id.indexOf(type) == 0)
        {
            dml.elements[i].checked = val;
        }
    }
}

function appendValueToAction(curFormName,formName,inputName)
{
	var actionURL = document.forms[curFormName].action;
	dml = document.forms[formName];
	len = dml.elements.length;
	var i = 0;
	var valueAppend;
	for (i = 0; i < len; i++)
    {
        if (dml.elements[i].name == inputName)
        {
            valueAppend = dml.elements[i].value;
        }
    }
	actionURL = actionURL + "&"+inputName+"="+valueAppend;
	document.forms[curFormName].action = actionURL;
	document.forms[curFormName].submit();
}

function toggleTableDetail(id) 
{ 
  body=document.getElementById(id);
	if (body) 
	{
		if (body.style.display == 'none') 
		{
			try {
			body.style.display='table-row';
			} catch(e) {
			body.style.display = 'block';
			}
		}	
		else 
		{
			body.style.display = 'none';
		}
	}
}

function disableRadio(thisBox, otherBox,formName)
{
    thisCheckBox = document.getElementById(thisBox);
    otherCheckBox = document.getElementById(otherBox);
	dml = document.forms[formName];
    len = dml.elements.length;
    var i = 0;
    for (i = 0; i < len; i++)
    {
        if (dml.elements[i].id == otherBox)
        {
			if (thisCheckBox.checked)
			{      
			   enableCheckBox(dml.elements[i]);
			}
			else
			{
				disableCheckBox(dml.elements[i]);				
			}
		}
	}
}
