﻿var req = null;
var inMathQuery=0,inMathWork=0;

function displayMath(inputText, divID, textID,e)
{	
    var ascii = new Array();
    var imgmatches = new Array();
    var tmp,tmpimgmatch;
    //inputText = inputText.replace("<br />","\r\n");
	var inputTextCopy = inputText;
	
	// REGEX doesn't work all that great when the deliminters are more than a single
	// character so I'll need to brute force this instead.....
	//ascii = inputText.match(/`[^`]*`/gi);
    //ascii = inputText.match(/\[BMTH\][^[\[EMTH\]]]*\[EMTH\]/gi);
    
    var tmpcopy;
    var loc_start;
    var loc_end;
    var len;
    var acount = 0;
    var math;
    tmpcopy = inputText.toUpperCase();
	
	// First process all the inline math stuff....
    //loc_start = tmpcopy.indexOf("[IMTH]");
    //loc_end = tmpcopy.indexOf("[\\IMTH]");
    //while (loc_start != -1 && loc_end != -1) {
	//	len = loc_end - loc_start + 7;
	//	ascii[acount++] = inputTextCopy.substr(loc_start, len);
		
	//	loc_start = tmpcopy.indexOf("[IMTH]", loc_end+7);
	//	loc_end = tmpcopy.indexOf("[\\IMTH]", loc_end+7);
    //}
      
    //math = document.createElement('div');
    //if(ascii!=null)
    //{
	//    for(var i =0; i < ascii.length; i++)
	//    {
	//	    tmp = ascii[i];
		 
			// we need to strip the begin/end tags off before processing.....   
	//	    ascii[i] = ascii[i].substr(6,ascii[i].length-13);
		    
		    // The ascii stuff was seriously interfering with some of the other text stuff so I dumped it....
		    //math.innerHTML = "<div class=\"math\" style=\"display:inline;\">" + AMTparseExpr(ascii[i],false)[0] + "</div>";		    
	//		math.innerHTML = "<div class=\"math\" style=\"display:inline;\">" + ascii[i] + "</div>";
	//	    jsMath.ProcessBeforeShowing(math);

	//	    inputTextCopy = inputTextCopy.replace(tmp,math.innerHTML);
	//    }
    //}
    
    // now do it all over again for the display math....
    tmpcopy = inputTextCopy.toUpperCase();
    ascii = new Array();
    acount = 0;
    loc_start = tmpcopy.indexOf("[DMTH]");
    loc_end = tmpcopy.indexOf("[\\DMTH]");
    while (loc_start != -1 && loc_end != -1) {
		len = loc_end - loc_start + 7;
		ascii[acount++] = inputTextCopy.substr(loc_start, len);
		
		loc_start = tmpcopy.indexOf("[DMTH]", loc_end+7);
		loc_end = tmpcopy.indexOf("[\\DMTH]", loc_end+7);
    }
      
    math = document.createElement('div');
    if(ascii!=null)
    {
	    for(var i =0; i < ascii.length; i++)
	    {
		    tmp = ascii[i];
		 
			// we need to strip the begin/end tags off before processing.....   
		    ascii[i] = ascii[i].substr(6,ascii[i].length-13);
		    
			math.innerHTML = "<div class=\"math\" style=\"display:inline;\">" + ascii[i] + "</div>";
		    jsMath.ProcessBeforeShowing(math);

		    inputTextCopy = inputTextCopy.replace(tmp, "" + math.innerHTML + "");
	    }
    }

	// This is no longer needed because I've got everyting in <pre></pre> tags....
	// In fact with this in here blank lines are all ignored.....
	//inputTextCopy = inputTextCopy.replace(/\r*\n/g,"<br />");			
	
    if(document.getElementById(divID) && e == "byclick")
    {
        document.getElementById(divID).style.display = display_div_on;
    }
    
    if (e == "ans_preview") {
		inputTextCopy = "<img src=\"/images/nerdreply.gif\" height=\"80\" width=\"64\" align=\"left\">"+inputTextCopy;
	}
	
	// In order for blank lines to be preserved in the question display (not the dynamic preview
	// in the question/answer pages) they have to be enclosed in <pre></pre> tags.  This preserves
	// the blank lines that would otherwise be eliminated upon display.  That also means that we 
	// don't need to put in an extra set of tags.
	//
	// The only time the <pre></pre> tags should probably be put in here is in the dynamic preview
	// in the question/answer pages.....
	//
	if ((inputTextCopy.substr(0, 5)).toUpperCase() == "<PRE>") {
		document.getElementById(textID).innerHTML = inputTextCopy;
	} else {
		document.getElementById(textID).innerHTML = "" + inputTextCopy + "";
	}
	
	//alert(inputTextCopy);
	//alert("<pre>"+inputTextCopy+"</pre>");
    //alert(document.getElementById(textID).innerHTML);
}

function hideMe(divID) 
{
    document.getElementById(divID).style.display = "none";
    if (divID.match("editorPreview"))
    {
        previewQuestionOn = false;
        previewWorkOn = false;
    }
}

function retriveName()
   {
       if(req.readyState == 4)
       {
          //alert(req.status);
          if(req.status == 200)
          { 
              //alert(req.responseText);
              document.getElementById("imgMathML").setAttribute("src",req.responseText);
          }
       }
   }
        
function changeHiddenMathML()
   {
       document.getElementById("hdnMathML").value = document.getElementById("divMathML").innerHTML;
   }        

//function PreviewOnOff(btnID, SrcEditor, PvwDivID, PvwTextID, PvwType, Lang)
function PreviewOnOff(PvwType, Lang, Refresh, ClientID)
{
	var strPre = (arguments.length == 4) ? ClientID : "";
	var btnPvwID = "btnPreview_FCK" + PvwType;
	var btnRefID = "btnRefresh_FCK" + PvwType;
	var SrcEditor;
	var PvwDivID = PvwType + "Preview";
	var PvwTextID = PvwType + "Display";
	
	var btnName;
	var btnNameRef;
	var MD_Text;
	var btnHide;
	var btnPvw = document.getElementById(btnPvwID);
	var btnRef = document.getElementById(btnRefID);

	var currentHTML = btnPvw.innerHTML.toUpperCase();
	var beginHTML = "<img src='/images/";
	var endHTML = ".gif' border='0'>";
	
	if (Lang == 1) {
		btnHide = "HidePreview";
		btnNameRef = "RefreshPreview"
	} else if (Lang == 2) {
		btnHide = "HidePreview_SP";
		btnNameRef = "RefreshPreview_SP"
	}  else {
		btnHide = "HidePreview";
		btnNameRef = "RefreshPreview"
	}
	
	// In the edit question page we need to send something other than
	// just Answer to get the preview to work properly so now we need 
	// to fix up PvwType to go back to Answer so we can get our hands
	// on the editor and to deal with chaning the button images....

	PvwType = (PvwType.indexOf("Answer") != -1) ? "Answer" : PvwType;
	
    if (currentHTML.indexOf("HIDEPREVIEW") == -1 || Refresh)
    {
		// we only need to do this if it's the first time around
    	if (btnPvw != null && !Refresh) {
    		btnPvw.innerHTML = beginHTML + btnHide + endHTML;
    	}

		// again only need to do this if it's the first time around
    	if (btnRef != null && !Refresh) {
    		btnRef.innerHTML = beginHTML + btnNameRef + endHTML;
    	}

    	SrcEditor = strPre + "FCK" + PvwType;
        var oEditor = FCKeditorAPI.GetInstance(SrcEditor);

        MD_Text = oEditor.GetData()
        
        // First do some replacements to strip out a lot of the junk that gets put into these tags as well as "bad" tags 
        //var RegEx_P = /<p\b(\"[^\"]*\"|[^']*'|[^'\">])*>/ig;
        var RegEx_P = /<p\b(\"[^\"]*\"|[^\">])*>/ig;
        var RegEx_B = /<b\b(\"[^\"]*\"|[^\">])*>/ig;
        var RegEx_I = /<i\b(\"[^\"]*\"|[^\">])*>/ig;
        var RegEx_U = /<u\b(\"[^\"]*\"|[^\">])*>/ig;
        var RegEx_BR = /<br\b(\"[^\"]*\"|[^\">])*>/ig;
        var RegEx_HR = /<hr\b(\"[^\"]*\"|[^\">])*>/ig;
        
        MD_Text = MD_Text.replace(RegEx_P,"<p>");
        MD_Text = MD_Text.replace(RegEx_B,"<b>");
        MD_Text = MD_Text.replace(RegEx_I,"<i>");
        MD_Text = MD_Text.replace(RegEx_U,"<u>");
        MD_Text = MD_Text.replace(RegEx_BR,"<br />");
        MD_Text = MD_Text.replace(RegEx_HR,"<hr />");
        
        // These just need to go.  Some are just to dangerous to leave in the text at all..
        var RegEx_Bad_Start = /<(a|img|font|script|embed|object|frameset|frame|iframe|meta|link|style|span)\b(\"[^\"]*\"|[^\">])*>/ig;
        var RegEx_Bad_End = /<\/(a|img|font|script|embed|object|frameset|frame|iframe|meta|link|style|span)>/ig
        
        MD_Text = MD_Text.replace(RegEx_Bad_Start,"");
        MD_Text = MD_Text.replace(RegEx_Bad_End,"");
        
        // Some extra stuff Word likes to put in that isn't needed
        MD_Text = MD_Text.replace(/<o:p>/ig, "");
        MD_Text = MD_Text.replace(/<\/o:p>/ig, "");
        
        // Now html Encode the input
        //Encoder.EncodeType ="entity";
        //MD_Text = Encoder.htmlEncode(MD_Text);
        
        // Now put the "allowed" html tags back in....
         //MD_Text = MD_Text.replace(/&lt;strong&gt;/ig, "<strong>");
         //MD_Text = MD_Text.replace(/&lt;\/strong&gt;/ig, "</strong>");
         //MD_Text = MD_Text.replace(/&lt;b&gt;/ig, "<b>");
         //MD_Text = MD_Text.replace(/&lt;\/b&gt;/ig, "</b>");
         //MD_Text = MD_Text.replace(/&lt;i&gt;/ig, "<i>");
         //MD_Text = MD_Text.replace(/&lt;\/i&gt;/ig, "</i>");
         //MD_Text = MD_Text.replace(/&lt;em&gt;/ig, "<em>");
         //MD_Text = MD_Text.replace(/&lt;\/em&gt;/ig, "</em>");
         
         //MD_Text = MD_Text.replace(/&lt;u&gt;/ig, "<u>");
         //MD_Text = MD_Text.replace(/&lt;\/u&gt;/ig, "</u>");
         //MD_Text = MD_Text.replace(/&lt;p&gt;/ig, "<p>");
         //MD_Text = MD_Text.replace(/&lt;\/p&gt;/ig, "</p>");
         //MD_Text = MD_Text.replace(/&lt;br \/&gt;/ig, "<br />");
         //MD_Text = MD_Text.replace(/&lt;hr \/&gt;/ig, "<hr />");
         //MD_Text = MD_Text.replace(/&nbsp;/ig, " ");
         //MD_Text = MD_Text.replace(/&amp;/ig, "&");
         //MD_Text = MD_Text.replace(/&quot;/ig, "\"");


        // THis needs to be here to prevent it from being stripped out....
        if (PvwType == "Answer") {
        	MD_Text = "<img src=\"/images/nerdreply.gif\" height=\"80\" width=\"64\" align=\"left\">" + MD_Text;
        }
        
        displayMath(MD_Text, PvwDivID, PvwTextID, 'byclick');
    }
    else
    {
    	hideMe(PvwDivID);

    	switch (PvwType) {
    		case 'Question':
    			btnName = "PreviewQuestion";
    			break;
    		case 'Answer':
    			btnName = "PreviewAnswer";
    			break;
    		case 'WorkDone':
    			btnName = "PreviewWorkDone";
    			break;
    		case 'Reply':
    			btnName = "PreviewReply";
    			break;
    		case 'Comment':
    			btnName = "PreviewComment";
    			break;
    		default:
    			btnName = "Preview";
    	}

    	if (Lang != 1) {
    		btnName += "_SP";
    	}

    	if (btnPvw != null) {
    		btnPvw.innerHTML = beginHTML + btnName + endHTML;
    	}

    	if (btnRef != null) {
    		btnRef.innerHTML = "";
    	}
    }
}
function TurnOnKeyUpOnlyWhenDisplayed(btnID,inputText,divID, textID, e, PvwType)
{   

    var btn = document.getElementById(btnID);
	
	var currentHTML = btn.innerHTML.toUpperCase();
	
    if (currentHTML.indexOf("HIDEPREVIEW.GIF") != -1)
    {
		if (PvwType == "A") {
			inputText = "<img src=\"/images/nerdreply.gif\" height=\"80\" width=\"64\" align=\"left\">" + inputText;
        }
        displayMath(inputText,divID, textID ,e);
    }
}
function clone(myObj)
{
	if(typeof(myObj) != 'object') return myObj;
	if(myObj == null) return myObj;

	var myNewObj = new Object();

	for(var i in myObj)
		myNewObj[i] = clone(myObj[i]);

	return myNewObj;
}
