var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		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)
					return data[i].identity;
			}
			else if (dataProp)
				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.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function getWindowSize() {
	var size = new Array();
	if (typeof(window.innerWidth)=='number') {
		size['width'] = window.innerWidth;
		size['height'] = window.innerHeight;
	} else {
    	if (document.documentElement && document.documentElement.clientWidth) {
			size['width'] = document.documentElement.clientWidth;
			size['height'] = document.documentElement.clientHeight;
		} else {
			if (document.body&&document.body.clientWidth) {
				size['width'] = document.body.clientWidth;
				size['height'] = document.body.clientHeight;
			}
		}
	}
	return size;
}

function textCounter(name,maxlimit) {
  var source = document.getElementById(name);
  var dest = document.getElementById(name+'-counter');

    var diff = maxlimit - source.value.length;
    if ( diff < 0 ) {
        source.value = escapeLineBreaks(source.value.substring(0,maxlimit));
	    dest.innerHTML = '0';
	    return false;
    }
    dest.innerHTML = diff;
    return true;
}

function escapeLineBreaks(data){
	data = data.replace(/\n/g,"");
	data = data.replace(/\r/g,"");
	return data;
}

function ConvertHtmlSpecialChars(str){
	var mystr = new String(str);
	var re;
	
	/* remove conditional comments */
	re=/<!--(.*?)-->/g;
	mystr=mystr.replace(re,"");
	
	/* replace certain characters with their html entities */
	var symbolLookup = [ [ 174, "" ], /* &reg; */
						[ 162, "" ], /* &cent; */
						[ 163, "" ], /* &pound; */
						[ 165, "" ], /* &yen; */
						[ 167, "*" ], /* § */
						[ 169, "" ], /* &copy; */
						[ 171, "" ], /* &laquo; */
						[ 176, "" ], /* &deg; */
						[ 177, "" ], /* &plusmn; */
						[ 181, "" ], /* &micro; */
						[ 182, "" ], /* &para; */
						[ 183, "*" ], /* &middot; */
						[ 186, "" ], /* &ordm; */
						[ 187, "" ], /* &raquo; */
						[ 188, "1/4" ], /* &frac14; */
						[ 189, "1/2" ], /* &frac12; */
						[ 190, "3/4" ], /* &frac34; */
						[ 216, "*" ], /* Ø */
						[ 8211, "-" ],
						[ 8212, "--" ],
						[ 8216, "'" ],
						[ 8217, "'" ],
						[ 8220, "\"" ], /* &quot; */
						[ 8221, "\"" ],
						[ 8226, "*" ] ]; /* bullet-point */
						
	for ( i = 0; i < symbolLookup.length; i++ ) {
		re=new RegExp(String.fromCharCode(symbolLookup[i][0]), "g");
	    mystr = mystr.replace(re, symbolLookup[i][1]);
	}
	
	return mystr;
}
function insertAtCursor(field, myValue) {
	var myField = document.getElementById(field);
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
			+ myValue
			+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
	return false;
}

function VendorLogoImage(src, id) {
	this.src = src;
	this.newHeight = 0;
	this.newWidth = 0;
	this.id = id;
}

VendorLogoImage.prototype = new Image;

VendorLogoImage.prototype.onload = function () {
	this.newHeight = this.height;
	this.newWidth = this.width;
	if ( this.height > 60 || this.width > 110 ) {
		var heightDivisor= this.height / 60;
		var widthDivisor = this.width / 110;

		var divisor = Math.max(heightDivisor, widthDivisor);
		this.newHeight= Math.floor(this.height / divisor);
		this.newWidth = Math.floor(this.width / divisor);
	}
	this.modifyExistingImageTag();
}

VendorLogoImage.prototype.modifyExistingImageTag = function () {
	var imgTag = document.getElementById("vendor-logo-"+this.id);
	imgTag.height = this.newHeight;
	imgTag.width = this.newWidth;
}

VendorLogoImage.prototype.getDomImageTag = function () {
	var imgTag = document.createElement("img");
	imgTag.setAttribute("src", this.src);
	imgTag.setAttribute("width", this.newWidth);
	imgTag.setAttribute("height", this.newHeight);
	return imgTag;
}

function checkForFirstLetterUpperCase(fieldName)
{	
	field = document.getElementById(fieldName);
	fieldValue = document.getElementById(fieldName).value;
	if (fieldValue.length > 0 && fieldValue.charAt(0).search(/[a-z]/) > -1) {
		field.value = fieldValue.charAt(0).toUpperCase() + fieldValue.substring(1,fieldValue.length);
	}
}

function addEvent(obj,type,fn){
    if(obj.addEventListener) obj.addEventListener(type,fn,false);
    else if(obj.attachEvent){
        obj["e"+type+fn]=fn;
        obj[type+fn]=function(){obj["e"+type+fn](window.event);}
        obj.attachEvent("on"+type,obj[type+fn]);
    }
}

function removeEvent(obj,type,fn){
  if(obj.removeEventListener) obj.removeEventListener(type,fn,false);
  else if(obj.detachEvent){
    obj.detachEvent("on"+type,obj[type+fn]);
    obj[type+fn]=null;
    obj["e"+type+fn]=null;
  }
}