var BTUtils = {
	
	browserName:navigator.appName,
	browserVer:parseInt(navigator.appVersion),

	//enable a field
	enableField:function(formName, fieldName) {
		this.setFieldState(true, formName, fieldName);
	},
		
	//disable a field
	disableField:function(formName, fieldName, bReadOnly) {
		this.setFieldState(false, formName, fieldName, bReadOnly);
	},
	
	//enableField() and disableField() really call this function....so the logic is defined once
	setFieldState:function(bEnable, formName, fieldName, bReadOnly) {
		frm = eval("document." + formName);
		obj = eval("frm." + fieldName);
		//alert("browser: " + browserName + " ver:" + browserVer);
		if ((this.browserName == "Microsoft Internet Explorer"  && this.browserVer >= 4) || (this.browserName == "Netscape" && this.browserVer >= 5)) {
			//alert("setting attributes for " + fieldName + ", which is of type " + obj.type);
			//obj.type undefined means radio buttons
			if(obj) {
				if (!obj.type) {
				//if (obj.length) {
					for (i=0; i<obj.length; i++) {
						if (bEnable) {
							obj[i].disabled = false;
							obj[i].readOnly = false;
						} else {
							if (bReadOnly) {
								obj[i].readOnly = true;
							} else {
								obj[i].disabled = true;
							}
						}
					}
				} else {
					if (bEnable) {
						obj.disabled = false;
						obj.readOnly = false;
						obj.style.backgroundColor = "white";
					} else {
						obj.style.backgroundColor = "silver";
						if (bReadOnly) {
							obj.readOnly = true;
						} else {
							obj.disabled = true;
						}
					}
				}
			}
		}
	},
	
	showContent:function(id) {
		// most any modern browser
		if (document.getElementById) {
			//alert("getElementById enabled");
			//document.getElementById(id).style.display = "inline";
			if (document.getElementById(id).style.display == "none") document.getElementById(id).style.display = "";
		//IE 4
		} else if (document.all) {
			document.all[id].style.display = "inline";
		//Netscape 4
		} else if (document.layers) {
			document.layers[id].visibility = "show";
		}
	},
	
	hideContent:function(id) {
		// most any modern browser
		if (document.getElementById) {
			//alert("getElementById enabled");
			document.getElementById(id).style.display = "none";
		//IE 4
		} else if (document.all) {
			document.all[id].style.display = "none";
		//Netscape 4
		} else if (document.layers) {
			document.layers[id].visibility = "hide";
		}
	},
	
	parseUrl:function(url_str) {
		var urlParts = new Array();
	
		if (url_str) {
			var pos, lastPos;
			
			// make sure there is a protocol.  need to handle user-entered url's'
			pos = url_str.indexOf(':');
			if (pos == -1)
				url_str = "http://" + url_str;
				
			//url_str = url_str.replace("\\","/");
		
			// Parse protocol part
			pos = url_str.indexOf('://');
			if (pos != -1) {
				urlParts['protocol'] = url_str.substring(0, pos);
				lastPos = pos + 3;
			}
	
			// Find port or path start
			for (var i=lastPos; i<url_str.length; i++) {
				var chr = url_str.charAt(i);
	
				if (chr == ':')
					break;
	
				if (chr == '/')
					break;
			}
			pos = i;
	
			// Get host
			urlParts['host'] = url_str.substring(lastPos, pos);
	
			// Get port
			urlParts['port'] = "";
			lastPos = pos;
			if (url_str.charAt(pos) == ':') {
				pos = url_str.indexOf('/', lastPos);
				urlParts['port'] = url_str.substring(lastPos+1, pos);
			}
	
			// Get path
			lastPos = pos;
			pos = url_str.indexOf('?', lastPos);
	
			if (pos == -1)
				pos = url_str.indexOf('#', lastPos);
	
			if (pos == -1)
				pos = url_str.length;
	
			urlParts['path'] = url_str.substring(lastPos, pos);
	
			// Get query
			lastPos = pos;
			if (url_str.charAt(pos) == '?') {
				pos = url_str.indexOf('#');
				pos = (pos == -1) ? url_str.length : pos;
				urlParts['query'] = url_str.substring(lastPos+1, pos);
			}
	
			// Get anchor
			lastPos = pos;
			if (url_str.charAt(pos) == '#') {
				pos = url_str.length;
				urlParts['anchor'] = url_str.substring(lastPos+1, pos);
			}
		}
	
		return urlParts;
	
	},	
	
	/** function to dynamically manipulate an element class **/
	changeCss:function(a,o,c1,c2) {
	  switch (a){
	    case 'swap':
	      o.className=!this.changeCss('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
	    break;
	    case 'add':
	      if(!this.changeCss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
	    break;
	    case 'remove':
	      var rep=o.className.match(' '+c1)?' '+c1:c1;
	      o.className=o.className.replace(rep,'');
	    break;
	    case 'check':
	      return new RegExp('\\b'+c1+'\\b').test(o.className)
	    break;
	  }
	},
	
	addLoadEvent:function(func) {
	    var oldQueue = window.onload? window.onload: function() {};
	    window.onload = function() {
	        oldQueue();
	        func();
	    }
	},

	addUnloadEvent:function(func) {
	    var oldQueue = window.onunload? window.onunload: function() {};
	    window.onunload = function() {
	        oldQueue();
	        func();
	    }
	},
	
	formatCurrency:function(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
			num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	},
	
	selectNode:function(node) {
		var selection, range, doc, win;
	
		if ((doc = node.ownerDocument)
				&& (win = doc.defaultView)
				&& typeof win.getSelection != 'undefined'
				&& typeof doc.createRange != 'undefined'
				&& (selection = window.getSelection())
				&& typeof selection.removeAllRanges != 'undefined') {
			range = doc.createRange();
			range.selectNode(node);
			selection.removeAllRanges();
			selection.addRange(range);
		} else if (document.body
				&& typeof document.body.createTextRange != 'undefined'
				&& (range = document.body.createTextRange())) {
			range.moveToElementText(node);
			range.select();
		}
	},


	clearSelection:function() {
		if (document.selection)
			document.selection.empty();
		else if (window.getSelection)
			window.getSelection().removeAllRanges();
	},
	
	getRadioValue:function(formName, btn) {
		frm = eval("document." + formName);
		obj = eval("frm." + btn);
		if (obj) {
			for (var i = 0; i < obj.length; i++) {
				if (obj[i].checked) 
					return obj[i].value;
			}
		}
		return null;
	},
	
	isValidUrl:function (url) {
		//var regexp = /^(http[s]?\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z0-9\/]{1,4}(?:\/[a-zA-Z0-9_\-\.\/]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z0-9\/])?(?:\?[a-zA-Z0-9_]+[\=a-zA-Z0-9_\-]*?)?(?:[\&a-zA-Z0-9_]+[\=a-zA-Z0-9_\-]*)*)$/
		var regexp = /(http[s]?):\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/
		return regexp.test(url);
	},
	
	trim: function(str){
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	},
	
	isValidEmail: function(email) {
	   var regexp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   return regexp.test(email);
	},

	emailValidate: function(emailID) {
	  var emailFilter=/^[A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}$/i;
	  //alert(emailFilter2.test(emailIDs));
	  return emailFilter.test(emailID);
	},

	multiEmailValidate: function(emailIDs) {
	  var emailFilter=/^[A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}(?:([\s]*[,][\s]*)[A-Z0-9\._%-]+@[A-Z0-9\.-]+\.[A-Z]{2,4})*$/i;
	  //alert(emailFilter.test(emailIDs));
	  return emailFilter.test(emailIDs);
	},
	
	/* JQuery based functions */
	showBusy: function(msg) {
		msg = msg||"Loading...";
		$('body').append('<div id="id-progress" class="progress">'+msg+'</div>');
		return $('#id-progress').css('margin-left',-$('#id-progress').width()/2-35);
	},

	hideBusy: function() {
		$('#id-progress').remove();
	},

	showMsg: function(msg) {
		this.showBusy(msg).animate({opacity: 0.8}, 2000).fadeOut("slow",function(){BTUtils.hideBusy()});
	},
	
	monthDiff: function(d1, d2) {
	    var months;
	    months = (d2.getFullYear() - d1.getFullYear()) * 12;
	    months -= d1.getMonth();
	    months += d2.getMonth();
	    return months;
	},

	dateDiff: function(d1, d2) {
			var oneDay=1000*60*60*24;
		    return Math.ceil((d2.getTime()-d1.getTime())/(oneDay));
	},

	daysInMonth: function(month, year)
	{
		return 32 - new Date(year, mnth, 32).getDate();
	},

	formatCurrency: function(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if (isNaN(num)) num = '0 ';
		var sign = (num == (num = Math.abs(num)));
		num = Math.floor(num * 100 + 0.50000000001);
		var cents = num % 100;
		num = Math.floor(num / 100).toString();
		if (cents < 10) cents = '0' + cents.toString();
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
			num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
		return (((sign) ? '' : '-') + '$' + num + '.' + cents.toString());
	},

	formatNumber: function(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if (isNaN(num)) num = '0 ';
		var sign = (num == (num = Math.abs(num)));
		//num = Math.floor(num * 100 + 0.50000000001);
		//var cents = num % 100;
		//num = Math.floor(num / 100).toString();
		//if (cents < 10) cents = '0' + cents.toString();
		num = num.toString();
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
			num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
		return (((sign) ? '' : '-') + num);
	},
	
	browserSupportsPlaceholder: function () {
	  var i = document.createElement('input');
	  return 'placeholder' in i;
	},
	
	isNumeric: function(n) {
        return (n!='NaN' && parseFloat(n)==n);
    }

	
	/*pageLoadFunctions:[],
	pageLoadDelays:[],
	handlePageLoad:function() {
		for(var i=0;i<this.pageLoadFunctions.length;i++) {
			setTimeout(this.pageLoadFunctions[i],this.pageLoadDelays[i]);
			//eval(this.pageLoadFunctions[i]);
		}
	},
	runOnPageLoad:function(fn,delay) {
		this.pageLoadFunctions.push(fn);
		this.pageLoadDelays.push(delay ? delay:0);
		window.onload = this.handlePageLoad();
	}*/
	
}

