
/**
 * General Javascript functions
 *
 * For various elements of the layout, however these are mostly being deprecated
 * by jQuery extensions ({@link jquery-pm-layout.js})
 *
 * @package pmweb
 * @subpackage script
 * @since 07/03/2007
 * @copyright Profitmaster Systems Ltd., www.profitmaster.co.uk
 * @author Pete Hurst, <peterh@profitmaster.co.uk>
 * @version $Id: general.js 1541 2008-08-14 16:24:01Z pete $ v1.4.0.1
 */
 
var layout = {

	fillHeight: function(id,margin) {
		totalHeight = this.getWindowHeight();
		if (totalHeight) {
			targetHeight = totalHeight-margin;
			element = document.getElementById(id);
			if (element) {
				element.style.height = targetHeight+"px";
			}
		}
	},
	
	matchHeight: function(idTarget,idSource,offset) {
		sourceElement = document.getElementById(idSource);
		targetElement = document.getElementById(idTarget);
		if (sourceElement&&targetElement) {
			sourceHeight = sourceElement.clientHeight;
			targetElement.style.height = (sourceHeight-offset)+"px";
		}
	},
	
	maxHeight: function(idTarget,idSource,offset) {
		sourceElement = document.getElementById(idSource);
		targetElement = document.getElementById(idTarget);
		if (sourceElement&&targetElement) {
			targetHeight = targetElement.clientHeight;
			sourceHeight = sourceElement.clientHeight;
			targetElement.style.height = (Math.max(sourceHeight,targetHeight)-offset)+"px";
		}
	},
	
	getWindowHeight: function() {
		if (window.innerHeight) // All except IE
			return window.innerHeight;
		if (document.documentElement && document.documentElement.clientHeight)
			return document.documentElement.clientHeight; // IE6 +4.01
		if (document.body && document.body.clientHeight)
			return document.body.clientHeight; // IE5 or DTD 3.2
		return 0;
	},
	
	getScrollPosition: function() {
		if (document.documentElement && document.documentElement.scrollTop)
			return document.documentElement.scrollTop; // IE6 +4.01
		if (document.body && document.body.scrollTop)
			return document.body.scrollTop; // IE5 or DTD 3.2
		return 0;
	},

	setScrollPosition: function(yPosition) {
		scrollTo(0, yPosition);
	}

};

var ui = {
	
	submit: function(form,buttonName) {
		// Create hidden field to simulate button click
		var input = document.createElement("input");
		input.setAttribute("type", "hidden");
		input.setAttribute("name", buttonName);
		input.setAttribute("value", buttonName);
		// Add to form collection
		form.appendChild(input);
		// Submit form
		if (form.onsubmit) {
			if (form.onsubmit())
				form.submit();
		}
		else {
			form.submit();
		}
		// Prevent link from activating
		return false;
	},
	
	// Save scroll height to a hidden input so that postback page can appear at same position
	scrollSavePost: function(form,hiddenInput) {
		if (!(hiddenInput==undefined)) {
			hiddenInput.value = layout.getScrollPosition();
		}
	},
	
	// Save scroll height to end of form URL so that postback page can appear at same position
	scrollSaveGet: function(form,queryToken) {
		if (form.action.match(/\?/)) {
			form.action+="&"+queryToken+"="+layout.getScrollPosition().toString();
		}
		else {
			form.action+="?"+queryToken+"="+layout.getScrollPosition().toString();
		}		
	},

	openWindow: function(url,width,height) {
		day = new Date();
		id = day.getTime();
		windowLeft = (screen.width - width) / 2;
		windowTop = (screen.height - height) / 2;
		eval("page"+id+"=window.open(url,'"+id+"','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+width+",height="+height+",left = "+windowLeft+",top = "+windowTop+"');");
	//	window.open(url, id,'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height+',left = '+left+',top = '+top);
		return false; // Prevent hyperlink from firing when JS is active
	}
	
};

/**
 *
 */
var keys = {
    getCharCode: function(evt){
            return (evt.charCode) ? evt.charCode :
                ((evt.which) ? evt.which : evt.keyCode);
    },
    isEnter:function(charCode){
            return (charCode == 13);
    },
    submitViaEnter:function(evt,form,buttonName) {
            evt = (evt) ? evt : event;
            charCode = this.getCharCode(evt);
            if (this.isEnter(charCode)) {                   
                    ui.submit(form,buttonName);
                    return false;
            }
            return true;
    }
};
