Oasys = {
	
	// public functions //
	callMethod: function(method, params, pleaseWait, onSuccess, onFailure) {
		var ajaxParams = new Object();
		ajaxParams.method = "post";
		ajaxParams.postBody = Object.toJSON(params);
		ajaxParams.onSuccess = Oasys.onAjaxSuccess;
		ajaxParams.onFailure = Oasys.onAjaxFailure;
		Oasys.clientOnAjaxFailure = onFailure;
		Oasys.clientOnAjaxSuccess = onSuccess;
	
		Oasys.clearErrors();
		Oasys.showMask(pleaseWait);
		
		new Ajax.Request( Oasys.root + "PorteVerte_Oasys/Proxy.php?method=" + method, ajaxParams );
	},
	
	// private functions //
	
	hasItems: function(array) {
		
		//HACKHERE - how to get number of associtive items?
		for(key in array) {
			if(typeof(array[key]) == "string") {
				return true;
			}
		}
		
		return false;
	},

    getSelected: function(selectField)
    {
		if(selectField.options)
		{
	    	return selectField.options[selectField.selectedIndex].value
	    }
	    else
    	{
    		////This is a <radio> object
    		if(typeof(selectField.length) == "undefined")
    			if(selectField.checked)
	    			return selectField.value
	    		else
	    			return ""
    		else
    			for(var i=0; i < selectField.length; i++)
    			{
    				if(selectField[i].checked)
    					return selectField[i].value
    			}

    		return ""
    	}
    },
	
	onAjaxFailure: function(response) {
		window.setTimeout(Oasys.onAjaxFailure2.bind(Oasys, response), 0);
	},

	onAjaxFailure2: function() {
		Oasys.hideMask();
		error = Oasys.strings.ajaxError.replace("@error", t.statusText + "' (" + t.status + ") - " + t.responseText);
		Oasys.showErrors({system: error});

		if(Oasys.clientOnAjaxFailure) {
			Oasys.clientOnAjaxFailure();
		}
	},

	onAjaxSuccess: function(response) {
		window.setTimeout(Oasys.onAjaxSuccess2.bind(Oasys, response), 0);
	},
	
	onAjaxSuccess2: function(response) {
		Oasys.hideMask();

		sResponse = response.responseText;
		
		// expected to be json in all cases:
		sResponse = sResponse.replace(/(\n|\r)/g, "");
		
		try {
			jResponse = sResponse.evalJSON();
		} catch(error) {
			Oasys.showErrors({system: Oasys.strings.ajaxError.replace("@error", "Unexpected response format (JSON) - " + sResponse)});
			if(this.clientOnAjaxFailure) {
				Oasys.clientOnAjaxFailure();
			}
			return;
		}
		
		if(jResponse.ValidationFailures.Failures.length > 0) {
			errors = {};
			for(var i=0; i < jResponse.ValidationFailures.Failures.length; i++) {
				errors[jResponse.ValidationFailures.Failures[i].Key] = jResponse.ValidationFailures.Failures[i].Value;
			}
				
			Oasys.showErrors(errors);
			if(Oasys.clientOnAjaxFailure) {
				Oasys.clientOnAjaxFailure();
			}
			return;
		}
		
		if(Oasys.clientOnAjaxSuccess) {
			Oasys.clientOnAjaxSuccess(jResponse);
		}
	},
	
	/// UI functions. override with your own if needed. //
	
	showMask: function(message)
	{
		Oasys.setInnerText(document.getElementById("ProgressMessage"), message)
	
		//document.body.onresize = Oasys.SetLayerPosition;
		//document.body.onscroll = Oasys.SetLayerPosition;
		Oasys.setLayerPosition();
	
		document.getElementById("masker").style.display = "block"; 
		document.getElementById("progress").style.display = "block"; 
	},
	
	hideMask: function()
	{
		window.onresize = function(){ }
		window.onscroll = function(){ }
	
		var masker = document.getElementById("masker");
		var progress = document.getElementById("progress");
	
		masker.style.display = "none"; 
		progress.style.display = "none";
	},
	
	showMessage: function(message) {
		$("Notification").className = "InfoMessage";
		$("Notification").innerHTML = message;
	},
	
	showErrors: function(errors) {
		errorMessage = [];
		for(field in errors) {
			if(typeof(errors[field]) != "string") {
				continue;
			}
			
			errorMessage.push(errors[field]);
		}
		
		$("Notification").className = "ErrorMessage";
	
		if(errorMessage.length == 1) {
			$("Notification").innerHTML = errorMessage[0];
		} else {
			$("Notification").innerHTML = "Please check the following and try again: <ul><li>" + errorMessage.join("</li><li>") + "</li></ul>";
		}
	
		///Scroll to the top of the page so they can see the new message:
		elem = document.getElementById("Notification");
		window.scrollTo(elem.scrollLeft, elem.scrollTop);
	},
	
	clearErrors: function() {
		$("Notification").innerHTML = "";
		$("Notification").className = "";
	},
	
	// helper methods used for the UI functions
	
	setInnerText: function(obj, newValue)
	{
		if (document.all) // IE;
			obj.innerText = newValue;
		else if (obj.textContent)
			obj.textContent = newValue;
	},
	
	setLayerPosition: function() 
	{
		///Masker should mask all the screen and scrolled areas:
		
		var shadow = document.getElementById("masker");
	
		if(typeof window.pageYOffset != "undefined")
		{
			///FireFox
			scrollTop = window.pageYOffset
			scrollLeft = window.pageXOffset
			
			///But this INCLUDES the scrollbar sizes, causing scrollbars to change/flicker during the 
			///masking
			scrollFudge = -30
		}
		else
		{
			//IE
			scrollTop = window.scrollTop
			scrollLeft = document.scrollLeft
			scrollFudge = 0
		}
		
		if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrollTop = window.pageYOffset;
		scrollLeft = window.pageXOffset;
	  } else {
		//DOM compliant
		scrollTop = document.body.scrollTop;
		scrollLeft = document.body.scrollLeft;
	  }
	
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			windowWidth = window.innerWidth;
			windowHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}
	  
		///Masker (shadow) should fill the entire scrollable area:
		shadow.style.width = windowWidth+ scrollFudge + "px";
		shadow.style.height = windowHeight + scrollFudge + "px";
		shadow.style.left = scrollLeft + "px"
		shadow.style.top = scrollTop + "px"
	
		///Progress div should be centered but in the current scrolled window area:
		var progress = document.getElementById("progress");
		progress.style.left = parseInt((windowWidth - 200) / 2) + scrollLeft + "px";
		progress.style.top = parseInt((windowHeight - 100) / 2) + scrollTop + "px";
	},
	
	// must set these in page before using any methods //
	root: "/",
	language: "en"
}

//http://www.netlobo.com/url_query_string_javascript.html

urlDecode = function(encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output.replace(/\+/g, " ");
}

function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return urlDecode(results[1]);
}

function getUrlParamNames( ) // gpn stands for 'get parameter names'
{
	var params = new Array( );
	var regex = /[\?&]([^=]+)=/g;
	while( ( results = regex.exec( window.location.href ) ) != null )
		params.push( results[1] );
	return params;
}

function getUrlQuery() {
	return window.location.href.match(/(\?.*)?$/)[0];
}

getSelected = function(selectField)
{
	if(typeof(selectField) == "string")
		selectField = document.getElementById(selectField);
		
	if(selectField.options)
	{
			if(selectField.selectedIndex < 0) { return ""; }
		return selectField.options[selectField.selectedIndex].value
	}
	else
	{
		////This is a <radio> object
		if(typeof(selectField.length) == "undefined")
			if(selectField.checked)
				return selectField.value
			else
				return ""
		else
			for(var i=0; i < selectField.length; i++)
			{
				if(selectField[i].checked)
					return selectField[i].value
			}

		return ""
	}
}

setCookie = function(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=; ; path=/"
}

getCookie = function(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
    {
		if(!aCrumb[1])
		{
			//alert("returning nothing");
			return ""
		}
		else
		{
			//alert("returning: " + unescape(aCrumb[1]))
			return urlDecode(aCrumb[1]);
		}
    }
  }

  // a cookie with the requested name does not exist
  return "";
}

deleteCookie = function( name ) 
{
	d = new Date()
	document.cookie = name + "=blah; expires=" + d.toGMTString() + "; ; path=/";
}


replaceUrlParam = function(strName, strValue, strURL)
{
	if(typeof(strURL) == "undefined")
		strURL = document.location.href;
		
	////If the current URL is an <ASP_URL>...
	//  - If strName is a <name>, this function replaces the associated
	//    <value> to strName with the new <value>, strValue.
	//  - Otherwise, this function adds a new <key_pair> to the <ASP_URL>
	//    with the given <name> and <value>, strName and strValue.
	//
	//  NOTE: Returns emptystring if the URL is malformed

	strOldValue = getUrlParam(strName, strURL)
	
	if(strOldValue == "")
	{
		//No matching pair, so add it:
		if(strValue != "")
		{
			//Add new value to the beginning of the <querystring>:
			iQMark = strURL.indexOf("?")

			if(iQMark == -1)
			{
				strURL = strURL + "?" + escape(strName) + "=" + escape(strValue)
			}
			else
			{
				strRightSide = strURL.substring(iQMark + 1, strURL.length)
				strLeftSide  = strURL.substring(0, iQMark + 1)
				
				strURL = strLeftSide + escape(strName) + "=" + escape(strValue) + "&" + strRightSide
			}
		}
		//else: actually is a delete, so leave unchanged.
	}
	else
	{
		//Replace the existing value:
		iQMark = strURL.indexOf("?")

		iValueStart = strURL.indexOf(escape(strName) + "=") + (escape(strName) + "=").length
		strLeftSide = strURL.substring(0, iValueStart)
		strRightSide = strURL.substring(iValueStart, strURL.length)

		iAmp = strRightPart.indexOf("&")
		
		if(iAmp == -1)
		{
			// Must be the last key_pair in the querystring.
			if(strValue != "")
			{
				strURL = strLeftSide + strValue
			}
			else
			{
				//actually want to delete the <name> from the querystring.
				//also delete the lefthand seperator, either '?' or '&':
				iKeypairStart = strLeftSide.lastIndexOf(escape(strName) + "=")
				strLeftSide = strLeftSide.substring(0, iKeypairStart)
				strURL = strLeftSide
			}
		}
		else
		{
			if(strValue != "")
			{
				strRightSide = strRightSide.substring(iAmp, strRightSide.length)
				strURL = strLeftSide + escape(strValue) + strRightSide
			}
			else
			{
				//actually want to delete the <name> from the querystring.
				//at this point there exists at least one other <key-pair> 
				//to the right. So delete the righthand token, either '?' or
				//'&', as well as this <key-pair>:
				strRightSide = strRightSide.substring(iAmp + 1, strRightSide.length)
				iKeypairStart = strLeftSide.lastIndexOf(escape(strName) + "=")
				strLeftSide = strLeftSide.substring(0, iKeypairStart)
				strURL = strLeftSide + strRightSide
			}
		}
	}

	return strURL
}
