/**--
 Copyright 2006 1fb.net Financial Services.
  
 This document may not be reproduced, distributed or used 
 in any manner whatsoever without the expressed written 
 permission of 1st Financial Bank USA.
 
 @author Frank Tong
**/

	 //Operation on checkbox groups	 
     function checkAll(checkSwitch,group){     	     	
     	checkSwitch.checked=true;     	
     	if(group!=null){
	     	if(group.length==null){
	     		group.checked=true;
	     	}else{
	     		for(i=0;i<group.length;i++){
	     			group[i].checked=true;
	     		}
	     	}
     	}
     }	
          
     //Operation on checkbox group
     function uncheckAll(checkSwitch,group){
       	checkSwitch.checked=false;
       	if(group!=null){       	
	     	if(group.length==null){
	     		group.checked=false;
	     	}else{
	     		for(i=0;i<group.length;i++){
	     			group[i].checked=false;
	     		}
	     	}
     	}
     }    
     
     //Operation on checkbox group
     function isAnyItemChecked(group){ 
     	if(group==null){
     		return false;
     	}
     	
     	if(group.length==null){
     		if(group.checked)
     			return true;
     	}else{
     		for (i=0; i<group.length; i++){           
	           	if(group[i].checked){      		
	           		return true;
	           	}
	        }
        }     	
        return false;
	}
	
	//Check if all checked
	function allChecked(group){
		if(group==null){
			return false;
		}
		
		if(group.length==null){
     		return group.checked;
     	}else{
     		for(i=0;i<group.length;i++){
     			if(group[i].checked==false){           		
		       		return false;
		       	}
     		}
     	}	
	    
	    return true;
	}
	
	//Check All changed
     function checkAllSwitch(checkSwitch,group){  		  	
		if(checkSwitch.checked)
		{			
			checkAll(checkSwitch,group);     		
		}else{
			uncheckAll(checkSwitch,group);
		}
	 }
	 
	//set check switch status
	function setCheckSwitch(checkSwitch,group){				
		if(allChecked(group)){
			checkSwitch.checked=true;
		}else{
			checkSwitch.checked=false;
		}		
	}
	
	//String utilization: get escaped string
	function getEscape(str){
 		str=replaceAll(str,/&/,"%26");
 		str=replaceAll(str,/\=/,"%3D");
 		str=replaceAll(str,/\?/,"%3F");
 		str=replaceAll(str,/\#/,"%23");
 		return str;
 	}
 	
 	//String utilization: replace all matched substring
 	function replaceAll(str,regExp,newSubStr){		
 		while(str.match(regExp)){
 			str=str.replace(regExp,newSubStr);
 		}
 		return str;
	}
	
	//-----------------refactor---------------------------
	function setCheckSwitchRefactor(switchAll, group) {
		if(hasAllChecked(group)) {
			switchAll.setProperty('checked', true);
		}
		else {
			switchAll.setProperty('checked', false);
		}
	}
	
	function checkAllSwitchRefactor(switchAll, group){  
		if(switchAll.getProperty('checked') == true)
		{			
			checkAllElement(group);     		
		}else{
			uncheckAllElment(group);
		}
	}
	
	function hasAllChecked(group) {
		var returnValue = true;
		group.each(function(ele) {
			if(ele.getProperty('disabled') == false && ele.getProperty('checked') == false) {
				returnValue = false;
			}
		});
		return returnValue;
	}
	
	function checkAllElement(group) {
		group.each(function(ele) {
			if(!ele.disabled){
				ele.setProperty('checked', true);
			}
		});
	}
	
	function uncheckAllElment(group) {
		group.each(function(ele) {
			ele.setProperty('checked', false);
		});
	}
	
	function dwrErrorHandle(element, exceptionMsg){
		if(element){
			AJAX_LOADER.errorHandle(element, exceptionMsg);
		}
		if(LOADING_BOX)LOADING_BOX.close();		
	}
	
	DWR_TIMEOUT = 60000;
	
	function dwrCallback(holder, maps){
		var params = {};
		params.errorHandler = function(errorMsg){dwrErrorHandle(holder, errorMsg)}.bind(this);
		params.timeout = DWR_TIMEOUT;
		for(key in maps){
			params[key] = maps[key];
		}
		return params
	}
	
	function hiddenElement(ele){
		if(!ele.hasClass("hidden")){
			ele.addClass("hidden");
		}
	}
	
	function displayElement(ele){
		ele.removeClass("hidden");
	}
	
	window.aboutPopup = null;
	popupCount = 0;
	function showAboutPageForTicker(event, url, windowWidth, windowHeight, scrollbars) {
		if(window.aboutPopup != null && !window.aboutPopup.closed) {
			window.aboutPopup.close();
			window.aboutPopup = null;
		}
		event = new Event(event);
		var element = event.target;
		if(element) {
			var coordinate = element.getCoordinates();					
			var leftMargin = null;
			var topMargin = null;
			if($defined(Window.screenLeft)) {
				leftMargin = coordinate.right + Window.screenLeft - windowWidth - Window.getScrollLeft();
				topMargin = coordinate.bottom + Window.screenTop + 3 - Window.getScrollTop();
			}
			else if($defined(Window.screenX)) {	
				var statusBarHeight = 0;	
				if(window.statusbar.visible) {
					statusBarHeight = 25;
				}
				var browserToolsHeight = window.outerHeight - window.innerHeight - statusBarHeight;
				leftMargin = coordinate.right + Window.screenX - windowWidth - Window.getScrollLeft();
				topMargin = coordinate.bottom + Window.screenY + 3 + browserToolsHeight - Window.getScrollTop();
			}
			if(window.webkit){
				topMargin = topMargin + 115;
			}
			if(window.opera){
				topMargin = topMargin - 74;
				leftMargin = leftMargin - 15;
			}
			if(url != null) {
				var aboutPopupName = "About";
				if(window.webkit){
					aboutPopupName = "About"+popupCount;
					popupCount ++ ;
				}
				window.aboutPopup = window.open(url,aboutPopupName,"top=" + topMargin + ",left=" + leftMargin + ",height=" + windowHeight + ",width=" + windowWidth + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=" + scrollbars + ",dependent=yes,resizable=no");     					
				window.aboutPopup.focus();
			}
		}		
	}
	window.centerPopup = null
	function popupInCenter(url, width, height, hasScroll, popupName){
		if(typeof(hasScroll) == "undefined"){
			hasScroll = "yes";
		}
		if( window.centerPopup != null && ! window.centerPopup.closed){		   
           window.centerPopup.close();
           window.centerPopup = null;
        }
		var topMargin = window.screen.height / 2 - height / 2;
      	var leftMargin = window.screen.width / 2 - width / 2;
      	
      	if(!popupName) {
      		popupName = "CenterPopup";
      	}
      	
		window.centerPopup = window.open(url,popupName,"top=" + topMargin + ",left=" + leftMargin + ",height="+height+",width="+width+",status=yes,toolbar=no,menubar=no,location=no,scrollbars="+hasScroll+",dependent=yes,resizable=no");
		window.centerPopup.focus();
	}
	
	function closePopups(){
		if(window.aboutPopup != null && !window.aboutPopup.closed) {
			window.aboutPopup.close();
			window.aboutPopup = null;
		}
		if( window.centerPopup != null && ! window.centerPopup.closed){
           window.centerPopup.close();
           window.centerPopup = null;
        }
	}
		
	function wrapElements(elements){
		elements.each(function(ele){
			ele.setStyle("word-break", "break-all");
			ele.setStyle("word-wrap", "break-word");
			ele.addClass("wordwrap");
			while(ele.getChildren().length>0){
				ele = ele.getElement('a');
			}
			if(window.gecko || window.opera || window.webkit){								
				var chars = ele.getText().trim().split("");				
				var newTxt = chars.join("<wbr></wbr>");
				ele.setHTML(newTxt);
			}
		});
	}
	
	function setCookie(name, value, expires, path){
		if(path == null){
			path = "/";
		}
        var str = name + "=" + value;
		if(expires != null) {
			var expiredate = new Date();
	        var timeStr = expiredate.getTime() + expires;
	        expiredate.setTime(timeStr);
	        str += ";expires=" + expiredate.toGMTString();
		}
		document.cookie = str + ";path=" + path;
	}
	
	function navigateToWithParams(button, url, params) {
		for(var paramName in params) {
			setCookie(paramName, params[paramName]);
		}
		if(url == 'submit') {
			var formElement = getFormElement(button);	
			if(formElement) {
				setTimeout(function() {
					formElement.submit();
					formElement = null;
				}, 0);
			}
		}
		else {
			setTimeout(function() {
				window.location.href = url;				
			}, 0);
		}
	}
	
	function getFormElement(element) {
		if(element.tagName != 'FORM') {
			return getFormElement(element.parentNode);
		}
		return element;
	}