// Sislu JavaScript Document

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}

/** Faz o submit da acao para o struts*/
function doSubmit(form,acao){
	form.elements['method'].value = acao;
	if (!document.isSubmitted) {
		document.isSubmitted = true;
		form.submit();
	}
}

function doForceSubmit(form,acao) {
	form.elements['method'].value = acao;
	form.submit();
}

function doPost(form, acao) {
	form.elements['validate'].value = true;
	var args = doPost.arguments;
	if(args.length > 2 && args.length % 2 == 0){
		form.action = form.action + "?";
		for(var i=2; i< args.length; i=i+2){
			if(i!=2) form.action = form.action + "&";
			form.action = form.action + args[i] + "=" + args[i+1];
		}
	}
	doSubmit(form,acao);
}

function doPostSkip(form, acao) {
	form.elements['validate'].value = false;
	var args = doPostSkip.arguments;
	if(args.length > 2 && args.length % 2 == 0){
		form.action = form.action + "?";
		for(var i=2; i< args.length; i=i+2){
			if(i!=2) form.action = form.action + "&";
			form.action = form.action + args[i] + "=" + args[i+1];
		}
	}
	doSubmit(form,acao);
}

function doPostAsk(form, acao, pergunta) {
	if (confirm(pergunta)) {
		doPost(form, acao);
		return true;
	}
	return false;
}

function doPostSkipAsk(form, acao, pergunta) {
	if (confirm(pergunta)) {
		doPostSkip(form, acao);
		return true;
	}
	return false;
}


function podeEnviar(form) {
	var elem = form.elements['method'];
    return (elem && trim(elem.value).length > 0)
}

function executaSeEnter(objEvent,botao,objInput){
	if(getKeyCode(objEvent) == 13){
		if(!objInput){
			botao.click();
			return true;
		}
		else if(objInput.value && trim(objInput.value).length > 0){
			botao.click();
			return true;
		}
	}
}

// Coloca o focus no primeiro campo de cada form
function focusFirstField(){
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
				document.forms[0].elements[i].focus();
				break;
	       	}
	   	}
	}
}