var doc = "document.";
var st = "";
var InitSMTP = "helo jinni\r\nmail from: <>\r\nrcpt to: <>\r\ndata";
var InitPOP3 = "user ashutosh.raghuwanshi\r\npass \r\nstat\r\nlist";
var InitFTP = "user Ashutosh Raghuwanshi\r\npass guest";	//anonymous
var InitHTTP = "GET /";
var PassPhrase = ""

//if(window.name != "full_sock")	window.open('./w_socket.html','full_sock','fullscreen=1,scrollbars=1');
//else
//	self.opener.close();

function init(){
var Range,WsShell;
	if (navigator.appName == "Microsoft Internet Explorer")	{ doc+="all.";st="style."; }
	if(document.all.txtCommand.innerText == "")	document.all.txtCommand.innerText = InitSMTP;
	Range = document.all.txtCommand.createTextRange();
	Range.move("word", 1)
	Range.expand("word")
	Range.select();
	Range = document.selection.createRange();
	Range.collapse(false);
	Range.select();
	document.all.txtCommand.focus();
}

function KeyFilter(){
var RegExpBsp = /.$/g;
	if(event.type == "keypress"){
		PassPhrase += String.fromCharCode(event.keyCode);
		event.keyCode = 0x2022;
	}
	else if(event.type == "keydown"){
		if(event.keyCode == 8)	PassPhrase = PassPhrase.replace(RegExpBsp, "");

	}
// Below is just here for Debugging
//	document.all.txtUtility.innerText = PassPhrase;
}

function GetPass(Source, Pass){
var strSource = ""+Source+"";		// It is to define the datatype of the arguments as string
var strPass = ""+Pass+"";			// so that the properties lile .length can be accessed
var RegExpPass = /(%u2022)+/gi;
var strReturn = "";
var Temp = "";
	strReturn = escape(strSource);
	Temp = RegExpPass.exec(strReturn);
	if(Temp == null) return null;

	if(unescape(Temp[0].toString()).length != strPass.length) return "";
	return unescape(strReturn.replace(RegExpPass, strPass));
}

function HandlePass(){
	with(document.all){
		if(chkPass.checked)	lblCommand.checked = true;
		txtCommand.onkeypress = txtCommand.onkeydown = chkPass.checked ? KeyFilter : null;
		txtCommand.focus();
	}
}

function ChooseProto(){
var Range
	with(document.all.txtCommand){
		document.all.txtPort.value = document.all.cmbPort.value;
		if( innerText == "" || innerText == InitSMTP ||  innerText == InitPOP3 || innerText == InitHTTP || innerText == InitFTP){
			if(document.all.txtPort.value == 25)	innerText = InitSMTP;
			if(document.all.txtPort.value == 110)	innerText = InitPOP3;
			if(document.all.txtPort.value == 21)	innerText = InitFTP;
			if(document.all.txtPort.value == 80)	innerText = InitHTTP;
		}
	}
	Range = document.all.txtCommand.createTextRange();
	Range.select();
}

function Scroll_txtResponce(){
var Range;
	Range = document.all.txtResponce.createTextRange();
	Range.move("character", Range.htmlText.length);
	Range.scrollIntoView();
}

function Connect(){
try{
	with(document.all){
		cmdConnect.blur();
		if(WS.State == 0){
			if(txtPort.value == ""){
				alert("You must fill a valid port number in the Port textbox.");
				txtPort.focus();
				return;
			}
			if(txtAddress.value == ""){
				alert("You must fill a valid server address in the Address textbox.");
				txtAddress.focus();
				return;
			}
			lblStatus.innerText = "Connecting...";
			WS.RemotePort = parseInt(txtPort.value);
			WS.RemoteHost = txtAddress.value;
			WS.Connect(WS.RemoteHost, WS.RemotePort);
		}
		else{
			lblStatus.innerText = "Disconnecting...";
			WS.close();
			txtResponce.innerText += "\r\n                       +-----------------------+ \r\n                       |   CONNECTION CLOSED   | \r\n                       +-----------------------+ \r\n________________________________________________________________________\r\n";
			Scroll_txtResponce();
			WS.RemotePort = 0;
			WS.RemoteHost = "";
			cmdConnect.innerHTML = "<u>C</u>onnect";
			if(WS.State == 0){
				lblStatus.innerText = "Disonnected.";
				cmdSend.disabled = true;
			}
		}
	}
}catch(err){alert(err.description);}
}

function Send_Data(){
var Data2Send = "";
var RegExpClr = /(%u2022)+/gi;
	with(document.all){
		if(lblCommand.checked){
			Data2Send = txtCommand.innerText;
			if(chkPass.checked){
				Data2Send = GetPass(txtCommand.innerText, PassPhrase);
				if(Data2Send == null){
					if(!confirm("You have checked the \"Password\" checkbox but didn't type any password\r\n\r\n" +
								"Should I continue?")) return;
					chkPass.checked = false;
					txtCommand.onkeypress = txtCommand.onkeydown = null;
					Data2Send = txtCommand.innerText;
				}
				else if(Data2Send == ""){
					PassPhrase = "";
					txtCommand.innerText = unescape(escape(txtCommand.innerText).replace(RegExpClr, ""));
					txtCommand.focus();
					alert(	"You have done some mistake while typing the password your password has been cleared" +
							" you may type it again");
					return;
				}
				else{
					PassPhrase = "";
					chkPass.checked = false;
					txtCommand.onkeypress = txtCommand.onkeydown = null;
					txtCommand.innerText = unescape(escape(txtCommand.innerText).replace(RegExpClr, ""));
					txtCommand.focus();
				}
			}
		}
		else
			Data2Send = txtUtility.innerText;
		WS.SendData(Data2Send + "\r\n");
		txtCommand.focus();
	}
}

function winopen(Res){
	winpopup = window.open("about:blank", "PreviewWin", "location=1, toolbar=1, directories=1, status=1, menubar=1, scrollbars=1, resizable=1, channel=1");
	winpopup.document.writeln(Res);
}
/*
var btnCloseDragEnded = true;
function HandleBar(obj){
var Source = "";
var Ele = null;
	if(obj.tagName == "BODY"){
		btnCloseDragEnded = true;
		return;
	}
	Source = (obj.tagName == "AREA") ? true : false;
	Ele = (obj.id == "areaClose") ? document.all.btnBar : document.all.dlgBtnBar;
	if(event.type == "mouseover")
		if(btnCloseDragEnded)
			Ele.src="../themes/blue/b_close" + (Source ? "_glow.gif" : ".gif");
		else
			Ele.src="../themes/blue/b_close" + (btnCloseDragEnded ? "_glow.gif" : "_pressed.gif");
	if(event.type == "mousedown")
		if(event.button == 1){
			Ele.src="../themes/blue/b_close" + (Source ? "_pressed.gif" : ".gif");
			btnCloseDragEnded = false;
		}
	if(event.type == "mouseup")
		if(event.button == 1){
			Ele.src="../themes/blue/b_close" + (Source ? "_glow.gif" : ".gif");
			btnCloseDragEnded = true;
		}
	if(event.type == "mouseout")
		Ele.src="../themes/blue/b_close.gif";
}
*/
function mOver(obj){
	if(obj.tagName == "TD") {	obj.style.background='#316AC5'; obj.style.color='#FFFFFF'; }
	if(obj.type == "button"){	obj.style.borderColor = '#666666';}
	if(obj.type == "text"){
		obj.style.backgroundColor = '#666666';
		obj.style.borderColor = '#FFFFFF';
		obj.style.borderStyle = 'ridge';
	}
}
function mOut(obj){
	if(obj.tagName == "TD") {	obj.style.background='#ECE9D8'; obj.style.color='#000000'; }
	if(obj.type == "button"){	obj.style.borderColor = '#000000'; }
	if(obj.type == "text"){		obj.style.backgroundColor = '#000000'; obj.style.borderColor = '#000000'; }
}

var ScrollStart = 0;
var ScrollIncrement = 1;
var ScrollEnd = 0;
var ScrollObject = null;
var ScrollCondition = "0";
var y = 0;
function scrollitems(obj, s, i, e)
{
	if(obj != null && ScrollObject != null) return;
	if(obj){
		ScrollObject = obj;
		ScrollStart = y = s;
		ScrollIncrement = i;
		ScrollEnd = e;
		if(ScrollStart < ScrollEnd && ScrollIncrement > 0) ScrollCondition = "y < ScrollEnd"
		if(ScrollStart > ScrollEnd && ScrollIncrement < 0) ScrollCondition = "y > ScrollEnd"
	}
	y += ScrollIncrement;
	ScrollObject.style.top = y;
	ScrollObject.style.width = "" + Math.abs((y * 100) / (ScrollStart - ScrollEnd)) + "%";
	ScrollObject.style.left = "" + Math.abs((y * 100) / (ScrollStart - ScrollEnd)) + "%";

	if(eval(ScrollCondition)){
		setTimeout("scrollitems()", 25);
	}
	else{
		ScrollObject.style.top = ScrollEnd;
		ScrollCondition = "0";
		ScrollObject = null;
	}
}
//external.addChannel("../channel/Color.Cdf");
//alert(navigator.userAgent);

function ShowProgress(count){

}


function ToggleProgress(stop, Delta){
var Pos = parseInt(document.all.barProgressInbox.style.left);

	if(!stop){
		Pos += Delta;
		if(Pos <= -100 || Pos >= 100){
			Delta *= (-1);
			document.all.barProgressInbox.filters.alpha.opacity = (Delta < 0) ? 100 : 20;
			document.all.barProgressInbox.filters.alpha.finishopacity = (Delta < 0) ? 20 : 100;
		}
		document.all.barProgressInbox.style.left = Pos + "px";
		setTimeout("ToggleProgress(" + stop + ", " + Delta + ");", 200);
	}
	else
		document.all.barProgressInbox.style.left = "-100px";
}

setTimeout("ToggleProgress(false, 5);", 500);
setTimeout("ToggleProgress(true, 5);", 30000);

