//
// 04/22/08     11743   JLD     moved BrowserDetect into UTIL.JS from editmode/routereseq
//
///////////////////////////////////////////////////
// BROWSER CHECK from QuirksMode.org (http://www.quirksmode.org/js/detect.html)
// modified 8/16/06 Jeff Doucette
//////////////////////////////////////////////////
function stringPair (l, s)
{
	this.main = l;
	this.alt = s;
}
var BrowserDetect = {
	init: function () 
	{
		var br = this.searchString(this.dataBrowser, true);
		this.browser = br.main || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "0";
		this.OS = this.searchString(this.dataOS, false) || "an unknown OS";
		this.shortname = (br.alt || "??");
		this.composite = (br.alt || "??") + this.version;
	},
	searchString: function (data, getShortname) 
	{
		for (var i=0;i<data.length;i++)	
		{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) 
			{
				if (dataString.indexOf(data[i].subString) != -1)
				{
					if (getShortname)
						return new stringPair(data[i].identity, data[i].shortname);
					else
						return data[i].identity;
				}
			}
			else if (dataProp)
			{
				if (getShortname)
					return new stringPair(data[i].identity, data[i].shortname);
				else
					return data[i].identity;
			}
		}
	},
	searchVersion: function (dataString) 
	{
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) 
			return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			shortname: "SA"
		},
		{
			prop: window.opera,
			identity: "Opera",
			shortname: "OP"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab",
			shortname: "IC"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror",
			shortname: "KO"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox",
			shortname: "FF"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape",
			shortname: "NS"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE",
			shortname: "IE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv",
			shortname: "GE"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla",
			shortname: "NN"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

BrowserDetect.init();
///////////////////////////////////////////////////
// END BROWSERCHECK CODE
///////////////////////////////////////////////////

//////////////////////////////////////////////
// SEVERITY-LEVEL MESSAGING
//////////////////////////////////////////////
var bDebug = false;

function debug(severity, str)
{
	var level = 0;
	if (bDebug && (parseInt(severity) > parseInt(level)))
		alert(str);
}

function chopErrorText(responseText)
{
	var output = "";
	var errorEnd;
	var errorLoc = responseText.indexOf("<!-- +++");

	try
	{
		if ( errorLoc > -1 )
		{
			errorEnd = responseText.indexOf("-->", errorLoc);
			output = responseText.substring(errorLoc, errorEnd + 3);
		}
		else
		{
			// not an error page
			output = responseText;
		}
	}
	catch(e)
	{
		alert(e.description);
	}
	return output;
}

function browserWidth()
{
	var ret = {x:0,y:0};
	
	if (self.innerHeight)
	{
		ret.x = self.innerWidth;
		ret.y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		ret.x = document.documentElement.clientWidth;
		ret.y = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		ret.x = document.body.clientWidth;
		ret.y = document.body.clientHeight;
	}
	
	return ret;
}

///////////////////////////////////////////////
// capture mouse position
///////////////////////////////////////////////
function getXY(e)
{
	var out = {x:0, y:0};

	if (!e)
		var e = window.event;
	
    if (e.pageX || e.pageY) 
	{
        out.x = e.pageX;
        out.y = e.pageY;
    } 
    else 
	{	
        var de = document.documentElement;
        var b = document.body;
		
        out.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        out.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
	}
	
	return out;
}

///////////////////////////////////////////////
// determine object positioning
///////////////////////////////////////////////
function coordObject(x1, x2, y1, y2)
{
	this.x1 = x1;
	this.x2 = x2;
	this.y1 = y1;
	this.y2 = y2;
	this.width = x2 - x1 + 1;
	this.height = y2 - y1 + 1;
}

function getCoords(elem, Xoff, Yoff)
{
	var retval;
	
	if (elem)
	{
		var x1 = elem.offsetLeft + Xoff;
		var y1 = elem.offsetTop + Yoff;
		var elemIdx = elem;
	
		while (elemIdx.offsetParent) 
		{
			elemIdx = elemIdx.offsetParent;
			x1 += elemIdx.offsetLeft;
			y1 += elemIdx.offsetTop;
		}
		
		var x2 = x1 + elem.offsetWidth - 1;
		var y2 = y1 + elem.offsetHeight - 1;
	
		retval = new coordObject(x1, x2, y1, y2);
	}
	else
		retval = new coordObject(0,0,0,0);
	
	return retval;
}



//////////////////////////////////////////////
// REGEXP-BASED FORM VALIDATION
// borrowed from Karen Gayda
// http://www.siteexperts.com/tips/functions/ts23/page1.asp
//////////////////////////////////////////////
function validateInteger( strValue ) 
{
  var objRegExp  = /(^-?\d\d*$)/;
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) 
{
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }  
   return false;
}

////////////////////////////////////////////////////////
// custom text implementation for JavaScript messages //
////////////////////////////////////////////////////////
function errorMsgText(msg, paramObj)
{
	var counter = "1"
	var objExists;
	var msgOut = msg;
	
	try
	{
		eval("objExists = paramObj.p" + counter);
		while (objExists)
		{
			eval("msgOut = msgOut.replace('^" + counter + "', paramObj.p" + counter + ")");
			counter = String(Number(Number(counter) + 1));
			eval("objExists = paramObj.p" + counter);
		}
		
		return msgOut;
	}
	catch(e)
	{
		alert("Exception occurred during message construction (" + e.description + ").");
	}
}
