//-----------------------------------------
//innovangecy scripts ---------------------
//-----------------------------------------
function Valida(elemento) {
  if (isNaN(elemento.value)) {
    alert("Dados incorrectos");
    elemento.value="";
    elemento.focus();
    return false;
  }
  return true;
}

function Avanca(next, event) {
  if (!event)
    event = window.event;

  var tg = (event.target) ? event.target : event.srcElement

  if ( event.keyCode == "9" || event.keyCode == "16" || event.shiftKey ) {
    event.cancelBubble = true;
    event.returnValue = false;
    return false;
  }
  else if ( tg.value.length == tg.maxLength ) {
    document.postalCodeSearchForm[next].focus();
    document.postalCodeSearchForm[next].select();
    return true;
  }
  return false;
}

function checkEnter(event) {
 	if (event.keyCode == 13 || event.keyCode == 10) {
  	  var f = document.forms['postalCodeSearchForm'];
  	  f.elements['postalCodeSearchForm:_link_hidden_'].value='postalCodeSearchForm:searchPC1';
    	f.elements['postalCodeSearchForm:_idcl'].value='postalCodeSearchForm:searchPC1';
  		f.submit();
  	}
}
//-----------------------------------------
//lucas scripts ---------------------------
//-----------------------------------------
var optionTest;

function setFocus(formName, element){
 	 var f = document.forms[formName];
   f[element].focus();
   f[element].select();
}

//dwr scripts
function populateConcelhos(distCombo, concComboName){
  var reply = function(data){
  	if (data != null){
			var concelhos = document.forms['postalCodeSearchForm'].elements[concComboName];
			for(i=0 ; i<data.length ; i+=2)
				concelhos.options[i/2+1] = new Option(data[i],data[i+1]);
			while(concelhos.options.length > (data.length/2)+1)
				concelhos.options[concelhos.options.length-1] = null;
  	}
  };
  if(optionTest){
	  var distrito = distCombo.options[distCombo.selectedIndex].value;
  	PostalCodeSearchManager.getDwrConcelhos(distrito, reply);
  }
}
//test if browser supports dynamic options
function pcsInit(){
	optionTest = true;
	var lgth = document.forms['postalCodeSearchForm'].elements['postalCodeSearchForm:concelhos2'].options.length;

	document.forms['postalCodeSearchForm'].elements['postalCodeSearchForm:concelhos2'].options[lgth] = new Option("_","_");
	if (document.forms['postalCodeSearchForm'].elements['postalCodeSearchForm:concelhos2'].options[lgth])
		document.forms['postalCodeSearchForm'].elements['postalCodeSearchForm:concelhos2'].options[lgth] = null;	
	else
		optionTest = false;
}


