 /**
 * Global browser detect class.
 *
 * @author David Thunman
 *
 * @description Example usage:
 *
 * var utils = new Utils();
 * var browser = utils.getBrowser();
 * var platform = utils.getPlatform();
 * var version = utils.getVersion();
 * var win = utils.popup(string, integer, integer [, string [, integer [, integer]]]);
 *
 */ 

/* 
* Contructor
*
* @author David Thunman
* @returns				nothing
*/
function Utils()
{
	this.sid = this.getRandomNumber();
	this.detect();
	this.win = null;
}

/* 
* Browser detect
*
* @author David Thunman
* @returns				nothing
*/
Utils.prototype.detect = function()
{
	this.browser = "";
	this.platform = "";
	this.version = 0;
	
	if(this.check('safari') > 0) this.browser =  "safari";
	else if(this.check('camino') > 0) this.browser =  "camino";
	else if(this.check('opera') > 0) this.browser =  "opera";
	else if(this.check('msie')) this.browser = "explorer";
	else if(this.check('mozilla')) this.browser = "mozilla";
	
	if(this.check('linux')) this.platform =  "linux";
	else if(this.check('mac')) this.platform = "mac";
	else if(this.check('windows')) this.platform = "windows";
	
	if(this.browser == "explorer")
	{
		var n = navigator.userAgent.indexOf('MSIE ') + 4;
		var m = navigator.userAgent.lastIndexOf(';');
		this.version = parseFloat(navigator.userAgent.substring(n, m));
	}
	else if(this.browser == "mozilla")
	{
		var n = navigator.userAgent.indexOf('rv:') + 3;
		var m = navigator.userAgent.lastIndexOf(')');
		this.version = parseFloat(navigator.userAgent.substring(n, m));
	}
	else
	{
		var n = navigator.userAgent.lastIndexOf('/') + 1;
		this.version = parseFloat(navigator.userAgent.substring(n));
	}
}

/* 
* Generate random number
*
* @author David Thunman
* @returns				nothing
*/
Utils.prototype.getRandomNumber = function()
{
	var day = new Date();
	var id = day.getTime();
	
	return id;
}

/**
*  window popup
*
* @author David Thunman
*
* @description popup(string, integer, integer [, string [, integer [, integer]]])
*
* @params	url		string		url of page to load
*			w		integer		width of window
*			h		integer		height of window
*			f		string		feature(s) parameter
*			t		integer		top position of window
*			l		integer		left position of window
*
* @returns window object reference
*/ 
Utils.prototype.popup = function(url, w, h, f, t, l){
	if(this.getBrowser() == "safari")
	{
		w = w - 2;
		h = h - 1;
	}
	else if(this.getBrowser() == "opera")
	{
		h = h + 22;
	}
	
	
	var id = this.getRandomNumber();
	var name = "popup_" + id;
	var features = "width="+w+",height="+h;
	
	/* try to set left and sceenX */
	try
	{
		if(l == null)
		{
			l = Math.round((screen.availWidth-w)/2);
			l = l < 0 ? 0 : l;
			features += ",left="+l+",screenX="+l;
		}
	}
	catch(e)
	{
		;
	}
	
	/* try to set top and sceenY */
	try
	{
		if(t == null)
		{
			t = Math.round((screen.availHeight-h-30)/2);
			t = t < 0 ? 0 : t;
			features += ",top="+t+",screenY="+t;
		}
	}
	catch (e)
	{
		;
	}

	/* try to set aditional features */
	try
	{
		if(f != null)
		{
			features += ","+f;
		}
	}
	catch(e)
	{
		;
	}
	
	var win = window.open(url, name, features);
	
	/* try to position window */
	try
	{
		win.moveTo(l, t);
	}
	catch(e)
	{
		;
	}
	
	/* try to focus window */
	try
	{
		win.focus();
	}
	catch(e)
	{
		;
	}
	
	/* Opera and Mac explorer resize fix */
	if(this.getBrowser() == "opera" || (this.getPlatform() == "mac" && this.getBrowser() == "explorer")){
		try
		{
			win.resizeTo(w,h);
		}
		catch(e)
		{
			;
		}
	}

	return win;
}

/* 
* Wrapper for handy window resizing 
*
* @author David Thunman
* @returns				nothing
*/
Utils.prototype.resizeWin = function(win, x, y, w, h)
{
	try
	{
		x = x == null ? 0 : x;
		y = y == null ? 0 : y;
		w = w == null ? screen.availWidth : w;
		h = h == null ? screen.availHeight : h;
		win.moveTo(x,y);
		win.resizeTo(w,h);
	}
	catch (e)
	{
		;
	}
}


/* 
* Wrapper for handy main site popup calls with single windows 
*
* @author David Thunman
* @returns				nothing
*/
Utils.prototype.openMain = function(url, w, h)
{
	try{
		if(this.win != null && !this.win.closed)
		{				
				this.win.focus();
		}
		else
		{
				this.win = this.popup(url, w, h,'resizable=no,scrollbars=no');
		}
	}
	catch(e)
	{
			;
	}
	finally
	{
		if(this.win == null)
		{
				this.win = this.popup(url, w, h,'resizable=no,scrollbars=no');
		}
	}	
}


/* 
* Handy way to confirm a form submission
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.confirmForm = function(msg)
{
	try
	{
		if(msg == null) msg = 'Are you sure?';
		
		if(confirm(msg))
		{
			this.disableButtons();
			return true;
		}
		
		return false;

	}
	catch(e)
	{
		;
	}
}


/* 
* Handy way to change all forms to disable submit button after submit in a document 
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.disableButtons = function()
{
	try
	{
		for(var i=0; i<document.forms.length; i++)
		{
			var f = document.forms[i];
			
			for(var j=0; j<f.length; j++)
			{
				var element = f[j];
				
				if(element.type.toLowerCase() == "submit" || element.type.toLowerCase() == "button")
				{
					element.disabled = true;
				}
			}
		}
	}
	catch(e)
	{
		;
	}
}

/* 
* Handy way to change all link targets in a document 
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.updateLinkTargets = function(frame)
{
	try
	{
		if(frame == null)
		{
			frame = "_blank";
		}
		for(var i=0; i<document.links.length; i++)
		{
			var href = document.links[i].href;
			var a = href.lastIndexOf('/');
			var b = href.lastIndexOf('=');
			var str = href.substring(a+1, b);
			
			if(str != 'download.php?pageFileID')
			{
				document.links[i].target = frame;
			}
		}
	}
	catch(e)
	{
		;
	}
}


/* 
* Handy way to print an object tag and maintain strict type
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.embedFlash = function(src, w, h)
{
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" id="bjbanner" align="middle">');
	document.write('<param name="movie" value="'+src+'" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="#FFFFFF" />');
	document.write('<embed src="'+src+'" quality="high" bgcolor="#FFFFFF" width="'+w+'" height="'+h+'" name="bjbanner" type="application/x-shockwave-flash"  pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

/* 
* Handy way to apply a new class to an object
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.setHighlight = function(name, color)
{
	try
	{
		n = this.getObj(name);
		n.style.color = color;
	}
	catch(e)
	{
		;
	}
}


				
/* 
* Handy way to swap images
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.swapImage = function(name, img)
{
	try
	{
		n = this.getObj(name);
		n.src = img.src;
	}
	catch(e)
	{
		;
	}
}

/* 
* Handy way to get an object
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.getObj = function(name)
{
	
	try
	{
		if (document.getElementById) 
		{
    		return document.getElementById(name);
    	}
  		else if (document.all) 
  		{
    		return document.all[name];
    	}
  		else if (document.layers) 
  		{
    		return document.layers[name];
    	}
  		else 
  		{
  			return false;
  		}
  	}
  	catch (e)
  	{
  		return false;
  	}
}


/* 
* Get session sid 
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.getSID = function()
{
	return this.sid;
}

/* 
* Get browser
*
* @author David Thunman
* @returns				string
*/
Utils.prototype.getBrowser = function()
{
	return this.browser;
}

/* 
* Get platfrom
*
* @author David Thunman
* @returns				string
*/
Utils.prototype.getPlatform = function()
{
	return this.platform;
}

/* 
* Get version
*
* @author David Thunman
* @returns				float
*/
Utils.prototype.getVersion = function()
{
	return this.version;
}

/* 
* Private check for string in navigator.userAgent
*
* @author David Thunman
* @returns				boolean
*/
Utils.prototype.check = function(s)
{
	var ua = navigator.userAgent.toLowerCase();
	var p = ua.indexOf(s) + 1;
	return p > 0 ? true : false;
}