﻿/* release-65 20090318 05:53:07 */
WvlsSession.prototype.openSession = _wvls_open_session;
WvlsSession.prototype.closeSession = _wvls_close_session;
WvlsSession.prototype.getImage = _wvls_get_image;
WvlsSession.prototype.getPanoImage = _wvls_get_pano;
WvlsSession.prototype.sendRequest = _wvls_send_request;
WvlsSession.prototype.sendRequestForReply = _wvls_send_request_for_reply;
WvlsSession.prototype.sendRequestSync = _wvls_send_request_sync;
WvlsSession.prototype.sendRequestForReplySync = _wvls_send_request_for_reply_sync;
WvlsSession.prototype.getStatus = _wvls_get_status;
WvlsSession.prototype._recv_session = _wvls_recv_session;
WvlsSession.prototype._recv_notice = _wvls_recv_notice;
WvlsSession.prototype._recv_request = _wvls_recv_request;
function WvlsSession(session_callback, notice_callback, request_callback) {
	this.host = "";
	this.session_cb = session_callback;
	this.notice_cb = notice_callback;
	this.request_cb = request_callback;
	this.url_head = "/-wvhttp-01-/";
	this.connection_id_old = "";
	this.connection_id_new = "";
	this.camera_id = "";
	this.seqNum = 0;
	this.httpObj = null;
	this.syncObj = null;
	this.noticeObj = null;
	this.status = "close";
	this.timer;
}
function _wvls_get_status() {
	return this.status;
}
function _wvls_open_session(host) {
	this.host = host;
	try {
		if (timeoutSec == null || isNaN(timeoutSec) || timeoutSec < 0) {
			timeoutSecP = 30;
		} else {
			timeoutSecP = Math.max(Math.min(timeoutSec, timeoutSecMax),timeoutSecMin);
		}
	} catch(e) {
		timeoutSecP = 30;
	}
	if (this.host == "") {
		this.url_head = "/-wvhttp-01-/";
	} else {
		this.url_head = "http://" + host + "/-wvhttp-01-/";
	}
	
	if (browserType == "IE" || browserType == "Firefox") {
		var c = document.cookie + ";";
		if (c.indexOf("ConnectionOpened") != -1) {
			show_msg_file("conflict","error");
			return;
		}
		if (browserVersion == "7" && viewerLoadMin != "0") {
			var start = c.indexOf("ViewerLoad=");
			if (start == -1) {
				document.cookie = "ViewerLoad=" + new Date().getTime() + ";path=" + viewerPath;
			} else {
				var end = c.indexOf(";", start);
				var time = eval(c.substring(start + 11, end));
				var now = new Date().getTime();
				if (time + eval(viewerLoadMin * 60000) < now) {
					show_msg_file("viewerload","error");
					return;
				}
			}
		}
		document.cookie = "TryConnection;path=" + viewerPath;
		c = document.cookie;
		if (c.indexOf("TryConnection") == -1) {
			show_msg_file("cookiedeny","error");
			document.cookie = " ;path=" + viewerPath;
			return;
		}
	}
	
	var url = this.url_head + "open.cgi?seq=" + Math.random();
	if (defaultReceiveImageSize != null && (defaultReceiveImageSize == "160x120" || defaultReceiveImageSize == "320x240" || defaultReceiveImageSize == "640x480")) {
		url = url + "&v=jpg:"+ defaultReceiveImageSize;
	} 
	if (viewerMode == "Admin") {
		url = url + "&type=admin";
	} else if (viewerMode == "Operator") {
		url = url + "&priority=40";
	} else if (viewerMode == "VB") {
		url = url + "&priority=0";
	}
	this.status = "connecting";
	if (this.httpObj != null) {
		this.httpObj.abort();
		delete this.httpObj;
	}
	this.httpObj = _wvls_createXMLHttpRequest();
	var myself = this;
	if (browserType != "Safari") {
		if (browserType == "IE" && viewerMode == "Admin") {
			this.httpObj.open("GET", url, false, adminuser, adminpass);
		} else if (browserType == "IE" && viewerMode == "Operator") {
			this.httpObj.open("GET", url, false, adminuser, adminpass);
		} else {
			this.httpObj.open("GET", url, false);
		}
	} else {
		this.httpObj.onreadystatechange = function() { myself._recv_session(); };
		this.httpObj.open("GET", url, true);
	}
	try {
		this.httpObj.send("");
	} catch (e) {
	}
	return true;
}
function _wvls_recv_session() {
	var owner = this;
	if (owner.status != "connecting") {
		return;
	}
	if ((owner.httpObj.readyState == 4) && ((owner.httpObj.status == 200)||(owner.httpObj.status == undefined))) {
		owner.httpObj.onreadystatechange = function() {};
		var resText = owner.httpObj.responseText.split('\r').join('').split('\n');
		var line;
		var s = "";
		for (i = 0; i < resText.length; i++) {
			line = resText[i].split(':=');
			if (line.length >= 2 && line[0] == "s") {
				s = line[1];
			} else if(line.length >= 2 && line[0] == "s.origin") {
				origin = line[1];
				if (origin.indexOf('[') == -1 && origin.indexOf(']') == -1) {
					var originarray = origin.split(':');
					originHost = originarray[0];
					originPort = originarray[1];
					if (originPort == null || originPort == "") {
						originPort = "80";
					}
				} else {
					originHost = "";
					originPort = "";
				}
			}
		}
		var statusMes;
		if (s == "") {
			owner.connection_id_old = "";
			owner.connection_id_new = "";
			owner.status = "close";
			statusMes = "Livescope-" + getLivescopeStatus(owner.httpObj.getResponseHeader("Livescope-Status"));
		} else {
			owner.connection_id_old = "connection_id=" + s;
			owner.connection_id_new = "s=" + s;
			owner.status = "open";
			if (browserType == "IE" || browserType == "Firefox") {
				document.cookie = "ConnectionOpened;path=" + viewerPath;
			}
		}
		if (owner.session_cb != null) {
			owner.session_cb(owner, statusMes);
		}
		if (owner.noticeObj != null) {
			owner.noticeObj.abort();
			delete owner.noticeObj;
		}
		if (owner.connection_id_old != "" && owner.notice_cb != null) {
			var url = owner.url_head + "info.cgi" + "?timeout=" + infoTimeoutSec + "&" + owner.connection_id_new + "&seq=" + owner.seqNum;
			owner.noticeObj = _wvls_createXMLHttpRequest();
			this.timer = setTimeout("connectTimeout('info.cgi')", (timeoutSecP + infoTimeoutSec) * 1000);
			owner.noticeObj.onreadystatechange = function() {owner._recv_notice()};
			owner.noticeObj.open("GET", url, true);
			if (browserType == "Firefox") {
				owner.noticeObj.setRequestHeader("Connection", "close");
			}
			try {
				owner.noticeObj.send("");
			} catch (e) {
			}
		}
	} else if ((owner.httpObj.readyState == 4) && (owner.httpObj.status != 200)) {
		owner.connection_id_old = "";
		owner.connection_id_new = "";
		owner.status = "close";
		if (owner.session_cb != null) {
			owner.session_cb(owner, "HTTP-" + owner.httpObj.status);
		}
	}
}
function _wvls_close_session() {
	clearTimeout(this.timer);
	if (this.status == "close") {
		return true;
	}
	if (this.status != "close" && this.connection_id_old == "") {
		if (this.httpObj != null) {
			this.httpObj.abort();
			delete this.httpObj;
		}
		if (this.noticeObj != null) {
			this.noticeObj.abort();
			delete this.noticeObj;
		}
		if (this.syncObj != null) {
			this.syncObj.abort();
			delete this.syncObj;
		}
		this.status = "close";
		return true;
	}
	this.status = "close";
	var url = this.url_head + "close.cgi?" + this.connection_id_new + "&seq=" + Math.random();
	this.httpObj.open("GET", url, false);
	try {
		this.httpObj.send("");
	} catch (e) {
	}
	if (this.httpObj != null) {
		this.httpObj.abort();
		delete this.httpObj;
	}
	if (this.noticeObj != null) {
		this.noticeObj.abort();
		delete this.noticeObj;
	}
	if (this.syncObj != null) {
		this.syncObj.abort();
		delete this.syncObj;
	}
	this.connection_id_old = "";
	this.connection_id_new = "";
	this.seqNum = 0;
	if (browserType == "IE" || browserType == "Firefox") {
		document.cookie = " ;path=" + viewerPath;
	}
	return true;
}
function _wvls_get_image() {
	if (this.connection_id_old == "") {
		return "";
	}
	var url = this.url_head + "GetLiveImage?" + this.connection_id_old + "&seq=" + Math.random();
	return url;
}
function _wvls_get_pano() {
	if (this.connection_id_old == "") {
		return "";
	}
	var url = this.url_head + "GetPanoramaImage?" + this.connection_id_old + "&seq=" + Math.random();
	return url;
}
function _wvls_recv_notice() {
	var owner = this;
	if (owner.connection_id_old == "") {
		return;
	}
	if (owner.noticeObj.readyState == 4) {
		if (browserVersion == "7" && viewerLoadMin != "0") {
			var c = document.cookie + ";";
			var start = c.indexOf("ViewerLoad=");
			if (start == -1) {
				document.cookie = "ViewerLoad=" + new Date().getTime() + ";path=" + viewerPath;
			} else {
				var end = c.indexOf(";", start);
				var time = eval(c.substring(start + 11, end));
				var now = new Date().getTime();
				if (time + eval(viewerLoadMin * 60000) < now) {
					do_disconnect("overLoadMin");
					show_msg_file("viewerload","error");
					return;
				}
			}
		}
		clearTimeout(this.timer);
		if ((owner.noticeObj.status == 200)||(owner.noticeObj.status == undefined)) {
			var statusMsg;
			var body = owner.noticeObj.responseText;
			try {
				statusMsg = owner.noticeObj.getResponseHeader("Livescope-Status");
			} catch(e) {
			}
			var noticeNum;
			noticeNum = _wvls_notice_from_body(body);
			if (statusMsg == undefined) {
				statusMsg = _wvls_status_from_body(body);
			}
			if (noticeNum == 31) {
				owner.status = "camera_control_ok";
			} else if (noticeNum == 32 || noticeNum == 132 || noticeNum == 34) {
				if (owner.status == "camera_control_ok") {
					owner.status = "open";
				}
			}
			if (owner.notice_cb != null) {
				var lines;
				var line;
				var items = new Array;
				lines = body.split('\r').join('').split('\n');
				for (var i = 0; i < lines.length; i++) {
					var index1 = lines[i].indexOf(':=');
					var index2 = lines[i].indexOf('==');
					if (index2 == -1 || index1 < index2) {
						line = lines[i].split(':=');
						if (line.length >= 2) {
							items[line[0]] = line[1];
							for (j=2; j<line.length; j++) {
								items[line[0]] = items[line[0]] + ":=" + line[j];
							}
							continue;
						}
					}
					line = lines[i].split('==');
					if (line.length >= 2) {
						items[line[0]] = line[1];
						if (line.length >= 2) {
							items[line[0]] = line[1];
							for (j=2; j<line.length; j++) {
								items[line[0]] = items[line[0]] + "==" + line[j];
							}
							continue;
						}
						continue;
					}
					line = lines[i].split('=');
					if (line.length >= 2) {
						items[line[0]] = line[1];
						continue;
					}
				}
				owner.notice_cb(owner, noticeNum, items);
			}
			if ((noticeNum == 23) || (statusMsg.indexOf("501") >= 0)) {
				owner.status = "close";
				owner.connection_id_old = "";
				owner.connection_id_new = "";
				if (browserType == "IE" || browserType == "Firefox") {
					document.cookie = " ;path=" + viewerPath;
				}
				if (owner.session_cb != null) {
					owner.session_cb(owner);
				}
			} else {
				owner.seqNum = owner.seqNum +1;
				if (owner.status != "close") {
					var url = owner.url_head + "info.cgi" + "?timeout=" + infoTimeoutSec + "&" + owner.connection_id_new + "&seq=" + owner.seqNum;
					this.timer = setTimeout("connectTimeout('info.cgi')", (timeoutSecP + infoTimeoutSec) * 1000);
					if (browserType == "IE") {
						owner.noticeObj.open("GET", url, true);
						owner.noticeObj.onreadystatechange = function() {owner._recv_notice()};
					} else {
						owner.noticeObj.onreadystatechange = function() {owner._recv_notice()};
						owner.noticeObj.open("GET", url, true);
					}
					if (browserType == "Firefox") {
						owner.noticeObj.setRequestHeader("Connection", "close");
					}
					try {
						owner.noticeObj.send("");
					} catch (e) {
					}
				}
			}
		}	
	} else if((owner.httpObj.readyState == 4) && (owner.httpObj.status != 200)) {
	}
}
function _wvls_recv_request(reqObj, req, prot, resp, to) {
	var owner = this;
	if (owner.connection_id_old == "") {
		if (resp) {
			owner.request_cb(req, prot, ["Livescope-501", null]);
		} else {
			owner.request_cb(req, prot, ["Livescope-501"]);
		}
		delete reqObj;
		return;
	}
	if (reqObj.readyState == 4) {
		clearTimeout(to);
		var httpStatus = reqObj.status;
		if (httpStatus != 200) {
			if (resp) {
				owner.request_cb(req, prot, ["HTTP-" + httpStatus, null]);
			} else {
				owner.request_cb(req, prot, ["HTTP-" + httpStatus]);
			}
			delete reqObj;
			return;
		}
		var statusMsg = reqObj.getResponseHeader("Livescope-Status");
		if(statusMsg != "0") {
			if (resp) {
				owner.request_cb(req, prot, ["Livescope-" + getLivescopeStatus(statusMsg), null]);
			} else {
				owner.request_cb(req, prot, ["Livescope-" + getLivescopeStatus(statusMsg)]);
			}
			delete reqObj;
			return;
		}
		var body = reqObj.responseText;
		if (resp) {
			owner.request_cb(req, prot, [null,body.split('\r').join('').split('\n')]);
		} else {
			owner.request_cb(req, prot, [null]);
		}
		delete reqObj;
	}
}
function _wvls_notice_from_body(body) {
	var noticeNum;
	if (body.indexOf("s.control==enabled") >= 0 || body.indexOf("s.control:=enabled") >= 0) {
		noticeNum = 31;
	} else if (body.indexOf("s.control:=disabled") >= 0) {
		noticeNum = 32;
	} else if (body.indexOf("s.control==disabled") >= 0) {
		noticeNum = 132;
	} else if (body.indexOf("s.control==waiting") >= 0 || body.indexOf("s.control:=waiting") >= 0) {
		noticeNum = 33;
	} else if (body.indexOf("c.1.pan==") >= 0 || body.indexOf("c.1.pan:=") >= 0 || body.indexOf("c.1.tilt==") >= 0 || body.indexOf("c.1.tilt:=") >= 0
	|| body.indexOf("c.1.zoom==") >= 0 || body.indexOf("c.1.zoom:=") >= 0 || body.indexOf("c.1.ae.brightness==") >= 0 || body.indexOf("c.1.ae.brightness:=") >= 0) {
		noticeNum = 41;
	} else {
		noticeNum = 0;
	}
	return noticeNum;
}
function _wvls_status_from_body(body) {
	if (body.indexOf("Unknown Connection ID") >= 0) {
		return "501";
	}
	return "";
}
function _wvls_send_request(command, params, protocol) {
	if (this.connection_id_old == "") {
		return "Livescope-501";
	}
	var url = "";
	var owner = this;
	if (params == "") {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&seq=" + Math.random();
		}
	} else {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&" + params + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&" + params + "&seq=" + Math.random();
		}
	}
	var reqObj = _wvls_createXMLHttpRequest();
	var to = setTimeout("connectTimeout('" + command + "')", timeoutSecP * 1000);
	if (browserType == "IE") {
		reqObj.open("GET", url, true);
		reqObj.onreadystatechange = function() {owner._recv_request(reqObj, command, protocol, false, to)};
	} else {
		reqObj.onreadystatechange = function() {owner._recv_request(reqObj, command, protocol, false, to)};
		reqObj.open("GET", url, true);
	}
	try {
		reqObj.send("");
	} catch (e) {
	}
	return null;
}
function _wvls_send_request_for_reply(command, params, protocol) {
	if (this.connection_id_old == "") {
		return "Livescope-501";
	}
	var url = "";
	var owner = this;
	if (params == "") {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&seq=" + Math.random();
		}
	} else {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&" + params + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&" + params + "&seq=" + Math.random();
		}
	}
	var reqObj = _wvls_createXMLHttpRequest();
	var to = setTimeout("connectTimeout('" + command + "')", timeoutSecP * 1000);
	if (browserType == "IE") {
		reqObj.open("GET", url, true);
		reqObj.onreadystatechange = function() {owner._recv_request(reqObj, command, protocol, true, to)};
	} else {
		reqObj.onreadystatechange = function() {owner._recv_request(reqObj, command, protocol, true, to)};
		reqObj.open("GET", url, true);
	}
	try {
		reqObj.send("");
	} catch (e) {
	}
	return null;
}
function connectTimeout(a) {
	if (browserType == "IE" || browserType == "Firefox") {
		document.cookie = " ;path=" + viewerPath;
	}
	do_disconnect("Timeout-" + a);
}
function _wvls_send_request_sync(command, params, protocol) {
	if (this.connection_id_old == "") {
		return "Livescope-501";
	}
	var url = "";
	if (params == "") {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&seq=" + Math.random();
		}
	} else {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&" + params + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&" + params + "&seq=" + Math.random();
		}
	}
	if(this.syncObj == null) {
		this.syncObj = _wvls_createXMLHttpRequest();
	}
	var to = setTimeout("connectTimeout('" + command + "')", timeoutSecP * 1000);
	this.syncObj.open("GET", url, false);
	try {
		this.syncObj.send("");
	} catch (e) {
	}
	clearTimeout(to);
	try {
		var httpStatus = this.syncObj.status;
	} catch (e) {
	}
	if (httpStatus != 200) {
		return "HTTP-" + httpStatus;
	}
	var statusMsg = this.syncObj.getResponseHeader("Livescope-Status");
	if(statusMsg != "0") {
		return "Livescope-" + getLivescopeStatus(statusMsg);
	}
	return null;
}
function _wvls_send_request_for_reply_sync(command, params, protocol) {
	if (this.connection_id_old == "") {
		return ["Livescope-501", null];
	}
	var url = "";
	if (params == "") {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&seq=" + Math.random();
		}
	} else {
		if (protocol == "old") {
			url = this.url_head + command + "?" + this.connection_id_old + "&" + params + "&seq=" + Math.random();
		} else {
			url = this.url_head + command + "?" + this.connection_id_new + "&" + params + "&seq=" + Math.random();
		}
	}
	if(this.syncObj == null) {
		this.syncObj = _wvls_createXMLHttpRequest();
	}
	var to = setTimeout("connectTimeout('" + command + "')", timeoutSecP * 1000);
	this.syncObj.open("GET", url, false);
	try {
		this.syncObj.send("");
	} catch (e) {
	}
	clearTimeout(to);
	try {
		var httpStatus = this.syncObj.status;
	} catch (e) {
	}
	if (httpStatus != 200) {
		return ["HTTP-" + httpStatus, null];
	}
	var statusMsg = this.syncObj.getResponseHeader("Livescope-Status");
	if(statusMsg != "0") {
		return ["Livescope-" + getLivescopeStatus(statusMsg), null];
	}
	var lines = this.syncObj.responseText.split('\r').join('').split('\n');
	return [null, lines];
}
function _wvls_createXMLHttpRequest() {
	var xhObj = null;
	try{
		xhObj = new XMLHttpRequest();
	} catch(e){
		try {
			xhObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e){
			try{
				xhObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				return null;
			}
		}
	}
	return xhObj;
}
function getLivescopeStatus(statusMsg) {
	var end = statusMsg.indexOf(" ");
	if (end < 1) {
		return statusMsg;
	}
	var statusCode = statusMsg.substring(0, end);
	return statusCode;
}
