// JavaScript Document

function setInputValue(obj,value){
	if(typeof(obj) == 'string'){
		obj = document.getElementById(obj);
	}
	if(null != obj){
		obj.value = value;
	}
}

functions = {
	
	setInputValue : function(obj,value){
		if(typeof(obj) == 'string'){
			obj = document.getElementById(obj);
		}
		if(null != obj){
			obj.value = value;
		}
	},

	// Returns array with page width, height and window width, height
	getPageSize : function(){
		
		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;
	},
	
	findPos : function(element) {
		var obj = (typeof(element) == 'string') ? document.getElementById(element) : element;
		var curleft = curtop = 0;
		if(null != obj){
			if (obj.offsetParent) {
				curleft = obj.offsetLeft
				curtop = obj.offsetTop
				while (obj = obj.offsetParent) {
					curleft += obj.offsetLeft
					curtop += obj.offsetTop
				}
			}
		}
		return [curleft,curtop];
	},
	
	updateElementHTML : function(element,htmlContent){
		var obj = (typeof(element) == 'string') ? document.getElementById(element) : element;
		if(null != obj){
			obj.innerHTML = htmlContent;
		}
	},
	
	trim : function(str){
	   return str.replace(/^\s+/,'').replace(/\s+$/,'');
	},
	
	doErrorAlert : function(msg){
	    var message = "We're sorry, an error has occurred on this page. Please try your submission again.\nIf this problem persists, please contact ala6@compassbank.com";
		alert((null == msg) ? message : msg);
	},
	
	setOpacity : function(obj,opacityMoz,opacityIE){
		if(typeof obj == 'string'){
			obj = document.getElementById(obj);
		}
		if(null != obj){
			obj.style.opacity = opacityMoz;
			obj.style.filter = 'alpha(opacity='+opacityIE+')';
		}
	},
	
	autotab : function(original,destination){
		try{
			if(typeof(original) == 'string'){ original = document.getElementById(original); }
			if(typeof(destination) == 'string'){ destination = document.getElementById(destination); }
			if (original.getAttribute&&original.value.length==original.getAttribute("maxlength")){
				destination.focus();
			}
		}
		catch(err){
			
		}
	},
	
	emailTab : function(original, destination){
		try{
			if(typeof(original) == 'string'){ original = document.getElementById(original); }
			if(typeof(destination) == 'string'){ destination = document.getElementById(destination); }
			if ( original.value.length > 0 && original.value.indexOf( "@" ) != -1 ){    
				destination.focus();
				original.value = original.value.substr( 0,original.value.length-1 );
			}  
		}
		catch(err){
			
		}
	},
	
	removeCharFromEnd : function(str, charToRemove){
	    if(str.length > 0){
			var len = str.length;
			if(str.charAt(len - 1) == charToRemove){
				return str.substring(0,len - 1);
			}
		}
		return str;
	},
	
	removeCharFromStart : function(str, charToRemove){
	    if(str.length > 0){
			var len = str.length;
			if(str.charAt(0) == charToRemove){
				return str.substring(1,len);
			}
		}
		return str;
	},
	
	isIE : function(){
	    return (navigator.appVersion.indexOf("MSIE")!=-1) ? true:false;	
	}

}
