var viewMode = 1; // WYSIWYG
var format;
var isIE;
var isGecko;
var isSafari;
var isDocAll;
var ua = navigator.userAgent.toLowerCase();
//alert (navigator.userAgent);
isIE = ((ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1)); 
isGecko = (ua.indexOf("gecko") != -1);
isSafari = (ua.indexOf("safari") != -1);
isDocAll = (isDocAll)?true:false;


function swapModes() {
 var iView=document.getElementById('iView').contentWindow;
 if (format!="HTML") {
  iView.document.body.innerText = iView.document.body.innerHTML;
  iView.document.body.style.fontFamily = "monospace";
  iView.document.body.style.fontSize = "10pt";
  format="HTML";
 }
 else {
   iView.document.body.innerHTML = iView.document.body.innerText;
   iView.document.body.style.fontFamily = "";
   iView.document.body.style.fontSize ="";
   format="design";
 }
 iView.focus()
 var s = iView.document.body.createTextRange()
 s.collapse(false)
 s.select()
}







function tablehilite (col, row)
{
	for(var r=1; r<=5; r++){
		for(var c=1; c<=5; c++){
			if(c<=col && r<=row){
				document.getElementById('r'+r+'c'+c).style.background='#006699';
			}else{
				document.getElementById('r'+r+'c'+c).style.background='#FFFFFF';
			}
		}
	}
}

function popDiv(divid, j)
{
	thediv=document.getElementById(divid);
	if (thediv.style.display == 'none'){
		thediv.innerHTML='';
		eval(unescape(j.replace(/\+/g," ")));
		thediv.style.position='absolute';
		thediv.style.zindex=100;
		thediv.style.left=mX;
		thediv.style.top=mY;
		thediv.style.background='#FFFFFF';
		thediv.style.display='block';
	}else{
		thediv.style.display='none';
		thediv.innerHTML='';
	}
}

function tablecreate(col, row)
{
	var insTxt='<table <!--startdesign>borders="1"<!--enddesign>>'
	for(var r=1; r<=row; r++){
		insTxt=insTxt+'<tr>';
		for(var c=1;c<=col; c++){
			insTxt=insTxt+'<td>&nbsp;</td>';
		}
		insTxt=insTxt+'</tr>'
	}
	insTxt=insTxt+'</table>';
	InsertText(insTxt);
}

function InsertText(insText) {
	var oRTE;
	var fullText;
	var selection;
	if (isIE) {
		oRTE = document.getElementById('iView');
		oRTE = document.getElementById('iView').contentWindow;
		selection = oRTE.document.selection; 
		if (selection != null) {
			rng = selection.createRange();
		}
	} else {
		oRTE = document.getElementById('iView').contentWindow;
		selection = oRTE.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
        /* stupid msie doesn't know what range. */
	}
	if ((insText != null) && (insText != "")) {
		oRTE.focus();
		fullText = oRTE.window.document.body.innerHTML;
		var szURL = "http://placeholder";
		try {
			oRTE.document.execCommand("InsertImage", false, szURL);
		} catch (e) {
			//do nothing
		}
		fullText = oRTE.window.document.body.innerHTML;
		var phPattern = new RegExp("<img\\ssrc=\"http:\\/\\/placeholder\">","i");
		fullText = fullText.replace(phPattern, insText);
		oRTE.window.document.body.innerHTML = fullText;
		fullText = oRTE.window.document.body.innerHTML;
	}
	oRTE.focus();
}





function InitEdit()
{
	document.getElementById('iView').contentWindow.document.designMode = 'On'; 
	document.getElementById('iView').contentWindow.document.execCommand('fontname', false, 'Arial');
	format='design';
}
function selOn(ctrl)
{
	ctrl.style.borderColor = '#000000';
	ctrl.style.backgroundColor = '#B5BED6';
	ctrl.style.cursor = 'hand';	
}
function selOff(ctrl)
{
	ctrl.style.borderColor = 'transparent';
	ctrl.style.backgroundColor = 'transparent';
}
function selDown(ctrl)
{
	ctrl.style.backgroundColor = '#8492B5';
} 
function selUp(ctrl)
{
	ctrl.style.backgroundColor = '#B5BED6';
}   
function doFile(filename)
{
	filename=unescape(filename);
	ext = filename.slice(filename.indexOf(".")).toLowerCase();
	if (ext == '.flv')
	{
	var ins='<div class="moviecontainer" style="display:inline-block;width:400;height:330;border: 1px #000000 solid;">Flash Movie Placeholder<!--\
		<object type="application/x-shockwave-flash" data="FlowPlayer.swf" width="100%" height="100%" id="FlowPlayer"> \
		<param name="allowScriptAccess" value="always" /> \
		<param name="movie" value="FlowPlayer.swf" /> \
		<param name="quality" value="high" /> \
		<param name="scaleMode" value="showAll" /> \
		<param name="allowfullscreen" value="true" /> \
		<param name="wmode" value="transparent" /> \
		<param name="allowNetworking" value="all" /> \
		<param name="flashvars" value="config={ \
		autoPlay: true, \
		loop: false, \
		initialScale: \'scale\', \
		showLoopButton: false, \
		showPlayListButtons: false, \
		playList: [ \
			{ url: \'main_clickToPlay.jpg\' }, \
			{ url: \''+filename+'\' }, \
		] \
		}"> \
		</object>--></div>'
	}
	if (ext == '.gif' || ext == '.jpg' || ext == '.png'){
		var ins='<img src="'+filename+'" alt="'+filename+'">'
	}
	InsertText(ins);

}
function doBold()
{document.getElementById('iView').contentWindow.document.execCommand('bold', false, null);}
function doItalic()
{document.getElementById('iView').contentWindow.document.execCommand('italic', false, null);}
function doUnderline()
{document.getElementById('iView').contentWindow.document.execCommand('underline', false, null);}
function doLeft()
{document.getElementById('iView').contentWindow.document.execCommand('justifyleft', false, null);}
function doCenter()
{document.getElementById('iView').contentWindow.document.execCommand('justifycenter', false, null);}
function doRight()
{document.getElementById('iView').contentWindow.document.execCommand('justifyright', false, null);}
function doOrdList()
{document.getElementById('iView').contentWindow.document.execCommand('insertorderedlist', false, null);}
function doBulList()
{document.getElementById('iView').contentWindow.document.execCommand('insertunorderedlist', false, null);}
function doForeCol(color)
{document.getElementById('iView').contentWindow.document.execCommand('forecolor', false, color);}
function doBackCol(color)
{document.getElementById('iView').contentWindow.document.execCommand('backcolor', false, color);}
function doLink()
{document.getElementById('iView').contentWindow.document.execCommand('createlink');}
function doImage()
{
    var imgSrc = prompt('Enter image location', '');   
    if(imgSrc != null)    
     document.getElementById('iView').contentWindow.document.execCommand('insertimage', false, imgSrc);
}
  
function doRule()
{document.getElementById('iView').contentWindow.document.execCommand('inserthorizontalrule', false, null);}
function doFont(fName)
{
    if(fName != '')
      document.getElementById('iView').contentWindow.document.execCommand('fontname', false, fName);
}
  
function doSize(fSize)
{
    if(fSize != '')
      document.getElementById('iView').contentWindow.document.execCommand('fontsize', false, fSize);
}
  
function doHead(hType)
{
    if(hType != '')
  {
      document.getElementById('iView').contentWindow.document.execCommand('formatblock', false, hType);  
	doFont(selFont.options[selFont.selectedIndex].value); 
  }
}
  
  function doToggleView()
{  
    if(viewMode == 1)
  {
      iHTML = document.getElementById('iView').contentWindow.document.body.innerHTML;
      document.getElementById('iView').contentWindow.document.body.innerText = iHTML;
      
      iView.focus();
      viewMode = 2; // Code
  }
    else
  {
      iText = document.getElementById('iView').contentWindow.document.body.innerText;
      document.getElementById('iView').contentWindow.document.body.innerHTML = iText;
      
      iView.focus();
      viewMode = 1; // WYSIWYG
  }
}
