﻿// JScript File

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var util = {
	cacheVersion: null,
	cacheQuery: null,
	
	getMSIEVersion: function() {
		if(util.cacheVersion != null) return util.cacheVersion;
		msv = 0;
		if(navigator.appVersion.indexOf('MSIE') > -1) {
			msv = navigator.appVersion.substr(navigator.appVersion.indexOf('MSIE')+5,10);
			msv = msv.substr(0, msv.indexOf(';'));
		}
		util.cacheVersion = msv * 1;
		return util.cacheVersion;
	},
	
	isVista: function() {	
		var result = false;
		var val = navigator.userAgent;
		var m = val.match(/NT \d+\.{0,1}\d*;/);
		if(m.length > 0) { if(((m[0].match(/\d+\.{0,1}\d*/)[0]) * 1.0) >= 6) result = true; }
		return result;
	},
	
	addEvent: function(object, eventName, fn) {
		if (document.addEventListener) object.addEventListener(eventName, fn, false);
		else object.attachEvent('on' + eventName, fn);
	},
	
	byId: function(id) {
		return document.getElementById(id);
	},
	
	windowSize: function() {
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			return { width: document.body.offsetWidth, height: document.body.offsetHeight };
		} else {
			return { width: window.innerWidth, height: window.innerHeight };
		}	
	}, 

		
	findPos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return {left:curleft,top:curtop};
	},
		
	createOption: function(ddlList, text, value, selected) {
		var option = document.createElement("OPTION");
		ddlList.options.add(option);
		option.text = text;
		option.innerText = text;
		option.value = value;
		if(selected) option.selected = true;
		return option;
	}, 
	
	setCookie: function(name, value, expires, path, domain, secure) {
		if(!expires) {
			expires = new Date();
			expires.setDate(expires.getDate()+1);
		}
		var curCookie = name + "=" + escape(value) +
	        ((expires		) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
		document.cookie = curCookie;
	},
	
	isVisible: function(obj) {
		var visible = true;
		if (obj.parentElement) {
			if(obj.style.display == 'none') visible = false;
			while (obj = obj.parentElement) {
				if(obj.style) if(obj.style.display == 'none') visible = false;
			}
		}
		return visible;
	},
	
	QueryString: function(name) {
		var result = '';
		if(util.cacheQuery == null) {
			util.cacheQuery = new Array();
			var url = document.location.href;
			if(url.indexOf('?') > -1) 
			{
				url = url.substring(url.indexOf('?')+1);
				queryVals = url.split(/&/);
				for(var i=0; i<queryVals.length; i++) 
				{
					util.cacheQuery[i] = new Object();
					util.cacheQuery[i].name = queryVals[i].substring(0, queryVals[i].indexOf('='))
					util.cacheQuery[i].value = queryVals[i].substring(queryVals[i].indexOf('=')+1)
				}
			}
		}
		for(var i=0; i<util.cacheQuery.length; i++)
		{
			if(util.cacheQuery[i].name.toLowerCase().trim() == name) {
				result = util.cacheQuery[i].value;
			}
		}
		return result;
	},
	
   makeRequest: function(url, postbackFunction) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         return false;
      }
      http_request.postbackFunction = postbackFunction;
      http_request.onreadystatechange = util.ajaxPostback;
      http_request.open('GET', url, true);
      http_request.send(null);
      return true;
   },
   
   ajaxPostback: function() {
	  if(this.readyState == 4) {
	     this.postbackFunction(this.responseText);
	  }
	  else if(this.status != 200)
	  {
		alert('There was a problem sending this request.  Please try again later.');
	  }
   },
   
	cacheImage: function(file) {
		var im = document.createElement('img');
		im.style.width = im.style.height = '1px';
		im.style.left = im.style.top = '-10px';
		im.style.position = 'absolute';
		im.src = file;
		document.body.appendChild(im);
		im.onload = function() { document.body.removeChild(im); }
	}
}

function search() { 
	var element = document.getElementById(searchTextId); 
	if(element.value != '') {
		document.location.replace("search.aspx?s=" +element.value.replace(/\<.*?\>/gi,' ') + "&r=" + Math.random());
	}
		
}
function searchTextKeyPress(e) {
	if(!e) e = event;
	if(e.keyCode == 13 && this.value != '') {
		document.forms[0].submit = function() { return false; }
		search();
		return false;
	}
}
function focusSearchText() {
	document.getElementById(searchTextId).select();
	document.getElementById(searchTextId).focus();
}

util.addEvent(window, 'load', function() {
	document.getElementById(searchTextId).onkeypress = searchTextKeyPress;
});

