if(typeof(String.prototype.trim) == "undefined"){
	// If we don't have a string triom function, create one
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
}

if (typeof(Xi)=="undefined"){
	Xi = {
		intVersion : 2,
		GetVersion : function(){
			return Xi.intVersion;
		}
	};
}

function AddBookmark(URL, Title) {
	if (arguments.length == 0) {
		URL = window.location.href;
		Title = document.title;
	} else if (arguments.length == 1) {
		URL = window.location.href;
	}
	
	if (window.external) {
		window.external.AddFavorite(location.href, document.title);
	} else if (window.sidebar) {
		window.sidebar.addPanel(Title, URL, "");
	} else {
		alert("I am sorry but your browser does not allow adding bookmarks by this way");
	}
}

function PrepareEvent(objEvent){
	var objTarget;
	if (!objEvent){
		if(window.event == null){
			return null;
		}
		var objEvent = window.event;
	}
	
	if (objEvent.bolPrepared == true){
		//This event has already been prepared
		return objEvent;
	}
	
	if (objEvent.target) {
		objTarget = objEvent.target;
	}else if (objEvent.srcElement) {
		objTarget = objEvent.srcElement;
		objEvent.target = objTarget;
	}
	
	if(typeof(objTarget) != 'undefined'){
		if (objTarget.nodeType == 3){ // defeat Safari bug	
			objTarget = objTarget.parentNode;
		}
	}
	
	objEvent.objTarget = objTarget;
	
	// this is for backwards compatability	
	objEvent.objRealTarget = objTarget;
	objEvent.bolPrepared = true;
	return objEvent;
}

function CancelEvent(objEvent){

	objEvent.returnValue = false;
	if(typeof(objEvent.preventDefault) != 'undefined'){
		objEvent.preventDefault();
	}
	return false;
}

function FindObjectInArray(arrSearch,strKey,strValue){
	var intLength = arrSearch.length;
	for (var i =0; i < intLength; i++){
		if(arrSearch[i][strKey] == strValue){
			return arrSearch[i];
		}
	}
	return;
}

function GetRealXPos(objElement){
	if(objElement == window){
		return 0;
	}else if(objElement != null) { 		
		if(objElement.nodeType == 1 && objElement.tagName == 'BODY'){
			return 0;
		}else{
			return objElement.offsetLeft + GetRealXPos(objElement.offsetParent);
		}
	}else{
		return 0;
	}
}

function GetRealYPos(objElement){
	if(objElement == window){
		return 0;
	}else if(objElement != null) {
		if(objElement.nodeType == 1 && objElement.tagName == 'BODY'){
			return 0;
		}else{		
			return objElement.offsetTop + GetRealYPos(objElement.offsetParent);
		}
	}else{
		return 0;
	}
}

function GetParentByClass(objElement,strClassName){	
	if(objElement.nodeType != 1){
		return GetParentByClass(objElement.parentNode,strClassName);
	}else if(objElement.tagName == 'BODY'){
		return false;
	}else if(objElement.className == strClassName){
		return objElement;			
	}else{
		return GetParentByClass(objElement.parentNode,strClassName);
	}
}

function SetOpacity(objElement,intValue) {
	objElement.style.opacity = intValue/10;
	objElement.style.filter = 'alpha(opacity=' + intValue*10 + ')';
}

function CreateCookie(strCookieName,value,intDays) {
	if (intDays) {
		var objDate = new Date();
		objDate.setTime(objDate.getTime()+(intDays*24*60*60*1000));
		var expires = "; expires="+objDate.toGMTString();
	}
	else var expires = "";
	document.cookie = strCookieName+"="+value+expires+"; path=/";
}

function ReadCookie(strCookieName) {
	var strCookieNameEQ = strCookieName + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(strCookieNameEQ) == 0) return c.substring(strCookieNameEQ.length,c.length);
	}
	return null;
}

function EraseCookie(strCookieName) {
	CreateCookie(strCookieName,"",-1);
}

/*HTTP Request stuff*/
function SendHTTPRequest(strMethod, strURL, strParams, funcCallbackFunc){
	if (!window.objRequest){
		try {
			objRequest = new XMLHttpRequest();
		} catch (trymicrosoft) {
			try {
				objRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (othermicrosoft) {
				try {
					objRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (failed) {
					objRequest = false;
				}
			}
		}
	}
	
	if(!window.arrCommandQueue){
		arrCommandQueue = Array();
	}
		
	if(objRequest.readyState > 0 && objRequest.readyState < 4){
		arrCommandQueue.push(Array(strMethod, strURL, strParams, funcCallbackFunc));	
		return;
	}
	
	if(strMethod == 'POST'){
		objRequest.open(strMethod,strURL,true);	
		if (strParams != null){			
			objRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			objRequest.setRequestHeader("Content-length", strParams.length);
			objRequest.setRequestHeader("Connection", "close");
		}	
		objRequest.send(strParams);
	}else{
		if (strParams != null){
			strURLandQuery = strURL+'?'+strParams;
		}else{
			strURLandQuery = strURL;
		}
		objRequest.open(strMethod,strURLandQuery,true);			
		objRequest.setRequestHeader("Content-type", "text/plain");				
		objRequest.setRequestHeader("Connection", "close");
		objRequest.send(null);
	}
	
	objRequest.onreadystatechange = function(){
		if (objRequest.readyState == 4){
			switch (objRequest.status){
				case 200:					
					var arrContentType = objRequest.getResponseHeader("Content-Type").split(";");
					var strMimeType = arrContentType[0].trim();					
					switch(strMimeType){
						case 'text/json':
							eval("var mxdTemp = "+objRequest.responseText);
							break;
						case 'text/javascript':
							eval(objRequest.responseText);
							break;
						default:
							var mxdTemp = objRequest.responseText;
					}
					funcCallbackFunc(mxdTemp);
				break;
				case 404:
					alert("404 Resource Not Found");
				break;
				case 403:
					alert("You are not allowed to use this resource. You may need to log in to use it");
				break;
				case 405:
					alert("405 Method Not Allowed");
				break;
				default:
					//alert("An error has occurred: "+objRequest.responseText);
			}
			//Tell the array to shift
			if(arrCommand = arrCommandQueue.shift()){
				SendHTTPRequest(arrCommand[0], arrCommand[1], arrCommand[2], arrCommand[3])
			}
		}		
	};	
}

function FormHelpIconClick(objEvent) {
	objEvent = PrepareEvent(objEvent);
	
	var arrFormHelpers = document.getElementsBySelector(".HelpDescription");
	var intHelperLengths = arrFormHelpers.length;
	var strHelpDivId = objEvent.objTarget.id.split('-')[0]+'-HelpDescription';
	
	for(i=0; i < intHelperLengths; i++){
		arrFormHelpers[i].style.display = 'none';
		if (arrFormHelpers[i].id==strHelpDivId){
			arrFormHelpers[i].style.display = 'block';
		}
	}
}

AddPageLoadFunction(function (){
	var objMyRules = {
		'.HelpIcon' : function(objElement) {
			addEvent(objElement,'click',FormHelpIconClick);
		}
	};	
	Behaviour.register(objMyRules);		 
});

