//commands ids
// DHTML Editing Component Constants for JavaScript
// InterAKT Online


var displayMode = 'RICH';//display mode
var swapToMode = 'CODE';// the mode to which the swapping is made 
var framesLoaded = false;
//
// Command IDs
//
DECMD_BOLD =                      5000;
DECMD_COPY =                      5002;
DECMD_CUT =                       5003;
DECMD_DELETE =                    5004;
DECMD_DELETECELLS =               5005;
DECMD_DELETECOLS =                5006;
DECMD_DELETEROWS =                5007;
DECMD_FINDTEXT =                  5008;
DECMD_FONT =                      5009;
DECMD_GETBACKCOLOR =              5010;
DECMD_GETBLOCKFMT =               5011;
DECMD_GETBLOCKFMTNAMES =          5012;
DECMD_GETFONTNAME =               5013;
DECMD_GETFONTSIZE =               5014;
DECMD_GETFORECOLOR =              5015;
DECMD_HYPERLINK =                 5016;
DECMD_IMAGE =                     5017;
DECMD_INDENT =                    5018;
DECMD_INSERTCELL =                5019;
DECMD_INSERTCOL =                 5020;
DECMD_INSERTROW =                 5021;
DECMD_INSERTTABLE =               5022;
DECMD_ITALIC =                    5023;
DECMD_JUSTIFYCENTER =             5024;
DECMD_JUSTIFYLEFT =               5025;
DECMD_JUSTIFYRIGHT =              5026;
DECMD_LOCK_ELEMENT =              5027;
DECMD_MAKE_ABSOLUTE =             5028;
DECMD_MERGECELLS =                5029;
DECMD_ORDERLIST =                 5030;
DECMD_OUTDENT =                   5031;
DECMD_PASTE =                     5032;
DECMD_REDO =                      5033;
DECMD_REMOVEFORMAT =              5034;
DECMD_SELECTALL =                 5035;
DECMD_SEND_BACKWARD =             5036;
DECMD_BRING_FORWARD =             5037;
DECMD_SEND_BELOW_TEXT =           5038;
DECMD_BRING_ABOVE_TEXT =          5039;
DECMD_SEND_TO_BACK =              5040;
DECMD_BRING_TO_FRONT =            5041;
DECMD_SETBACKCOLOR =              5042;
DECMD_SETBLOCKFMT =               5043;
DECMD_SETFONTNAME =               5044;
DECMD_SETFONTSIZE =               5045;
DECMD_SETFORECOLOR =              5046;
DECMD_SPLITCELL =                 5047;
DECMD_UNDERLINE =                 5048;
DECMD_UNDO =                      5049;
DECMD_UNLINK =                    5050;
DECMD_UNORDERLIST =               5051;
DECMD_PROPERTIES =                5052;

//
// Enums
//

// OLECMDEXECOPT  
OLECMDEXECOPT_DODEFAULT =         0;
OLECMDEXECOPT_PROMPTUSER =        1;
OLECMDEXECOPT_DONTPROMPTUSER =    2;

// DHTMLEDITCMDF
DECMDF_NOTSUPPORTED =             0;
DECMDF_DISABLED =                 1;
DECMDF_ENABLED =                  3;
DECMDF_LATCHED =                  7;
DECMDF_NINCHED =                  11;

// DHTMLEDITAPPEARANCE
DEAPPEARANCE_FLAT =               0;
DEAPPEARANCE_3D =                 1;

// OLE_TRISTATE
OLE_TRISTATE_UNCHECKED =          0;
OLE_TRISTATE_CHECKED =            1;
OLE_TRISTATE_GRAY =               2;

  var StyleSheet=null;
  var errorString = "Sorry but this web page needs\nWindows95 and Internet Explorer 5 or above to view."
  var Ok = "false";
  var name =  navigator.appName;
  var version =  parseFloat(navigator.appVersion);
  var platform = navigator.platform;
	if (platform == "Win32" && name == "Microsoft Internet Explorer" && version >= 4){
		Ok = "true";
	} else {
		Ok = "false";
	}

	if (Ok == "false") {
		alert(errorString);
	}
function fix4NS(instr){
	var re  = /STYLE=\"WIDTH\s*:\s*(\d+)px;\s*HEIGHT:\s*(\d+)px;*\s*\"/gi;
        var re1 = /<A\s+href=".*?"\s*>\s*<\/A>/gi; // for taking out blank links
        instr=instr.replace(re1, "");
	return(instr.replace(re, "width=$1 height=$2"));
}	

function copyValue() {
	var theHtml = "" + document.frames("activeElement").document.frames("textEdit").document.body.innerHTML + "";
	document.all.EditorValue.value = theHtml;
}

function OnFormSubmit(){
		copyValue();
		document.fHtmlEditor.submit();
}

function open_bg_palette(){
	rs("selectColor","selcolor.php",400,300); 
}

function InsertImage(){
	rs("uploadImage","image.php", 270, 100);
}
function rs(u,n,w,h) {
  args="width="+w+",height="+h+",resizable=no,scrollbars=no,status=1";
  remote=window.open(n, u,args);
  if (remote != null) {
    if (remote.opener == null)
      remote.opener = self;
  }
  return remote;
}



var MENU_SEPARATOR = ""; 
var docComplete = false;
var initialDocComplete = false;
var QueryStatusToolbarButtons = new Array();
var QueryStatusEditMenu = new Array();
var QueryStatusFormatMenu = new Array();
var QueryStatusHTMLMenu = new Array();
var QueryStatusTableMenu = new Array();
var QueryStatusZOrderMenu = new Array();
var ContextMenu = new Array();
var GeneralContextMenu = new Array();
var TableContextMenu = new Array();
var AbsPosContextMenu = new Array();
//constructor for context menu
function ContextMenuItem(string, cmdId) {
  this.string = string;
  this.cmdId = cmdId;
}

function Initialize(obiectul, oName) {
    activeElement=obiectul;
	  GeneralContextMenu[0] = new ContextMenuItem("Cut", DECMD_CUT);
    GeneralContextMenu[1] = new ContextMenuItem("Copy", DECMD_COPY);
    GeneralContextMenu[2] = new ContextMenuItem("Paste", DECMD_PASTE);
    TableContextMenu[0] = new ContextMenuItem(MENU_SEPARATOR, 0);
    TableContextMenu[1] = new ContextMenuItem("Insert Row", DECMD_INSERTROW);
    TableContextMenu[2] = new ContextMenuItem("Delete Rows", DECMD_DELETEROWS);
    TableContextMenu[3] = new ContextMenuItem(MENU_SEPARATOR, 0);
    TableContextMenu[4] = new ContextMenuItem("Insert Column", DECMD_INSERTCOL);
    TableContextMenu[5] = new ContextMenuItem("Delete Columns", DECMD_DELETECOLS);
    TableContextMenu[6] = new ContextMenuItem(MENU_SEPARATOR, 0);
    TableContextMenu[7] = new ContextMenuItem("Insert Cell", DECMD_INSERTCELL);
    TableContextMenu[8] = new ContextMenuItem("Delete Cells", DECMD_DELETECELLS);
    TableContextMenu[9] = new ContextMenuItem("Merge Cells", DECMD_MERGECELLS);
    TableContextMenu[10] = new ContextMenuItem("Split Cell", DECMD_SPLITCELL);
    AbsPosContextMenu[0] = new ContextMenuItem(MENU_SEPARATOR, 0);
    AbsPosContextMenu[1] = new ContextMenuItem("Send To Back", DECMD_SEND_TO_BACK);
    AbsPosContextMenu[2] = new ContextMenuItem("Bring To Front", DECMD_BRING_TO_FRONT);
    AbsPosContextMenu[3] = new ContextMenuItem(MENU_SEPARATOR, 0);
    AbsPosContextMenu[4] = new ContextMenuItem("Send Backward", DECMD_SEND_BACKWARD);
    AbsPosContextMenu[5] = new ContextMenuItem("Bring Forward", DECMD_BRING_FORWARD);
    AbsPosContextMenu[6] = new ContextMenuItem(MENU_SEPARATOR, 0);
    AbsPosContextMenu[7] = new ContextMenuItem("Send Below Text", DECMD_SEND_BELOW_TEXT);
    AbsPosContextMenu[8] = new ContextMenuItem("Bring Above Text", DECMD_BRING_ABOVE_TEXT);
}

function InsertTable() {
  var pVar = document.ObjTableInfo;
  var args = new Array();
  var arr = null;
     
  args["NumRows"] = document.ObjTableInfo.NumRows;
  args["NumCols"] = document.ObjTableInfo.NumCols;
  args["TableAttrs"] = document.ObjTableInfo.TableAttrs;
  args["CellAttrs"] = document.ObjTableInfo.CellAttrs;
  args["Caption"] = document.ObjTableInfo.Caption;
  arr = null;
  arr = showModalDialog( NEXT_ROOT+"includes/next/ktedit/instable.php",
                             args,
                             "font-family:Verdana; font-size:12; dialogWidth:36em; dialogHeight:26em");
  if (arr != null) {
    for ( elem in arr ) {
      if ("NumRows" == elem && arr["NumRows"] != null) {
        document.ObjTableInfo.NumRows = arr["NumRows"];
      } else if ("NumCols" == elem && arr["NumCols"] != null) {
        document.ObjTableInfo.NumCols = arr["NumCols"];
      } else if ("TableAttrs" == elem) {
        document.ObjTableInfo.TableAttrs = arr["TableAttrs"];
      } else if ("CellAttrs" == elem) {
        document.ObjTableInfo.CellAttrs = arr["CellAttrs"];
      } else if ("Caption" == elem) {
        document.ObjTableInfo.Caption = arr["Caption"];
      }
    }
    activeElement.ExecCommand(DECMD_INSERTTABLE,OLECMDEXECOPT_DODEFAULT, pVar);  
  }
}

function Editor_ShowContextMenu() {
  var menuStrings = new Array();
  var menuStates = new Array();
  var state;
  var i
  var idx = 0;
  // Rebuild the context menu. 
  ContextMenu.length = 0;
  // Always show general menu
  for (i=0; i<GeneralContextMenu.length; i++) {
    ContextMenu[idx++] = GeneralContextMenu[i];
  }
  // Is the selection inside a table? Add table menu if so
  if (activeElement.QueryStatus(DECMD_INSERTROW) != DECMDF_DISABLED) {
    for (i=0; i<TableContextMenu.length; i++) {
      ContextMenu[idx++] = TableContextMenu[i];
    }
  }
  // Is the selection on an absolutely positioned element? Add z-index commands if so
  if (activeElement.QueryStatus(DECMD_LOCK_ELEMENT) != DECMDF_DISABLED) {
    for (i=0; i<AbsPosContextMenu.length; i++) {
      ContextMenu[idx++] = AbsPosContextMenu[i];
    }
  }
  // Set up the actual arrays that get passed to SetContextMenu
  for (i=0; i<ContextMenu.length; i++) {
    menuStrings[i] = ContextMenu[i].string;
    if (menuStrings[i] != MENU_SEPARATOR) {
      state = activeElement.QueryStatus(ContextMenu[i].cmdId);
    } else {
      state = DECMDF_ENABLED;
    }
    if (state == DECMDF_DISABLED || state == DECMDF_NOTSUPPORTED) {
      menuStates[i] = OLE_TRISTATE_GRAY;
    } else if (state == DECMDF_ENABLED || state == DECMDF_NINCHED) {
      menuStates[i] = OLE_TRISTATE_UNCHECKED;
    } else { // DECMDF_LATCHED
      menuStates[i] = OLE_TRISTATE_CHECKED;
    }
  }
  // Set the context menu
  activeElement.SetContextMenu(menuStrings, menuStates);
}

function Editor_ContextMenuAction(itemIndex) {  
  if (ContextMenu[itemIndex].cmdId == DECMD_INSERTTABLE) {
    InsertTable();
  } else {
    activeElement.ExecCommand(ContextMenu[itemIndex].cmdId, OLECMDEXECOPT_DODEFAULT);
  }
}
function open_bg_palette(){	
  var arr = showModalDialog(  NEXT_ROOT+"includes/next/ktedit/selcolor.php",
                             "",
                             "font-family:Verdana; font-size:12; dialogWidth:30em; dialogHeight:30em" );
  if (arr != null) {
    activeElement.ExecCommand(DECMD_SETBACKCOLOR,OLECMDEXECOPT_DODEFAULT, arr);
  }
  activeElement.DOM.focus();
}
function open_fg_palette(){
  var arr = showModalDialog( NEXT_ROOT+"includes/next/ktedit/selcolor.php",
                             "",
                             "font-family:Verdana; font-size:12; dialogWidth:30em; dialogHeight:30em" );
if (arr != null) {
    activeElement.ExecCommand(DECMD_SETFORECOLOR,OLECMDEXECOPT_DODEFAULT, arr);
  }
  activeElement.DOM.focus();
}
function ChangeColor (what){
  activeElement.ExecCommand(DECMD_SETFORECOLOR,OLECMDEXECOPT_DODEFAULT, what);
}
// DisplayChanged handler. Very time-critical routine; this is called
// every time a character is typed. QueryStatus those toolbar buttons that need
// to be in synch with the current state of the document and update. 
function Editor_DisplayChanged() {
  
}
function doFormat(what) {
  switch (what) {
    case 'Cut':  activeElement.ExecCommand(DECMD_CUT,OLECMDEXECOPT_DODEFAULT);break;          
    case 'Copy':   activeElement.ExecCommand(DECMD_COPY,OLECMDEXECOPT_DODEFAULT);break;
    case 'Paste':   activeElement.ExecCommand(DECMD_PASTE,OLECMDEXECOPT_DODEFAULT);break;
    case 'toggleInvisables': activeElement.ShowBorders = !activeElement.ShowBorders;
                             activeElement.ShowDetails = !activeElement.ShowDetails;
                             break;
    case 'FontName':  activeElement.ExecCommand(DECMD_SETFONTNAME, OLECMDEXECOPT_DODEFAULT, arguments[1]);break;
    case 'SelectAll':  activeElement.ExecCommand(DECMD_SELECTALL,OLECMDEXECOPT_DODEFAULT);break;
    case 'FontSize': activeElement.ExecCommand(DECMD_SETFONTSIZE, OLECMDEXECOPT_DODEFAULT, parseInt(arguments[1]));break;
    case 'Bold': activeElement.ExecCommand(DECMD_BOLD,OLECMDEXECOPT_DODEFAULT);break;
    case 'Italic': activeElement.ExecCommand(DECMD_ITALIC,OLECMDEXECOPT_DODEFAULT);break;
    case 'Underline': activeElement.ExecCommand(DECMD_UNDERLINE,OLECMDEXECOPT_DODEFAULT);break;
    case 'JustifyLeft': activeElement.ExecCommand(DECMD_JUSTIFYLEFT,OLECMDEXECOPT_DODEFAULT);break;
    case 'JustifyCenter': activeElement.ExecCommand(DECMD_JUSTIFYCENTER,OLECMDEXECOPT_DODEFAULT);break;
    case 'JustifyRight': activeElement.ExecCommand(DECMD_JUSTIFYRIGHT,OLECMDEXECOPT_DODEFAULT);break;
    case 'InsertOrderedList':  activeElement.ExecCommand(DECMD_ORDERLIST,OLECMDEXECOPT_DODEFAULT);break;
    case 'InsertUnorderedList': activeElement.ExecCommand(DECMD_UNORDERLIST,OLECMDEXECOPT_DODEFAULT);break;
    case 'Indent': activeElement.ExecCommand(DECMD_INDENT,OLECMDEXECOPT_DODEFAULT);break;
    case 'Outdent': activeElement.ExecCommand(DECMD_OUTDENT,OLECMDEXECOPT_DODEFAULT);break;
    case 'InsertHorizontalRule': InsertRAWHTML("<HR>");break;
    case 'InsertImage': activeElement.ExecCommand(DECMD_IMAGE,OLECMDEXECOPT_DONTPROMPTUSER,arguments[1]);break;
    case 'FindText':activeElement.ExecCommand(DECMD_FINDTEXT);break;
    case 'RemoveLink':activeElement.ExecCommand(DECMD_UNLINK);break;
  }
  activeElement.DOM.focus();
}



// switch between WYSIWYG and HTML edit mode
function setDisplayMode(mode){
	if(mode=='CODE'){
		activeElement.DOM.body.clearAttributes;
		activeElement.DOM.body.style.fontFamily='Courier New';
		activeElement.DOM.body.style.fontSize='10pt';
		if(activeElement.DOM.body.innerHTML != "")
			activeElement.DOM.body.innerText=fix4NS(activeElement.FilterSourceCode(activeElement.DOM.body.innerHTML));
		else
			activeElement.DOM.body.innerText="";
		displayMode='CODE';
	} else{
		activeElement.DOM.body.clearAttributes;
		activeElement.DOM.body.style.fontFamily='';
		activeElement.DOM.body.style.fontSize='';
		if(activeElement.DOM.body.innerText != "")
			activeElement.DOM.body.innerHTML=activeElement.FilterSourceCode(activeElement.DOM.body.innerText);
		else
			activeElement.DOM.body.innerHTML="";
		displayMode='RICH';
	}
}
function InsertRAWHTML(sHTML){ 
  if(activeElement.DOM.selection.type == "Control") {
    activeElement.DOM.selection.clear();
  }  
  activeElement.DOM.selection.createRange().pasteHTML(sHTML);
}
function DECMD_IMAGE_onclick() {
  arr=null;
  var args=new Array();
  if (activeElement!=null) {
    if(activeElement.DOM.selection.type == "Control"){
       var oImg = activeElement.DOM.selection.createRange().item(0);
       args["ImgUrl"] = oImg.src;
      args["AltText"] = oImg.alt;
      args["ImgBorder"] = oImg.border;
      args["HorSpace"] = oImg.hspace;
      args["VerSpace"] = oImg.vspace;
      args["ImgAlign"] = oImg.align;
      args["ImgHeight"] = oImg.height;
      args["ImgWidth"] = oImg.width;
    } 
  } else {
    alert("No active element to insert picture");  
  }  
  arr = showModalDialog( NEXT_ROOT+"includes/next/ktedit/insimage.php", args, "font-family:Verdana; font-size:12; dialogWidth:38em; dialogHeight:46em");
  if (arr != null) {
    sHTML='<img src="' + arr["ImgUrl"] + '" height="' + arr["ImgHeight"] + '" width="' + arr["ImgWidth"] + '" alt="' + arr["AltText"] + '" vspace="' + arr["VerSpace"] + '" hspace="' + arr["HorSpace"] +  '"align="' + arr["ImgAlign"] +'"border="' + arr["ImgBorder"]+ '">';     
    if (displayMode == 'RICH'){
      InsertRAWHTML(sHTML);
    }
  }
  activeElement.DOM.focus();
}

function InsertLink(){
  if(document.frames["scule"].url.value!="") {
    sUrl = document.frames["scule"].protocol_type.value + document.frames["scule"].url.value;
	  activeElement.ExecCommand(DECMD_HYPERLINK,OLECMDEXECOPT_DONTPROMPTUSER,sUrl);
	} else {
	    activeElement.ExecCommand(DECMD_HYPERLINK,OLECMDEXECOPT_PROMPTUSER);
	  }  
}
function loadIframes(){
		for(i=0;i<document.frames.length;i++){
			ifr = document.frames[i].name;
			if(ifr.match(/_htmlObject$/)){
				hfi = ifr.replace(/_htmlObject$/, "");
				if(document.all[hfi]){
					if(document.all[hfi].value){
						document.frames[i].document.write(document.all[hfi].value);
					}
				}
			}
		}
		framesLoaded = true;
}