function locateElement(pName){
	var eElem = document.getElementById(pName);
	if (eElem != undefined)
		return eElem;
	return false;
}

function submitForm(pLink) {
    if (locateElement('submitForm')) {
        locateElement('submitForm').action = pLink;
        locateElement('submitForm').submit();
    }
}

function processForm(pLink) {
	if (locateElement('processForm')){
		locateElement('processForm').action = pLink;
		locateElement('processForm').submit();
	}
}

function submitFormMultiPart(pLink) {
	if (locateElement('submitForm')){
		locateElement('submitForm').encoding = "multipart/form-data";
		locateElement('submitForm').action = pLink;
		locateElement('submitForm').submit();
	}
}


function getSelectText(pName){
	var eSelect;
	if (locateElement(pName)) {
		eSelect = locateElement(pName);
		return eSelect.options[eSelect.selectedIndex].text;
	}
}

function getSelectValue(pName){
	var eSelect;
	if (locateElement(pName)) {
		eSelect = locateElement(pName);
		return eSelect.options[eSelect.selectedIndex].value;
	}
}

function processHTML(){
		var tempValue = oEdit1.getHTML();
		locateElement('Release').value = tempValue.replace(/\&nbsp;/g, '-nbsp-');
}

function updatePage(pPage){
	locateElement('Page').value = pPage;
}

function updateInput(pInput,pValue){
	locateElement(pInput).value = pValue;
}

function bulkAddIgnore() {
    if (locateElement("SkipUpdates")) {
        if (locateElement("SkipUpdates").checked) {
            locateElement("RemoveClients").disabled = true;
            locateElement("RemoveMailingLists").disabled = true;
        }
        else {
            locateElement("RemoveClients").disabled = false;
            locateElement("RemoveMailingLists").disabled = false;
        }
    }
}

function editUserAccessChange() {
    var eAccessID = locateElement('PRAccessID');
    if (eAccessID) {
        iAccessID = getSelectValue('PRAccessID');
        if (iAccessID == 1 || iAccessID == 2) {
            locateElement('clientArea').style.visibility = "hidden";
            locateElement('mailinglistArea').style.visibility = 'hidden';
        }
        else {
            locateElement('clientArea').style.visibility = "visible";
            locateElement('mailinglistArea').style.visibility = 'visible';
        }
    }
}

function removeEmbargo() {
	var sDate = "";
	var dDate = new Date();
	if (locateElement('EmbargoDate')) {
		if (dDate.getDate() < 10)
			sDate = "0";
		sDate = sDate.toString() + dDate.getDate().toString();
		sDate = sDate.toString() + "-";
		if (dDate.getMonth()+1 < 10)
			sDate = sDate.toString() + "0";
		sDate = sDate.toString() + (dDate.getMonth()+1);
		sDate = sDate.toString() + "-";
		sDate = sDate.toString() + dDate.getFullYear().toString();
		locateElement('EmbargoDate').value = sDate;
	}
	if (locateElement('EmbargoHours'))
		locateElement('EmbargoHours').selectedIndex = 1;
	if (locateElement('EmbargoMins'))
		locateElement('EmbargoMins').selectedIndex = 1;
}

function countPlainChars(pElement){
	if (locateElement('plainCharCount') != undefined){
		if (locateElement('ReleaseTypeID').value == '1')
			locateElement('plainCharCount').innerHTML = "N/A";
		else {
			locateElement('plainCharCount').innerHTML = pElement.value.length + "/160";
		}
	}
}

function updateFileName(pElement){
	if (locateElement('FileNameCopy')!= undefined) {
		locateElement('FileNameCopy').value = pElement.value;
		if (locateElement('goButton')){
			if (locateElement('FileNameCopy').value.length < 1) {
				locateElement('goButton').style.visibility = "visible";
			}
			else {
				locateElement('goButton').style.visibility = "hidden";
			}
		}
	}
}

function adjustPreviewFrame() {
	parent.window.document.getElementById('previewFrame').style.height = (locateElement('previewBody').scrollHeight + 0) + 'px';
	parent.window.document.getElementById('buttons').style.visibility = 'visible'; 
}

function updateTemplateIDListWindow(pElem) {
	if (locateElement('templateWindow')){
		locateElement('templateWindow').src="/admin/backoffice/templateSelect.asp?ID=" + getSelectValue(pElem.id);
	}
}

function updateTemplateIDList() {
	var i;
	var tempSelection;
	var newOption;
	var optValue;
  var optText;
	var parentSelect =  parent.window.document.getElementById('TemplateID');
	var thisSelect = locateElement('TemplateID');

	if (parentSelect != undefined){
		tempSelection = parentSelect.options[parentSelect.selectedIndex].value;
		for(i=parentSelect.options.length;i>=0;i--){
			parentSelect.remove(i);
		}
		for(i=0;i<thisSelect.options.length;i++){
			newOption = elOptNew = document.createElement('option');
			newOption.value = thisSelect.options[i].value;
			newOption.text = thisSelect.options[i].text;
			if (i == 1 && thisSelect.options.length == 2)
			    newOption.selected = true;
			try {
				parentSelect.add(newOption, null);
			}
			catch(ex) {
		    parentSelect.add(newOption);
		  }
		}
	}
}

function highlight(pElement) {
    if (pElement.id == '') {
        if (pElement.style.backgroundColor == '')
            pElement.style.backgroundColor = '#E0FFFF'
        else
            pElement.style.backgroundColor = '';
    }
    else {
        var sName = pElement.id.replace(/row-[0-9]-/i, '');
        for (i = 1; i < 10; i++) {
            if (locateElement('row-' + i + '-' + sName)){
                if (locateElement('row-' + i + '-' + sName).style.backgroundColor == '')
                    locateElement('row-' + i + '-' + sName).style.backgroundColor = '#E0FFFF'
                else
                    locateElement('row-' + i + '-' + sName).style.backgroundColor = '';
            }
        }
    }
}

function ChangeTipRule(pValue) {
    var mysheet = document.styleSheets[0]
    var myrules = mysheet.cssRules ? mysheet.cssRules : mysheet.rules
    for (i = 0; i < myrules.length; i++) {
        if (myrules[i].selectorText.toLowerCase() == ".tip") {
            targetrule = myrules[i]
            targetrule.style.borderColor = pValue;
            break;
        }
    } 
}  

function tipCookie(pElement) {
    if (pElement.checked) {
        document.cookie = 'tips=1;path=/';
        ChangeTipRule('#ff0000');
    }
    else {
        document.cookie = 'tips=;path=/';
        ChangeTipRule('transparent');
    }

}

function animatePulldown() {
    var i;
    var tempHeight;
    var tempControlHeight
    var pResize = false;
    for (i = 0; i <= 255; i++) {
        if (locateElement('pull-' + i)) {
            if (locateElement('pull-' + i).className == 'pullDownUp' && locateElement('pullContent-' + i).clientHeight > 0) {
                tempHeight = locateElement('pullContent-' + i).clientHeight - 10;
                if (tempHeight < 0)
                    tempHeight = 0;
                locateElement('pullContent-' + i).style.height = tempHeight + 'px';
                pResize = true;
            }
            if (locateElement('pull-' + i).className == 'pullDownDown' && locateElement('pullContent-' + i).clientHeight < document.getElementById('pullContent-' + i).scrollHeight) {
                tempControlHeight = locateElement('pullContent-' + i).scrollHeight
                tempHeight = locateElement('pullContent-' + i).clientHeight + 10;
                if (tempHeight > tempControlHeight)
                    tempHeight = tempControlHeight;
                locateElement('pullContent-' + i).style.height = tempHeight + 'px';
                if (locateElement('pullContent-' + i).clientHeight > locateElement('pullContent-' + i).scrollHeight)
                    locateElement('pullContent-' + i).clientHeight = locateElement('pullContent-' + i).scrollHeight + 'px';
                pResize = true;
            }
        }
    }
    if (pResize == false)
        clearInterval(pulldownTimer);
}

function showPulldown(pNum) {
    var iClear;
    iClear = false;
    pulldownTimer = setInterval("animatePulldown()", 25);
    var pPulldown = locateElement('pull-' + pNum);
    var pPulldownButton = locateElement('pullButton-' + pNum);
    var i;

    if (pPulldown.className == 'pullDownUp') {
        pPulldown.className = 'pullDownDown';
        pPulldownButton.src = '/images/button-down.png';
    }
    else {
        pPulldown.className = 'pullDownUp';
        pPulldownButton.src = '/images/button-up.png';
    }

    if (locateElement("collapse"))
        if (locateElement("collapse").checked)
            iClear = true;

    for (i = 0; i <= 255; i++) {
        if (i != pNum && iClear) {
            if (locateElement('pull-' + i)) {
                locateElement('pull-' + i).className = 'pullDownUp';
                locateElement('pullButton-' + i).src = '/images/button-up.png';
            }
        }
    }
}

function releaseMailingLists(pID) {
    var i;
    if (typeof mailingListTimer != 'undefined')
        clearInterval(mailingListTimer);
    if (locateElement("mailingListArea") && locateElement('mailingListBoxes-' + pID)) {
        for (i = 0; i <= 255; i++) {
            if (locateElement('mailingListBoxes-' + i))
                locateElement('mailingListBoxes-' + i).style.display = "none";
            if (locateElement('mailingListLink-' + i))
                locateElement('mailingListLink-' + i).className = "releaseMailingListOff";
        }
        locateElement('mailingListBoxes-' + pID).style.display = "block";
        locateElement('mailingListLink-' + pID).className = "releaseMailingListOn";
    }
    locateElement('mailingListArea').style.height = '1px';
    mailingListTimer = setInterval("animateMailingList()", 25);
}

function animateMailingList() {
    var i;
    var tempHeight;
    var tempControlHeight;
    var pResize = false;
    if (locateElement('mailingListArea')) {
        tempHeight = locateElement('mailingListArea').clientHeight;
        tempControlHeight = locateElement('mailingListArea').scrollHeight;
        for (i = 0; i <= 3; i++) {
            if (tempHeight >= tempControlHeight) {
                tempHeight = tempHeight - 2;
            }
            else {
                tempHeight = tempHeight + 1;
            }

        }
        if (tempHeight != tempControlHeight)
            pResize = true;
        locateElement('mailingListArea').style.height = tempHeight + 'px';
        
    }
    if (pResize == false)
        clearInterval(mailingListTimer);
}

function releaseMailingListAll(pNum) {
    var i;
    var aList;
    if (locateElement('mailingListBoxes-' + pNum)) {
        var eElement = locateElement('mailingListBoxes-' + pNum);
        aList = eElement.getElementsByTagName('INPUT');
        for (i = 0; i <= aList.length; i++) {
            aList[i].checked = true;
        }
    }
}

function selectAll(pName) {
    var i;
    var aList;
    if (locateElement(pName)) {
        var eElement = locateElement(pName);
        aList = eElement.getElementsByTagName('INPUT');
        for (i = 0; i <= aList.length; i++) {
            aList[i].checked = true;
        }
    }
}

function createPlainText() {
    var plainText = '';
    var stripHTML = RegExp("(<([^>]+)>)", "gi");
    var editorData = CKEDITOR.instances.ReleaseHTML.getData();

    editorData = editorData.replace(/\t/g, "");
    editorData = editorData.replace(/\n/g, "");
    editorData = editorData.replace(/\r/g, "");
    editorData = editorData.replace(/&nbsp;/g, "");

    editorData = editorData.replace(/<br>/g, "\n");
    editorData = editorData.replace(/<br\/>/g, "\n");
    editorData = editorData.replace(/<br \/>/g, "\n");
    editorData = editorData.replace(/<\/p>/g, "\n\n");
    editorData = editorData.replace(/<\/div>/g, "\n");
    editorData = editorData.replace(/<\/tr>/g, "\n");
    editorData = editorData.replace(/<\/h1>/g, "\n");
    editorData = editorData.replace(/<\/h2>/g, "\n");
    editorData = editorData.replace(/<\/h3>/g, "\n");
    editorData = editorData.replace(/<\/h4>/g, "\n");
    editorData = editorData.replace(/<\/h5>/g, "\n");
    editorData = editorData.replace(/<\/li>/g, "\n\n");
    editorData = editorData.replace(/<\/table>/g, "\n");

    editorData = editorData.replace(/<blockquote/g, "\n<blockquote");
    editorData = editorData.replace(/<table/g, "\n<table");

    editorData = editorData.replace(/&ndash;/g, "–");
    editorData = editorData.replace(/&mdash;/g, "—");
    editorData = editorData.replace(/&iexcl;/g, "¡");
    editorData = editorData.replace(/&iquest;/g, "¿");
    editorData = editorData.replace(/&quot;/g, '"');
    editorData = editorData.replace(/&laquo;/g, "«");
    editorData = editorData.replace(/&raquo;/g, "»");
    editorData = editorData.replace(/&amp;/g, "&");
    editorData = editorData.replace(/&cent;/g, "¢");
    editorData = editorData.replace(/&copy;/g, "©");
    editorData = editorData.replace(/&deg;/g, "°");
    editorData = editorData.replace(/&divide;/g, "÷");
    editorData = editorData.replace(/&times;/g, "×");
    editorData = editorData.replace(/&gt;/g, ">");
    editorData = editorData.replace(/&lt;/g, "<");
    editorData = editorData.replace(/&micro;/g, "µ");
    editorData = editorData.replace(/&middot;/g, "·");
    editorData = editorData.replace(/&plusmn;/g, "±");
    editorData = editorData.replace(/&euro;/g, "€");
    editorData = editorData.replace(/&pound;/g, "£");
    editorData = editorData.replace(/&reg;/g, "®");
    editorData = editorData.replace(/&sect;/g, "§");
    editorData = editorData.replace(/&yen;/g, "¥");
    editorData = editorData.replace(/&#180;/g, "´");
    editorData = editorData.replace(/&#96;/g, "`");
    editorData = editorData.replace(/&aacute;/g, "á");
    editorData = editorData.replace(/&Aacute;/g, "Á");
    editorData = editorData.replace(/&agrave;/g, "à");
    editorData = editorData.replace(/&Agrave;/g, "À");
    editorData = editorData.replace(/&acirc;/g, "â");
    editorData = editorData.replace(/&Acirc;/g, "Â");
    editorData = editorData.replace(/&aring;/g, "å");
    editorData = editorData.replace(/&Aring;/g, "Å");
    editorData = editorData.replace(/&atilde;/g, "ã");
    editorData = editorData.replace(/&Atilde;/g, "Ã");
    editorData = editorData.replace(/&auml;/g, "ä");
    editorData = editorData.replace(/&Auml;/g, "Ä");
    editorData = editorData.replace(/&aelig;/g, "æ");
    editorData = editorData.replace(/&AElig;/g, "Æ");
    editorData = editorData.replace(/&ccedil;/g, "ç");
    editorData = editorData.replace(/&Ccedil;/g, "Ç");
    editorData = editorData.replace(/&ETH;/g, "Ð");
    editorData = editorData.replace(/&eacute;/g, "é");
    editorData = editorData.replace(/&Eacute;/g, "É");
    editorData = editorData.replace(/&egrave;/g, "è");
    editorData = editorData.replace(/&Egrave;/g, "È");
    editorData = editorData.replace(/&ecirc;/g, "ê");
    editorData = editorData.replace(/&Ecirc;/g, "Ê");
    editorData = editorData.replace(/&euml;/g, "ë");
    editorData = editorData.replace(/&Euml;/g, "Ë");
    editorData = editorData.replace(/&iacute;/g, "í");
    editorData = editorData.replace(/&Iacute;/g, "Í");
    editorData = editorData.replace(/&igrave;/g, "ì");
    editorData = editorData.replace(/&Igrave;/g, "ì");
    editorData = editorData.replace(/&icirc;/g, "î");
    editorData = editorData.replace(/&Icirc;/g, "Î");
    editorData = editorData.replace(/&iuml;/g, "ï");
    editorData = editorData.replace(/&Iuml;/g, "Ï");
    editorData = editorData.replace(/&ntilde;/g, "ñ");
    editorData = editorData.replace(/&Ntilde;/g, "Ñ");
    editorData = editorData.replace(/&oacute;/g, "ó");
    editorData = editorData.replace(/&Oacute;/g, "Ó");
    editorData = editorData.replace(/&ograve;/g, "ò");
    editorData = editorData.replace(/&Ograve;/g, "Ò");
    editorData = editorData.replace(/&ocirc;/g, "ô");
    editorData = editorData.replace(/&Ocirc;/g, "Ô");
    editorData = editorData.replace(/&oslash;/g, "ø");
    editorData = editorData.replace(/&Oslash;/g, "Ø");
    editorData = editorData.replace(/&otilde;/g, "õ");
    editorData = editorData.replace(/&Otilde;/g, "Õ");
    editorData = editorData.replace(/&ouml;/g, "ö");
    editorData = editorData.replace(/&Ouml;/g, "Ö");
    editorData = editorData.replace(/&szlig;/g, "ß");
    editorData = editorData.replace(/&uacute;/g, "ú");
    editorData = editorData.replace(/&Uacute;/g, "Ú");
    editorData = editorData.replace(/&ugrave;/g, "ù");
    editorData = editorData.replace(/&Ugrave;/g, "Ù");
    editorData = editorData.replace(/&ucirc;/g, "û");
    editorData = editorData.replace(/&Ucirc;/g, "Û");
    editorData = editorData.replace(/&uuml;/g, "ü");
    editorData = editorData.replace(/&Uuml;/g, "Ü");
    editorData = editorData.replace(/&yuml;/g, "ÿ");
    editorData = editorData.replace(/&yacute;/g, "ý");
    editorData = editorData.replace(/&Yacute;/g, "Ý");

    editorData = editorData.replace(/&scaron;/g, "Š");
    editorData = editorData.replace(/&Scaron;/g, "Š");

    editorData = editorData.replace(/&#39;/g, "'");
    editorData = editorData.replace(/&lsquo;/g, "'");
    editorData = editorData.replace(/&ldquo;/g, '"');
    editorData = editorData.replace(/&rsquo;/g, "'");
    editorData = editorData.replace(/&rdquo;/g, '"');
    editorData = editorData.replace(/&bsquo;/g, '‚');
    editorData = editorData.replace(/&bdquo;/g, '„');
    editorData = editorData.replace(/&hellip;/g, "...");

    editorData = editorData.replace(/&ordf;/g, 'ª');
    editorData = editorData.replace(/&macr;/g, '¯');
    editorData = editorData.replace(/&sup1;/g, '¹');
    editorData = editorData.replace(/&sup2;/g, '²');
    editorData = editorData.replace(/&sup3;/g, '³');
    editorData = editorData.replace(/&frac14;/g, '¼');
    editorData = editorData.replace(/&frac12;/g, '½');
    editorData = editorData.replace(/&frac34;/g, '¾');


    plainText = editorData.replace(stripHTML, "");
    locateElement('ReleasePlainText').value = plainText;
}

