/*** Freeware Open Source writen by ngoCanh 2002-05                  */
/*** Original by Vietdev  http://vietdev.sourceforge.net             */
/*** Release 2004-03-15  R9.0                                        */
/*** GPL - Copyright protected                                       */
/*********************************************************************/

/*** CONFIGURATION - HERE YOU CAN SET DEFAULT-VALUES ********************/
if(typeof(SECURE)=="undefined") SECURE=1; //=0,1
if(typeof(VISUAL)=="undefined") VISUAL=1; //=0,1,2,3 see bottom of this file
if(typeof(POPWIN)=="undefined") POPWIN=1; //=1,0 Rightclick Popup dialog for textarea
if(typeof(DFFACE)=="undefined") DFFACE=''; // 'times new roman'; // Default fontFamily of Editor
if(typeof(DFSIZE)=="undefined") DFSIZE=''; // '14px'; // Default fontSize
if(typeof(DCOLOR)=="undefined") DCOLOR=''; // 'blue'; // Default color
if(typeof(DBGCOL)=="undefined") DBGCOL=''; // 'green'; // Default backgroundColor
if(typeof(DBGIMG)=="undefined") DBGIMG=''; // Default URL-backgroundImage 
if(typeof(DCSS)=="undefined") DCSS=''; // 'test.css'; // Default-Stylesheet-URL
if(typeof(SYMBOLE)=="undefined") SYMBOLE='<QBFBR>' ; // Symbole for end-of-field in clipboard-chipcard.
if(typeof(USETABLE)=="undefined") USETABLE=1; // Enable table editor
if(typeof(USEFORM)=="undefined") USEFORM=0; // Enable form input
if(typeof(RETURNNL)=="undefined") RETURNNL=1; // Return-Button= Newline; Shift+Return= New Paragraph
if(typeof(FULLCTRL)=="undefined") FULLCTRL=0; //=0,1; 0=fast loading; 1=all control rows at bottom of Edi.
if(typeof(VDEVCSS)=="undefined") VDEVCSS= 'vdev.css'; // Edi. layout file - not for content of editor
if(typeof(LANGUAGE)=="undefined") LANGUAGE= 'language.js'; //your language file
if(typeof(UNICODE)=='undefined') UNICODE=0; // 0,1 
if(typeof(NOLOCURL)=='undefined') NOLOCURL=1; // enable local url "file:///"
/*********************** END CONFIGURATION ****************************/

var FID; //***   IFRAME ID
var TXT=null; //***   TEXT Obj
var mod=new Array();
var viewm=new Array();
var FACE= new Array();
var SIZE= new Array();
var COLOR= new Array();
var BCOLOR= new Array();
var BIMAGE= new Array();
var CSS= new Array();
var FWORD, FLAGS=0;
var UPLOADURL='';
var UPLOADURI='';



function getFullScriptPath(script)
{

  var i=0,p='';
  var scrA=document.getElementsByTagName('script')
  while(scrA[i])
  { 

   var src= scrA[i].src
   if( src && src.lastIndexOf(script)>=0 ){ p=src.substring(0,src.lastIndexOf(script)); break;}
   i++
  }

  if(p.indexOf("://")>=0||p.indexOf("/")==0)return p
  p= p.replace(/^\.\//,"/")

  var href= document.location.href
  if(href.indexOf('?')>=0) href= href.substring(0,href.indexOf('?'));
  href= href.substring(0,href.lastIndexOf('/'))

  if(p=='.' || p=='') return href
  else if(p.indexOf('..')>=0)
   {
    var sub= ''
    if(p.length>2) sub= p.substr(p.lastIndexOf('../')+2)
    var temp= p.split('..')
    for( var i=1; i<temp.length;i++)
     { href= href.substring(0,href.lastIndexOf('/')); }
    if(sub!='/..') href += sub
   }
  else if(p!='') href += (p.substring(0,1) == '/' ? '' : '/') + p;
  return href
}


QBPATH= getFullScriptPath('htmleditor/quickbuild.js');
QBPATH = QBPATH + "htmleditor";

var rexp=new RegExp('^.*?\/((javascript)?\/htmleditor\/uploads\/)$', 'i')
UPLOADURL=QBPATH+'/uploads/';
UPLOADURI=UPLOADURL.replace(rexp, '$1')
// Prepend the protocol and hostname if it does not exist
if(UPLOADURL.slice(0,4).toLowerCase() != 'http') {
	rexp=new RegExp('^(.+?)\/?'+document.location.pathname+'.*$', 'i')
	var base=document.location.href.replace(rexp, '$1/')
	if(UPLOADURL.charAt(0) == '/') {
		UPLOADURL=UPLOADURL.substr(1)
	}
	UPLOADURL=base+UPLOADURL
}

document.writeln('<style>@import url("' + QBPATH + '/skin/'+ VDEVCSS +'");</style>');
document.writeln('<script src="'+QBPATH+'/skin/'+ LANGUAGE +'"></script>');

if(document.all) document.writeln('<script src="'+QBPATH+'/quickbuild_IE.js"></script>');
else document.writeln('<script src="'+QBPATH+'/quickbuild_Moz.js"></script>');


function ensureVisualEditorsDestroyed()
{
  var els=document.getElementsByTagName('iframe');
  for (var i=0; i < els.length; i++) {
	if(els[i].id.length >= 5 && els[i].id.indexOf("VDevID") >= 0) {
		return !(window.confirm(M_CONFIRMSUBMIT));
	}   
  }
  return true;
}

function searchAndReplaceOnPreInit(str1)
{
  // Replace relative path (URI) of images will full path (URL)
  // This allows us to easily move data and images between sites
  var rexp=new RegExp('(src=["\']?)'+UPLOADURI, "gim")
  str1=str1.replace(rexp, '$1'+UPLOADURL)

  // Escape the special HTML characters in the <code> and </code> tags so 
  // they appear in the visual editor
  rexp=new RegExp('(</?code>)', "gim")
  str1=str1.replace(rexp, encodeCodeTag)

  return str1
}

function searchAndReplaceOnDestroy(str1)
{
  // Replace full path (URL) of images with relative path (URI)
  // This allows us to easily move data and images between sites
  var rexp=new RegExp('(src=["\']?)'+UPLOADURL, "gim")
  str1=str1.replace(rexp, '$1'+UPLOADURI)

  // Override font size tags -- convert <font size="X"> to <font class="sizeoverride_X" size="X">
  // We do this because we use an HTML selector to explicitly set a fixed font size for the <font> 
  // tag in the CSS file.
  // Remove existing instances of the sizeoverride_X class
  var rexp1=new RegExp('\\s*class=["\']?sizeoverride_\\d["\']?\\s*', "gim")
  // Insert new instances of the sizeoverride_X class based on the value in <font size="X">
  var rexp2=new RegExp('<font([^>]*?)(\\s*size=["\']?(\\d)["\']?)([^>]*?)>', "gim")
  str1=str1.replace(rexp1, ' ').replace(rexp2, '<font$1 class="sizeoverride_$3" size="$3"$4>')

  // Escape any HTML characters that appear inside the <code> tag so they appear as literal
  // characters on the web page
  rexp=new RegExp('(<code>)(.*?)(</code>)', "gim")
  str1=str1.replace(rexp, encodeCodeContent)

  return str1
}

function encodeCodeTag(str, match1, offset, s)
{
  return encodeHTML(match1)
}

function encodeCodeContent(str, match1, match2, match3, offset, s)
{
  return match1 + encodeHTML(match2) + match3
}

function encodeHTML(str) {
  var s = new String(str);
  s = s.replace(/&/g, "&amp;");
  s = s.replace(/</g, "&lt;");
  s = s.replace(/>/g, "&gt;");
  s = s.replace(/"/g, "&quot;");
  return s;
}

function enableVisualMode(controlID, formatControlID) 
{
  if(typeof ASKED != 'undefined') delete ASKED;

  addEventToObjById(controlID); 

  if(formatControlID) {
    // Change the content-type dropdown to HTML formatting
    var felem = document.getElementById(formatControlID);
    if(felem) {
      autoSelectDropDown(felem, "1");
    }
  }
}
