
function AppModeChange(border){
	if(Application_Mode) App_Object.outerHTML = "<?xml:namespace prefix = hta /><hta:application id=\"App_Object\" application=\"yes\" applicationname=\"Hindi Editor\" border=\""+(border?"thick":"none")+"\" borderstyle=\"normal\" caption=\"yes\" contextmenu=\"yes\" icon=\"hindi_editor.ico\" innerborder=\"yes\" maximizebutton=\"yes\" minimizebutton=\"yes\" navigable=\"no\" scroll=\"no\" scrollflat=\"yes\" selection=\"no\" showintaskbar=\"yes\" singleinstance=\"yes\" sysmenu=\"yes\" version=\"1.23\" windowstate=\"normal\"></hta:application>"
	if(border){
		document.all.CaptionBar.style.display="none";
		document.all.ClientArea.style.borderWidth=0;
	}
	else{
		document.all.CaptionBar.style.display="block";
		document.all.ClientArea.style.borderWidth=3;
		document.all.ClientArea.style.borderTopWidth=0;
	}
}

function ApplyTheme(Theme){
var NewCaption = "";
	ThemeName = Theme;
	AppModeChange(Theme=="none");
	document.all.ThemeScript.src="../themes/"+((Theme=="none")?"":(Theme+"/"))+"theme.js";
	document.all.ThemeStyleSheet.href="../themes/"+((Theme=="none")?"":(Theme+"/"))+"theme.css";
	b_close.src = ThemeFolder + Theme + "/b_close.gif"
	b_close_glow.src = ThemeFolder + Theme + "/b_close_glow.gif"
	b_close_pressed.src = ThemeFolder + Theme + "/b_close_pressed.gif"
	c_left.src = ThemeFolder + Theme + "/c_left.gif"
	c_mid.src = ThemeFolder + Theme + "/c_mid.gif"
	document.all.CaptionBar.all.tags("td")[0].style.backgroundImage = "url(" + c_left.src + ")";
	document.all.CaptionBar.all.tags("td")[1].style.backgroundImage = "url(" + c_mid.src + ")";
	document.all.dlgBtnBar.src = b_close.src;
}

function ASCII2Hindi(){
var tmpRange, Range;
var text = "", i=0;
	tmpRange = document.all.txtEdit.createTextRange();
	text = tmpRange.text;
	tmpRange.text = "";
	tmpRange.select();
	for(i=0; i<text.length; ++i){
		event.keyCode = text.charCodeAt(i);
		event.returnValue = true;
		ITrans_KeyFilter();
		if(event.returnValue){
			Range = document.selection.createRange();
			Range.collapse(false);
			Range.text = String.fromCharCode(event.keyCode);
		}
	}
}


// A few usefull named constants.
var vbOKOnly = 0;
var vbOKCancel = 1;
var vbCancel = 2;
var vbYesNo = 4 ;
var vbYes = 6;
var vbNo = 7;
var vbInformation = 64;
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;

function HandleErr(Exception, Message){
var Error = "";
	Error = "Error: " + (Exception.number & 0xFFFF).toString() + "\tWhile: " + Message + "\r\n";
	Error += "Description:\t" + Exception.description + "\r\n";
	Error += "Time:\t" + LogTime.toString() + "\r\n\r\n";
	alert(Error);
}

function OpenFile(){
var FilePath = "";
var File;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
	if(!Application_Mode){
		alert('This and all Open/Save related features will work only in application mode.');
		return;
	}
	document.all.oComDlg.ShowOpen();
	FilePath = document.all.oComDlg.FileName;
	if(FilePath == "") return;
	FSO = new ActiveXObject("Scripting.FileSystemObject");
	File = FSO.GetFile(FilePath);
	TextStream = File.OpenAsTextStream(ForReading, TristateUseDefault);
	try{FileText = TextStream.ReadAll();}
	catch(exp){alert(exp, "Reading " + FilePath);}
	if(FileText == "") return;
	if(FileText.length != File.size)
		HandleErr(new Error(0, "It might have NULL chars in it."), "Reading " + FilePath);
	TextStream.Close();
	UTF_Stream = new Stream(FileText);
	document.all.txtEdit.innerText = UTF8_to_UCS2(UTF_Stream);
}

function SaveFile(){
var FilePath = "";
var File;
var TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0;
	if(!Application_Mode){
		alert('This and all Open/Save related features will work only in application mode.');
		return;
	}
	document.all.oComDlg.ShowSave();
	FilePath = document.all.oComDlg.FileName;
	if(FilePath == "") return;
	UTF_Stream = new Stream(document.all.txtEdit.innerText);
	FileText = UCS2_to_UTF8(UTF_Stream);
	FSO = new ActiveXObject("Scripting.FileSystemObject");
	if(FSO.FileExists(FilePath)){
		File = FSO.GetFile(FilePath);
		TextStream = File.OpenAsTextStream(ForWriting, TristateUseDefault);
	}
	else
		TextStream = FSO.CreateTextFile(FilePath);
	try{TextStream.Write(FileText);}
	catch(exp){alert(exp, "Writing " + FilePath);}
	TextStream.Close();
}

var btnCloseDragEnded = true;
function HandleBar(obj){
var Source = "";
var Ele = null;
	event.cancelBubble=true;
	if(obj.tagName == "BODY" && event.type == "mouseup"){
		btnCloseDragEnded = true;
		return;
	}
		
	if(obj.id == "areaDlgClose")
		Ele = document.all.dlgBtnBar;
	if(event.type == "mouseover")
		if(btnCloseDragEnded)
			Ele.src = b_close_glow.src;
		else
			Ele.src=b_close_pressed.src;
	if(event.type == "mousedown")
		if(event.button == 1){
			Ele.src=b_close_pressed.src;
			btnCloseDragEnded = false;
		}
	if(event.type == "mouseout")
		Ele.src = b_close.src;
}

function mOver(obj){
	if(obj.tagName == "TD") {	obj.style.background='#316AC5'; obj.style.color='#FFFFFF'; }
}

function mOut(obj){
	if(obj.tagName == "TD") {	obj.style.background='#ECE9D8'; obj.style.color='#000000'; }
}

var StartX = -1;
var StartY = -1;
function MoveWindow(){
	if(!Application_Mode) return;
	if(event.button != 1){
		StartX = StartY = -1;
		return;
	}
	if(event.type == "mousedown"){
		StartX = event.x;
		StartY = event.y;
	}
	else if(event.type == "mousemove" && !(StartX < 0 || StartY < 0)){
		window.moveBy((event.x - StartX), (event.y - StartY));
	}
}

function AppWindowStart(AppIcon){

	document.write('<map name="dlgButtonXP">');
	document.write('	<area id="areaDlgClose" shape="rect" coords="7,6, 28,27" nohref title="Close" onmouseover="HandleBar(this);" onmouseout="HandleBar(this);" onmousedown="HandleBar(this);" onmouseup="HandleBar(this);" onclick="if(Application_Mode) window.close(); else alert(\'This will work only in Application mode.\');">');
	document.write('	<area shape="rect" coords="0,0, 34,30" nohref>');
	document.write('</map>');
	document.write('<table id="AppWindow" width="100%" align="center" border="0" cellpadding="0" cellspacing="0" oncontextmenu="return false;" onselectstart="return false;">');
	document.write('<tr><td>');
// Make it AppWindow_CaptionBar and AppWindow_ClientArea
	document.write('	<table id="CaptionBar" width="100%" border="0" cellpadding="0" cellspacing="0" style="display:'+(Application_Mode?'none':'block')+';height:30px;table-layout:fixed;overflow:hidden;" onmousedown="MoveWindow();" ondblclick="if(Application_Mode){window.moveTo(0, 0);window.resizeTo(screen.availWidth, screen.availHeight);}">');
	document.write('	<tr style="height:30px;cursor:inherit;">');
	document.write('		<td style="width:34px;background-image:url('+c_left.src+');"><span style="width:34px;"><img src="'+unescape(AppIcon)+'" align="right" hspace="5" border="0" height="16" width="16"></span></td>');
	document.write('		<td class="AppWindowCaption" style="width:100%;background-image:url('+c_mid.src+');padding-left:0;white-space:nowrap;">'+document.title+'</td>');
	document.write('		<td style="width:34px"><img id="dlgBtnBar" src="'+b_close.src+'" border=0 height="30" width="34" usemap="#dlgButtonXP"></td>');
	document.write('	</tr>');
	document.write('	</table>');
	document.write('</td></tr>');

	document.write('<tr><td id="ClientArea" class="AppWindowBorder" style="height:100%;">');
}

function AppWindowFinish(){
	document.write('</td></tr>');
	document.write('</table>');
}

function Resize_Window(){
var Adjust = (Application_Mode && ThemeName == "none") ? 3 : 8;
var ClientAdjust, WindowHeight, ClientHeight;
	with(document.all){
		ClientAdjust = CaptionBar.offsetHeight + MenuBar.offsetHeight + ToolBar.offsetHeight;
	}
	WindowHeight = document.body.offsetHeight - Adjust;
	ClientHeight = document.body.offsetHeight - ClientAdjust - Adjust;
	document.all.AppWindow.style.height = (WindowHeight < 0) ? 0 : WindowHeight;
	document.all.txtEdit.style.height = (ClientHeight < 0) ? 0 : ClientHeight;
}

