
Form = function (util) 

{

	this.form_id;

	this.util	  = util;

	/**  
	 * @method setFormId 
	 * @param  string form_id
	 * @return void
	 * @desc:   
	 */	
	this.setFormId = function (form_id)
	{
		this.form_id = form_id;
	}


	/**  
	 * @method getForm 
	 * @param  string form_id
	 * @return object Form
	 * @desc:   
	 */	
	this.getForm = function (form_id)
	{
		return document.getElementById(form_id);
	}

	/**  
	 * @method campoFocus 
	 * @param  string id
	 * @return void
	 * @desc:   
	 */	
	this.campoFocus = function(id)
	{
		if(document.getElementById(id))
			document.getElementById(id).focus();	
	}


	/**  
	 * @method getFields 
	 * @param  void
	 * @return Array form.fields
	 * @desc:   
	 */	
	this.getFields = function()
	{
		return document.getElementById(this.form_id).fields;
	} 


	/**  
	 * @method getValueField
	 * @param  string name
	 * @return mixed  value;
	 * @desc:   
	 */	
	this.getValueField = function(name)
	{
		return document.getElementById(name).value;
	} 


	/**  
	 * @method getValueSelect
	 * @param  string name
	 * @return mixed  value;
	 * @desc:   
	 */	
	this.getValueSelect = function(id)
	{
		return document.getElementById(id)[document.getElementById(id).selectedIndex].value;
	}


	/**  
	 * @method setFieldValue  
	 * @param  name, value
	 * @return void
	 * @desc:   
	 */	
	this.setFieldValue = function(name, value)
	{
		document.getElementById(name).value = value;
	}


	/**  
	 * @method listFields
	 * @param  void
	 * @return void
	 * @desc:   
	 */	
	this.listFields = function()
	{
		var str = "";
		var num_fields = document.getElementById(this.form_id).fields.length;

		for(var i = 0; i < num_fields; i++)
		{
			str += "";
		}
	} 


	/**  
	 * @method listFields
	 * @param  void
	 * @return void
	 * @desc:   
	 */	
	this.displayTabForm = function(prefix, indice, num_tabs) 
	{
		for(i = 1; i <= num_tabs; i++)
		{
			var id  = "form_tab_" + prefix + "_" + i; 

			if(indice == i)
				document.getElementById(id).style.visibility  =  "visible";
			else 
				document.getElementById(id).style.visibility  =  "hidden";
		}

	}


   /**  
	* @method selecionarOpcao
	* @param  select_id, opc
	* @return void
	* @desc:  Funcao para setar o option do select com o id passado
	* param string select_id, mixed opc
	*/
	this.selecionarOpcao = function(select_id, opc)
	{
		var select_ 	= document.getElementById(select_id);
		var num_options = select_.options.length;

		for(var i = 0; i < num_options; i++)
		{
			// verifica se o indice o select_id eh igual a opc
			// se o teste for igual a true, breca o loop e seta para 
			// true a opcao do select. 
			if(select_.options[i].value == opc)
			{
				select_.options[i].selected = true;		
				break;
			}
		}
	}


	/**  
	* @method selecionarOpcao
	* @param  select_id, opc
	* @return void
	* @desc:  Funcao para setar o option do select com o id passado
	* param string select_id, mixed opc
	*/
	this.removerOpcao = function(select_id, opc)
	{
		var select_ 	= document.getElementById(select_id);
		var num_options = select_.options.length;

		for(var i = 0; i < num_options; i++)
		{
			// verifica se o indice o select_id eh igual a opc
			// se o teste for igual a true, breca o loop e seta para 
			// true a opcao do select. 
			if(select_.options[i].value == opc)
			{
				select_.remove(select_.options[i]);		
				break;
			}
		}
	}


	/**  
	* @method disableOpcoes
	* @param  select_id
	* @return void
	* @desc:  Funcao para desabilitar o option do select com o id passado
	* param string select_id
	*/
	this.disableOpcoes = function(select_id)
	{
		var select_ 	= document.getElementById(select_id);

		var num_options = select_.options.length;

		for(var i = 0; i < num_options; i++)
		{
			select_.options[i].selected = false;		
		}
	}


	/* 
	
	<script language="Javascript">
	function AddOpcao(objCombo, strValue, strDescricao, defaultSelected, selected){
		var Nova_Opcao = new Option(strDescricao, strValue, defaultSelected, selected);
		objCombo.options[objCombo.length] = Nova_Opcao;
	}
	
	function DelOpcao(objCombo, strIndice){
		objCombo.options.remove(strIndice);
	}
	</script>
	*/
	
	
   /**  
	* @method preencheSelect
	* @param  select_id
	* @return void
	* @desc:  Funcao para preencher um combo com options(vetor json)
	* param   string select_id
	*/
	this.preencheSelect = function(id, result, first_option)
	{
		var c = document.getElementById(id);
		var campos = eval(result);
		
	 	 //limpa o select           
		while(c.options.length > 0)
			c.options[0] = null;

		if(first_option != null)	
			c.options[0] = new Option(first_option, "");
		else 
			c.options[0] = new Option(" --- ", "");
			
		for (x in campos)
		{	
			y = 0;

			for(j in campos[x])
			{	
				if(y == 1)
					texto = this.util.url_decode(campos[x][j]);
				else 
					value = this.util.url_decode(campos[x][j]);
				++y;
			}	
			
			c.options[c.options.length] = new Option(texto, value);	
			y = 0;
		}		
	}



	/* Implementar method */
	this.setCheckBox = function()
	{
		
	}


   /**  
	* @method setParametersForm
	* @param  string id, mixed data
	* @return void
	* @desc:  Funcao generica para setar os parametros de um form.
	* Obs : Para funcao funcionar, os campos dos formularios devem ser iguais aos dos objetos retornados. 
	*/
	this.setObjToForm = function(id, obj)
	{
		if(obj != "" && obj != "undefined")
		{
			this.zerarForm(id);

			var form		  = document.getElementById(id); // simplificacao da chamada ao form
			var num_elementos = form.length; // numero de campos do formulario

			// Looping para setar os valores do obj form
			for(var i = 0; i < num_elementos; i++)
			{
				
				
				if(form.elements[i].name != "" && form.elements[i].type != "button" && form.elements[i].type != "submit" &&
				   form.elements[i].name.indexOf('[]') == -1)
				{
					// Constroi a variavel a partir do nome do form.  
					var tmp_val  = eval("obj." + form.elements[i].name); // 	
					// Checa se o valor nao eh indefinido, e seta o campo do form se nao for.
					if(tmp_val != "undefined" && tmp_val != "" && tmp_val != null) // criar funcao para checagem de valor vazio
					{ 	
						// Definir regra para setar o form. tipo text, textarea, button, checkbox, option e radio  
						//form.elements[i].type = "";
						// selecionarOpcao(select_id, opc)   	
						// Checagem de tipo do campo do form
						if(form.elements[i].type == "select-one" || form.elements[i].type == "select") { // select
							this.selecionarOpcao(form.elements[i].name, tmp_val);						
							/*
							if(this.removerOpcao(select_id, opc))
								document.getElementById(id)
							*/
						
						}
						else if(form.elements[i].name.indexOf("data") != -1 && form.elements[i].name.indexOf("extenso") == -1) {
							form.elements[i].value = this.util.converteDataAB(tmp_val);	 // converter data
						}
						
						else if(form.elements[i].type == "checkbox" || form.elements[i].type == "radio")  {
							if(form.elements[i].value == this.util.url_decode(tmp_val))
								form.elements[i].checked="checked";
						}
						else  {
							form.elements[i].value = this.util.url_decode(tmp_val);	
						}
					
						
					}
					
					else {
						if(form.elements[i].type != "checkbox" && form.elements[i].type != "radio")
							form.elements[i].value = ""; 
					}
					/*
					if(form.elements[i].type != "button") // verificar tipo e setar checkbox, e radio button
						form.elements[i].value = ""; 
		
					//////////
					<input type="checkbox" checked>
					////////////
					<input type="radio" checked>
					*/
				}
			}
		}
	}



	/**  
	* @method  zerarForm 
	* @param  string id
	* @return void
	* @desc: Funcao generica para setar os parametros de um form.
	* Obs : Para funcao funcionar, os campos dos formularios devem ser iguais aos dos objetos retornados. 
	*/
	this.zerarForm = function (id)
	{
		var num_elementos = document.getElementById(id).length; // numero de campos do formul?rio
		var form		  = document.getElementById(id); // simplificaao da chamada ao form

		// Looping para apagar todos os valores de um formulario
		// incluir apagar select, checkbox e etc
		for(var i = 0; i < num_elementos; i++)
		{
			if(form.elements[i].type != "button" && form.elements[i].type != "submit" && 
			   form.elements[i].type != "checkbox" && form.elements[i].type != "radio") {
				form.elements[i].value = ""; 
			}
		}
	}


	/**  
	* @method  alterFieldsForm 
	* @param  form_id, focus_id, normal, destaque
	* @return string
	* @desc:  Metodo para alterar o estilo dos campos do formulario form_id conforme o evento.  
	*/
	this.alterFieldsForm = function(form_id, focus_id, normal, destaque, controle)
	{
		var x = document.getElementById(form_id);
		
		if (!x) 
			return;

		for (var i = 0; i < x.length; i++)
		{
			var text_form = document.getElementById(x[i].id);	

			if(text_form.type != "button" && text_form.type != "submit")
			{
				text_form.onmouseover = function () { 
								   	  controle.form.changeForm(this, destaque); 
								   }

				text_form.onmouseout  = function () { 
									  controle.form.changeForm(this, normal); 
								   }

				text_form.onfocus 	 = function () { 
								      controle.form.changeForm(this, destaque); 
							       }

				text_form.onblur 	 = function () { 
									  controle.form.changeForm(this, normal); 
								   }
			}
		}

		document.getElementById(focus_id).focus();	
	}


	/**  
	 * @method  changeForm 
	 * @param  void
	 * @return string
	 * @desc:   
	 */	
	this.changeForm = function(obj, newClass) 
	{
		obj.className = newClass;
	}

	

	/**  
	* @method  setNameInput
	* @param  form_id, focus_id, normal, destaque
	* @return string
	* @desc:  Este medoto tem como objetivo diminuir o numero de tags html nos formularios, neste
	* especificamento, o atributo name do form, nao sendo necessario informa-lo diretamente.  
	*/
	this.setNameInput = function(form_id)
	{
		var form   = document.getElementById(form_id);
		var length = form.length;
		form.method = "post"; // temp

		for(i = 0; i < length; i++) {
			form.elements[i].name = form.elements[i].id;	
		}
	} 


	/**  
	* @method  setNameInput
	* @param  form_id, focus_id, normal, destaque
	* @return string
	* @desc:  Este medoto tem como objetivo diminuir o numero de tags html nos formularios, neste
	* especificamento, o atributo name do form, nao sendo necessario informa-lo diretamente.  
	* <a href="javascript:addInput('form_chave', 'campo_2', 'text', '30', 'Campo 2', 'com');">Criar Campo</a>
	*/
	this.addInput = function(lista_id, input_id, tipo, size, rotulo, classe)
	{
		var input = "";
		input += "<br id='br_" + input_id + "' />\n";
		input += "<label for='" + input_id + "' id='label_" + input_id + "'> " + rotulo + " </label>\n";
		input += "<input id='" + input_id + "' name='" + input_id + "' type='" + tipo +"'";
		input += "size='" + size + "' class='" + classe + "' />\n";

		document.getElementById(lista_id).innerHTML = input;	
	}

};