 /**
 * 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(id)
{
	this.sid = this.getRandomNumber();
	this.detect();
	this.win = null;

	this.id = id;
	this.page = 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)
{	
	var id = this.getRandomNumber();
	var name = "popup_" + id;
	

	/* try to set width and height */
	try
	{
		if(w == null || h == null)
		{
			w = screen.availWidth;
			h = screen.availHeight;
		}
	}
	catch(e)
	{
		;
	}
	
	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)
	{
		;
	}
}



/* 
* Handy way to confirm a form submission
*
* @author 				David Thunman
* @returns				nothing
*/
Utils.prototype.confirmSubmit = 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 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;
}

var utils = new Utils();