var clientBrowserType = "I";
var inputTextBox = null;
var areaOfOutput = null;
var hiddenOutputContent=null;
var current_form=null;
var englishFontFace = "Arial";
var banglaFontFace = "BanglaPlain";
var englishFontSize = 2;
var banglaFontSize = 3;

function isValidEmail(email){
	var stEmail = /^.+\@.+\..+$/;
	return stEmail.test(email);	
}	
	
function isBlank(s){
	return ((s == null) || (s.length == 0))
}

function init(input, output,hidd_field,form_to_submit) {
    if(parseInt(navigator.appVersion.charAt(0)) >= 4) {
        if (navigator.appName == "Netscape") {
            clientBrowserType = "N";
        }
    }
    inputTextBox = input;
	areaOfOutput = output;
	hiddenOutputContent = hidd_field;
	current_form = form_to_submit;
}
function processChangeEvent(e)
{
	//checkEnter(e);
	makeItAgain();
	flushIt();
	document.forms[0].query.value = document.forms[0].inputBox.value;
}
function checkEnter(e)
{
	var keynum;
	if(window.event) // IE
		{
		keynum = e.keyCode;
		}
	else if(e.which) // Netscape/Firefox/Opera
		{
		keynum = e.which;
		}
	if (keynum == 13)
	{
		goToHell(e);
		return false;
	}
	return true;
}
function goToHell(e)
{
	var MAX_POST_SIZE = 800;

	//document.forms[0].sub_butt.disabled = true;



	document.forms[1].query.value = document.forms[0].inputBox.value;

	document.forms[1].submit();

	//if (!isValidEmail(current_form.field_2.value))
	//{
	//	alert ("Please give a valid email");
	//	return false;
	//}
	//var tname = current_form.field_1.value;
	//current_form.field_1.value = ":&nbsp;<b>" + tname +"</b>";
	//var tmail = current_form.field_2.value;
	//current_form.field_2.value = ":&nbsp;<b>" + tmail +"</b>";
	//current_form.submit();
}

function Paragraph(sPlainText) {
    this.PlainText = sPlainText;
    this.CipherText = "";
    this.StartOfChange = false;
}

var container = new Array();
var paraEndsWith = new Array(); //an array which keeps the state of para



function makeItAgain() {
    var sText = inputTextBox.value;
    var aParagraphs = sText.split("\n");
    var collector = new Array();
    var endsWith = new Array();
    var iLastIndex = -1;
    var iSaved = iNotSaved = 0;
    var bStartOfChange = false;
    for (i = 0; i < aParagraphs.length; i++) {
        bFound = false;
        for (j = iLastIndex + 1; j < container.length; j++) {
            if (container[j].PlainText == aParagraphs[i]) {
                iLastIndex = j;
                bFound = true;
                collector[i] = container[j];
                endsWith[i] = paraEndsWith[j];
                collector[i].StartOfChange = false;
                iSaved++;
                break;
            }
        }
        if (!bFound) {
            collector[i] = new Paragraph(aParagraphs[i]);
            endsWith[i] = "";
            if (!bStartOfChange) {
				collector[i].StartOfChange = true;
				bStartOfChange = true;
            }
            iNotSaved++;
        }    
    }
    container = collector;
    paraEndsWith = endsWith;
}
 
function flushIt() {
	var iNumberOfParagraphs = container.length;
	var sBookmark = "";
	var sOutput = "";
	var rawOutput="";
	var bChangerFound = false;
	
	if ("I" == clientBrowserType)
		sBookmark = "<span id=divEnd></span>";
	else
		sBookmark = "<layer name=divEnd></layer>";
	

	for (i = 0; i < iNumberOfParagraphs; i++) {
		var theParagraph = container[i];

		if ("" == theParagraph.CipherText) {
		    var sWillEndWith = "B";
		    
		    if (i > 0)
		        sWillEndWith = paraEndsWith[i - 1];
		        
		    var sContent = theParagraph.PlainText;
		    var iNextAt = -1;
		    var sDelimiter = "";
		    var sExtract = "";
		    var sCipher = "";
		    
		    while ("" != sContent) {
		        iNextAt = sContent.search("[<>]");
		        if (iNextAt == -1){
		        	sExtract = sContent;
		        	sDelimiter = "";
		        	sContent = "";
		        }
		        else {
		        	sExtract = sContent.substr(0, iNextAt);
		        	sDelimiter = sContent.charAt(iNextAt);
		        	sContent = sContent.substr(iNextAt + 1);
		        }

		        if (sWillEndWith == "E") {
		        	sCipher = sCipher + "<font face=\"" + englishFontFace + "\" size=" + englishFontSize + ">" + sExtract + "</font>";
		        }
		        else {
		        	sCipher = sCipher + "<font face=\"" + banglaFontFace + "\" size=" + banglaFontSize + ">" +  document.applets[0].transliterateIt(sExtract) + "</font>";
		        }

		        if (sDelimiter == "<"){
		        	if (sWillEndWith == "B"){ 
		        		sWillEndWith = "E";
		        		sDelimiter = "";
		        	}
		        	else {
		        		sDelimiter = "&lt;"
		        	}	
		        }
		        else if (sDelimiter == ">"){
		        	if (sWillEndWith == "E"){ 
		        		sWillEndWith = "B";
		        		sDelimiter = "";
		        	}
		        	else {
		        		sDelimiter = "&gt;"
		        	}	
		        }
		        sCipher = sCipher + sDelimiter;
		    } /* end of while */
		    
		    theParagraph.CipherText = sCipher;
		    
		    if (sWillEndWith != paraEndsWith[i]) {
				paraEndsWith[i] = sWillEndWith;
		        if (i < iNumberOfParagraphs - 1) {
		            container[i+1].CipherText = "";
		            paraEndsWith[i+1] = "";
		        }
		    }
		        
		} // if ciphertext = ""
		
		
		if (theParagraph.StartOfChange) {
			sOutput = sOutput + sBookmark + theParagraph.CipherText + "<br>";
			rawOutput = rawOutput + theParagraph.CipherText + "<br>";
			bChangerFound = true;
		}
		else {
			sOutput = sOutput + theParagraph.CipherText + "<br>";
			rawOutput = rawOutput + theParagraph.CipherText + "<br>";
		}
	}
	
	if (!bChangerFound)
		sOutput = sOutput + sBookmark;
	
	if ("I" == clientBrowserType) {
		var theFrame = areaOfOutput;
		theFrame.document.all("divContent").innerHTML = sOutput;
		theFrame.scrollTo(0, theFrame.document.all("divEnd").offsetTop);
	}
	else {
    	areaOfOutput.document.open("text/html", "replace");
		areaOfOutput.document.writeln("<html><head><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=user-defined\"></head><body>");
		areaOfOutput.document.writeln(sOutput);
		areaOfOutput.document.writeln("</body></html>");
		areaOfOutput.document.close();
		areaOfOutput.scrollTo(0, areaOfOutput.document.divEnd.top);
	}
	//hiddenOutputContent.value = rawOutput + "<hr>";
	hiddenOutputContent.value = rawOutput;
	
}

