//HTTP requests
HttpRequest.prototype.MS_PROGIDS=new Array(
"Msxml2.XMLHTTP.7.0",
"Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.5.0",
"Msxml2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP"
);
HttpRequest.prototype.READY_STATE_UNINITIALIZED=0;
HttpRequest.prototype.READY_STATE_LOADING=1;
HttpRequest.prototype.READY_STATE_LOADED=2;
HttpRequest.prototype.READY_STATE_INTERACTIVE=3;
HttpRequest.prototype.READY_STATE_COMPLETED=4;
HttpRequest.prototype.successCallback=null;
HttpRequest.prototype.failureCallback=null;
HttpRequest.prototype.url=null;
HttpRequest.prototype.username=null;
HttpRequest.prototype.password=null;
HttpRequest.prototype.requestHeaders=new Array();
HttpRequest.prototype.status=null;
HttpRequest.prototype.statusText=null;
HttpRequest.prototype.responseXML=null;
HttpRequest.prototype.responseText=null;
HttpRequest.prototype.abort=HttpRequestAbort;
HttpRequest.prototype.setRequestHeader=HttpRequestSetRequestHeader;
HttpRequest.prototype.clearRequestHeaders=HttpRequestClearRequestHeaders;
HttpRequest.prototype.get=HttpRequestGet;
HttpRequest.prototype.post=HttpRequestPost;
HttpRequest.prototype.initiateRequest=HttpRequestInitiateRequest;
HttpRequest.prototype.getResponseHeader=HttpRequestGetResponseHeader;
HttpRequest.prototype.getAllResponseHeaders=HttpRequestGetAllResponseHeaders;
HttpRequest.prototype.report=HttpRequestReport;
function HttpRequest()
{
this.xmlHttpRequest=null;
if(window.XMLHttpRequest!=null)
this.xmlHttpRequest=new window.XMLHttpRequest();
else if(window.ActiveXObject!=null)
{
var success=false;
for(var i=0;i<HttpRequest.prototype.MS_PROGIDS.length&&!success;i++)
{
try
{
this.xmlHttpRequest=new ActiveXObject(HttpRequest.prototype.MS_PROGIDS[i]);
success=true;
}
catch(ex)
{}
}
}
if(this.xmlHttpRequest==null)
{
alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");
return;
}
}
function HttpRequestReport(s)
{
alert(s);
}
function HttpRequestAbort()
{
this.xmlHttpRequest.abort();
}
function HttpRequestSetRequestHeader(name,value)
{
for(var i=0;i<this.requestHeaders.length;i++)
{
var pair=this.requestHeaders[i].split("\n");
if(pair[0].toLowerCase()==name.toLowerCase())
{
this.requestHeaders[i]=name+"\n"+value;
return;
}
}
this.requestHeaders.push(name+"\n"+value);
}
function HttpRequestClearRequestHeaders()
{
this.requestHeaders=new Array();
}
function HttpRequestGet()
{
this.initiateRequest("GET",null);
}
function HttpRequestPost(data)
{
this.initiateRequest("POST",data);
}
function HttpRequestGetResponseHeader(name)
{
return this.xmlHttpRequest.getResponseHeader(name);
}
function HttpRequestGetAllResponseHeaders()
{
return this.xmlHttpRequest.getAllResponseHeaders();
}
function HttpRequestInitiateRequest(method,data)
{
this.status=null;
this.statusText=null;
this.responseText=null;
this.responseXML=null;
var refObj=this;
this.xmlHttpRequest.onreadystatechange=
function()
{
refObj.readyState=refObj.xmlHttpRequest.readyState
if(refObj.readyState==HttpRequest.prototype.READY_STATE_COMPLETED)
{
refObj.status=refObj.xmlHttpRequest.status;
refObj.statusText=refObj.xmlHttpRequest.statusText;
refObj.responseText=refObj.xmlHttpRequest.responseText;
refObj.responseXML=refObj.xmlHttpRequest.responseXML;
if(refObj.status==200)
{
if(refObj.successCallback!=null)
refObj.successCallback(refObj);
}
else
{
if(refObj.failureCallback!=null)
refObj.failureCallback(refObj);
}
}
}
var url=this.url;
if(this.queryString!=null)
url=url+"?"+this.queryString;
this.xmlHttpRequest.open(method,url,true,this.username,this.password);
for(var i=0;i<this.requestHeaders.length;i++)
{
var pair=this.requestHeaders[i].split("\n");
this.xmlHttpRequest.setRequestHeader(pair[0],pair[1]);
}
try{
if(data){
this.xmlHttpRequest.setRequestHeader('Content-length',data.length);
}
this.xmlHttpRequest.send(data);
}
catch(e){
refObj.report("Cannot send xmlHttpRequest (no data) in "+refObj.url+"?"+refObj.queryString);
}
}
// client
var chr16 = String.fromCharCode(16);
var chr17 = String.fromCharCode(17);
var chr18 = String.fromCharCode(18);
var chr20 = String.fromCharCode(20);
var isIE = navigator.userAgent.toLowerCase().indexOf("msie") != -1;
var newWindow = null;
var runId = null;

function getObj(id){
	return document.getElementById(id);
};

function resetStyle(i){
	el = getObj(i);
	el.className = el.className.replace(/update/,"");
};

function handleUpdate(req){
	var res = req.responseText;
	if (res!=""){
		var lines = ids.split(",");
		// unpack data
		var rar = res.split(chr17);
		var vals = [];
		for (var i=0,lei=rar.length;i<lei;i++){
			if (rar[i]!=""){
				var dline = rar[i].split(chr16);
				if (!vals[dline[0]]) vals[dline[0]] = [];
				var data = dline[1].split(chr20);
				if (dline[1]){
					var data = dline[1].split(chr20);
					for (var y=0,ley=data.length;y<ley;y++){
						if (data[y] != ""){
							var fdata = data[y].split(chr18);
							vals[dline[0]][fdata[0]] = fdata[1];
						}
					}
				}
			}
		}
		for (var i=0,lei=lines.length;i<lei;i++){
			var s = lines[i].split("_");
			if (vals[s[0]]){
				for (y in vals[s[0]]){
					var el = getObj(lines[i] + y);
					if (el){
						if (y == "f14"){
							var src = flatImg;
							if (vals[s[0]][y]=="-1")src = downImg;
							if (vals[s[0]][y]=="1")src = upImg;
							el.innerHTML = "<img border=0 src='" + src + "'>";
						}
						else {
							var v = parseFloat(el.innerHTML);
							el.innerHTML = vals[s[0]][y];
							if (!isNaN(v)){
								var nv = parseFloat(vals[s[0]][y]);
								if (!isNaN(nv)){
									var c = "updatedown";
									if (v < nv) c = "updateup";
									el.className = el.className.split(' ')[0] + " " + c;
									setTimeout("resetStyle('" + (lines[i] + y) + "')",1000);
								}
							}
						}
					}
				}
			}
		}
		interval = minInterval;
	}
	else{
		interval = Math.min((2*interval),maxInterval);
	}
	startUpdate();
};


function handleNewsUpdate(req){
	document.getElementsByTagName('BODY')[0].innerHTML = req.responseText;
};


function requestUpdate(){
	if (priceUrl!=''){
		var rob = new HttpRequest();
		rob.url = priceUrl;
		rob.successCallback = handleUpdate;
		rob.get();
	}
	if (newsUrl!=''){
		var rob1 = new HttpRequest();
		rob1.url = newsUrl;
		rob1.successCallback = handleNewsUpdate;
		rob1.get();
	}
}
function stopUpdate(){
  if (runId)
    clearTimeout(runId);
    
  runId = null;
}
function startUpdate(){
	stopUpdate();
	setTimeout("requestUpdate()",interval);
};


function handleContext(e){
	var d = new Date();
	var y = d.getFullYear();
	alert("(c) GL Trade (Suisse) SA " + y);
	if (!e && isIE) e = event;
	if (e) {
		if (isIE) {
			e.cancelBubble = true;
			e.returnValue = false;
		}
		else{
			e.preventDefault();
			e.stopPropagation();
		}
	}
};
oncontextmenu=handleContext;
document.oncontextmenu=handleContext;
function needOpener(){
	for (var i=0;i<hasOpener.length;i++)
		if (location.href.toUpperCase().indexOf(hasOpener[i])!=-1 && 
			(!opener || opener.location.href.toLowerCase().indexOf(root)==-1))
			self.location.href = root + "error.asp?msg=" + escape("Unauthorized Access");
}
var clientLoaded = null;
onload = function () {needOpener();if(clientLoaded) clientLoaded();}

function closeNewWindow(){
	if (newWindow && newWindow.open && !newWindow.closed) newWindow.window.close();
};
function openChart(sym, name){
	closeNewWindow();
	newWindow = window.open(root + "chart.asp?s=" + escape(sym) + "&n=" + escape(name), "Chart", "toolbarnewWindow,location=no,directories=no,status=no,menubar=no,resizable=1,width=" + chartWidth +",height=" + chartHeight);
};
function openVista(sym, name){
	closeNewWindow();
	newWindow = window.open(root + "vista.asp?s=" + escape(sym) + "&n=" + escape(name), "Vista", "toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=1,width=" + vistaWidth +",height=" + vistaHeight);
};
function openStory(hl, k){
	closeNewWindow();
	newWindow = window.open(root + "newsstory.asp?hl=" + escape(hl) + "&k=" + escape(k), "NewsStory", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=yes,width=" + storyWidth+",height=" + storyHeight);
};


