function copyText(obj) {
	//alert("here");
	if (obj.type=="text" || obj.type=="textarea"){
		var rng = obj.createTextRange();
	} 
	else {
		var rng = document.body.createTextRange();
		rng.moveToElementText(obj);
	}
	rng.scrollIntoView();
	rng.select();
	if (confirm('Copy the selected text to the ClipBoard?')) 
		rng.execCommand("Copy");rng.collapse(false);rng.select();
}

function PreviewFile(strId){
	obj =parent.document.getElementById(strId);
	var number = obj.selectedIndex;
	var path = obj.options[number].value;
	var strText = new String();
	//alert(path);
	strText=path;
	path=strText.replace('\\','\\\\');
	PreviewWindow=window.open(path,"PreviewWindow","toolbar=no,scrollbars=yes,directories=no,status=no,menubar=no,resizable=yes,width=700,height=600,left=20,top=20");
	return false;
}
function GetFilePath(strId){
	obj =parent.document.getElementById(strId);
	var number = obj.selectedIndex;
	var path = obj.options[number].value;
	var strText = new String();
	
	strText=path;
	path=strText.replace('\\','\\\\');
	    
	return path;
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function SubmitLanguageChange()
   {
	 //var number = document.SubmitForm.cmbCodelanguage.selectedIndex;
	 //var lang = document.SubmitForm.cmbCodelanguage.options[number].value
	 //document.SubmitForm.action="SubmitCode.asp?Language=" + lang
	 document.SubmitForm.submit();

	//var number = document.SubmitForm.cmbCodelanguage.selectedIndex;
	//alert(document.SubmitForm.cmbCodelanguage.options[number].value);
   }


function InsertText(strInsertText)
{
	//if (Submitted) return false;
	if (strInsertText <= 0) return false;
	var objTextArea = document.SubmitForm.txtLongDesc;
	if (objTextArea)
	{
		if (document.selection && document.selection.createRange)
		{
			objTextArea.focus();
			var objSelectedTextRange = document.selection.createRange();
			var strSelectedText = objSelectedTextRange.text;
			objSelectedTextRange.text = strInsertText + strSelectedText;
		}
		else
		{
			objTextArea.value += strInsertText;
			objTextArea.focus();
		}
	}
	return false;
}

function ResizeTextArea()
{
	var TextArea = document.SubmitForm.txtLongDesc;
	var scrnX = document.body.clientWidth;
	if (TextArea)
	{
		var scrnY = document.body.clientHeight;
		TextArea.cols = (scrnX / 12) - 5;
		TextArea.rows = (scrnY / 40);
	}
	TextArea = document.SubmitForm.Sig;
	if (TextArea)
	{
		TextArea.cols = (scrnX / 12) - 5;
	}
}

function ToggleWrapSelection(strTag)
{

	var ch1=String.fromCharCode(171);
	var ch2=String.fromCharCode(187);
	var ch3=String.fromCharCode(215);
	var StartTag,EndTag;
	
	//var number = document.SubmitForm.cmbSubmissionType.selectedIndex;
	//var submittype = document.SubmitForm.cmbSubmissionType.options[number].value;
	//var submittype=2;	
	//Alert(document.Submitform.cmbArticleLanguage.Selectedindex);

	var number = document.SubmitForm.cmbCodeblockLang.selectedIndex;
	var lang = document.SubmitForm.cmbCodeblockLang.options[number].value
	
	if (strTag.length <= 0)  return false;
	//if ((strTag.length <= 0) || (submittype=2)) return false;
	//alert(strTag);

    if(strTag=="LI"){
    	//StartTag = ch1 + strTag + ch2;
    	StartTag = "<" + strTag + ">";
    	EndTag   = "";
    }
	else if(strTag=="HTML"){
		strTag="code";
     	StartTag = ch1 + strTag + " LangId=999" + ch2;
    	EndTag   = ch1 + "/" + strTag + ch2;
    }
	else if(strTag=="code"){
     	StartTag = ch1 + strTag + " LangId=" + lang + ch2;
    	EndTag   = ch1 + "/" + strTag + ch2;
	}
	else if(strTag=="img"){
		var BR="<" + "BR" + ">";
		var PS="<" + "P Align=center" + ">";
		var PE="<" + "/P" + ">";
		
     	StartTag = BR + PS + "<" + strTag + " src=\'" + GetFilePath('lstFiles') + "\'/" + ">" + PE + BR ;
    	EndTag   = "";
	}
	else{
		StartTag = "<" + strTag + ">";
    	EndTag   = "<" + "/" + strTag + ">";
	}
	
     
	var objTextArea = document.SubmitForm.txtLongDesc;
	

	if (objTextArea)
	{
		if (document.selection && document.selection.createRange)
		{
			objTextArea.focus();
			var objSelectedTextRange = document.selection.createRange();
			var strSelectedText = objSelectedTextRange.text;
			var nStrtLen = StartTag.length;
			var nEndLen  = EndTag.length;
			var nSelLen  = strSelectedText.length;
			
			if (strSelectedText.substr(0, nStrtLen) == StartTag &&
			    strSelectedText.substr(nSelLen-nEndLen) == EndTag)
			{
				objSelectedTextRange.text = strSelectedText.substr(nStrtLen, nSelLen-nStrtLen-nEndLen);
			}
			else
			{
			    if((strTag=="UL")|| (strTag=="OL"))
			    {
					if (strTag=="OL")			    
						objSelectedTextRange.text = StartTag + "<" + "LI" + ">" + replaceSubstring(strSelectedText, "\n", "<" + "LI" + ">" )  + EndTag;
					else
						objSelectedTextRange.text = StartTag + "<" + "LI" + ">" + replaceSubstring(strSelectedText, "\n", "<" + "LI" + ">" )  + EndTag;}
			    else
				    objSelectedTextRange.text = StartTag + strSelectedText + EndTag;
			}

			if (strSelectedText.length == 0)
			{
				objSelectedTextRange.move("character", -(strTag.length + 3));
				objSelectedTextRange.select();
			}
			objTextArea.focus();
		}
		else
		{
			var strAppendText = StartTag + EndTag;
			objTextArea.value += strAppendText;
			objTextArea.focus();
		}
	}
	return false;

}

function InLinkErAte(NewWindow)
{

	var ch1=String.fromCharCode(171);
	var ch2=String.fromCharCode(187);
	var ch3=String.fromCharCode(215);
	
	//if (Submitted) return false;
	var objTextArea = document.SubmitForm.txtLongDesc;
	if (objTextArea)
	{
		if (document.selection && document.selection.createRange)
		{
			objTextArea.focus();
			var objSelectedTextRange = document.selection.createRange();
			var strSelectedText = objSelectedTextRange.text;
			var objRegEx = new RegExp("(ht|f)tps?:\/\/");
			var strProtocol = "";
			if (strSelectedText.length == 0)
			{
				if (NewWindow)
				{
					alert("Please select a URL within the message text to convert");
					return false;
				}
			}
			else
			{
				if (!objRegEx.test(strSelectedText)) strProtocol = "http://";
			}

			var URI = strProtocol + strSelectedText;

			var offset = 0;
			if (NewWindow)
			{
				//objSelectedTextRange.text = "«a href=\"" + URI + "\'»" + strSelectedText + "«/a»
				//                             «a target=_blank title='New Window' href="\'" + URI + "\'» «img src=\'nw.gif\' height=11 width=11 alt=\"New window\" border=0» «/a»";

								objSelectedTextRange.text = "<" + "a href=\'" + URI + "\'" + ">" + strSelectedText
											+ "<" +"/a"+ ">" + "<" +"a target=_blank title='New Window' href=\'"
											+ URI + "\'" + ">" + "<img style=\'textdecoration:none;\' src=\' images/nw.gif\' height=11 width=11 alt=\'New window\' border=0>" + "</a>" ;


				offset = 8 + URI.length + 47;
			}
			else
			{
				objSelectedTextRange.text = "<" + "a href=\'" + URI + "\'"+ ">"
											+ strSelectedText + "<" + "/a" + ">" ;
				offset = 4;
			}

			//if (strSelectedText.length == 0)
			{
				objSelectedTextRange.move("character", -offset);
				if (NewWindow)
				{
					objSelectedTextRange.moveStart("character", -strSelectedText.length);
					objSelectedTextRange.moveEnd("character", 0);
				}

				objSelectedTextRange.select();
			}
		}
		else
		{
			var strAppendText = "<" + "a href=\'" + strTarget + ">" + "<" +"/a" + ">";
			objTextArea.value += strAppendText;
			objTextArea.focus();
		}
	}
	return false;
}

function new_window(record){
//document.PreviewForm._txtLongDesc.value =document.SubmitForm.txtCodename.value;

var number = document.SubmitForm.cmbArticleLanguage.selectedIndex;
var lang = document.SubmitForm.cmbArticleLanguage.options[number].value

//alert(lang);

document.PreviewForm._txtLongDesc.value=document.SubmitForm.txtLongDesc.value
document.PreviewForm._txtSourceCode.value=document.SubmitForm.txtSourceCode.value
document.PreviewForm._txtCodeName.value=document.SubmitForm.txtCodeName.value
document.PreviewForm._cmbArticleLanguage.value=lang

//document.PreviewForm._FileArticleImage.value=document.SubmitForm.FileArticleImage.value

//alert(document.SubmitForm.txtLongDesc.value);
//alert(document.PreviewForm._txtLongDesc.value);
//alert(document.PreviewForm._txtSourceCode.value);
//alert(document.PreviewForm._txtCodeName.value);

var theform;
	theform = document.forms["PreviewForm"];

PreviewWindow=window.open("/main/Members/PreviewForm.aspx","PreviewWindow","toolbar=no,scrollbars=yes,directories=no,status=no,menubar=no,resizable=yes,width=700,height=600,left=20,top=20");
theform.target="PreviewWindow"
theform.submit();

 return false;
}

