/**
 * Rits Framework
 *
 * LICENSE
 * 
 * This source file is subject to the Rits Commercial license.
 * 
 * @copyright  2008 Rits Comunicação & Tecnologia. (http://www.rits.com.br)
 * @license    Rits Commercial License 1.0
 * @version    $Id:$
 */
 
/**
 * Form 
 *
 * @category   Rits
 * @package    Rits
 * @copyright  2008 Rits Comunicação & Tecnologia. (http://www.rits.com.br)
 * @license    Rits Commercial License 1.0
 */
Rits.Form = {

	_mask : {
		Phone: '(##) ####-####',
		Cpf: '###.###.###-##',
		Cnpj: '##.###.###/####-##',
		Cep: '##.###-###',
		Date: '##/##/####',
		Time: '##/##/#### ##:##:##'
	},
	
	/**
	 * Submits a form
	 *
	 * @param Object form
	 * @param string action	 
	 */
	submit: function(form, action) {		
		if(action) {
			form.action = action;
		}		
		form.submit();
	},
	
	/**
	 * Cancels a form
	 */
	cancel: function() {
		if (window.history.length) {
			history.back();
		} else if (window.opener && !window.opener.closed) {
			self.close();
		}
	},
	
	/**
	 * Modifies a url to search parameters of the form
	 *
	 * @param Object form
	 * @param string url
	 * @param string field_text
	 * @param string field_select
	 * @param string field_checkbox
	 */
	search: function(form, url, field_text, field_select, field_checkbox, field_chkarray) {
		
		// url
		if(!url) {
			url = location.href;
		}
		
		url = url.replace(/#$/, '');
		
		// fields
		if(!field_text) {
			field_text = 'SEARCH_TEXT[]';
		}
		if(!field_select) {
			field_select = 'SEARCH_SELECT[]';
		}
		if(!field_checkbox) {
			field_checkbox = 'SEARCH_CHECKBOX[]';
		}
		if(!field_chkarray) {
			field_chkarray = 'SEARCH_CHKARRAY[]';
		}
		
		var stxt = form.elements[field_text];
		var ssel = form.elements[field_select];
		var schk = form.elements[field_checkbox];				
		var schkarray = form.elements[field_chkarray];				
		
		
		if(stxt) {
			if(!stxt.length) {
				url = Rits.Util.modifyUrlParameter('stxt1', stxt.value, url, true);
			} else {
				for(var i=0; i<stxt.length; i++) {
					url = Rits.Util.modifyUrlParameter('stxt'+(i+1), stxt[i].value, url, true);
				}
			}			
		}
		if(ssel) {
			if(ssel.selectedIndex != undefined) {
				url = Rits.Util.modifyUrlParameter('ssel1', ssel.options[ssel.selectedIndex].value, url, true);
			} else {
				for(var i=0; i<ssel.length; i++) {
					url = Rits.Util.modifyUrlParameter('ssel'+(i+1), ssel[i].options[ssel[i].selectedIndex].value, url, true);					
				}
			}
		}
		
		if(schk) {
			if(!schk.length) {				
				url = Rits.Util.modifyUrlParameter('schk1', (schk.checked ? schk.value : ''), url, true);
			} else {
				for(var i=0; i<schk.length; i++) {
					url = Rits.Util.modifyUrlParameter('schk'+(i+1), (schk[i].checked ? schk[i].value : ''), url, true);
				}
			}			
		}
		if(schkarray) {
			if(!schkarray.length) {				
				url = Rits.Util.modifyUrlParameter('schkarray', (schkarray.checked ? schkarray.value : ''), url, true);
			} else {
				var chk = '';
				for(var i=0; i<schkarray.length; i++) {
					if(schkarray[i].checked) {
						if(chk.length) {
							chk += ',';
						}
						chk += schkarray[i].value;
					}
				}				
				url = Rits.Util.modifyUrlParameter('schkarray', chk, url, true);
			}			
		}
		
		url = Rits.Util.modifyUrlParameter('offset', '', url, true);

		location.href = url;
	},

	/**
	 * Modifies a url to search parameters of the form
	 *
	 * @param Object form
	 * @param string url
	 */
	searchDefault: function(form, url) {

		// url
		if(!url) {
			url = location.href;
		}
		
		url = url.replace(/#$/, '');
		
		// fields
		field_text = 'SEARCH';
		
		var stxt = form.elements[field_text];
		
		if(stxt) {
			url = Rits.Util.modifyUrlParameter('search', stxt.value, url, true);
		}

		location.href = url;
	},
	
	/**
	 * Modifies url to order parameter
	 * 
	 * @param string name	 
	 */
	orderby: function(name) {
		var url = location.href;
		
		Rits.Util.modifyUrlParameter('order', escape(name), url, false);		
	},
	
	/**
	 * Modifies url to limit parameter
	 *
	 * @param Object field
	 */
	limit: function(field) {
		var limit = field.options[field.selectedIndex].value;
		var url = location.href;
	
		Rits.Util.modifyUrlParameter('limit', limit, url, false);
	},
	
	/**
	 * Applies action to form
	 *
	 * @param Object form
	 * @param string checkbox
	 * @param string url
	 * @param string confirmation
	 */
	applyAction: function(form, checkbox, url, confirmation) {
		
		if(checkbox.length) {
			if(!this.checkboxAnyChecked(form, checkbox)) {
				alert(Rits.Language.get('util.record.check'));
				return;
			}
		}
		if(confirmation) {
			if(confirm(confirmation)) {
				this.submit(form, url);
			}
		} else {
			this.submit(form, url);
		}
	},
	
	/**
	 * Verifies if any checkbox is checked
	 * 
	 * @param Object form
	 * @param string name
	 * @return boolean
	 */
	checkboxAnyChecked: function(form, name) {
		var field = form.elements[name];
		if(!field) {
			return false;
		}
		if(!field.length) {
			if(field.checked) {
				return true;
			}
		} else {
			for(var i=0; i<field.length; i++) {
				if(field[i].checked) {
					return true;
				}
			}
		}
		return false;
	},
	
	/**
	 * Change all checkboxes to some value
	 *
	 * @param Object form	 
	 * @param string name
	 * @param Object check
	 */
	checkboxChangeAll: function(form, name, check) {
		var field = form.elements[name];		
		if(!field) {
			return false;
		}
		if(!field.length) {
			field.checked = check.checked;
		} else {
			for(var i=0; i<field.length; i++) {
				field[i].checked = check.checked;
			}
		}
	},
	
	/**
	 * Change all checkboxes to some value updating array_cb
	 *
	 * @param Object form	 
	 * @param string name
	 * @param Object check
	 */
	checkboxChangeAllArrayCb: function(form, name, check) {
		var field = form.elements[name];		
		if(!field) {
			return false;
		}
		if(!field.length) {
			field.checked = check.checked;
		} else {
			for(var i=0; i<field.length; i++) {
				field[i].checked = check.checked;
				atualizaCb(field[i].value, check.checked);
			}
		}
	},
		
	/**
	 * Set a masked input
	 * 
	 * @param Object input
	 * @param string mask
	 */
	setMask: function(input, mask) {		
		YAHOO.util.Event.addListener(input, 'keypress', Rits.Form.applyMask, mask);		
	},
	
	/**
	 * Applies a mask to an input
	 * 
	 * @param event event
	 * @param string mask
	 */
	applyMask: function(event, mask) {
		
		var key = Rits.Form.getKey(event);

		if(Rits.Form.isPrintable(key)) {

			var character = String.fromCharCode(key);
			var temp = this.value + character;
			var position = temp.length - 1;

			var ok = true;

			if(position < mask.length) {
				if(mask.charAt(position) == '#') {
					if(!(character >= '0' && character <= '9')) {
						ok = false;
					}
				} else if(mask.charAt(position) == 'A') {
					if(!((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'))) {
						ok = false;
					}
				} else if(mask.charAt(position) == 'B') {
				} else {
					temp = this.value;
					while(position < mask.length) {
						if(mask.charAt(position) == '#') {
							if(!(character >= '0' && character <= '9')) {
								ok = false;
							}
							break;
						} else if(mask.charAt(position) == 'A') {
							if(!((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z'))) {
								ok = false;
							}
							break;
						} else if(mask.charAt(position) == 'B') {
							break;
						} else {
							temp += mask.charAt(position);
						}
						position++;
					}

					temp += character;
				}

				position++;

				while(position < mask.length) {
					if(mask.charAt(position) == '#') {
						break;
					} else if(mask.charAt(position) == 'A') {
						break;
					} else if(mask.charAt(position) == 'B') {
						break;
					} else {
						temp += mask.charAt(position);
					}
					position++;
				}

			} else {
				ok = false;
			}

			if(ok) {
				this.value = temp;
			}
	
			YAHOO.util.Event.stopEvent(event);
		}

	},
	
	/**
	 * Sets a limit to an input
	 *
	 * @param Object input
	 * @param int limit
	 * @param Object counter
	 */
	setLimit: function(input, limit, counter) {			
		YAHOO.util.Event.addListener(input, 'keyup', Rits.Form.applyLimit, {limit: limit, counter: counter});
		YAHOO.util.Event.addListener(input, 'keydown', Rits.Form.applyLimit, {limit: limit, counter: counter});		
	},
	
	/**
	 * Applies a limit to an input
	 *
	 * @param event event
	 * @param int limit
	 * @param Object counter
	 */
	applyLimit: function(event, config) {
				
		if(config.limit < this.value.length) {
			this.value = this.value.substring(0, config.limit);			
		}
		config.counter.value = config.limit - this.value.length;		
	},
	
	/**
	 * Get the key code associated to the event
	 *
	 * @param event e
	 */
	getKey: function(e) {
		return window.event ? window.event.keyCode : e ? e.which : 0;		
	}, 
	
	/**
	 * Returns if the key is printable
	 * 
	 * @param int key
	 */
	isPrintable: function(key) {
		return ( key >= 32 && key < 127 );
	}
};

