var submitted = false;

 $(document).ready(function() {

    $("input").bind("keypress", function(e) {
         var key=e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
         if(key==34 || key==35 || key == 36 || key == 37 || key == 39) {
             return false;
         }
         return true;
    });


 	$("#provinciaFilterSelect").change(function () {
 		$("#filterForm").submit();
	})

	$("#argentinaForm").submit(function(thisForm) {
		return validarPedido($("#argentinaForm").get(0));
	});
	

	$("#informeButtonLocalizacion").click(function() {
		$("#type").val("LOCALIZACION");
		$("#argentinaForm").submit();
	});

	$("#informeButtonComercial").click(function() {
		$("#type").val("COMERCIAL");
		$("#argentinaForm").submit();
	});

 });

function initializeForm() {
  if (document.argentinaForm != null) {
    if ( typeof noFocus == 'undefined' ) {
      document.argentinaForm.nombre.focus();
    }
  }
}

function nuevoPedidoGLAR() {
  document.location = "/globinfo";
  return true;
}

function setStyleSheet() {
   if (screen.width < 1024) {
      stylesheet = '800';
   }
   else {
      stylesheet = '1024';
   }

   stylesheet = '800';

   document.writeln('<link rel="stylesheet" type="text/css" media="screen,projection,print" href="./css/globinfo_setup_' + stylesheet + '.css" /><link rel="stylesheet" type="text/css" media="screen,projection,print" href="./css/globinfo_text_' + stylesheet + '.css" />');

  return;
}

function validarPedido(formArgentina) {
   if (submitted) {
      alert('Su pedido ya fue enviado, aguarde unos instantes por favor.');
      return false;
   }

   if (jQuery.trim(formArgentina.nombre.value).length < 1) {
      alert('Debe ingresar el NOMBRE de la persona sobre la que desea su informe Globinfo Argentina');
      return false;
   }

   if (jQuery.trim(formArgentina.apellido.value).length < 1) {
      alert('Debe ingresar el APELLIDO de la persona sobre la cual desea su informe Globinfo Argentina');
      return false;
   }

   if (jQuery.trim(formArgentina.apellido.value).length < 3) {
      alert('El APELLIDO de la persona debe contener al menos 3 letras');
      return false;
   }

   if (jQuery.trim(formArgentina.dni.value).length > 1) {
      formArgentina.dni.value = formArgentina.dni.value.replace(/\./g, "");

     if (!formArgentina.dni.value.match(/^\d+$/)) {
        alert('El DNI de la persona s\xf3lo puede contener n\xfameros');
        return false;
     }
   }

   formArgentina.email.value = jQuery.trim(formArgentina.email.value);
   if (formArgentina.email.value.length < 1) {
      alert('Debe ingresar la direcci\xf3n de E-MAIL donde desea recibir su informe Globinfo Argentina');

      return false;
   }

   if (!formArgentina.email.value.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
      alert('La direcci\xf3n de E-MAIL ingresada no es v\xe1lida. Verifique haberla escrito correctamente.');

      return false;
   }

   submitted = true;

   return true;
}

function doBlink(elementId) {
  if (!submitted) {
      var element = document.getElementById(elementId);

      if (element.style.color == "#7bcdef" || element.style.color == "rgb(123, 205, 239)") {
        element.style.color = "#FFFFFF";
      }
      else {
       element.style.color = "#7bcdef";
      }
  }
}

function select(element, status) {
  if (status) {
    element.style.border = "1px blue";
  }
  else {
    element.style.border = "1px rgb(200, 200, 200)";
  }
}

function selectMedioPago(medioPago) {
  document.forms[0].medioPago.value = medioPago;
  document.forms[0].submit();

  return true;
}

var speed = 255;

function StringArray (n) {
  this.length = n;
  for (var i =1; i <= n; i++) {
    this[i] = ' ';
  }
}

fader = new StringArray(16);

for(var i = 0; i < 10; i++) {
  fader[i] = i;
}

fader[10] = "a";
fader[11] = "b";
fader[12] = "c";
fader[13] = "d";
fader[14] = "e";
fader[15] = "f";

function hexadecimal(i) {
    return ("" + fader[Math.floor(i/16)] + fader[i%16]);
}

function fade(r2,g2,b2,r1,g1,b1) {
  for(var i = 0; i <= speed; i++) {
    var r = hexadecimal(Math.floor(r2 * ((speed-i)/speed) + r1 * (i/speed)));
    var g = hexadecimal(Math.floor(g2 * ((speed-i)/speed) + g1 * (i/speed)));
    var b = hexadecimal(Math.floor(b2 * ((speed-i)/speed) + b1 * (i/speed)));
    document.bgColor = "#" + r + g + b;
  }
}

function runSWF(archivo, ancho, alto, version, bgcolor, id, menu, FlashVars, quality,   allowScriptAccess) { // tutorial by IVI CONCEPT - www.ivi-concept.com
  if(version!=""){
    var version_data=version;
  }
  else {
    var version_data = "6,0,0,0";
  }
  if(menu != "") {
    menu_data = menu;
  }
  else {
    menu_data = false;
  }

  if (bgcolor != "") {
    var bgcolor_data = bgcolor;
  }
  else {
    var bgcolor_data = "#FFFFFF";
  }

  if (id != "") {
    id_data = id;
  }
  else {
    id_data = "flashMovie";
  }

  if (quality != "") {
    quality_data = quality;
  }
  else {
    quality_data = "low";
  }

  if (allowScriptAccess != "") {
    allowScriptAccess_data = allowScriptAccess;
  }
  else {
    allowScriptAccess_data = "always";
  }

  var quality="high"; // calidad de visualizaciï¿½n de la peli

  document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase= "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version= '+version_data+'" width='+ancho+' height='+alto+' id='+id_data+'>\n');
  document.write('<param name="movie" value='+archivo+'>\n');
  document.write('<param name= "allowScriptAccess" value= '+allowScriptAccess_data+'>\n');
  document.write('<param name="quality" value='+quality_data+'>\n');
  document.write('<param name="wmode" value="transparent">\n');
  document.write('<param name="FlashVars" value='+FlashVars+'>\n');
  document.write('<param name="bgcolor" value='+bgcolor_data+'>\n');
  document.write('<param name="menu" value='+menu_data+' >\n');
  document.write('<embed src='+archivo+' bgcolor='+bgcolor_data+' FlashVars='+FlashVars+' menu='+menu_data+' allowScriptAccess='+allowScriptAccess_data+' quality='+quality_data+' pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width='+ancho+' height='+alto+' swLiveConnect=true name='+id_data+'  wmode="transparent"></embed>');
  document.write('</object>\n');
}

// Agregados Leo Arias

function eventTrigger (e) {
    if (! e)
        e = event;
    return e.target || e.srcElement;
}

function calificaRadioClick (e) {
    var obj = eventTrigger (e);
    if (obj.value == "-1"){
        var texto = document.getElementById('otro_item_text').value="";
        document.getElementById('otro_item_text').disabled=false;
    }
    else{
        var texto = document.getElementById('otro_item_text').value="Especificar...";
        document.getElementById('otro_item_text').disabled=true;
    }
    return true;
}

function formCalificarSubmit(recomienda){
    // verifico que se haya ingresado texto cuando la
    // opcion es Otros.
    var radio = document.formcalificar.item_mejorar_id;
    var i;
    for (i=0;i<radio.length;i++){
       if (radio[i].checked)
          break;
    }
    var item_id = radio[i].value;

    var text    = document.getElementById('otro_item_text').value;
    if (item_id == "-1" && text.length < 1){
        alert('Debe especificar el item a mejorar.');
        return false;
    }

    document.getElementById('recomienda').value = recomienda;
    document.forms['formcalificar'].submit();
    return false;
}

function formContactos(){
    //valido los emails ingresados
    var email1 = document.getElementById('email1').value;
    var email2 = document.getElementById('email2').value;
    var email3 = document.getElementById('email3').value;
    var email4 = document.getElementById('email4').value;

    if (email1.length > 0 &&
        !email1.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
      alert('La direccion de E-MAIL del Contacto 1 ingresada no es valida. Verifique haberla escrito correctamente.');
      return false;
    }

    if (email2.length > 0 &&
        !email2.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
      alert('La direccion de E-MAIL del Contacto 2 ingresada no es valida. Verifique haberla escrito correctamente.');
      return false;
    }

    if (email3.length > 0 &&
        !email3.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
      alert('La direccion de E-MAIL del Contacto 3 ingresada no es valida. Verifique haberla escrito correctamente.');
      return false;
    }

    if (email4.length > 0 &&
        !email4.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
      alert('La direccion de E-MAIL del Contacto 4 ingresada no es valida. Verifique haberla escrito correctamente.');
      return false;
    }

    document.forms['formContactos'].submit();
    return false;
}

function validateDownloadForm(form) {
   if (!form.declaro_interes.checked) {
      alert('Debe ACEPTAR la Declaraci\xf3n de Inter\xe9s Leg\xedtimo para poder descargar su informe Globinfo Argentina');
      return false;
   }

   return true;
}

function submitPayPalForm() {
  document.forms[0].submit();
  return true;
}

function submitDineroMailForm() {
  document.forms[0].submit();
  return true;
}