// JavaScript Document

jQuery(document).ready(function() { 

	/**
	 * Query UI Date Picker
	 */
	jQuery(".date_picker").datepicker({ 
		dateFormat: 'dd/mm/yy' 
	});
	
	jQuery( "#due_date" ).datepicker({
		dateFormat: 'dd M yy',
		altField: "#real_date",
		altFormat: "dd/mm/yy"
	});
	
	
	/**
	 * Saving the form
	 */
	jQuery('#next_step').click(function(e) {
		
		$("#saving_popup").do_saving();							
		
		$('form').each(function(index) {
			/*$.post(
				"/answers/ajax_save", 
				$(this).serialize()
			);*/
			
			//alert ('Start Saving...');
			
			var text = $.ajax({
				type: 'POST',
				url: "/answers/ajax_save",
				data: $(this).serialize(),
				async: false,
				error:function (xhr, error, errorThrown){
                    alert(xhr.status);
					alert(xhr.error);
                    alert(errorThrown);
                }
			}).responseText;
						
			while (text == '') {
				// Not sure if this is really needed. Also not sure how to test if this really
				// does anything. The idea is that if the jquery starts to proceed this line of code,
				// before the ajax call has finished then we can stop the code from continuing.
				//
				// What we do know if that if we put an alert after the ajax call it all seems to work
			}
			
			//alert ('...done');
		});
		//e.preventDefault();
	});
		
	$.fn.do_saving = function() {
		// Get page sizes
		var arrPageSizes = getPageSize();
		var arrPageScroll = getPageScroll();
			
		$(this).css({
			width:				arrPageSizes[0],
			height:				arrPageSizes[1]
		}).show();
				
		$('img', this).css({
			top:	arrPageScroll[1] + ((arrPageSizes[3] / 2) - 93 ),
			left:	arrPageScroll[0] + ((arrPageSizes[2] / 2) - 243 )
		}).show();
		
		
	}
	
	$.fn.sort_select_box = function(){
		var my_options = $("#" + this.attr('id') + ' option');
		my_options.sort(function(a,b) {
			if (a.text > b.text) return 1;
			else if (a.text < b.text) return -1;
			else return 0
		});
	   // return my_options;
	   this.empty().append(my_options);
	   $('option:first-child', this).attr("selected", "selected");
	}
		
		
	// Borrowed from lightbox
	function getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	};
	
	function getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	};
});


function disableEnterKey(e)
{
	 var key;     
	 if(window.event)
		  key = window.event.keyCode; //IE
	 else
		  key = e.which; //firefox     

	 return (key != 13);
}



