//Gral Functions.
function swap(imgIndex) {
	if (imgIndex.alt == imgIndex.name+"2"){
		imgIndex.alt = imgIndex.name+"1";
		imgIndex.src = "core/rte/images/"+imgIndex.alt+".jpg";
	}else if(imgIndex.alt == imgIndex.name+"1"){
		imgIndex.alt = imgIndex.name+"2";
		imgIndex.src = "core/rte/images/"+imgIndex.alt+".jpg";
	}
}

// RTE Functions
function stylefunction(strStyle, strFrameName){
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

	switch(strStyle){
		case "Bold":
			objWindow.document.execCommand("Bold", false, null);
			break;
		case "Italic":
			objWindow.document.execCommand("Italic", false, null);
			break;
		case "Underline":
			objWindow.document.execCommand("Underline", false, null);
			break;
		case "Superscript":
			objWindow.document.execCommand("Superscript", false, null);
			break;
		case "Subscript":
			objWindow.document.execCommand("Subscript", false, null);
			break;
		case "Strikethrough":
			objWindow.document.execCommand("Strikethrough", false, null);
			break;
		default:
			break;
	}
}

function alignfunction(strAlign, strFrameName){
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

	switch(strAlign){
		case "Center":
			objWindow.document.execCommand("JustifyCenter", false, null);
			break;
		case "Left":
			objWindow.document.execCommand("JustifyLeft", false, null);
			break;
		case "Right":
			objWindow.document.execCommand("JustifyRight", false, null);
			break;
		case "Full":
			objWindow.document.execCommand("JustifyFull", false, null);
			break;
		case "Outdent":
			objWindow.document.execCommand("Outdent", false, null);
			break;
		case "Indent":
			objWindow.document.execCommand("Indent", false, null);
			break;
		default:
			break;
	}
}

function listsfunction(strOrder, strFrameName){
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

	switch(strOrder){
		case "O":
			objWindow.document.execCommand("InsertOrderedList", false, null);
			break;
		case "U":
			objWindow.document.execCommand("InsertUnorderedList", false, null);
			break;
		default:
			break;
	}
}

function insertHorizontalRule(strFrameName) {
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

	objWindow.document.execCommand("InsertHorizontalRule", false, null);
}

function fontfacefunction(strFont, strFrameName) {
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

    objWindow.document.execCommand("FontName", false, strFont);
}

function rteSelectAll(strFrameName) {
    var objWindow = window.frames[strFrameName];
    objWindow.focus();
    objWindow.document.execCommand("SelectAll", false, "");
}

function rteSelectNone(strFrameName) {
    var objWindow = window.frames[strFrameName];
    objWindow.focus();
    objWindow.document.execCommand("Unselect", false, "");
}

function fontsizefunction(strSize, strFrameName){
	var objWindow = window.frames[strFrameName];
	objWindow.focus();
	switch(strSize){
		case "Pequeño":
			objWindow.document.execCommand("FontSize", false, 0);
			break;
		case "Mediano":
			objWindow.document.execCommand("FontSize", false, 2);
			break;
		case "Grande":
			objWindow.document.execCommand("FontSize", false, 4);
			break;
		case "XGrande":
			objWindow.document.execCommand("FontSize", false, 7);
			break;
		default:
			break;
	}
}

function fontcolorfunction(strColor, strFrameName){
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

	objWindow.document.execCommand("ForeColor", false, strColor);
}

function insertLink(strFrameName) {
	var objWindow = window.frames[strFrameName];
	objWindow.focus();

	var strLinkTo = window.prompt("Link:");

	objWindow.document.execCommand("CreateLink", false, strLinkTo);
}

var objSharedRTEWindow = false;
function insertImg(strFrameName, objButton) {
     
     //var objWindow = window.frames[strFrameName];
     objSharedRTEWindow = window.frames[strFrameName];
     objSharedRTEWindow.focus();
     
     var arrCoordenadas = getObjAbsoluteCoordinates(objButton);
     arrCoordenadas["left"] -= 160;
     arrCoordenadas["top"] += 35;

     window.open("rte_image_selector.php?host_db&strFrameName="+strFrameName, "windowImageSelector", "fullscreen=no,channelmode=no,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=320,height=150,left="+arrCoordenadas["left"]+",top="+arrCoordenadas["top"]+"").focus();

     //var strLinkTo = window.prompt("Link:");

     //objWindow.document.execCommand("InsertImage", false, strLinkTo);    
}

function insertTwoCols(strFrameName) {
	var objWindow = window.frames[strFrameName];

	if (navigator.userAgent.indexOf('MSIE')==-1) {
		var objTable;
		var objRow;
		var objCell;

		objTable = objWindow.document.createElement("table");
		objTable.cellSpacing = 0;
		objTable.cellPadding = 0;
		objTable.width = "100%";
		objTable.border = "0";

		objRow = objWindow.document.createElement("tr");

		objCell = objWindow.document.createElement("td");
		objCell.width = "50%";
		objCell.style.paddingRight = "10px";
		objCell.style.borderRight = "1px solid #eeeeee";
		objCell.vAlign = "top";
		objCell.innerHTML = "&nbsp;";
		objRow.appendChild(objCell);

		objCell = objWindow.document.createElement("td");
		objCell.width = "50%";
		objCell.style.paddingLeft = "10px";
		objCell.style.borderLeft = "1px solid #eeeeee";
		objCell.vAlign = "top";
		objCell.innerHTML = "&nbsp;";
		objRow.appendChild(objCell);

		objTable.appendChild(objRow);

		objWindow.document.body.appendChild(objTable);
	}
	else {
		objWindow.document.body.insertAdjacentHTML("BeforeEnd", "<table width='100%' cellspacing='2' cellpadding='2' border='0'><tr><td valign='top' style='border-right:1px solid black;'>&nbsp;</td><td valign='top' style='border-left:1px solid black;'>&nbsp;</td></tr></table>");
	}
	objWindow.focus();
}

function insertTwoRows(strFrameName) {
	var objWindow = window.frames[strFrameName];

	if (navigator.userAgent.indexOf('MSIE')==-1) {
		var objTable;
		var objRow;
		var objCell;

		objTable = objWindow.document.createElement("table");
		objTable.cellSpacing = 0;
		objTable.cellPadding = 0;
		objTable.width = "100%";
		objTable.border = "0";

		objRow = objWindow.document.createElement("tr");
		objCell = objWindow.document.createElement("td");
		objCell.style.paddingBottom = "10px";
		objCell.style.borderBottom = "1px solid #eeeeee";
		objCell.innerHTML = "&nbsp;";
		objRow.appendChild(objCell);
		objTable.appendChild(objRow);

		objRow = objWindow.document.createElement("tr");
		objCell = objWindow.document.createElement("td");
		objCell.style.paddingTop = "10px";
		objCell.style.borderTop = "1px solid #eeeeee";
		objCell.innerHTML = "&nbsp;";
		objRow.appendChild(objCell);
		objTable.appendChild(objRow);

		objWindow.document.body.appendChild(objTable);
	}
	else {
		objWindow.document.body.insertAdjacentHTML("BeforeEnd", "<table width='100%' cellspacing='2' cellpadding='2' border='0'><tr><td style='border-bottom:1px solid black;'>&nbsp;</td></tr><tr><td style='border-top:1px solid black;'>&nbsp;</td></tr></table>");
	}
	objWindow.focus();
}

var objSharedRTEWindow = false;
function insertImage(strFrameName, objButton) {
	//var objWindow = window.frames[strFrameName];
	objSharedRTEWindow = window.frames[strFrameName];

	var arrCoordenadas = getObjAbsoluteCoordinates(objButton);
	arrCoordenadas["left"] -= 160;
	arrCoordenadas["top"] += 35;

	window.open("rte_image_selector.php", "windowImageSelector", "fullscreen=no,channelmode=no,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=320,height=150,left="+arrCoordenadas["left"]+",top="+arrCoordenadas["top"]+"").focus();
}

function rte_remplazar( texto, busqueda, remplazo ){
    
    if( texto.indexOf(busqueda) != -1 ) {
        texto =  texto.replace( busqueda, remplazo);
        texto = rte_remplazar( texto, busqueda, remplazo );
    }
    
    return texto;
}