/**
 * @author Todd.Baker
 * This is the ChaCha Utilities namespace class.
 */
var ChaCha = {};

ChaCha.util = function(){
	
	return {	

		// RETURNS: true if valid, false otherwise
		isValidZipCode: function(z){
			var reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
			return reZip.test(z);
		},
		
		// RETURNS: true if valid, false otherwise
		isValidPhoneNumber: function(p) {
			var rePhone = new RegExp(/^([2-9][0-8]\d-[2-9]\d\d-\d\d\d\d)|([2-9][0-8]\d[2-9]\d\d\d\d\d\d)$/);
			return rePhone.test(p);
		},
		
		// RETURNS: true if valid, false otherwise
		isValidEmailAddress: function(a) {
			var reEmail = new RegExp(/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/);
			return reEmail.test(a);
		},
		
		// RETURNS: true if valid, false otherwise
		isValidDate: function(d){
			var reDate = new RegExp(/(?:0[1-9]|[12][0-9]|3[01])\/(?:0[1-9]|1[0-2])\/(?:19|20\d{2})/);
			return reDate.test(d);
		},
		
		isNumeric: function(v) {
			var reNumeric = new RegExp(/^\d+$/);
			return reNumeric.test(v);
		},
		
		removeWhiteSpace: function(s) {
			return s.replace(/\s/g,'');
		},
		
		trim: function(s) {
			return s.replace(/^\s+|\s+$/g,"");
		},
		
		ltrim: function(s) {
			return s.replace(/^\s+/,"");
		},
		
		rtrim: function(s) {
			return s.replace(/\s+$/,"");
		},
		
		scrubData: function(d) {
			d = d.replace(/%/g, "%25");
		//	d = d.replace(/\"/g, "\\\"");
			d = d.replace(/&quot;/, "\\\"");
		//	d = d.replace(/'/g, "\\\'");
			d = d.replace(/&apos;/g, "\\\'");
			d = d.replace(/</g, "\<");
			d = d.replace(/>/g, "\>");
			d = d.replace(/=/g, "\=");
			d = d.replace(/\+/g, "%2B");
		//	d = d.replace(/\(/g, "&#40;");
		//	d = d.replace(/\)/g, "&#41;");
			d = d.replace(/\&/g, escape("&"));
			d = d.replace(/\n/g, " ");
			d = d.replace(/\r/g, " ");
			
			return d;
		}
		
	};
	
}();

// a little wrapper around jQuery's AJAX functionality
ChaCha.ajax = function() {
	
	return {
		
		// url: the action to send the request to
		// body: the data paylod (JSON, query string, etc)
		// method: GET or POST
		// success: the onSuccess method to excute after the request
		// failure: the onFailure method to execute after the request
		// cbParams: any call back parameters we'd like to send
		sendRequest: function(url, body, method, succ, fail, cbParams) {
			jQuery.ajax({
				type: method,
				url: url,
				data: body,
				success: function(resp, success) { 
					var res = null;
					if (resp !== null && ChaCha.util.trim(resp) !== "") {
						res = JSON.parse(resp);
					} 
					ChaCha.ajax.checkForRedirect(res);
					succ.call(this, res);
				},
				failure:  function(resp, failure) { 
					var res = null;
					if (resp !== null && ChaCha.util.trim(resp) !== "") {
						res = JSON.parse(resp);
					} 
					ChaCha.ajax.checkForRedirect(res);
					fail.call(this, res);
				}		
			});
		},
		
		checkForRedirect: function(res) {
			if(res && res.redirect) {
				location.href = res.redirect;
				return;
			}
		}
	};
}();

// configurable constants for UI interactions
ChaCha.consts = function() {
	
	var answersPalletSize = 10;
	var answersAjaxPollingInterval = 30000;
	var affinityPollInterval = 47000;
	
	//return {	
	
		//ANSWERS_PALLET_SIZE: function(){
		//	return answersPalletSize;
		//},
		//ANSWERS_AJAX_POLLING_INTERNVAL: function(){
		//	return answersAjaxPollingInterval;
		//},
		//AFFINITY_POLL_INTERVAL: function(){
		//	return affinityPollInterval;
		//}
	//}
	
	return {	
		ANSWERS_PALLET_SIZE: 10,
		ANSWERS_AJAX_POLLING_INTERNVAL: 30000,
		AFFINITY_POLL_INTERVAL: 47000
	}
}();

ChaCha.events = function() {
	
	return {
		getPressedKeycode: function(e) {
			if (!e) {e = window.event;}
			if (e.keyCode) {code = e.keyCode;}
			else if (e.which) {code = e.which;}
			
			return code;
		}
	};
}();