
var viewMode = 1; // 1 - HTML View , 2 - Source View
var editMode = 1; // 1 - None Editable, 2 - Editable
var broswer, refEditor;
browser = navigator.appName;
  
function Init()
{
	document.getElementById('contentEdit').style.display = '';
    if(browser == "Netscape")
    {
		refEditor = document.getElementById('iView').contentDocument;
        document.getElementById('iView').contentDocument.designMode = "On";
    }
    else if(browser == "Microsoft Internet Explorer")
    {
		refEditor = iView.document;
        iView.document.designMode = 'On';    
    }
	// Change to None-Editable View
	document.getElementById('contentView').style.display = '';
	document.getElementById('contentEdit').style.display = 'none';
	document.getElementById('butToggleView').style.display = 'none';

}

function selOn(ctrl)
{
    ctrl.style.borderColor = '#000000';
    ctrl.style.backgroundColor = '#B5BED6';
    ctrl.style.cursor = 'pointer';    
}
  
function selOff(ctrl)
{
    ctrl.style.borderColor = '#D6D3CE';  
    ctrl.style.backgroundColor = '#D6D3CE';
}
  
function selDown(ctrl)
{
    ctrl.style.backgroundColor = '#8492B5';
}
  
function selUp(ctrl)
{
    ctrl.style.backgroundColor = '#B5BED6';
}
    
    
    
function doBold()
{
	refEditor.execCommand('bold', false, null);
}

function doItalic()
{
	refEditor.execCommand('italic', false, null);
}

function doUnderline()
{
	refEditor.execCommand('underline', false, null);
}
  
function doLeft()
{
	refEditor.execCommand('justifyleft', false, null);
}

function doCenter()
{
	refEditor.execCommand('justifycenter', false, null);
}

function doRight()
{
	refEditor.execCommand('justifyright', false, null);
}

function doOrdList()
{
	refEditor.execCommand('insertorderedlist', false, null);
}

function doBulList()
{
	refEditor.execCommand('insertunorderedlist', false, null);
}
  
function doForeCol()
{
    var fCol = prompt('Enter foreground color', '');
    
    if(fCol != null)
		refEditor.execCommand('forecolor', false, fCol);
}

function doBackCol()
{
    var bCol = prompt('Enter background color', '');

    if(bCol != null)
		refEditor.execCommand('backcolor', false, bCol);
}

function doLink()
{

	var objWin = document.getElementById('iView').contentWindow;
	var strURL = prompt("Enter a URL:", "http://");
	try {
		if(strURL != null && strURL > "") {
			//ignore error for blank urls
			objWin.document.execCommand("Unlink", false, null);
			objWin.document.execCommand("CreateLink", false, strURL);
		} else if(strURL != null){
			objWin.document.execCommand("Unlink", false, null);
		}
	} catch (e) {
		//do nothing
	}
	//refEditor.execCommand('createlink', true, null);

}
  
function doImage()
{
    var imgSrc = prompt("Insert Image File (You can use your local image file) : ", "http://www.aspalliance.com/Yusuf/Article10/sample.jpg");

    if(imgSrc != null)
		refEditor.execCommand('insertimage', false, imgSrc);
}
  
function doRule()
{
	refEditor.execCommand('inserthorizontalrule', false, null);
}
  
function doFont(fName)
{
	if(fName != '')
		refEditor.execCommand('fontname', false, fName);
}
  
function doSize(fSize)
{
	if(fSize != '')
		refEditor.execCommand('fontsize', false, fSize);
}
  
function doHead(hType)
{
	if(hType != '')
	{
		refEditor.execCommand('formatblock', false, hType);
		doFont(selFont.options[selFont.selectedIndex].value);
	}
}
  
function doToggleView()
{    
    if(viewMode == 1)
    {
        viewMode = 2; // Source View
		
        if(browser == "Netscape")
        {
            iHTML = document.getElementById('iView').contentWindow.document.body.innerHTML;
            document.getElementById('iView').contentWindow.document.body.textContent = iHTML;

            // Hide all controls
			document.getElementById('tblCtrls').style.display = 'none';
            document.getElementById('iView').contentWindow.focus();
              
        }
        else if(browser == "Microsoft Internet Explorer")
        {
            iHTML = iView.document.body.innerHTML;
            iView.document.body.innerText = iHTML;

            // Hide all controls
            tblCtrls.style.display = 'none';
//            selFont.style.display = 'none';
//            selSize.style.display = 'none';
//            selHeading.style.display = 'none';
            iView.focus();
              
        }
    }
    else
    {
        viewMode = 1; // HTML View
		
        if(browser == "Netscape")
        {
//            iText = document.getElementById('iView').contentWindow.document.body.innerText;
            iText = document.getElementById('iView').contentWindow.document.body.textContent;
            document.getElementById('iView').contentWindow.document.body.innerHTML = iText;
            
			// Show all controls
			document.getElementById('tblCtrls').style.display = '';
            document.getElementById('iView').contentWindow.focus();
          
        }
        else if(browser == "Microsoft Internet Explorer")
        {
            iText = iView.document.body.innerText;
            iView.document.body.innerHTML = iText;
      
            // Show all controls
            tblCtrls.style.display = '';
//            selFont.style.display = '';
//            selSize.style.display = '';
//            selHeading.style.display = '';
            iView.focus();
          
        }
    }
}

function doContentUpdate()
{
	if(browser == "Netscape")
	{
		 document.getElementById('contentView').innerHTML = document.getElementById('iView').contentWindow.document.body.innerHTML;
	} 
    else if(browser == "Microsoft Internet Explorer")
	{
		document.getElementById('contentView').innerHTML = iView.document.body.innerHTML;
	}
}

function doToggleEdit() {
	if(viewMode == 2)
		doToggleView();
		
	if(editMode == 1) {
		editMode = 2;
		// Change to Editable View
		document.getElementById('contentView').style.display = 'none';
		document.getElementById('contentEdit').style.display = '';
		document.getElementById('butToggleView').style.display = '';
	} else {
		editMode = 1;
		// Change to None-Editable View
		document.getElementById('contentView').style.display = '';
		document.getElementById('contentEdit').style.display = 'none';
		document.getElementById('butToggleView').style.display = 'none';
		doContentUpdate();
	}
}

function ProcessPageContent(frm) {
	if(browser == "Netscape")
	{
		htmlCode = document.getElementById('iView').contentWindow.document.body.innerHTML;
	} 
    else if(browser == "Microsoft Internet Explorer")
	{
		htmlCode = iView.document.body.innerHTML;
	}
	frm.page_content.value = htmlCode;
}

function doSave() {
	ProcessPageContent(document.frmPageContent);
	document.frmPageContent.submit();
}
