Utils = {

	/**
	 * Find the X position of an html element
	 * @author Peter-Paul Koch & Alex Tingle
	 * @return {Number} The x position in pixels
	 * @method
	 */
	getX: function(obj){

		// Start at 0
		var curleft = 0;
		
		// If the item has an offset parent
		if(obj.offsetParent){
			
			// At least once
			while(1) {
				
				// Add the offset
				curleft += obj.offsetLeft;
				
				// Keep going?
				if(!obj.offsetParent) break;
				
				// Continue up the chain
				obj = obj.offsetParent;
			}
		}
		
		// Object has an x value? Add it
		else if(obj.x) curleft += obj.x;
		
		// Return the result
		return curleft;
	},

	/**
	 * Find the Y position of an html element
	 * @author Peter-Paul Koch & Alex Tingle
	 * @return {Number} The y position in pixels
	 * @method
	 */
	getY: function(obj){
		
		// Start at 0
		var curtop = 0;
		
		// If the item has an offset parent...
		if(obj.offsetParent)
		
		// Loop at least once
		while(1){
		
			// Add the offset
			curtop += obj.offsetTop;
			
			// Keep going?
			if(!obj.offsetParent) break;
			
			// Continue up the chain
			obj = obj.offsetParent;
		}

		// Object has a y value? Add it
		else if(obj.y) curtop += obj.y;

		// Return the result
		return curtop;
	},
	
	/**
	 * Returns the window scroll value.
	 * @return	{Number}	The scroll value.
	 */
	getScrollTop: function() {
	  var scrollY = 0;
	  
	  if( document.documentElement && document.documentElement.scrollTop ) {
	    scrollY = document.documentElement.scrollTop;
	  }
	  else if( document.body && document.body.scrollTop ) {
	    scrollY = document.body.scrollTop;
	  }
	  else if( window.pageYOffset ) {
	    scrollY = window.pageYOffset;
	  }
	  else if( window.scrollY ) {
	    scrollY = window.scrollY;
	  }
	  return scrollY;
	},
	
	/**
	 * Returns the height of the document
	 * @return {Number}
	 */
	getDocHeight: function() {
	    var D = document;
	    return Math.max(
	        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
	        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
	        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	    );
	},
	
	/**
	 * Returns the height of the window
	 * @return {Number}
	 */
	getWindowHeight: function(){
		
		return (window.innerHeight) ?
			Math.max(
				window.innerHeight, 
				document.documentElement.clientHeight
			):
			Math.max(
				document.body.clientHeight, 
				document.documentElement.clientHeight
			);
	},
	
	/**
	 * Get mouse coordinates
	 * @return {Object}
	 */
	getMousePosition: function(e){
		/*@cc_on e = window.event; return {x: e.clientX, y: e.clientY} @*/
		return {x: e.pageX, y: e.pageY}
	},

	/**
	 * Normalize a mouse wheel event into something useable
	 * @param	{Event}		e	The wheel event
	 * @return	{Number}	A value that can be used for scrolling.
	 * @method
	 */
	 normalizeWheelEvent: function(e){
	 	return e.detail ? e.detail * -1 : e.wheelDelta / 40;
	 },

	/**
	 * Posts an HTML form
	 * @param	{HTMLFormElement}	form		The form to post.
	 * @param	{Function}			callback	Function to handle the ajax response.
	 * @method
	 */
	postForm: function(form, config){

		// Query string
		var params = "";

		// URLize an input value
	    function urlize(name, value) {
	        params += (params.length > 0 ? "&" : "")
	            + escape(name).replace(/\+/g, "%2B") + "="
	            + escape(value ? value : "").replace(/\+/g, "%2B");
				//+ escape(value ? value : "").replace(/\n/g, "%0D");
	    }

		// Loop through form elements
	    for (var i = 0; i < form.elements.length; i++) {

			// Distill iteration
	        var element = form.elements[i];

			// Get element name
	        var elemName = element.name;
			
			// Get element type
			var elemType = element.nodeName.toUpperCase();

			// If the element has a name
	        if (elemName) {
				
				// Switch on element type
				switch(elemType) {

					// Input fields
					case "INPUT":

						// Check boxes
						if (element.type == "checkbox" && element.checked) {
							urlize(elemName, element.value ? element.value : "On");
						}

						// Radio boxes
						if (element.type == "radio" && element.checked) {
							urlize(elemName, element.value);
						}

						// Input fields
						if (element.type == "text" || element.type == "password" || element.type == "hidden"){
							urlize(elemName, element.value);
						}

						break;

					// Select boxes
					case "SELECT":

						// Loop through select options
						for (var j = 0; j < element.options.length; j++) {

							// Distill iteration
							var option = element.options[j];

							// If the option is selected and has a value
							if (option.selected) {
								
								// Add it
								urlize(elemName, option.value);
							}
						}
						break;
						
					// Ignore Fieldsets
					case "FIELDSET":
						break;

					// All other field types
					default:
						urlize(elemName, element.value);
						break;
				}
	        }
	    }
		
		// Params created, ajax time
		new Ajax({
			
			// Use form action
			path: form.action,
			
			// Add params 
			params: params,
			
			// Do a post request
			method: "POST",
			
			// Add callback
			onComplete: config.onComplete,
			
			// Add failure callback
			onFailure: config.onFailure
		});
	}
}