/*
*Core utilities
*A number of convenience methods including methods to:
*display offsite message
*check whether a string is an internal/external website
*History
*Version		Date		By		Description
*-------------------------------------------------------------------------------------------------
*1.5			07/01/2005	ml		Added bustFrames() as a workaround for Peoplesoft frames
*1.4			09/20/2004	ml		Added initPage() and changes to browser detection for firefox.
*									Change to browserType.isMozillaOrigin()	so that it checks for Gecko to
*									give a more generic solution.
*1.3			06/17/2004	ml		BrowserType and associated functions/variables etc.
*1.2			06/02/2004	ml		getArgs()
*1.1			03/04/2004	ml		Absolute link of offsite.html to allow use across servers
*1.0			02/23/2004	ml		New
*/

/**
* Dummy function to allow all templates to implement their own initABQ to listen
* for document.onload events without disrupting main.dwt
* @return void
*/
function initPage(){
}

/**
* Boolean convenience method to check whether the site is a City "owned" site.
* A URL is considered internal if it does not start with http:// or it is one of the reserved
* websites www, w3, mesa, newmesa, eweb or albuq (all internal) or www.infax.com
* Implemented using 2 regexps to simplify regexp and ensure compatibility with earlier JS versions
* @param theSite - the site to check
* @returntrue if is city owned else false
*/
function isCABQSite(theSite){
	var retValue = true;

	if(theSite.search(/^http:\/\//) != -1){
		retValue = theSite.search(/(w(ww|3)|cogpub|mesa|newmesa|eweb|albuq)\.cabq\.gov|www\.infax\.com/) != -1;
	}
	
	return(retValue);
}

/**
* Convenience method.  Assumimg that <code>listName</code> refers to an HTML list box,
* go to the URL pointed to by the selected option's <code>value</code> attribute
* If the site is determined as offsite by a call to <code>isCABQSite()</code> then
* the offsite message is automatcially displayed
* @param listName - HTML <select> element to use
* @param inNewWindow - true to open a new window, false if the document should be kept in
* the existing window
* @return void
*/
function listGoToURL(listName, inNewWindow){
	var target = null;
	var selIndex = listName.selectedIndex;
	
	if(selIndex > -1)
		target = listName.options[selIndex].value;
	
	if(target != null)
		goToURL(target, inNewWindow);
}

/**
* Go to the URL pointed to by <code>target</code>
* If the site is determined as offsite by a call to <code>isCABQSite()</code> then
* the offsite message is automatcially displayed
* @param target - target to open
* @param inNewWindow - true to open a new window, false if the document should be kept in
* the existing window
* @return void
*/
function goToURL(target, inNewWindow){
	var currWindow = null;
	
	if(isCABQSite(target) == false)
		showOffSiteMessage();

	if(inNewWindow == true)
		open(target);
	else
		location = target;
}

/**
* Display the offsite message to be used when leaving a CABQ page
* @return void
*/
function showOffSiteMessage(){
	open("http://www.cabq.gov:80/offsite.html", "offsite", "toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=400,height=280,top=0,left=0");
}

/**
*  Retrieve arguments as a name/value pairing from the current window.location.
*  Based on Flannagan D., 2002, JavaScript - the definitive guide , o'Reilly publishing p 274
* @return Object containing arg names as properties and arg values as the property values.
*/
function getArgs(){
	var argList = new Object();
	var queryStr = location.search.substring(1);	//Move beyond '?'
	var argPairs = queryStr.split("&");
	var counter = -1;
	var idx = -1;
	var name = null;
	var value = null;

	for(counter = 0; counter < argPairs.length; counter++){
		
		if((idx = argPairs[counter].indexOf('=')) != -1){
			name = argPairs[counter].substring(0, idx);
			value = argPairs[counter].substring(idx+1);
			argList[name] = unescape(value);
		}
	}
	
	return(argList);
}

/**
* Browser object containing interesting facts about the browser that's
* calling us.
*/
function BrowserType(){
	this.name = null;
	this.version = null;
	this.os = null;
}

/**
* Global variable pointing to BrowserType object
*/
var browser = new BrowserType();

/**
* Prototype for BrowserType to check whether browser is IE compatible
* @return true if IE compatible (i.e. name == Microsoft), else false
*/
BrowserType.prototype.isIECompatible = function(){
	return(this.name == "Microsoft");
};

/**
* Prototype for BrowserType to check whether browser really is Netscape
* @return true if Netscape compatible (i.e. name == Netscape), else false
*/
BrowserType.prototype.isNetscapeCompatible = function(){
	return(this.name == "Netscape");
};

/**
* Prototype for BrowserType to check whether browser is based on Mozilla code base
* (truly!)
* @return true if Netscape, Opera or Safari
*/
BrowserType.prototype.isMozillaOrigin = function(){
	return(this.name == "Netscape" || navigator.userAgent.indexOf("Gecko") != -1);
};

/**
* Prototype for BrowserType to check whether Netscape browser is < 6.0.
* @return true if < 6.0 && Netscape, else false
*/
BrowserType.prototype.isOldNetscape = function(){
	return(this.name == "Netscape" && parseFloat(this.version) < 6);
};

/**
* Helper method to determine browser type, version, platform and os
* Note that this does not return sub-divisions of OS (e.g. NT/XP etc) as
* this should be meaningless for the sort of code that we write.
* @param record - BrowserType to set
* @return void
*/
function determineBrowser(record){
	var tempStr = navigator.appName.toLowerCase();
	
	if(tempStr != null && tempStr != ""){
		
		if(tempStr.indexOf("netscape") != -1)
			record.name = "Netscape";
		else
			if(tempStr.indexOf("microsoft") != -1)
				record.name = "Microsoft";
			else
					if(tempStr.indexOf("mozilla") != -1)
						record.name = "Mozilla";
					else
						if(tempStr.indexOf("safari") != -1)
							record.name = "Safari";
						else
							record.name = tempStr;
	}
	tempStr = navigator.userAgent.toLowerCase();
			
	if(tempStr.indexOf("opera") != -1)
		record.name = "Opera";
	else
		if(tempStr.indexOf("firefox") != -1)
			record.name = "Firefox";
	
	if(record.isNetscapeCompatible() == true){
		tempStr = tempStr.substring(tempStr.indexOf("netscape"));
		tempStr = tempStr.match(/[0-9]+\.*[0-9]*/);
		record.version = tempStr[0];
	}
	else{
		if(record.isIECompatible() == true){
			tempStr = tempStr.substring(tempStr.indexOf("msie"));
			tempStr = tempStr.match(/[0-9]+\.*[0-9]*/);
			record.version = tempStr[0];
		}
		else
			if(record.name == "Opera"){
				tempStr = tempStr.substring(tempStr.indexOf("opera "));
				tempStr = tempStr.match(/[0-9]+\.*[0-9]*/);
				record.version = tempStr[0];
			}
			else
				if(record.name == "Firefox"){
					tempStr = tempStr.substring(tempStr.indexOf("firefox"));
					tempStr = tempStr.match(/[0-9]+\.*[0-9]*/);
					record.version = tempStr[0];
				}
	}
	tempStr = navigator.userAgent;
	
	if(tempStr != null && tempStr != ""){
		tempStr = tempStr.toLowerCase();
		
		if(tempStr.indexOf("win") != -1)
			record.os= "Windows";
		else
			if(tempStr.indexOf("mac") != -1)
				record.os = "Macintosh";
			else
				if(tempStr.indexOf("x11") != -1)
					record.os = "Unix";
				else
					record.os = tempStr;
	}
	
}
//Set the browser type with current info
determineBrowser(browser);

/*
* Convenience method to redisplay this page at the top most level of a frameset.
* Intended usage: as high up in the HTML page to be busted as possible.  The page will be
* downloaded as part of the frameset and this method will then set the document location
* of the top level frame to the document being downloaded.
* @return void
*/
function bustFrames(){
	var args = getArgs();
	var prop = null;
	var url = "";

	if(self != top){

		for(prop in args){
	
			if(url.length == 0)
				url += '?';
			else
				url += '&';
		
			url += prop + '=' + escape(eval("args." + prop));
		}
		url = "http://" + location.host + location.pathname + url;
		top.location = url;
	}
}