// JavaScript Document

// verify required fields
function checkrequired(form) {
	var pass = true;
	for(i = 0; i < form.length; i++) {
		var tempobj = form.elements[i];
		if(tempobj.alt == "required") {
			if(tempobj.value == '') {
				pass = false;
				break;
			}
		}
	}
	if(!pass) {
		alert("Entre com as informações solicitadas.");
		tempobj.focus(); // set focus to missing field
		return false;
	}
	else { return true; } 
}

// image swap for view page
function swap(target, fname) {
	document[target].src = "fotos/" + fname;
}

// marca sure at least one search criteria has been provided
function checksearch(form) {
	var count = 0;
	
	for(i = 0; i < form.length; i++) {
		var tempobj = form.elements[i];
		if(tempobj.value == '') {
			count++;
		}
	}
	i-=2;
	if(count == i) {
		alert("Selecione um critério para realizar uma busca.");
		return false;
	}
	else {
		//alert("You DID enter search criteria." + count + "  " + i);
		return true;
	} 
}

// confirm removal of vehicle listing
function verify_removal(codigo) {
	msg = "Você tem certeza de que deseja remover este veículo do sistema?";
	if(confirm(msg)) {
		window.location='apagar.php?codigo=' + codigo;
	} else {
		return false;
	}
}

// confirm removal of image from vehicle listing
function verify_image(imageid, codigo) {
	msg = "Você tem certeza de que deseja remover esta foto?";
	if(confirm(msg)) {
		window.location='apagar_foto.php?id=' + imageid + '&codigo=' + codigo;
	} else {
		return false;
	}
}

// display invalid cadastro number message
function invalid_cadastro(cadastro) {
	msg = "O número de cadastro: " + cadastro + " é inválido.";
	if(confirm(msg)) {
		window.location='controle.php';
	} else {
		window.location='controle.php';
	}
}
function linkauto(url) {
   window.location.href = url;
}
function BlockKeybord()
                {
                        if(window.event) // IE
                        {
                                if((event.keyCode < 48) || (event.keyCode > 57))
                                {
                                        event.returnValue = false;
                                }
                        }
                        else if(e.which) // Netscape/Firefox/Opera
                        {
                                if((event.which < 48) || (event.which > 57))
                                {
                                        event.returnValue = false;
                                }
                        }

                        
                }

                function troca(str,strsai,strentra)
                {
                        while(str.indexOf(strsai)>-1)
                        {
                                str = str.replace(strsai,strentra);
                        }
                        return str;
                }

                function FormataMoeda(campo,tammax,teclapres,caracter)
                {
                        if(teclapres == null || teclapres == "undefined")
                        {
                                var tecla = -1;
                        }
                        else
                        {
                                var tecla = teclapres.keyCode;
                        }

                        if(caracter == null || caracter == "undefined")
                        {
                                caracter = ".";
                        }

                        vr = campo.value;
                        if(caracter != "")
                        {
                                vr = troca(vr,caracter,"");
                        }
                        vr = troca(vr,"/","");
                        vr = troca(vr,",","");
                        vr = troca(vr,".","");

                        tam = vr.length;
                        if(tecla > 0)
                        {
                                if(tam < tammax && tecla != 8)
                                {
                                        tam = vr.length + 1;
                                }

                                if(tecla == 8)
                                {
                                        tam = tam - 1;
                                }
                        }
                        if(tecla == -1 || tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105)
                        {
                                if(tam <= 2)
                                {
                                        campo.value = vr;
                                }
                                if((tam > 2) && (tam <= 5))
                                {
                                        campo.value = vr.substr(0, tam - 2) + ',' + vr.substr(tam - 2, tam);
                                }
                                if((tam >= 6) && (tam <= 8))
                                {
                                        campo.value = vr.substr(0, tam - 5) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
                                }
                                if((tam >= 9) && (tam <= 11))
                                {
                                        campo.value = vr.substr(0, tam - 8) + caracter + vr.substr(tam - 8, 3) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
                                }
                                if((tam >= 12) && (tam <= 14))
                                {
                                        campo.value = vr.substr(0, tam - 11) + caracter + vr.substr(tam - 11, 3) + caracter + vr.substr(tam - 8, 3) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
                                }
                                if((tam >= 15) && (tam <= 17))
                                {
                                        campo.value = vr.substr(0, tam - 14) + caracter + vr.substr(tam - 14, 3) + caracter + vr.substr(tam - 11, 3) + caracter + vr.substr(tam - 8, 3) + caracter + vr.substr(tam - 5, 3) + ',' + vr.substr(tam - 2, tam);
                                }
                        }
                }

                function maskKeyPress(objEvent)
                {
                        var iKeyCode;
                                                
                        if(window.event) // IE
                        {
                                iKeyCode = objEvent.keyCode;
                                if(iKeyCode>=48 && iKeyCode<=57) return true;
                                return false;
                        }
                        else if(e.which) // Netscape/Firefox/Opera
                        {
                                iKeyCode = objEvent.which;
                                if(iKeyCode>=48 && iKeyCode<=57) return true;
                                return false;
                        }
                        
                        
                }
