/**
 * com/teamfindr/utils.js: A module of utility functions
 *
 * This module creates a single global symbol named com.teamfindr.utils if it
 * does not already exist. All utility functions are placed in the 
 * com.teamfindr.utils namespace.
 *
 * (For full comments see the util.js full comments file)
 **/
var com;
if (!com) com = {};
else if (typeof com != "object")
    throw new Error("com already exists and is not an object");
if (!com.teamfindr) com.teamfindr = {}
else if (typeof com.teamfindr != "object")
    throw new Error("com.teamfindr already exists and is not an object");
if (com.teamfindr.utils)
    throw new Error("com.teamfindr.utils already exists");

com.teamfindr.utils = {
	enterOnClick: 
		function(event, linkElementId) {
     		if (event.keyCode == 13) {
  				document.getElementById(linkElementId).onclick();
  			} else {
  				return true;
  			}
  		},
  		
  	enterSubmit:
  		function(event, formElementId) {
     		if (event.keyCode == 13) {
  				document.getElementById(formElementId).submit();
  			} else {
  				return true;
  			}
  		},
  		
  	resetSelector:
  		function(elementId, resetValue) {
  			document.getElementById(elementId).value = resetValue;
  		},
  	
  	scrollWindowFlag: false,
  	
  	searchReady: true,
  	
  	scrollWindow:
  		function(newYValue) {
  			if (com.teamfindr.utils.scrollWindowFlag) {
  				window.scrollTo(window.scrollX, newYValue);
  				com.teamfindr.utils.scrollWindowFlag=false;
  			}
  		},
  	
	displayTab:
		function (newTabId, oldTabId) {
			document.getElementById('tab'+oldTabId+'Focus').style.display = "none";
   			document.getElementById('tab'+oldTabId+'Content').style.display = "none";
   			document.getElementById('tab'+newTabId+'Link').style.display = "none";
   			
   			document.getElementById('tab'+newTabId+'Focus').style.display = "block";
   			document.getElementById('tab'+newTabId+'Content').style.display = "block";
   			document.getElementById('tab'+oldTabId+'Link').style.display = "block";
   		},
		
  	wrapOnClick:
  		function(link, functionName, params) {
  			if (link.onclick == com.teamfindr.utils.onClickWrapperFunctions[functionName]) {
  				return;
  			}
              
  			com.teamfindr.utils.onClickFunctions[functionName] = link.onclick;
  			link.onclick = com.teamfindr.utils.onClickWrapperFunctions[functionName];
  			com.teamfindr.utils.wrapperFunctionParams[functionName] = params;
  		},
  	
  	onClickFunctions: new Array(),
  	
  	onClickWrapperFunctions: new Array(),
  	
  	wrapperFunctionParams: new Array(),
	
	confirmDelete: 
		function() {
    		if (confirm(com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.confirmDelete'].confirmMessage)) {
    			return com.teamfindr.utils.onClickFunctions['com.teamfindr.utils.confirmDelete']();
    		} else {
    			return false;
    		} 
    	},
    
    checkBoxSelected:
    	function () {
    		if (document.getElementById(com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.checkBoxSelected'].checkBoxElementId).checked == true) { 
  	  			return com.teamfindr.utils.onClickFunctions['com.teamfindr.utils.checkBoxSelected']();
  	  		} else {
  	  			alert(com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.checkBoxSelected'].errorMessage);
    			return false;
    		}
    	},
    	
    showOrHideGMap:
    	function (selectorElementId, gMapWrapperId, gMapElementId, minShowValue) {
    		if (document.getElementById(selectorElementId).value >= minShowValue) { 
				com.teamfindr.utils.showGMap(gMapWrapperId, gMapElementId);
			} else {
				com.teamfindr.utils.hideGMap(gMapWrapperId, gMapElementId);
			}
     	},
     
    toggleGMapVisibility:
    	function (gMapWrapperId, gMapElementId) {
    		var wrapper = document.getElementById(gMapWrapperId);
    		
    		if (wrapper.style.height == '1px') {
    			com.teamfindr.utils.showGMap(gMapWrapperId, gMapElementId);	
    		} else {
				com.teamfindr.utils.hideGMap(gMapWrapperId, gMapElementId);
			}
     	},
    
    showGMap:
    	function (gMapWrapperId, gMapElementId) {
    		var wrapper = document.getElementById(gMapWrapperId);
    		var map = document.getElementById(gMapElementId);
    		
        	wrapper.style.height = '385px';
        	wrapper.style.marginTop = '10px';
        	map.style.top = '0px';
     	},
     
    hideGMap:
    	function (gMapWrapperId, gMapElementId) {
    		var wrapper = document.getElementById(gMapWrapperId);
    		var map = document.getElementById(gMapElementId);
    		
    		map.style.top = '-1500px';
    		wrapper.style.marginTop = "0px";
        	wrapper.style.height = '1px'; 
     	},
     	 
    showOrHideElement:
    	function (selectorElementId, elementId, minShowValue) {
    		if (document.getElementById(selectorElementId).value >= minShowValue) { 
				com.teamfindr.utils.showElement(elementId);
			} else {
				com.teamfindr.utils.hideElement(elementId);
			}
     	},
    
    showOrHideElementInline:
    	function (selectorElementId, elementId, minShowValue) {
    		var selector = document.getElementById(selectorElementId);
    		
    		if (selector != null) {
    			if (selector.value != null &&
    				selector.value >= minShowValue) {
					com.teamfindr.utils.showElementInline(elementId);
				} else {
					com.teamfindr.utils.hideElement(elementId);
				}
			}
     	},
    
    showOrHideAvailability:
    	function (availabilityElementId, elementId) {
    		if (document.getElementById(availabilityElementId).value == 'I') { 
				com.teamfindr.utils.showElement(elementId);
			} else {
				com.teamfindr.utils.hideElement(elementId);
			}
     	},
     		
    showElement:
    	function (elementId) {
    		var element = document.getElementById(elementId);
    		
    		element.style.display = 'block';
     	},
    
    showElementInline:
    	function (elementId) {
    		var element = document.getElementById(elementId);
    		
    		element.style.display = 'inline';
     	},
     	
    hideElement:
    	function (elementId) {
    		var element = document.getElementById(elementId);
    		
    		element.style.display = 'none';
     	},
     	     	
    toggleElementVisibility:
    	function (elementId) {
    		var element = document.getElementById(elementId);
			
    		if (element.style.display == 'none') {
				element.style.display = 'block';
			} else if (element.style.display == 'block') {
				element.style.display = 'none';
			}
     	},
     	
    showClubTab:
    	function () {
    		com.teamfindr.utils.hideElement('playerLclNatIntlBlock');
    		com.teamfindr.utils.hideElement('natlLocationInput');
    		com.teamfindr.utils.hideElement('intlLocationInput');
    		com.teamfindr.utils.hideElement('playerSearchLinkBlock');
    		
    		com.teamfindr.utils.displayTab(1,2);
    		
    		com.teamfindr.utils.showElement('lclLocationInput');
    		com.teamfindr.utils.showOrHideGMap('searchForm:lclRange', 'mapLocationInput', 'mapBlock', 1);
    		com.teamfindr.utils.showElement('clubBasicSearchLinkBlock');
     	},
     	
    showPlayerTab:
    	function () {
    		com.teamfindr.utils.hideElement('clubBasicSearchLinkBlock');
    		com.teamfindr.utils.hideElement('clubAdvSearchLinkBlock');
    		com.teamfindr.utils.hideElement('clubAdvInput');
    		
    		com.teamfindr.utils.displayTab(2,1);
    		
    		com.teamfindr.utils.showElement('playerLclNatIntlBlock');
    		document.getElementById('searchForm:availability').value = 'L';
    		com.teamfindr.utils.showElement('playerSearchLinkBlock');
     	},
     
    toggleAdvOptions:
    	function () {
    		com.teamfindr.utils.toggleElementVisibility('clubAdvInput');
    		com.teamfindr.utils.toggleElementVisibility('clubBasicSearchLinkBlock');
    		com.teamfindr.utils.toggleElementVisibility('clubAdvSearchLinkBlock');
    	},
    
    changeAvailabilityOptions:
    	function () {
    		var availability = document.getElementById('searchForm:availability').value;
    		
    		if (availability == 'N') {
    			com.teamfindr.utils.hideElement('lclLocationInput');
    			com.teamfindr.utils.hideGMap('mapLocationInput', 'mapBlock');
    			com.teamfindr.utils.hideElement('intlLocationInput');
    			
    			com.teamfindr.utils.showElement('natlLocationInput');  			
    		} else if (availability == 'I') {
    			com.teamfindr.utils.hideElement('lclLocationInput');
    			com.teamfindr.utils.hideGMap('mapLocationInput', 'mapBlock');
    			com.teamfindr.utils.hideElement('natlLocationInput');
    			
    			com.teamfindr.utils.showElement('intlLocationInput');
    		} else {
    			com.teamfindr.utils.hideElement('natlLocationInput');
    			com.teamfindr.utils.hideElement('intlLocationInput');
    			
    			com.teamfindr.utils.showElement('lclLocationInput');
    			com.teamfindr.utils.showOrHideGMap('searchForm:lclRange', 'mapLocationInput', 'mapBlock', 1);    		
    		}		
    	},
     	
    submitPoll: false,
    
    pollCount: null,
    
    limitPoll:
    	function (pollCountValue, busyPageURL) {
    		if (com.teamfindr.utils.pollCount == null) {
    			com.teamfindr.utils.pollCount = pollCountValue;
    			
    			return true;
    		} else if (com.teamfindr.utils.pollCount <= 0) {
    			window.location = busyPageURL;
    			
    			return false;
    		} else {
    			com.teamfindr.utils.pollCount--;
    			
    			return true;
    		}
    	},
    	 
    textAreaCharLimit:
		function () {
			var textArea = document.getElementById(com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.textAreaCharLimit'].textAreaElementId);
			var errorElement = document.getElementById(com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.textAreaCharLimit'].errorElementId);
			var maxChars = com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.textAreaCharLimit'].maxChars;
			var errorMessage = com.teamfindr.utils.wrapperFunctionParams['com.teamfindr.utils.textAreaCharLimit'].errorMessage;
		
			if (textArea.value.length > maxChars) {
					errorMessage = errorMessage.replace('{0}', (textArea.value.length - maxChars));
					errorElement.innerHTML = errorMessage;
					return false;
			} else {
				return com.teamfindr.utils.onClickFunctions['com.teamfindr.utils.textAreaCharLimit']();
			}
		},
	
	getInternetExplorerVersion:
		function () {
			// Returns the version of Internet Explorer or a -1
			// (indicating the use of another browser).
			{
				var rv = -1; // Return value assumes failure.
				if (navigator.appName == 'Microsoft Internet Explorer')
				{
					var ua = navigator.userAgent;
					var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
				
					if (re.exec(ua) != null)
      					rv = parseFloat( RegExp.$1 );
  				}
  				return rv;
			}
		}
};

(function() {
	com.teamfindr.utils.onClickWrapperFunctions['com.teamfindr.utils.confirmDelete'] = com.teamfindr.utils.confirmDelete;
	com.teamfindr.utils.onClickWrapperFunctions['com.teamfindr.utils.checkBoxSelected'] = com.teamfindr.utils.checkBoxSelected;
	com.teamfindr.utils.onClickWrapperFunctions['com.teamfindr.utils.textAreaCharLimit'] = com.teamfindr.utils.textAreaCharLimit; 
})();
