function SetFocus(TargetFormName) {
  var target = 0;
  if (TargetFormName != "")
    for (i=0; i<document.forms.length; i++) {
      if (document.forms[i].name == TargetFormName) {
        target=i;
        break;
      }
    }

  var TargetForm = document.forms[target];
    
  for (i=0; i<TargetForm.length; i++) {
    if ( (TargetForm.elements[i].type != "image") && 
         (TargetForm.elements[i].type != "hidden") && 
         (TargetForm.elements[i].type != "reset") && 
         (TargetForm.elements[i].type != "submit") ) {

      TargetForm.elements[i].focus();

      if ( (TargetForm.elements[i].type == "text") || 
           (TargetForm.elements[i].type == "password") )
        TargetForm.elements[i].select();
      
      break;
    }
  }
}

function RemoveFormatString(TargetElement, FormatString) {
  if (TargetElement.value == FormatString)
    TargetElement.value = "";
    TargetElement.select();
}

function CheckDateRange(from, to) {
  if (Date.parse(from.value) <= Date.parse(to.value))
    return true;
  else
    return false;
}

function IsValidDate(DateToCheck, FormatString) {
  var strDateToCheck;
  var strDateToCheckArray;
  var strFormatArray;
  var strFormatString;
  var strDay;
  var strMonth;
  var strYear;
  var intday;
  var intMonth;
  var intYear;
  var intDateSeparatorIdx = -1;
  var intFormatSeparatorIdx = -1;
  var strSeparatorArray = new Array("-"," ","/",".");
  var strMonthArray = new Array("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec");
  var intDaysArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

  strDateToCheck = DateToCheck.toLowerCase();
  strFormatString = FormatString.toLowerCase();
  
  if (strDateToCheck.length != strFormatString.length) {
    return false;
  }

  for (i = 0; i < strSeparatorArray.length; i++) {
    if (strFormatString.indexOf(strSeparatorArray[i]) != -1) {
      intFormatSeparatorIdx = i;
      break;
    }
  }

  for (i = 0; i < strSeparatorArray.length; i++) {
    if (strDateToCheck.indexOf(strSeparatorArray[i]) != -1) {
      intDateSeparatorIdx = i;
      break;
    }
  }
  
  if (intDateSeparatorIdx != intFormatSeparatorIdx)
    return false;

  if (intDateSeparatorIdx != -1) {
    strFormatArray = strFormatString.split(strSeparatorArray[intFormatSeparatorIdx]);
    if (strFormatArray.length != 3) {
      return false;
    }

    strDateToCheckArray = strDateToCheck.split(strSeparatorArray[intDateSeparatorIdx]);
    if (strDateToCheckArray.length != 3) {
      return false;
    }

    for (i = 0; i < strFormatArray.length; i++) {
      if (strFormatArray[i] == 'mm' || strFormatArray[i] == 'mmm')
        strMonth = strDateToCheckArray[i];
      if (strFormatArray[i] == 'dd')
        strDay = strDateToCheckArray[i];
      if (strFormatArray[i] == 'yyyy')
        strYear = strDateToCheckArray[i];
    }
  }
  else {
    if (FormatString.length>7) {
      if (strFormatString.indexOf('mmm') == -1)
        strMonth = strDateToCheck.substring( strFormatString.indexOf('mm'), 2 );
      else
        strMonth = strDateToCheck.substring( strFormatString.indexOf('mmm'), 3 );
      strDay = strDateToCheck.substring( strFormatString.indexOf('dd'), 2 );
      strYear = strDateToCheck.substring( strFormatString.indexOf('yyyy'), 2 );
    }
    else {
      return false;
    }
  }

  if (strYear.length != 4) {
    return false;
  }
  intday = parseInt(strDay, 10);
  if (isNaN(intday)) {
    return false;
  }
  if (intday < 1) {
    return false;
  }

  intMonth = parseInt(strMonth, 10);
  if (isNaN(intMonth)) {
    for (i = 0;i < strMonthArray.length; i++) {
      if (strMonth == strMonthArray[i]) {
        intMonth = i+1;
        break;
      }
    }
    if (isNaN(intMonth)) {
      return false;
    }
  }
  if (intMonth>12 || intMonth<1) {
    return false;
  }

  intYear = parseInt(strYear, 10);
  if (isNaN(intYear)) {
    return false;
  }
  if (IsLeapYear(intYear) == true) {
    intDaysArray[1] = 29;
  }

  if (intday > intDaysArray[intMonth - 1]) {
    return false;
  }
  
  return true;
}

function IsLeapYear(intYear) {
  if (intYear % 100 == 0) {
    if (intYear % 400 == 0)
      return true;
  }
  else {
    if ((intYear % 4) == 0)
      return true;
  }

  return false;
}

function calculaFrete ( obj ) {
	if ((obj.seq != '' || obj.faixa != '')) {
		if(obj.resetar == null) obj.resetar = 0;
	new Ajax.Updater('div_frete', 'calcfrete_avulso.cfm',
					 {
						 method: 'get',
						 parameters: obj
					 });
	}
}

function changeType(tp) {
	if (tp == 1) {
		n1pj.style.visibility = 'hidden';
		n1pf.style.visibility = 'visible';
		n2pj.style.visibility = 'hidden';
		n2pf.style.visibility = 'visible';
	}
	else {
		n1pf.style.visibility = 'hidden';
		n1pj.style.visibility = 'visible';
		n2pf.style.visibility = 'hidden';
		n2pj.style.visibility = 'visible';
	}
}
function resetStateText(theForm) {
  theForm.state.value = '';
  if (theForm.zone_id.options.length > 1) {
	theForm.state.value = '-- Selecione Acima --';
  }
}

function resetZoneSelected(theForm) {
  if (theForm.zone_id.options.length > 1) {
	theForm.state.value = '-- Selecione Acima --';
  }
}
function update_zone(theForm) {

  var NumState = theForm.zone_id.options.length;

  while(NumState > 0) {
	NumState--;
	theForm.zone_id.options[NumState] = null;
  }

  var SelectedCountry = "";

  SelectedCountry = theForm.country.options[theForm.country.selectedIndex].value;

  if (SelectedCountry == "30") {
	theForm.zone_id.options[0] = new Option("--", "");
	theForm.zone_id.options[1] = new Option("Acre", "193");
	theForm.zone_id.options[2] = new Option("Alagoas", "202");
	theForm.zone_id.options[3] = new Option("Amapá", "198");
	theForm.zone_id.options[4] = new Option("Amazonas", "196");
	theForm.zone_id.options[5] = new Option("Bahia", "200");
	theForm.zone_id.options[6] = new Option("Ceará", "206");
	theForm.zone_id.options[7] = new Option("Distrito Federal", "192");
	theForm.zone_id.options[8] = new Option("Espírito Santo", "189");
	theForm.zone_id.options[9] = new Option("Goiás", "191");
	theForm.zone_id.options[10] = new Option("Maranhão", "208");
	theForm.zone_id.options[11] = new Option("Mato Grosso", "183");
	theForm.zone_id.options[12] = new Option("Mato Grosso do Sul", "190");
	theForm.zone_id.options[13] = new Option("Minas Gerais", "182");
	theForm.zone_id.options[14] = new Option("Pará", "197");
	theForm.zone_id.options[15] = new Option("Paraiba", "204");
	theForm.zone_id.options[16] = new Option("Paraná", "186");
	theForm.zone_id.options[17] = new Option("Pernambuco", "203");
	theForm.zone_id.options[18] = new Option("Piauí", "207");
	theForm.zone_id.options[19] = new Option("Rio de Janeiro", "185");
	theForm.zone_id.options[20] = new Option("Rio Grande do Norte", "205");
	theForm.zone_id.options[21] = new Option("Rio Grande do Sul", "187");
	theForm.zone_id.options[22] = new Option("Rondônia", "194");
	theForm.zone_id.options[23] = new Option("Roraima", "195");
	theForm.zone_id.options[24] = new Option("Santa Catarina", "188");
	theForm.zone_id.options[25] = new Option("São Paulo", "184");
	theForm.zone_id.options[26] = new Option("Sergipe", "201");
	theForm.zone_id.options[27] = new Option("Tocantins", "199");
  }
  resetStateText(theForm);
}

function check_form() {
  var error = 0;
  var error_field = '*';
  var error_message = "Há alguns erros em seu formulário!\nPor favor, faça as seguintes correções:\n\n";

  var firstname = document.add_entry.firstname.value;
  var lastname = document.add_entry.lastname.value;
  var street_address = document.add_entry.street_address.value;
  var postcode = document.add_entry.postcode.value;
  var city = document.add_entry.city.value;

  if (document.add_entry.person[0].checked || document.add_entry.person[1].checked) {
  } else {
	error_message = error_message + "* Selecione Pessoa Física ou Jurídica.\n";
	error = 1;
	if (error_field == '*') error_field = document.add_entry.person[0];
  }
  if (document.add_entry.elements['firstname'].type != "hidden") {
	if (firstname == '' || firstname.length < 4) {
	  error_message = error_message + "O Nome ou Empresa deve ter no mínimo 4 caracteres.\n";
	  error = 1;
	  if (error_field == '*') error_field = document.add_entry.firstname;
	}
  }

  if (document.add_entry.elements['lastname'].type != "hidden") {
	if (lastname == '' || lastname.length < 4) {
	  error_message = error_message + "O Sobrenome ou Responsável deve ter no mínimo 4 caracteres.\n";
	  error = 1;
	  if (error_field == '*') error_field = document.add_entry.lastname;
	}
  }

  if (document.add_entry.elements['street_address'].type != "hidden") {
	if (street_address == '' || street_address.length < 5) {
	  error_message = error_message + "O endereço deve ter no mínimo 5 caracteres.\n";
	  error = 1;
	  if (error_field == '*') error_field = document.add_entry.street_address;
	}
  }

  if (document.add_entry.elements['city'].type != "hidden") {
	if (city == '' || city.length < 3) {
	  error_message = error_message + "A cidade deve ter no mínimo 3 caracteres.\n";
	  error = 1;
	  if (error_field == '*') error_field = document.add_entry.city;
	}
  }

  if (document.add_entry.elements['zone_id'].type != "hidden") {
	if (document.add_entry.zone_id.options.length <= 1) {
	  if (document.add_entry.state.value == '' || document.add_entry.state.value.length < 4 ) {
		 error_message = error_message + "Selecione um estado.\n";
		 error = 1;
		 if (error_field == '*') error_field = document.add_entry.zone_id;
	  }
	} else {
	  document.add_entry.state.value = '';
	  if (document.add_entry.zone_id.selectedIndex == 0) {
		 error_message = error_message + "Selecione o estado a partir da lista do país.\n";
		 error = 1;
		 if (error_field == '*') error_field = document.add_entry.state;
	  }
	}
  }

  if (document.add_entry.elements['country'].type != "hidden") {
	if (document.add_entry.country.value == 0) {
	  error_message = error_message + "Escolha o país.";
	  error = 1;
	  if (error_field == '*') error_field = document.add_entry.country;
	}
  }

  if (error == 1) {
	alert(error_message);
	error_field.focus();
	error_field.select();
	return false;
  } else {
	return true;
  }
}

function procImp(nuPed) {
  document.close();
  window.open("sec/PrintOrder.cfm?order_id="+nuPed,"NewWin","width=600,height=500,scrollbars=1,left=40,top=20");
}

