function CalculaPreco(Quantidade, Campo, Valor){
				 document.form1.elements["total"].value = 0;
				 document.form1.elements[Campo].value = formatanumero(Quantidade.value * Valor, 2);
				 for (i=0; i<document.form1.length; i++){
						 if(document.form1.elements[i].name.substring(0,5) == "valor"){
								document.form1.elements["total"].value = parseFloat(document.form1.elements["total"].value) + parseFloat(document.form1.elements[i].value);
						 }
				 }
}

//formatar nº com casas decimais
function formatanumero(numero,decimais)
 {
   if (isNaN(numero)) { return 0};
   if (numero=='') { return 0};

   var IsNegative=(parseInt(numero)<0);
   if(IsNegative)numero=-numero;

   var snum = new String(numero);
   var sec = snum.split('.');
   var whole = parseInt(sec[0]);
   var result = '';
   if(sec.length > 1){
  var dec = new String(sec[1]);
  dec = parseInt(dec)/Math.pow(10,parseInt(dec.length-decimais-1));
  Math.round(dec);
  dec = parseInt(dec)/10;

  if(IsNegative)
  {
    var x = 0-dec;
    x = Math.round(x);
    dec = - x;
  }
  else
  {
    dec = Math.round(dec);
  }

  /*
   * If the number was rounded up from 9 to 10, and it was for 1 'decimal'
   * then we need to add 1 to the 'whole' and set the dec to 0.
   */
  if(decimais==1 && dec==10)
  {
    whole+=1;
    dec="0";
  }

  dec = String(whole) + "." + String(dec);
  var dot = dec.indexOf('.');
  if(dot == -1){
    dec += '.';
    dot = dec.indexOf('.');
  }
  var l=parseInt(dot)+parseInt(decimais);
  while(dec.length <= l) { dec += '0'; }
  result = dec;
   } else{
  var dot;
  var dec = new String(whole);
  dec += '.';
  dot = dec.indexOf('.');
  var l=parseInt(dot)+parseInt(decimais);
  while(dec.length <= l) { dec += '0'; }
  result = dec;
   }
   if(IsNegative)result="-"+result;
   return result;
}

function Verifica(){

		var which;
		var campofoco;
		which = document.form1;
		var aviso = "";
		var foco = false;			
	
		if(which.Nome.value == ""){
			aviso += "- Nome\n";
			if(foco == false){
				campofoco = 'Nome';
				foco = true;
			}
		}
	

		if(which.Rua.value == ""){
			aviso += "- Endereço\n";
			if(foco == false){
				campofoco = 'Rua';
				foco = true;
			}
		}


		if(which.Bairro.value == ""){
			aviso += "- Bairro\n";
			if(foco == false){
				campofoco = 'Bairro';
				foco = true;
			}
		}


		if(which.Cidade.value == ""){
			aviso += "- Cidade\n";
			if(foco == false){
				campofoco = 'Cidade';
				foco = true;
			}
		}


		if(which.Estado.value == ""){
			aviso += "- Estado\n";
			if(foco == false){
				campofoco = 'Estado';
				foco = true;
			}
		}


	
		if(which.emailped.value == ""){
			aviso += "- E-mail\n";
			if(foco == false){
				campofoco = 'emailped';
				foco = true;
			}				
		}else{
			if(checkEmail(which.emailped.value) == false){
				aviso += "- E-mail Inválido!\n";
				if(foco == false){
					campofoco = 'emailped';
					foco = true;
				}						
			}
		}
	
		if(which.total.value == "0"){
				aviso += "- Você deverá selecionar ao menos um item\n";
		}

	
		if(aviso == ""){				
			which.submit();
			return true;
		}else{
			aviso = "Campos não preenchidos:\n" + aviso;							
			alert(aviso);	
			if(foco){
				which[campofoco].focus();
			}
			
			return false;
		}
}



function checkEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}	
	}

	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}

	if (len<2) {
		return false;
	}
	return true;
}