﻿/// <reference path="jquery-1.3.2.js"/>

OWL.helpers = OWL.helpers || {};
(function() {
	var that = OWL.helpers;
	var thatObj = {
		defaultErrorCallback: function(xhr, textStatus, errorThrown) {
		
			var text;
			if (xhr.responseText) {
				text = xhr.responseText;
			} else if (textStatus) {
				text = textStatus.responseText;
			}
			
			try {
				//eval the responseText as json
				// this dies if the responseText isn't valid json

				var err = eval("(" + text + ")");

				if (err.Error) {
					err = err.Error;
				}

				//display the error if the eval worked
				alert(err.Header + '\r\n' + err.Message);
			}
			catch (e) {
				if(text){
					alert(text);
				}
				else {
					that.alertDefaultError();
				}
			}
		},
		alertDefaultError: function() {
			alert("There was an error. An unexpected result was returned from the server.\r\nYour session may have ended. If this is the case please try logging in again.");
		},
		findPosX: function(obj) {
			var curleft = 0;
			if (obj.offsetParent)
				while (1) {
				curleft += obj.offsetLeft;
				if (!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
			else if (obj.x)
				curleft += obj.x;
			return curleft;
		},
		findPosY: function(obj) {
			var curtop = 0;
			if (obj.offsetParent)
				while (1) {
				curtop += obj.offsetTop;
				if (!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
			else if (obj.y)
				curtop += obj.y;
			return curtop;
		},
		toJson: function(text) {
			try {
				return eval("(" + text + ")");
			}
			catch (e) {
				return null;
				//return empty object
				return eval("({})");
			}
		},
		wireParseInts: function() {
			jQuery(".parseInt").blur(function() {
				var v = parseInt(this.value, 10); //base-10
				if (isNaN(v)) {
					this.value = 0;
				}
				else {
					this.value = v;
				}
			});
		}
	};
	OWL.lang.augmentObject(that, thatObj, true);
})();
