var vReturn;

/* Infragfistic values */
var iged_obj=null;
var iged_update=false;
var iged_loadTime=0;
var iged_clrChange=false;
var iged_src=null;


function openWinSmilies(theURL,winName,features) {
  	window.open(theURL,winName,features);
}

//Function to format text in the text box
function FormatText(command, option){
	//Colour pallete
	if ((command == "forecolor") || (command == "hilitecolor")) {		
		parent.command = command;
		buttonElement = document.all(command);
		frames.message.focus()
		document.all.colourPalette.style.left = getOffsetLeft(buttonElement) + "px";
		document.all.colourPalette.style.top = (getOffsetTop(buttonElement) + buttonElement.offsetHeight) + "px";		
		if (document.all.colourPalette.style.visibility=="hidden")
			document.all.colourPalette.style.visibility="visible";
		else {
			document.all.colourPalette.style.visibility="hidden";
		}
		
		//get current selected range
		var sel = frames.message.document.selection; 
		if (sel != null) {
			colour = sel.createRange();
		}
	}
	
	frames.message.focus();
  	frames.message.document.execCommand(command, false, option);
  	frames.message.focus();
}

//Function to add image
function AddImage(){	
	imagePath = prompt("Enter the web address of the image", "http://");				
	
	if ((imagePath != null) && (imagePath != "")){	
		frames.message.focus(); 				
		frames.message.document.execCommand("InsertImage", false, imagePath);
	}
	frames.message.focus();			
}

//Function to switch to HTML view
function HTMLview() {

	//WYSIWYG view
	if (htmlOn == true) {
		var html = frames.message.document.body.innerText;
		frames.message.document.body.innerHTML = html;
		ToolBar1.style.visibility="visible";
		ToolBar2.style.visibility="visible";
		htmlOn = false;
	
	//HTML view
	} else {
		
		var html = frames.message.document.body.innerHTML;
		frames.message.document.body.innerText = html;
    		ToolBar1.style.visibility="hidden";
    		ToolBar2.style.visibility="hidden";
    		htmlOn = true;
    	}	
}

//Function to set colour
function setColor(color) {

	//retrieve selected range
	var sel = frames.message.document.selection; 
	if (sel!=null) {
		var newColour = sel.createRange();
		newColour = colour;
		newColour.select();
	}
		
	frames.message.focus();
	frames.message.document.execCommand(parent.command, false, color);
	frames.message.focus();
	document.all.colourPalette.style.visibility="hidden";
}


//Function to add smiley
function AddSmileyIcon(imagePath){	
	frames.message.focus();								
	frames.message.document.execCommand("InsertImage", false, imagePath);
}

// '***********************************************
// '*** 	JavaScript for colour palette 	   *****
// '***********************************************


//Colour pallete top offset
function getOffsetTop(elm) {
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

//Colour pallete left offset
function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}

//Function to hide colour pallete
function hideColourPallete() {
	document.all.colourPalette.style.visibility="hidden";
}

//Function to perform spell check
function checkspell() {
	try {
		var tmpis = new ActiveXObject("ieSpell.ieSpellExtension");
		tmpis.CheckAllLinkedDocuments(document);
	}
	catch(exception) {
		if(exception.number==-2146827859) {
			if (confirm("<% = strTxtIeSpellNotDetected %>"))
				openWin("http://www.iespell.com/download.php","DownLoad", "");
		}
		else
			alert("Error Loading ieSpell: Exception " + exception.number);
	}
}

function endlookupCheckText(xmlHttp)
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{
		var vResponseText = new String(xmlHttp.responseText);
		

		if (vResponseText.substring(0,1) == "1")
		{
		    alert('Please Edit Your Subject');
		    vReturn = false;
		}
		else
		    vReturn = true;
	}
}
	
function lookupCheckText(text)
{
	var xmlHttp = CreateXmlHttp();
	
	xmlHttp.onreadystatechange = function(){endlookupCheckText(xmlHttp)};	
	xmlHttp.open("GET", "SmutValidator.aspx?Smut=" + text, false);
	xmlHttp.send("");
}
	
function CreateXmlHttp()
{
	var xmlHttp;    
	// Probamos con IE
	try
	{
		// Funcionará para JavaScript 5.0
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{   
			xmlHttp = null;
		}
	}

	// Si no se trataba de un IE, probamos con esto
	if(!xmlHttp && typeof XMLHttpRequest != "undefined")
	{
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}

//Function to add smiley

function iged_imgStrSmiley()
{
	var img=iged_obj;
	if(!img)return null;
	var s="<IMG src='"+(iged_src?iged_src:img.src)+"'";
	var v=img.alt;if(v!=""&&v)s+=" alt='"+v+"'";
	v=img.height;if(v!=""&&v)s+=" height='"+v+"'";
	v=img.width;if(v!=""&&v)s+=" width='"+v+"'";
	v=img.border;if(v!=""&&v)s+=" border='"+v+"'";
	v=img.align;if(v!=""&&v)s+=" align='"+v+"'";
	return s+" />";
}

function iged_updateImgSmile(noFrame, imageUrl)
{
	//debugger;
	var v=imageUrl;
	if(v=="")return null;
	var img=document.createElement("IMG");
	img.src=iged_src=v;
	img.alt='none';
	img.height='17';
	img.width='17';
	iged_obj=img;
	img=iged_imgStrSmiley();
	return img;
}

function iged_updateImg(noFrame, imageText, imageUrl, vAlign, vBorder)
{
	//debugger;
	var v=imageUrl;
	if(v=="")return null;
	var img=document.createElement("IMG");
	img.src=iged_src=v;
	img.align = vAlign;
	img.border = vBorder;
	img.alt='none';
	iged_obj=img;
	img=imageText;
	return img;
}

function AddSmileyIconHtmlEditor(imageUrl)
{
	//debugger;
	var txt=iged_updateImgSmile(true, imageUrl);
	var img=iged_obj,o=this.iged_all._cur;
	if(iged_update)
	{
		if(document.all)this.iged_doImgUpdate(txt);
		else this.iged_doImgUpdate(img);
	}
	else
	{
		if(document.all)this.iged_insText(txt,false,false);
		else
		{
			if(this.iged_replaceS(this.iged_getEditTxt(),"\r\n","")=="<br>"&&o)o.setText("<p>");
			this.iged_insNodeAtSel(img);
		}
	}

}

function AddImageHtmlEditor(imageText, imageUrl, vAlign, vBorder)
{
	//debugger;
	var txt=iged_updateImg(true, imageText, imageUrl, vAlign, vBorder);
	var img=iged_obj,o=this.iged_all._cur;
	if(iged_update)
	{
		if(document.all)this.iged_doImgUpdate(txt);
		else this.iged_doImgUpdate(img);
	}
	else
	{
		if(document.all)this.iged_insText(txt,false,false);
		else
		{
			if(this.iged_replaceS(this.iged_getEditTxt(),"\r\n","")=="<br>"&&o)o.setText("<p>");
			this.iged_insNodeAtSel(img);
		}
	}

}

function AddSmileyIconHtmlEditorPopUp(imageUrl)
{
	//debugger;
	var txt=iged_updateImgSmile(true, imageUrl);
	var img=iged_obj,o=opener.iged_all._cur;
	if(iged_update)
	{
		if(document.all)opener.iged_doImgUpdate(txt);
		else opener.iged_doImgUpdate(img);
	}
	else
	{
		if(document.all)opener.iged_insText(txt,false,false);
		else
		{
			if(opener.iged_replaceS(opener.iged_getEditTxt(),"\r\n","")=="<br>"&&o)o.setText("<p>");
			opener.iged_insNodeAtSel(img);
		}
	}

}
