var DataFrame_ = 'window.top.FrameBottom';
var PreviewFrame_ = 'window.parent.FrameRightMiddle';
var EditionFrame_ = 'window.parent.FrameLeftBottom';
var EditionForm_ = 'window.parent.FrameLeftBottom.document.EditionForm';
var LinkBarForm_ = 'window.parent.FrameLeftTop.document.LinkBarForm';
var PopupForm_ = 'document.ModalPopup';

var Browser_ = null;
switch (navigator.appName) {
	case "Microsoft Internet Explorer" :
		Browser_ = "IE";
		break;
	case "Netscape" :
		Browser_ = "NS";
		break;
}

function InsertImage(sObject) {
	var ImageSRC = showModalDialog("/Lib/ASP/HTMLEditor/ImageBankIndex.asp", "", "dialogWidth:450px; dialogHeight:540px; status:no; center:yes; scroll:no; help:no;");
	if (ImageSRC) {
		return ImageSRC;
	}
}

function SetEditionFormAction(sValue) {
	if (eval(EditionForm_)) {
		eval(EditionForm_).action = sValue;
	}
	else {
		self.setTimeout('SetEditionFormAction(\'' + sValue + '\')', 500);
	}
}

function DisableSubmitButton(bValue) {
	if (eval(EditionForm_)) { 
		if (eval(EditionForm_).SubmitButton) {
			eval(EditionForm_).SubmitButton.disabled = bValue;
		}
	}
	else {
		self.setTimeout('DisableSubmitButton(\'' + bValue + '\')', 500);
	}
}

function SetListButtonValue(sValue, sLink, sAddon) {
	if (eval(LinkBarForm_)) {
		
		ListButton = window.parent.FrameLeftTop.document.getElementById("ListButton");
		
		if (eval(ListButton) + '' == 'null' ) {
		
			var NewButton	= window.parent.FrameLeftTop.document.createElement("button");
			var NewText		= window.parent.FrameLeftTop.document.createTextNode(sValue);
			
			NewButton.name		= "ListButton";
			NewButton.id		= "ListButton";
			NewButton.onclick	= function SetListButtonValue_() { eval(DataFrame_).location = sLink; };
			if (navigator.appName == "Microsoft Internet Explorer"){
				NewButton.className = "BotaoDinamico Botao4 BotaoFormTopo";
			}
			else {
				NewButton.className = "BotaoDinamico Botao5 BotaoFormTopo";
			}
			NewButton.appendChild(NewText);
			
			eval(LinkBarForm_).appendChild(NewButton);
		}
		else {
			ListButton.onclick	= function SetListButtonValue_() { eval(DataFrame_).document.location.href = sLink; };
		}
		
	}
	else {
		self.setTimeout('SetListButtonValue(\'' + sValue + '\',\'' + sLink + '\',\'' + sAddon + '\')', 500);
	}
}

function SetNewButtonValue(sValue, sLink, sAddon) {
	if (eval(LinkBarForm_)) {
		
		oNewButton = window.parent.FrameLeftTop.document.getElementById("NewButton");
		
		if (eval(oNewButton) + '' == 'null') {
			
			var NewButton	= window.parent.FrameLeftTop.document.createElement("button");
			var NewText		= window.parent.FrameLeftTop.document.createTextNode(sValue);
			
			NewButton.name		= "NewButton";
			NewButton.id		= "NewButton";
			NewButton.className = "BotaoDinamico Botao4 BotaoFormTopo";
			NewButton.onclick	= function SetNewButtonValue_() { document.location.href = sLink; };
			NewButton.appendChild(NewText);
			
			eval(LinkBarForm_).appendChild(NewButton);
		}
		else {
			oNewButton.onclick	= function SetNewButtonValue_() { document.location.href = sLink; };
		}
	}
	else {
		self.setTimeout('SetNewButtonValue(\'' + sValue + '\',\'' + sLink + '\',\'' + sAddon + '\')', 500);
	}
}

function InitPreview(sValue) {
	if (eval(PreviewFrame_)) {
		eval(PreviewFrame_).document.location.href = sValue;
	}
	else {
		self.setTimeout('InitPreview(' + sValue + ')', 500);
	}
}

function CreateModalPopupWindow(sURL, iWidth, iHeight, sParam, sScroll, sName) {
	//var NewWindow = showModalDialog(sURL, sParam, 'dialogWidth:' + iWidth + 'px; dialogHeight:' + iHeight + 'px; status:no; center:yes; resizable:no; unadorned:yes; help:no; scroll:' + sScroll + ';');
	//if (NewWindow) {
	//	return NewWindow;
	//}
		
	var iTop = (screen.height - iHeight) / 2;
	var iLeft = (screen.width - iWidth) / 2;
	
	if (sName == "") {
		sName = null;
	}
	window.open(sURL,sName,'width=' + iWidth + ',height=' + iHeight + ',top=' + iTop + ',left=' + iLeft + ',status=no,toolbar=no,menubar=no,location=no,scrollbars=' + sScroll);
}

function CreateHelpPopupWindow(sValue, iLeft, iTop, iWidth, iHeight) {
	oPopup = window.createPopup();
	oPopupBody = oPopup.document.body;
	oPopupBody.style.backgroundColor = "lightyellow";
	oPopupBody.style.fontFamily = "Arial";
	oPopupBody.style.fontSize = "11px";
	oPopupBody.style.margin = "5px";
	oPopupBody.style.border = "solid black 1px";
	oPopupBody.style.cursor = "help";
    oPopupBody.innerHTML = sValue;
    oPopup.show(iLeft, iTop, iWidth, iHeight, oPopupBody.innerHTML);
	setTimeout('oPopup.hide()', 10000);
}

function CreateSelectOption(sWindowName, sFormName, sValue, sText, sClass) {
	var NewOption	= eval(sWindowName).document.createElement('option');
	var NewText		= eval(sWindowName).document.createTextNode(sText);
	
	NewOption.appendChild(NewText);
	NewOption.value = sValue;
	//NewOption.text = sText;
	NewOption.defaultSelected = false;
	NewOption.selected = true;
	NewOption.className = sClass;
	
	var myForm = eval(sWindowName + '.' + sFormName);
	//eval(sWindowName).eval(sFormName).add(NewOption);
	myForm.appendChild(NewOption);
}

function DeleteSelectOption(sWindowName, sFormName, sValue) {
	//var myForm = eval(sWindowName).eval(sFormName);
	var myForm = eval(sWindowName + '.' + sFormName);
	for (i = 0; i < myForm.length; i++) {
		if (myForm.options[i].value == sValue) {
			myForm.remove(i);
			if (myForm.options[0]) { 
				myForm.options[0].selected = true;
			}
			break;
		} 
	}
}

function ChangeSubmitButtonValue(sValue) {
	if (eval(EditionForm_)) { 
		if (eval(EditionForm_).SubmitButton) {
			eval(EditionForm_).SubmitButton.value = sValue;
		}
	}
	else {
		self.setTimeout('ChangeSubmitButtonValue(\'' + sValue + '\')', 500);
	}
}

function CreateDGLAdminWindow(sURL) {
	var iWidth = screen.width;
	var iHeight = screen.height;
	var sWindowName = 'ISATIDMCDAdminWindow';
	var sScroll = "YES"
	
	if (iWidth == 800 && iHeight == 600) {
		WindowWidth = screen.availWidth - 28;
		WindowHeight = screen.availHeight - 60;
	}
	else {
		WindowWidth = 1000;
		WindowHeight = 680;
	}
	
	xPosition = (screen.availWidth) ? (screen.availWidth - WindowWidth - 10) / 2 : 0;
	yPosition = (screen.availHeight) ? (screen.availHeight - WindowHeight - 20) / 2 : 0;

	if (Browser_ == "IE") {
		//Cria console em uma Modal Window. O unico problema 
		//eh que nao se consegue minizar a janela.
		//NewWindow = CreateModalPopupWindow(sURL, WindowWidth, WindowHeight, window, sScroll);
		//document.location.href = '/Lib/Login/LogoutAction.asp';
		
		//Cria console em popup comum.
		NewWindow = window.open(sURL, sWindowName, 'width=' + WindowWidth + ',height=' + WindowHeight + ',menubar=no,resizable=no,toolbar=no,status=yes,scrollbars=' + sScroll + ',screenX=' + xPosition + ',screenY=' + yPosition + ',left=' + xPosition + ',top=' + yPosition);
	}
	else if (Browser_ == "NS") {
		NewWindow = window.open(sURL, sWindowName, 'width=' + WindowWidth + ',height=' + WindowHeight + ',menubar=no,resizable=no,toolbar=no,status=no,scrollbars=' + sScroll + ',screenX=' + xPosition + ',screenY=' + yPosition + ',left=' + xPosition + ',top=' + yPosition);
	}
}

function Expired(SessionTimeout) {
	alert("Sua sessão expirou por ultrapassar o tempo limite de inatividade (" + SessionTimeout + " minutos)\nou por alguma alteração no sistema.\n\nClique em 'OK' para fechar esta janela.");
	CloseDGLAdminWindow();
}

function CloseDGLAdminWindow() {
	//window.top.opener.location.href = '/';
	window.top.close();
}

//--------------------------------------------------------------
// Ativa a aba do link ativo e seleciona o link no campo select
//--------------------------------------------------------------
function ChangeMenu(sValue, StatusSelect) {

	if (StatusSelect == "S") {
		for (var i = 0; i < window.top.FrameTop.MenuForm.Menu.length; i++) {
			if (window.top.FrameTop.MenuForm.Menu.options[i].text == sValue) {
				window.top.FrameTop.MenuForm.Menu.options[i].selected = true;
			}
		}
	}
	
	var CssExibe, Exibe	= null;
	var CssEmail		= "MenuEmail";
	var CssCampanha		= "MenuCampanha";
	var CssGrupo		= "MenuGrupo";
	var CssRelatorio	= "MenuRelatorio";
	var CssPesquisa		= "MenuPesquisa";
	var Email			= window.top.FrameTop.document.getElementById("Email");
	var Campanha		= window.top.FrameTop.document.getElementById("Campanha");
	var Grupo			= window.top.FrameTop.document.getElementById("Grupo");
	var Relatorio		= window.top.FrameTop.document.getElementById("Relatorio");
	var Pesquisa		= window.top.FrameTop.document.getElementById("Pesquisa");
	
	
	switch (sValue) {
		case "E-Mails" :
			CssExibe	= CssEmail + " Ativo";
			Exibe 		= Email;
			break;
		case "Campanhas" :
			CssExibe	= CssCampanha + " Ativo";
			Exibe 		= Campanha;
			break;
		case "Grupos" :
			CssExibe 	= CssGrupo + " Ativo";
			Exibe 		= Grupo;
			break;
		case "Relatórios" :
			CssExibe 	= CssRelatorio + " Ativo";
			Exibe 		= Relatorio;
			break;
		case "Pesquisas" :
			CssExibe 	= CssPesquisa + " Ativo";
			Exibe 		= Pesquisa;
			break;
		default :
			CssExibe 	= "";
			Exibe 		= "";
			break;
	}
	Email.className		= CssEmail;
	Campanha.className	= CssCampanha;
	Grupo.className		= CssGrupo;
	Relatorio.className	= CssRelatorio;
	Pesquisa.className	= CssPesquisa;
	Exibe.className 	= CssExibe;
}

function ExpandCollapseCustom(ObjMenu, ObjImage) {
	if (ObjMenu.style.display == "none") {
       ObjMenu.style.display = "";
	   ObjImage.src = "/Imagens/Default/icon_minus.gif"
	}
	else {
		ObjMenu.style.display = "none";
	   ObjImage.src = "/Imagens/Default/icon_plus.gif"
	}
	
}

function ExpandCollapse(Obj) {
	if (Obj.style.display == "none") {
       Obj.style.display = "";
	}
	else {
		Obj.style.display = "none";
	}
}

function ChangeSRC(Obj, Image) {
	Obj.src = Image;
}

function trim(str)
{
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}

function ClosePopup(returnValue) {
	if (returnValue == 1) {
		window.parent.window.opener.location.reload();
	}
	window.parent.window.close();
}

function EditorInsertElement(objEditor, oElement, sText, bPopup) {
	
	if (navigator.appName == "Microsoft Internet Explorer"){ // is IE
		sel = objEditor.document.selection.createRange();
		sel.pasteHTML(sText);
    }
    else {

		if (bPopup) {
			objEditor.designEditor = eval("window.parent.window.opener.document.getElementById('" + objEditor.id + "_designEditor').contentWindow");
			doc = window.parent.window.opener.document;
		}
		else {
			objEditor.designEditor = eval("document.getElementById('" + objEditor.id + "_designEditor').contentWindow");
			doc = document;
		}

		var toBeInserted = eval(oElement);	    
		var sel = objEditor.designEditor.getSelection();
		var range = EditorCreateRange(objEditor.designEditor, sel);
		
		// remove the current selection
		sel.removeAllRanges();
		range.deleteContents();
		
		var node = range.startContainer;
		var pos = range.startOffset;
		
		switch (node.nodeType) {
		    case 1: // Node.ELEMENT_NODE
				var selnode = toBeInserted;
				node.insertBefore(toBeInserted, node.childNodes[pos]);
				break;
			case 3: // Node.TEXT_NODE
				if ( toBeInserted.nodeType == 3 )
				{
					// do optimized insertion
					node.insertData(pos, toBeInserted.data);
					range = EditorCreateRange(objEditor.designEditor, sel);
					range.setEnd(node, pos + toBeInserted.length);
					range.setStart(node, pos + toBeInserted.length);
					sel.addRange(range);
				}
				else
				{
					node = node.splitText(pos);
					if ( toBeInserted.nodeType == 11 /* Node.DOCUMENT_FRAGMENT_NODE */ )
					{
					selnode = selnode.firstChild;
					}
					node.parentNode.insertBefore(toBeInserted, node);
				}
				break;
		}
    } 
}

function EditorCreateRange(objDesignEditor, sel) {
	if (typeof sel != "undefined") {
		try {
			range = sel.getRangeAt(0);
		} catch(e) {
			range =  objDesignEditor.document.createRange();
		}
	} else {
		range = objDesignEditor.document.createRange();
	}
	return range;
}

function EditorGetSelection(Editor) {
	if (navigator.appName == "Microsoft Internet Explorer"){
		return eval(Editor).document.selection.createRange().text;
	}
	else {
		objDesignEditor = document.getElementById(Editor).contentWindow;
		return objDesignEditor.getSelection();
	}
}

function EditorInsertLink(objEditor, sUrl, bPopup) {
	//if (bPopup) {
	//	objEditor.designEditor = eval("window.parent.window.opener.document.getElementById('" + objEditor.id + "_designEditor').contentWindow");
	//	doc = window.parent.window.opener.document;
	//}
	//else {
	//	objEditor.designEditor = eval("document.getElementById('" + objEditor.id + "_designEditor').contentWindow");
	//	doc = document;
	//}
	doc = window.parent.window.opener.document;
	var oLink = doc.createElement('a');
	var oText = doc.createTextNode('testeLink');
	oLink.href= sUrl;
	oLink.appendChild(oText);
	EditorInsertElement(objEditor, oLink, 'teste', bPopup);
}
