function JSONscriptRequest(fullUrl) {
this.fullUrl = fullUrl; 
this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
this.headLoc = document.getElementsByTagName("head").item(0);
this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
this.scriptObj = document.createElement("script");
this.scriptObj.setAttribute("type", "text/javascript");
this.scriptObj.setAttribute("charset", "utf-8");
this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
this.scriptObj.setAttribute("id", this.scriptId);}
JSONscriptRequest.prototype.removeScriptTag = function () {
this.headLoc.removeChild(this.scriptObj); }
JSONscriptRequest.prototype.addScriptTag = function () {
this.headLoc.appendChild(this.scriptObj);}
function URLParser(url) {
this._fields = {'Username' : 4, 'Password' : 5, 'Port' : 7, 'Protocol' : 2, 'Host' : 6, 'Pathname' : 8, 'URL' : 0, 'Querystring' : 9, 'Fragment' : 10};
this._values = {};
this._regex = null;
this.version = 0.1;
this._regex = /^((\w+):\/\/)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#]+)?\??([^#]+)?#?(\w*)/;
for(var f in this._fields)
this['get' + f] = this._makeGetter(f);
if (typeof url != 'undefined')
this._parse(url); }
URLParser.prototype.setURL = function(url) {
this._parse(url);}
URLParser.prototype._initValues = function() {
for(var f in this._fields)
this._values[f] = '';}
URLParser.prototype._parse = function(url) {
this._initValues();
var r = this._regex.exec(url);
if (!r) throw "DPURLParser::_parse -> Invalid URL"
for(var f in this._fields) if (typeof r[this._fields[f]] != 'undefined')
this._values[f] = r[this._fields[f]];}
URLParser.prototype._makeGetter = function(field) {
return function() {
return this._values[field];}}
var Auth = {
setAttribute:function(name,value){
var metaTag = null;
if (this.getAttribute(name)!=null)
metaTag = document.getElementById(name);
else {
metaTag = document.createElement('meta');
metaTag.setAttribute('id',name);}
metaTag.setAttribute('content',value);
var headTags=document.getElementsByTagName('head');
if (headTags==null || headTags.length==0) alert('Your document must have a &lt;head&gt; tag');
headTags[0].appendChild(metaTag);},
getAttribute:function(name){
var ele = document.getElementById(name);
return ele!=null ? ele.getAttribute('content') : null;},
addToolSession:function(url,sessionId,sessionTimeout){
var host = this.getSessionHost(url);
this.setAttribute(host+'la',(new Date()).toString());
this.setAttribute(host+'sid',sessionId);
this.setAttribute(host+'to',sessionTimeout);},
getToolSessionId:function(url) {
var host = this.getSessionHost(url);
return this.getAttribute(host+'sid');},
getToolSessionTimeout:function(url) {
var host = this.getSessionHost(url);
return this.getAttribute(host+'to');},
getSessionHost:function(url) {
var loc = new URLParser(url);
return (loc.getProtocol()+':\/\/'+loc.getHost()+(loc.getPort()!=null && loc.getPort().length>0?':'+loc.getPort():''));},
isSessionAlive:function(url){
var result = false;
var host = this.getSessionHost(url);
var la = this.getAttribute(host+'la');
if (la!=null) {
var theTimeout=this.getToolSessionTimeout(host);
theTimeout=theTimeout*1000;
var toTime = (new Date(la)).getTime()+theTimeout;
var nowTime = (new Date()).getTime();
// this.writeMsg("toTime="+(new Date(toTime)).toString()+", nowTime="+(new Date(nowTime)).toString());
if (toTime > nowTime) result = true;}
return result;},
prepareUrl:function(url) {
var host = this.getSessionHost(url);
var sessionId = this.getToolSessionId(url);
var preparedUrl = url;
if (sessionId != null) {
if (!this.isSessionAlive(host)) {
this.newSession(host,false);
alert('Your session for this tool has ended. Attempts will be made to renew your session, however, you may need to reload this page.');}
var loc = new URLParser(url);
this.setAttribute(host+'la',(new Date()).toString());
var preparedUrl = (this.getSessionHost(url)+loc.getPathname()+';jsessionid='+sessionId+'?'+loc.getQuerystring());}
return preparedUrl;},
newSession:function(url,useReloadFunction) {
var host = this.getSessionHost(url);
//this.writeMsg('Loading new session for '+host);
var scriptTags=document.getElementsByTagName('script');
var newScriptSrc=null;
for(var i=0; i<scriptTags.length; i++){
var oldScriptTag=scriptTags[i];
if (oldScriptTag.src.indexOf(host)>-1){
if (oldScriptTag.src.indexOf('domainId')>-1) newScriptSrc=oldScriptTag.src;
//this.writeMsg('Removing script tag '+oldScriptTag.src);
oldScriptTag.parentNode.removeChild(oldScriptTag);}}
var newScriptTag = new JSONscriptRequest(newScriptSrc);
newScriptTag.buildScriptTag();
newScriptTag.addScriptTag();},
writeMsg:function(aStr){
var msgEle = document.getElementById('msgLog');
if (msgEle!=null) msgEle.innerHTML=msgEle.innerHTML+'<p>'+aStr+'</p>';}
}
var host = window.location.href;
host = host.substring(0,host.lastIndexOf('/'));
var request = 'http://gis.cdph.ca.gov/ws/GetTool;jsessionid=29DD689D7DE1E1DCCD6CB7DAC0841FE4?domain='+host+'&rk=359691817578354';
var aObj = new JSONscriptRequest(request);
aObj.buildScriptTag();
aObj.addScriptTag();

