//*** MENU - BEGIN
function expand_menu(submenu){
	window.document.getElementById(submenu).style.display = 'block';
	window.document.getElementById(submenu+'Af').style.display = 'block';
	window.document.getElementById(submenu+'Ico').innerHTML = '-';
}
function colapse_menu(submenu){
	window.document.getElementById(submenu).style.display = 'none';
	window.document.getElementById(submenu+'Af').style.display = 'none';
	window.document.getElementById(submenu+'Ico').innerHTML = '+';
}
function action_menu(submenu){
	if(typeof(window.document.all)!="object"){ return }
	if(window.document.getElementById(submenu).style.display=='block'){ colapse_menu(submenu) }
	else{ expand_menu(submenu) }
}
//*** MENU - END

function ret_plicas(value){
	return(new String(value).replace(/'/gi,"''"));
}

function valida_form_revisao(){
	var msg = "", prim_el = "";
	if(!valida_obrigatorios('nome')){ msg = 'O nome é um campo obrigatório!\n'; if(prim_el==""){ prim_el = window.document.getElementById('nome').id } }
	if(!valida_obrigatorios('tlf')){ msg += 'O telefone é um campo obrigatório!\n'; if(prim_el==""){ prim_el = window.document.getElementById('tlf').id } }
	else{ if(!valida_numero('tlf',9)){ msg += 'O número de telefone é inválido!\n'; if(prim_el==""){ prim_el = window.document.getElementById('tlf').id } } }
	if(string_comp_sem_esps('email')!=""){ if(valida_email(window.document.getElementById('email').value)){ msg += "O Email é inválido!\n"; if(prim_el==""){ prim_el = window.document.getElementById('email').id } } }
	if(!valida_obrigatorios('moto')){ msg += 'A marca/modelo é um campo obrigatório!\n'; if(prim_el==""){ prim_el = window.document.getElementById('moto').id } }
	if(msg==""){ return(true) }else{ alert(msg); window.document.getElementById(prim_el).focus(); return(false) }
}

function valida_new_subsc(email){
	if(valida_email(email)){
		alert("Email inválido!");
		window.document.getElementById('news_email').value = '';
		window.document.getElementById('news_email').focus();
		return(false);
	}else{ return(true) }
}

function valida_obrigatorios(id_el){
	var valor = new String(window.document.getElementById(id_el).value);
	valor = valor.replace(/ /g,"");
	if(valor==""){ return(false) }else{ return(true) }
}

function valida_numero(id_el,el_len){
	var valor = new String(window.document.getElementById(id_el).value);
	if(valor.length!=9){ return(false) }
	valor = valor.replace(/\d/gi,"");
	if(valor==""){ return(true) }else{ return(false) }
}

function valida_email(str){
	str = str.replace(/^ +/gi,"").replace(/ +$/gi,"");
	if(str.indexOf(" ")!=-1){ return(true) }
	if(str.length==0){ return(true) }
	else{
		var at_posi = str.indexOf("@");
		if(at_posi==-1 || at_posi==0 || at_posi==str.length-1 || at_posi!=str.lastIndexOf("@")){ return(true) }
		str = str.replace(/.*@/i,"");
		if(str.indexOf(".")==-1 || str.indexOf(".")==str.length-1 || str.indexOf(".")==0 || str.lastIndexOf(".")==str.length-1){ return(true) }
	}
	return(false);
}


function valida_data(el){
	var checkstr = "0123456789";
	var DateField = el;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "-";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
   err = 0;
   DateValue = DateField.value;

   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;

   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}

   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }

   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }

   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }

   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }

   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }

   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }

   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
   }

   else {
      alert("Data inválida!");
      DateField.select();
	  DateField.focus();
   }
}

function string_comp_sem_esps(id_el){
	var valor = new String(window.document.getElementById(id_el).value);
	valor = valor.replace(/ +/g,"");
	return(valor);
}

function valida_ges_num(el){
	if(isNaN(el.value)){
		alert("O valor introduzido é inválido!");
		el.select();
		el.focus();
	}
}

function valida_ges_data(el){
	valida_data(el);
}

function valida_novo(f){
	var erro = "";
	if(isNaN(f[5].value)){ erro = "O valor da cilindrada é inválido!\n" }
	if(isNaN(f[6].value)){ erro += "O valor do preço é inválido!\n" }
	if(isNaN(f[8].value)){ erro += "O valor da potência é inválido!\n" }
	if(isNaN(f[9].value)){ erro += "O valor do binário é inválido!\n" }
	if(isNaN(f[10].value)){ erro += "O valor do peso é inválido!\n" }
	if(isNaN(f[11].value)){ erro += "O valor do comprimento é inválido!\n" }
	if(isNaN(f[12].value)){ erro += "O valor da largura é inválido!\n" }
	if(isNaN(f[13].value)){ erro += "O valor da altura é inválido!\n" }
	if(isNaN(f[14].value)){ erro += "O valor da capacidade do depósito é inválido!\n" }
	if(erro!=""){
		alert(erro);
		return(false);
	}else{ return(true) }
}

function valida_usado(f){
	var erro = "";
	if(isNaN(f[4].value)){ erro = "O valor do ano é inválido!\n" }
	if(isNaN(f[5].value)){ erro += "O valor do preço é inválido!\n" }
	if(isNaN(f[6].value)){ erro += "O valor dos kilometros é inválido!\n" }
	if(isNaN(f[7].value)){ erro += "O valor da cilindrada é inválido!\n" }
	if(erro!=""){
		alert(erro);
		return(false);
	}else{ return(true) }
}

function valida_noticia(f){
	var erro = "";
	valida_data(f[2]);
	if(f[2].value==""){ erro = "Data inválida!\n" }
	if(erro!=""){
		alert(erro);
		return(false);
	}else{ return(true) }
}

function valida_newsletter(f){
	var erro = "";
	valida_data(f[2]);
	if(f[2].value==""){ erro = "Data inválida!\n" }
	if(erro!=""){
		alert(erro);
		return(false);
	}else{ return(true) }
}