Date.prototype.toYYYYMMDD = function() {
  return this.getFullYear().toString() + "-" +
  ((this.getMonth()+1) < 10 ? "0" + (this.getMonth()+1).toString() : (this.getMonth()+1).toString()) + "-" +
  (this.getDate() < 10 ? "0" + this.getDate().toString() : this.getDate().toString());
}
Date.prototype.toDDMMYYYY = function() {
  return  (this.getDate() < 10 ? "0" + this.getDate().toString() : this.getDate().toString()) + "-" + ((this.getMonth()+1) < 10 ? "0" + (this.getMonth()+1).toString() : (this.getMonth()+1).toString()) + "-" + this.getFullYear().toString();
}
Date.prototype.setAuto = function(d) {
	if(!(res = d.split(/-/)) || res.length!=3) return false;
	this.setMonth(parseInt(res[1], 10)-1);
	if(res[2]>99){
		this.setDate(parseInt(res[0], 10));
		this.setYear(parseInt(res[2], 10));
	}else{
		this.setYear(parseInt(res[0], 10));
		this.setDate(parseInt(res[2], 10));
	}
	return 1;
}

function e(mess, obj) {
	alert(mess);
	if(obj) obj.focus();
	return false;
}

// Permite os campos de formularios do type=text terem um valor inicial que é apagado quando tem focus
// utilização: <input onfocus=fc(this) onfocuslost=lfc(this)>
// necessita de para cada campo ter uma chamada de initField(nome do form, nome do campo, valor inicial, obrigatorio(0|1), texto de erro)
var campos=new Array();
function fc(campo,ivalue){
	if(campos[campo.form.name]){
		var v1=campos[campo.form.name];
		for(i=0;i<v1.length;i++)if((v1[i][0]==campo.name)&&(campo.value==v1[i][1]))campo.value="";
	}
}
function lfc(campo){
	if(campos[campo.form.name]){
		var v1=campos[campo.form.name];
		for(i=0;i<v1.length;i++)if((v1[i][0]==campo.name)&&(campo.value==""))campo.value=v1[i][1];
	}
}
// Validacao de Formularios
function initField(form_name,campo,inicial,obrigatorio,texto){
	if(!campos[form_name])campos[form_name]=new Array();
	campos[form_name][campos[form_name].length]=new Array(campo,inicial,obrigatorio,texto);
	if(document.getElementById(form_name) && document.getElementById(form_name).elements[campo].value=="")document.getElementById(form_name).elements[campo].value=inicial;
}
function tstForm(form_nome){
	if(campos[form_nome]){
		var v1=campos[form_nome];
		for(i=0;i<v1.length;i++){
			c=v1[i];
			if(c[2] && (document.forms[form_nome].elements[c[0]].value=="") || (document.forms[form_nome].elements[c[0]].value==c[1]))return e(c[3],document.forms[form_nome].elements[c[0]]);
		}
	}
	return 1;
}
function submitenter(myfield,er)
{
	var keycode;
	if(window.event)keyCode=window.event.keyCode;
	else if(er)keyCode=er.which;
	else return true;
	if(keyCode == 13){
		if(tstForm(myfield.form.name))myfield.form.submit();
		return false;
	}else return true;
}

function tstData(f) {
	data=f.value;
	var res = null;
	if(!(res = data.split(/-/)) || res.length!=3) return false;
	var dia = parseInt(res[0], 10);
	var mes = parseInt(res[1], 10);
	var ano = parseInt(res[2], 10);
	if(dia.lenght>2){
		ano=dia;
		dia=parseInt(res[2], 10);
	}
	if(dia > 31) return false;
	if(mes > 12) return false;
	if(ano < 1900) return false;
	return true;
}

function tstDataInput(f){
	var tc = event.keyCode;
	if(tc==46)event.returnValue=false;
	if((tc==45) && ((f.value.length==0)||(f.value.lastIndexOf('-')==f.value.length-1))){
		event.returnValue=false;
		return;
	}
	if((tc==45 && f.value.length==1)){
		f.value="0"+f.value;
		return;
	}
	if((f.value.length==1)||(f.value.length==4)){
		if((tc==45) && (res=f.value.split(/-/)) && res.length==2){
			var dia = res[0];
			var mes = "0"+res[1];
			f.value=dia+"-"+mes+"-";
		}else{
			f.value+=String.fromCharCode(tc)+"-";
		}
		event.returnValue=false;
	}
	if(tc==45){
		count = 0;
		pos = f.value.indexOf("-");
		while ( pos != -1 ) {
		   count++;
		   pos=f.value.indexOf("-",pos+1);
		}
		if(count==2)event.returnValue=false;
	}
}

function addOption(txt,val){
	var objSel = document.f.s;
	objSel.options[objSel.options.length] = new Option(txt,val);
}

// COLOCA FOCUS
function setfocus(){
  var bFound = false;
  // for each form
  for (f=0; f < document.forms.length; f++)
  {
    // for each element in each form
    for(i=0; i < document.forms[f].length; i++)
    {
      // if it's not a hidden element
      if (document.forms[f][i].type != "hidden")
      {
        // and it's not disabled
        if (document.forms[f][i].disabled != true)
        {
            // set the focus to it
            document.forms[f][i].focus();
            var bFound = true;
        }
      }
      // if found in this element, stop looking
      if (bFound == true)
        break;
    }
    // if found in this form, stop looking
    if (bFound == true)
      break;
  }
}
