var JbbUtils = {};

JbbUtils.Request = {
	getContextPath: function(){
		//var set by JSP
		return __request_context_path;
	}
};
	
JbbUtils.String = {
	regExpBeginning: /^\s+/,
	regExpEnd: /\s+$/,
	 
	trim: function(aString) {
	    return aString.replace(this.regExpBeginning, "").replace(this.regExpEnd, "");
	},
	
	ltrim: function(aString) {
	    return aString.replace(this.regExpBeginning, "");
	},
	 
	rtrim: function(aString) {
	    return aString.replace(this.regExpEnd, "");
	} 
};
	
JbbUtils.String.trim.bind(JbbUtils.String);
JbbUtils.String.ltrim.bind(JbbUtils.String);
JbbUtils.String.rtrim.bind(JbbUtils.String);
	
JbbUtils.Measure = {
	getScrollBarWidth: function(){
		if(!this.scrollBarWidth){
	        var inner = new Element('p').setStyles('width: 100%; height: 200px;');
	        var outer = new Element('div')
	            .adopt(inner)
	            .setStyles('position: absolute; visibility: hidden; width: 200px; height: 150px; overflow: hidden')
	            .injectInside(document.body);
	        var w1 = inner.offsetWidth;
	        outer.style.overflow = 'scroll';
	        var w2 = inner.offsetWidth;
	        if (w1 == w2) w2 = outer.clientWidth;  
	        outer.remove();                               
	        this.scrollBarWidth = (w1 - w2);
		}
		return this.scrollBarWidth;
	}
};
JbbUtils.Measure.getScrollBarWidth.bind(JbbUtils.Measure);
	
JbbUtils.Forms = {
	
	/*
	** Saves the user immputs of a form in an array.
	** The array has the folowing structure [[formElementName, inputValue],[...,...], ...]
	** In the case of radio buttons the input value is the value of the checked element.
	** In the case of checkboxes the value is checkBox.checked
	*/
	saveUserInputs: function(formToSave){
		var form = $(formToSave);
		var savedFormData = $H();
		$A(form.getFormElements()).each(function(el){
			el = $(el); 
			var elData = null;
			switch(el.getTag()){
				case 'input': 
					switch(el.type.toLowerCase()){
						case 'text' :
						case 'password':
						case 'hidden':
							elData = el.value; 
							break;
						case 'checkbox':
							elData = el.checked;
							break;
						case 'radio':
							if(el.checked){
								elData = el.value;
							}
							break;
					}
					break;
				case 'textarea':
					elData = el.value;
					break;
			}
			if(elData !== null || savedFormData.get(el.name) === null){
				savedFormData.set(el.name, elData);
			}
		});
		return savedFormData;
	},
	
	/*
	** Restores in a form the user imputs saved by saveUserInputs().
	** for data should have the structure described in saveUserInputs().
	*/
	restoreUserImputs: function(formData, formToRestore){
		var form = $(formToRestore);
		formData.each(function(elData, elName){
			var el = form[elName];
			
			var injectValue = function(){
				switch(el.getTag()){
					case 'input': 
						switch(el.type.toLowerCase()){
							case 'text' :
							case 'password':
							case 'hidden':
								el.value = elData; 
								break;
							case 'checkbox':
								el.checked = elData;
								break;
							case 'radio':
								if(elData === el.value){
									el.checked = true;
								}
								else{
									el.checked = false;
								}
								break;
						}
						break;
					case 'textarea':
						el.value = elData;
						break;					
				}
			};
			if($type(el) === 'element'){
				el = $(el);
				injectValue();
			}
			else{
				$A(el).copy().each(function(colEl){
					el = $(colEl);
					injectValue();
				});
			}
		});
	} 
};
	
JbbUtils.Scripts = {
	/*
	** Evaluates the script embeded in an element.
	** Does not eval js files pointed by src in scripts tag.
	*/
	evalScripts: function(element){
		var script;
		var scripts = [];
		var htmlText = element.innerHTML;
		var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
		
		if($type($(element)) === 'element'){
			while ((script = regexp.exec(htmlText))) {
				scripts.push(script[1]);
			}
			scripts = scripts.join('\n');
			if(window.execScript) {
				 window.execScript(scripts);
			}
			else{
				 window.setTimeout(scripts, 0);
			}
		}
	}
};
	
JbbUtils.JSON = {
 	handleRedirectResponse: function(responseText) {
 		var redirect = Json.evaluate(responseText, true);
 		if(redirect !==null && $type(redirect.url) === 'string'){
 			if(!redirect.modal){
 				window.location = redirect.url;
 			}
 			else{
 				AjaxBox.click({href: redirect.url, rel : redirect.modalWindowWidth});
 			}
 		}
 	}
};
	
JbbUtils.Cookies = {
	takesCookies : function(){
		var testCookieName = 'takescookies';
    	Cookie.set(testCookieName, 'true');
    	var takesCookies = $chk(Cookie.get(testCookieName)) ? true : false;
		Cookie.remove(testCookieName);	
		return takesCookies;
    }

}



