/* 
gtProxy.js gets nonsecure resources from Proxy.java for secure pages

IMAGE EXAMPLE:
	<script type="text/javascript"><!--
	proxyImage('image/gif','UTF-8','http://www.google.ca/intl/en_ca/images/logo.gif',0,0);
	--></script>

HTML EXAMPLE:
	<div id="gt_proxy_id1">
	<script type="text/javascript"><!--
	proxyHtml('gt_proxy_id1','text/html','UTF-8','http://www.wta.org/dropdown_nav.html');
	--></script>
	</div>
*/

function proxyImage(ctype,enc,rUrl,width,height)
{
	var tmp = new Date();
	var url = '/members/Proxy?ElementId=1&ContentType='+ctype+'&CharacterEncoding='+enc+'&URL='+rUrl+'&'+tmp.getTime();
	
	if (width != 0 && height != 0)
		document.write('<img src="'+url+'" width="'+width+'" height="'+height+'" border="0">');
	else if (width != 0)
		document.write('<img src="'+url+'" width="'+width+'" border="0">');
	else if (height != 0)
		document.write('<img src="'+url+'" height="'+height+'" border="0">');
	else
		document.write('<img src="'+url+'" border="0">');
		
}

function proxyHtml(id,ctype,enc,rUrl)
{	var tmp = new Date();
	var url = '/members/Proxy?ElementId='+id+'&ContentType='+ctype+'&CharacterEncoding='+enc+'&URL='+rUrl+'&'+tmp.getTime();
	makeRequest(url,"POST","Text")
}

function onProxyResponse(response)
{	var mySlit = response.split('=',1);
	//var id = mySlit[0];
	//var rest = response.substring(mySlit[0].length + 1);
	document.getElementById(mySlit[0]).innerHTML = response.substring(mySlit[0].length + 1);
}

//Include this here incase the gtRequest.js is being included in the page already

function makeRequest(url,method,resonseType)
{ 
var req = null; 
if (window.XMLHttpRequest)
{	req = new XMLHttpRequest();
	if (req.overrideMimeType) {
		req.overrideMimeType('text/xml');
	}
} 
else if (window.ActiveXObject) {
	try {	
		req = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) {	
		try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
}

req.onreadystatechange = function()
{ 	if(req.readyState == 4)
	{	if(req.status == 200)
		{	
			if (resonseType =="Text")
				onProxyResponse(req.responseText);
			else if (resonseType =="XML")
				onProxyResponse(req.responseXML);
			else
				onProxyResponse(req.responseText);
		}	
		else	
		{	alert("There was a problem retrieving the data:\n" + req.status + " " + req.statusText); 
			onProxyResponse("");
		}	
	} 
}; 

if (method == '')
	method = 'GET';

if (url.length >= 2048)
	method = 'POST';

if (method == 'GET') {	
	req.open(method, url, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	req.send(null); 
}
else if (method == 'POST'){	
	var uri = url.substring(url.indexOf('?') + 1);
	var url = url.substring(0,url.indexOf('?'));
	req.open(method, url, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); 
	req.send(uri);
}

} 
