var type;
var IMAGE_ROOT_DIRECTORY = "" ;
var IdNo = 0;
function button_over(eButton,imageName )
	{
	    eButton.src = "../HTMLEditor/Images/" + imageName;		
	    //eButton.style.backgroundColor =	"LightSlateGray";
		//eButton.style.borderColor =	"darkblue darkblue darkblue	darkblue";
	}
function button_out(eButton)
	{
		eButton.style.backgroundColor =	"#e0edf7";
		eButton.style.borderColor =	"threedface";
	}
function button_down(eButton)
	{
		eButton.style.backgroundColor =	"#8494B5";
		eButton.style.borderColor =	"darkblue darkblue darkblue	darkblue";
	}
function button_up(eButton)
	{
		eButton.style.backgroundColor =	"#B5BDD6";
		eButton.style.borderColor =	"darkblue darkblue darkblue	darkblue";
		eButton	= null;	
	}
var	isHTMLMode=false;
function document.onreadystatechange()
	{
		if(typeof(NewsBody_rich) != 'undefined')
		NewsBody_rich.document.designMode="On";		
	}
function cmdExec(cmd,opt) 
	{
		if (isHTMLMode)
		{
			alert("Formatting happens only in Normal mode");
			return;
		}
		NewsBody_rich.focus();
		NewsBody_rich.document.execCommand(cmd,"",opt);
		NewsBody_rich.focus();
	} // end cmdExec

function insertText(txt) 
	{
		if (isHTMLMode)
		{
			alert("Formatting happens only in Normal mode");
			return;
		}
		NewsBody_rich.focus();
		NewsBody_rich.document.execCommand('Paste');
		NewsBody_rich.focus();
	} // end cmdExec

function ChangeImage (ImageName,FileName) {
var img = document.getElementById(ImageName);
img.src = FileName;
}

var intHCount = 0;
var intNCount = 0;

function setMode(Nbut, Hbut) 
	{
		var	sTmp;
		isHTMLMode = Hbut;
		isNormalMode = Nbut;

		if(Hbut == 1)
		{
			intHCount++;
			intNCount = 0;
		}

		if(Nbut == 1)
		{
			intNCount++;
			intHCount = 0;
		}	

		if (isHTMLMode && intHCount == 1)
		{

			sTmp=NewsBody_rich.document.body.innerHTML;
			NewsBody_rich.document.body.innerText=sTmp;
			//ChangeImage ('HTMLbut',IMAGE_ROOT_DIRECTORY + '/HTML_On.gif');
			//ChangeImage ('Normalbut',IMAGE_ROOT_DIRECTORY + '/Normal_Off.gif'); 
			

			return true;
		}
		if(isNormalMode && intNCount == 1)
		{
			sTmp=NewsBody_rich.document.body.innerText;
			NewsBody_rich.document.body.innerHTML=sTmp;
			//ChangeImage ('HTMLbut',IMAGE_ROOT_DIRECTORY + '/HTML_Off.gif');
			//ChangeImage ('Normalbut', IMAGE_ROOT_DIRECTORY + '/Normal_On.gif'); 
			
			return true;
		}
		NewsBody_rich.focus();
	} // end setMode
	
function createLink()
	{
		if (isHTMLMode){alert("Formatting happens only in Normal mode");return;}
		cmdExec("CreateLink");
	} // end createLink
	

	
function backColor()
	{
		if (isHTMLMode)
		{
			alert("Formatting happens only in Normal mode");
			return;
		}
		var	arr	= showModalDialog("ColorPalette.htm","","font-family:Verdana;	font-size:12; dialogWidth:18;	dialogHeight:18" );
		if (arr	!= null) cmdExec("BackColor",arr);	
	} // end backColor
	
function foreColor(arg)
	{
		if (isHTMLMode)
		{
			alert("Formatting happens only in Normal mode");
			return;
		}
		var	arr	= showModalDialog("../htmleditor/ColorPalette.htm","","font-family:Verdana;	font-size:12; dialogWidth:18;	dialogHeight:18" );
		if (arr	!= null) cmdExec(arg,"foreColor",arr);	
	} // end foreColor
	

function setImage()	
{ 
	var	imgSrc = " ";
	var	flag = 0;	
	while(imgSrc ==	" ")
	{
		imgSrc = prompt('Enter image location with image name ', ''); 
		if (imgSrc == "	") alert("Please enter the required	URL");
		if(imgSrc != "")
		{
			cmdExec('insertimage', imgSrc);	
		}
	} // end while
} // end setImage
function ShowCM(t,headerText)
	{
	NewsBody_rich.document.designMode='On';
	if (divContent.style.display=="block")
		{
			divContent.style.display="none";
		}
	else
		{
			divContent.style.display="block";
		}
	type=t;
	spnHeader.innerText=headerText;
	if(document.getElementById(type).value != '')
		{
		NewsBody_rich.document.body.innerHTML=document.getElementById(type).value;
		}
	}
function fillTxt()
		{
		var State = new Object()
		var aa;
		document.getElementById("hdnmsg").innerText=NewsBody_rich.document.body.innerHTML; 
		}
				
function ClearIFrame()
	{
	NewsBody_rich.document.body.innerHTML ='';
	divContent.style.display="none";
	}
function DisableSave(bool)
	{
		document.Form1.btnSaveHead.style.display="none";
		if(bool=='show')
		{	
			if(document.Form1.btnSaveHead.style.display=="none")
			{
				document.Form1.btnSaveHead.style.display="block";
			}
			else
			{
				document.Form1.btnSaveHead.style.display="none";
			}
		}
	}

function getInnerHTML() 
{
    return window.frames.NewsBody_rich.document.body.innerHTML ;
}

function Formats(style)
{
    if (isHTMLMode)
		{
			alert("Formatting happens only in Normal mode");
			return;
		}
		
        NewsBody_rich.focus();
    // Apply the new style
		NewsBody_rich.document.execCommand(style);
    // Set the focus back to the text
		NewsBody_rich.focus();
}

// Added JS functions
    //   function insertBreak()
    //   {
    //   if (isHTMLMode)
    //		{
    //			alert("Formatting happens only in Normal mode");
    //			return;
    //		}
    //   var myRange = document.all.NewsBody_rich.document.selection.createRange();
    //   var m = myRange.pasteHTML('<br>');
    //   }
          
     function SpellChecker(TextValue)
     {
        
        if(isHTMLMode)
        { 
            alert("Can not perform spell check in HTML Mode");
            return;
        }
        else
        {
        var objWordobject;
        var objDocobject ;
        var strReturnValue="";
        //Create a new instance of word Application;
        objWordobject = new  ActiveXObject("Word.Application");
        objWordobject.visible = false;
        objWordobject.WindowState = 2;
        
        //Create a new instance of Document;
        objDocobject = objWordobject.Documents.Add();
        objDocobject.Content=TextValue;
        objDocobject.CheckSpelling();
        //Return spell check completed text data;
        strReturnValue = objDocobject.Content.Text;
        //alert(strReturnValue);
                //objDocobject.Selection.WholeStory();
                //strReturnValue =objDocobject.Selection.Text;
        //Close Word Document ;
                //  objDocobject.Close false;
                objDocobject.Close(0);
        //Set Document to null;
        objDocobject = null;
        //Quit Word;
        objWordobject.Quit();
        //Set word object to null;
        objWordobject= null;
        return(strReturnValue);
        }
   } 
   

   function setBackColor()
   {
    //   cmdExec('SelectAll');
        if (isHTMLMode)
		{
			alert("Formatting happens only in Normal mode");
			return;
		}
        var div1 = NewsBody_rich.document.getElementById('div1');
        var	arr	= showModalDialog("ColorPalette.htm","","font-family:Verdana;	font-size:12; dialogWidth:18;	dialogHeight:18" );
        if(div1 == null)
        {
                NewsBody_rich.focus();
		        NewsBody_rich.document.execCommand('SelectAll',"","");
		        NewsBody_rich.focus();
		        var txt = NewsBody_rich.document.body.innerHTML;
                document.all.holdtext.innerText = "";
                cmdExec('delete')
		        var newDiv = document.createElement('div');
		        NewsBody_rich.focus();
                var myRange = document.all.NewsBody_rich.document.selection.createRange();
                var m = myRange.pasteHTML('<div id="div1" style="background-color:'+arr+'">'+txt+' </div>');
        }
        else
        {
            div1.style.backgroundColor = arr;
         }
   }

   // Function put on Jun 06, 2008
   
       function NormalView()
    {
    //document.all.btnApply.disabled = false;
    
    HTMLView();
    setMode(1,0);
    document.getElementById('Normalbut').src = IMAGE_ROOT_DIRECTORY + '/NormalModeAfter.gif';
    document.getElementById('HTMLbut').src = IMAGE_ROOT_DIRECTORY + '/HTMLModeBefore.gif';
    //HTMLModeAfter, HTMLModeBefore
    }
    
    function HTMLView()
    {
    //document.all.btnApply.disabled = true;
    
    setMode(0,1);
    document.getElementById('HTMLbut').src = IMAGE_ROOT_DIRECTORY + '/HTMLModeAfter.gif';
    document.getElementById('Normalbut').src = IMAGE_ROOT_DIRECTORY + '/NormalPreview2Normal.gif';

    }
    
    function checkSpell()
    {
    if(isHTMLMode)
        { 
            alert("Can not perform spell check in HTML Mode");
            return;
        }
    if (confirm('Please Note: \n 1. For Spell Check to work you need to do the Following: \n \t In Internet Options security Settings --> Custom Level --> Initialize and Script controls not Marked as safe \n \t --> enable. \n 2.  This is Plain text Spell Checker so All HTML Formatting will be lost on Spell-Check. \n Continue with Spell-Check ?')) 
        NewsBody_rich.document.body.innerText=SpellChecker(NewsBody_rich.document.body.innerText);
    }

// 
function getDate()
    {
    
    var currentTime = new Date();
    var dtToday = formatDate(currentTime, 'MM/dd/yyyy');
//    alert(dtToday);
//        var month = currentTime.getMonth() + 1;
//        var day = currentTime.getDate();
//        var year = currentTime.getFullYear();
        
        //document.write(month + "/" + day + "/" + year)
//        var d = month + "/" + day + "/" + year;
    insertValue(dtToday);
//    document.all.holdtext.innerText = d ;//ddn;
//    var tr = document.all.holdtext.createTextRange(); 
//    tr.execCommand("Copy");
//    insertText(tr.text) ;
    //var d=parseDate(this.form.date5.value)
    
    }

    function insertValue(val)
    {
        if(isHTMLMode)
        { 
            alert("Formatting happens only in Normal mode");
            return;
        }
        NewsBody_rich.focus();
        var myRange = NewsBody_rich.document.selection.createRange();
        var m = myRange.pasteHTML(val);
        NewsBody_rich.focus();
    }    
    function getTime()
    {
        if(isHTMLMode)
        { 
            alert("Formatting happens only in Normal mode");
            return;
        }
    var currentTime = new Date();
    var hrs = currentTime.getHours() ;
    var min = currentTime.getMinutes() ;
    var d ;
    if(min<10)
    d = hrs + ":0" + min ;
    else
    d = hrs + ":" + min ;
        //    if(hrs > 11)
        //    d += " PM";
        //    else
        //    d += " AM";
    //    document.all.holdtext.innerText = d ;//ddn;
    //    var tr = document.all.holdtext.createTextRange(); 
    //    tr.execCommand("Copy");
    insertValue(d) ;
    }
    
    function getCaps()
    {
            if(isHTMLMode)
        { 
            alert("Formatting happens only in Normal mode");
            return;
        }
        var txt = NewsBody_rich.document.selection.createRange().text;
        var myRange = NewsBody_rich.document.selection.createRange();
        var m = myRange.pasteHTML(txt.toUpperCase());
        //insertText(tr.text) ;
        //clearMem();
    }
    
    function getSmall()
    {
        if(isHTMLMode)
        { 
            alert("Formatting happens only in Normal mode");
            return;
        }
        var txt = NewsBody_rich.document.selection.createRange().text;
        var myRange = NewsBody_rich.document.selection.createRange();
        var m = myRange.pasteHTML(txt.toLowerCase());
        
    //        var txt = NewsBody_rich.document.selection.createRange().text
    //        document.all.holdtext.innerText = txt.toLowerCase();
    //        var tr = document.all.holdtext.createTextRange(); 
    //        tr.execCommand("Copy");
    //        insertText(tr.text) ;
    //        clearMem();
    }
    
    
    function clearMem()
    {
     //   document.all.holdtext.innerText="";
        var tr = document.all.holdtext.createTextRange();
        tr.execCommand("Copy");
    }
    
    /*
    For Spell Check you need to do the Following
    In Internet Options security Settings --> Custom Level --> Initialize and Script controls not Marked as safe --> enable 
    */
     
   
   function insertBreak()
   {
//   var myRange = document.all.NewsBody_rich.document.selection.createRange();
//   var m = myRange.pasteHTML('<br>');
        if (isHTMLMode)
		{
			alert("Image can be inserted only in Normal mode");
			return;
		}
		else
		  insertValue('<BR>');
   }
   
//   function setBackColor()
//   {
//        if (isHTMLMode)
//		{
//			alert("Image can be inserted only in Normal mode");
//			return;
//		}
//		
//        var txt = NewsBody_rich.document.body.innerHTML;
//        var div1 = NewsBody_rich.document.getElementById('div1');
//        var	arr	= showModalDialog("ColorPalette.htm","","font-family:Verdana;	font-size:12; dialogWidth:18;	dialogHeight:18" );
//        if(div1 == null)
//        {
//                NewsBody_rich.focus();
//		        NewsBody_rich.document.execCommand('SelectAll',"","");
//		        NewsBody_rich.focus();
//                document.all.holdtext.innerText = "";
//                cmdExec('delete')
//		        var newDiv = document.createElement('div');
//		        NewsBody_rich.focus();
//                var myRange = document.all.NewsBody_rich.document.selection.createRange();
//                var m = myRange.pasteHTML('<div id="div1" style="background-color:'+arr+'">'+txt+' </div>');
//        }
//        else
//        {
//            div1.style.backgroundColor = arr;
//        }
//   }
   
function openImages()
    {
        if (isHTMLMode)
		{
			alert("Image can be inserted only in Normal mode");
			return;
		}
    NewsBody_rich.focus();
    window.open("ImageSelector.aspx", "imagePicker","width=600,height=800,resizable=no,scrollbars=1") ; //, "width=00,height=350"
    //window.showModelessDialog("ImageSelector.aspx", "","resizable:1;dialogWidth:600px;dialogHeight:800px") ; //, "width=00,height=350"
    }   

   function setImagePath(imPath,alt,width,height,hSpace,vSpace,brder,align)
    {
    //cmdExec('InsertImage', imPath);
        
        //execCommand('InsertInputImage',"Image1");
        //document.all.Image1.src = imPath;
    //var myRange = content.document.selection.createRange();
    //NewsBody_rich.document.execCommand
    if (isHTMLMode)
		{
			alert("Image can be inserted only in Normal mode");
			return;
		}
		
    var myRange = document.all.NewsBody_rich.document.selection.createRange();
    var m = myRange.pasteHTML('<IMG  id="Image'+ IdNo + '" src="' +imPath+'" alt="'+alt+'" width="'+ width+'" height="'+height+'" hspace="'+ hSpace+'" vspace="' +vSpace + '" align="' + align+'">');
    var imag = NewsBody_rich.document.getElementById('Image'+IdNo);

    imag.style.border = brder;
    IdNo++;
    }
    ////
function setArticleHTML(msgVal)
        {
        
              if(msgVal != 0)
              {
               alert('Please Apply the changes after you have finished Editing');
              }
              var content = window.opener.getArticleHTML();
              //alert(content);
              
              //alert(typeof textEdit);
              
              //alert(content +'--Text Edit Type---' + typeof(textEdit));
              //textEdit.document.designMode="On";
              //textEdit.document.open();
              //NewsBody_rich.document.body.innerHTML = content;
    //              if(textEdit.contentWindow.document.body)
    //              {
                    //alert('Value='+ document.getElementById("NewsBody_rich").contentWindow.document.body + '\n Type:' +typeof(document.getElementById("NewsBody_rich").contentWindow.document.body) );
                    if(document.getElementById("NewsBody_rich").contentWindow.document.body != null)
                      {
                      document.getElementById("NewsBody_rich").contentWindow.document.body.innerHTML = content;
                      }
   
        }
        