
	FOCUSED_DIV = "body";
	COLOR_METHOD = "";
	EDITABLE = false;
	
	window.onload = detect_editable;
	
	
	function doHead(selheadings)
	{
	    var headtext = selheadings.options[selheadings.selectedIndex].text;
	    var headvalue = selheadings.options[selheadings.selectedIndex].value;	    
		if(headvalue != '' && headtext!="Format")
		{		    
	        if (navigator.userAgent.indexOf("IE")!=-1)	//code for IE
	            document.execCommand('formatblock', false, headvalue); 
	        else	            	    
		        document.execCommand('formatblock', false, headtext);//document.execCommand('heading', false, headtext);		    
		}
		selheadings.selectedIndex = 0;
	}
	
	
	function ed_applysize(val)
	{
		identify_focus();		
		document.execCommand('FontSize',false,val);
	}
	
	
	function ed_swf()
	{
		popup("main/toolbar/swf_selector.aspx");
	}
	
	function insert_swf(swf)
	{
		var sText = body.document.selection.createRange();
		identify_focus();
		
		textA = "<embed src='images/";
		textB ="' width=160 height=94 type='application/x-shockwave-flash'></embed>";
		sText.pasteHTML(textA + swf + textB);
		
		
		/*
		if(sText.text !="")
		{
			sText.pasteHTML(vtemplate);
		}
		else 
		{
			body.innerHTML += vtemplate;
		}
		*/
		
	}
	
	
	
	function ed_bold()
	{
		if(body.isContentEditable)
		{
			var sText = right_column.document.selection.createRange();
			//alert(sText.text);
			
			if (sText.text!="")
			{
				//create link
				right_column.document.execCommand("bold");
			}
		}
	}
	

	
	function ed_italic()
	{
	
		identify_focus();		
		document.execCommand('Italic');
	
	}	
	
	function fnonbeforepaste()
	{
		event.returnValue = false;
		//alert("this is before paste");
		clip.innerHTML =window.clipboardData.getData("Text");
		window.clipboardData.setData("Text",clip.innerText);
	}



	function ed_link()
	{
		var oSelection = body.document.selection.createRange();
		if(isAnchor(oSelection))
			document.execCommand('CreateLink');
		else 
		{
			insert_link();
			//alert("not a hyperlink");		  
		}
			//document.execCommand('CreateLink');
	}

	function ed_link_site()
	{
	
		popup2("main/toolbar/page_link_selector.aspx");

		//alert("to do..");
	}
	
	function insert_page_link(link)
	{
		document.execCommand('CreateLink',false,link);
	}
	
	
	function apply_color(color)
	{
		var oSelection = body.document.selection.createRange();
		
		if(COLOR_METHOD=="bg")
		{
			//var sText = body.document.selection.createRange();
			//e = sText.parentElement();
			//e.style.backgroundColor = 	color;	
			//document.execCommand('BackColor', false,color)
			oSelection(0).bgColor = color;
			
		}
		if(COLOR_METHOD=="fg")
		{
			document.execCommand('ForeColor', false,color)

		}
	}

	
	function insert_link()
	{
		
		var oSelection = body.document.selection.createRange();
		
		if(document.selection.type == "Text")
		{	
			oText = oSelection.text;
			if(oSelection.text !="")						
			{
				document.execCommand('CreateLink');
				//if(oSelection.parentElement().tagName =="A")
				//	oSelection.parentElement().innerText=oText;
			}
		}
		
					//sText.parentElement().innerText=t;
				//sText.parentElement().innerText=sText.parentElement().href;
				//document.execCommand("ForeColor","false","#FF0033");
			
		else
		{
			//alert("Please select some blue text!");
			document.execCommand('CreateLink');
		} 
		
	}
	
	function ed_color_bg()
	{
		//identify_focus();	
		var oSelection = body.document.selection.createRange();
		//if(sText.parentElement().tagName == "TABLE")
		//body.document.execCommand('BackColor', true,'RED');
		//alert(sText.parentElement().tagName );
		//e = sText.parentElement();
		//e.style.backgroundColor = 	"#FF8C00";	
		//e.style.backgroundColor = 	"RED";	
		
		COLOR_METHOD="bg";
		if(isTable(oSelection))
		{
			popup("main/toolbar/color_selector.htm");
			
		}
		//
	}
	
	
	
	function ed_color_fg()
	{
		//identify_focus();		
	//document.execCommand('ForeColor', false,'#FF8C00');//body.focus();
	COLOR_METHOD="fg";
	popup("main/toolbar/color_selector.htm");
	
	}
	
	function ed_image()
	{
		
		//identify_focus();		
		
		popup("main/toolbar/image_selector.aspx");
		//var sText = body.document.selection.createRange();
		//document.execCommand('InsertImage', true,'images/edu1.jpg');
	
	}
	
	function ed_edit_image()
	{
	

		var oSelection = document.selection.createRange();
		if(isImage(oSelection))
		{		
				document.execCommand('InsertImage', true,'opt');
		}
		else 
		{
			alert("Not an Image");
		}
		
	}
	
	
	/* old insert image function
	function insert_image(image)
	{
		my_popup.close();
		identify_focus();
		document.execCommand('InsertImage', false,'images/'+image);
		//ed_edit_image();
		
	}
	*/
	
	function insert_image(image,Check,wrapimage)
	{
			identify_focus();		
			var sText = document.selection.createRange();
			
			var thepic = "thepic"+Math.round( 1+Math.random()*1000);
					
			var path = 'images/'+image;
					
			textA = "<IMG id="+thepic+ wrapimage +"name="+thepic+" SRC='" +path+ "'>";
			
			sText.pasteHTML(textA);
				
			/*high = document.getElementById(thepic).height;
			wide = document.getElementById(thepic).width;			
			
			var h;
			var w;
			
			if (wide > 200)
			{			
				a = high;
				b = wide;
				c = 200;
    			
				d = (a*c)/b;
				
				document.getElementById(thepic).height = d;
				document.getElementById(thepic).width = c;
			}*/
			
			my_popup.close();		
	}
	
	function insert_text()
	{
		var sText = body.document.selection.createRange();
		newLink = document.createElement("A");
		someText = document.createTextNode("hello");
		newLink.href = "Hello";
		newLink.appendChild(someText);
	}
	
	function insert_template(vtemplate)
	{
		if(identify_focus())
		{
		var sText = body.document.selection.createRange();
		sText.pasteHTML(vtemplate);
		}
		my_popup.close();
	}
	
	
	function ed_underline()
	{
		document.execCommand('Underline');
	}
	
	function ed_align_left()
	{
		identify_focus();		
		document.execCommand('JustifyLeft');

	}
	
	function ed_align_right()
	{
		identify_focus();		
		document.execCommand('JustifyRight');
	}
	
	function ed_align_center()
	{
		identify_focus();		
		document.execCommand('JustifyCenter');
	}
	
	function ed_custom1()
	{
		popup("main/toolbar/color_selector.htm");
				
	}
	
	function ed_custom2()
	{
			popup("main/toolbar/template_selector.htm");	
	}	
	function popup(page)
	{	
		my_popup= window.open (page, "my_popup","resizable=1,location=0,status=0,scrollbars=1,width=500,height=440");
	}

	function popup2(page)
	{	
	my_popup= window.open (page, "my_popup","resizable=1,location=0,status=0,scrollbars=1,width=300,height=340");
	}
	
	// Helping Functions
	 
	function isImage(oSelection)
	{
		if(document.selection.type == "Control")
			{
			if(oSelection(0).tagName == "IMG")
				return true;
			else
				return false;
			}
		else 
			return false;
	}
	
	function isTable(oSelection)
	{
		if(document.selection.type == "Control")
			{
			if(oSelection(0).tagName == "TABLE")
				return true;
			else
				return false;
			}
		else 
			return false;
	}
	
	
	function isAnchor(oSelection)
	{
		if(document.selection.type == "Control")
		{
			oElement = oSelection(0).parentElement;
		}
	
		else
		{
			oElement =oSelection.parentElement();
		}
		//document.Form1.TraceBox.value = oElement.tagName;

		
		while (oElement.parentElement != null)
		{
			if(oElement.tagName =="A")
			break;
			oElement = oElement.parentElement;
		}
		if(oElement.tagName == "A")
		{
		return true;
		}
		else 
		{
		return false;
		}
	}
	
/**
	EDITABLE PORTION IDENTIFICATION AND FOCUS
 **/
function change_to()
	{
		oElement=body.document.activeElement;
		if(	oElement.id == "header" |
			oElement.id == "banner1" |
			oElement.id == "body" | 
			oElement.id == "right_column" | 
			oElement.id == "left_column" |
			oElement.id == "footer" )
		FOCUSED_DIV = oElement.id;
	}

function identify_focus()
	{
		
		document.all[FOCUSED_DIV].setActive();
		document.all[FOCUSED_DIV].focus();
		oElement=body.document.activeElement;
		if(	oElement.id == "header" |
			oElement.id == "banner1" |
			oElement.id == "body" | 
			oElement.id == "right_column" | 
			oElement.id == "left_column" |
			oElement.id == "footer" )
			return true;
			

		return false;
		
	}
	
	function ed_bullet()
	{
		identify_focus();		
		document.execCommand('InsertUnorderedList');
	}
	
function detect_editable()
	{
		if(document.getElementById("body") != null) 
		if(body.isContentEditable)
		{
			
			document.onselectionchange = change_to;
			apply_editable_style();	
			document.body.onbeforepaste = fnonbeforepaste;
		}
		
		if(document.getElementById("right_column") != null) 
		if(right_column.isContentEditable)
		{
			
			document.onselectionchange = change_to;
			apply_editable_style();	
			//if(document.getElementById("right_column") != null)		
			//document.right_column.onbeforepaste = fnonbeforepaste;
		}
		
		if(document.getElementById("left_column") != null)
		if(left_column.isContentEditable)
		{
			
			document.onselectionchange = change_to;
			apply_editable_style();			
			//document.left_column.onbeforepaste = fnonbeforepaste;
		}
		
	
	}
	
function apply_editable_style()
{
	if(document.getElementById("body") != null)
	document.getElementById("body").className = "body_portion_editable";

	if(document.getElementById("left_column") != null)
	document.getElementById("left_column").className = "left_column_portion_editable";
	
	if(document.getElementById("right_column") != null)
	document.getElementById("right_column").className = "right_column_portion_editable";
}
