/* JavaScript Document
Autor: Edy Segura - infoedy@gmail.com
Descrição: Objeto Form
Referência: Livro JavaScript for Web Developer, página do livro 339, página do PDF 367.
*/

var Form = {
	
	focusOnFirst: function() {
		if(document.forms.length > 0) {
			var oForm = document.forms[0];
			
			for(var i=0; i<oForm.elements.length; i++) {
				var oField = oForm.elements[i];
				
				if(typeof oField.type != "undefined" && oField.type != "hidden") {
					if(!oField.value) oField.focus();
					else continue;
					return;
				}
				
			}//fim for
		}//fim if
	},//fim focusOnFirst
	
	
	isEmpty: function(sValue) {
		var sCaractere;
		
		if(sValue == "" || sValue == null) return true;
		
		sCaractere = sValue.charAt(0);
		if((sCaractere == " ") || 
			 (sCaractere == "\t") && 
			 (sCaractere == "\n")) {
			return true;
		}
		
		return false;
	},//fim isEmpty
	
	
	getFormUrlEncodedValues: function(oForm) {
		var sParams = new String;
		var aParams = new Array;
		
		oForm = (typeof oForm == "string") ? $(oForm) : oForm;
		
		for(var i=0; i<oForm.elements.length; i++) {
			var oElement = oForm.elements[i];
			
			if(oElement.name == "" || oElement.name == undefined) continue;
      if(oElement.type == "" || oElement.type == undefined) continue;
			
			switch(oElement.type) {

				case "text"       :
				case "textarea"   :
				case "password"   :
				case "hidden"     :
				case "select-one" :
					
					sParams = oElement.name + "=" + encodeURIComponent(oElement.value);
					if(sParams) aParams.push(sParams);
					
				break;

				case "select-multiple" :
					
					for(var j=0; j<oElement.options.length; j++) {
            sParams = (oElement.options[j].selected) ? oElement.name + "=" + encodeURIComponent(oElement.options[j].value) : "";
						if(sParams) aParams.push(sParams);
          }

				break;

				case "radio"    :
				case "checkbox" :

					sParams = (oElement.checked) ? oElement.name + "=" + encodeURIComponent(oElement.value) : "";
					if(sParams) aParams.push(sParams);

				break;
			}//fim switch
		}//fim do for

		return aParams.join("&");

	}//fim getValues
	
};//fim Form

