var ID_TIMEOUT_CARRITO = null;
var TIMEOUT_CARRITO = 500;

function $(id){
   return document.getElementById(id);
}

function newsletear(e) {

	var keynum = (window.event) ? e.keyCode : e.which;
	
	if(keynum == 13) {
		location = 'newsletter.php?email='+$("newsletter").value;	
	}
		
}

function enviarpago(id)
{
	if(document.getElementById('paypal').checked == true)
	{
		post('ajax.php?id_pedidos='+id,'metodo=Paypal');
		document.form_paypal.submit();
	}
	else if(document.getElementById('acuenta').checked == true)
	{
		post('ajax.php?id_pedidos='+id,'metodo=Pago a cuenta');
		location = "http://www.grupoath.com/carrito.php?act=thankyou";
	}
	else if(document.getElementById('Transferencia').checked == true)
	{
		post('ajax.php?id_pedidos='+id,'metodo=Transferencia Bancaria');
		location = "http://www.grupoath.com/carrito.php?act=thankyou";
	}
	else
	{
		alert('Debe seleccionar una opcion de pago');
	}
}

function post(url, vars){
	var xml = null;
	try {
		xml = new ActiveXObject('Microsoft.XMLHTTP');
	}catch(exception){
		xml = new XMLHttpRequest();
	}
	xml.open('POST',url,false);
	xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xml.send (vars);
	if(xml.status == 404) alert('Url no valida');
	return xml.responseText;
}

function goAnchor(){
	location = location + "#anchor";
}


/*#########################################################################
## IMAGENES DETALLE
#########################################################################*/
var load_image = null;
function seleccionar_imagen(num){
   var imagen_box =  document.getElementById('imagen_box');
   imagen_box.src = imagenes_cuadro_grande[num];
   imagen_seleccionada = num;
}

function zoom_seleccionada(){
   viewPic( imagenes_originales[imagen_seleccionada] );
}

function viewPic(img){
    picfile = new Image();
    picfile.src =(img);
    fileCheck(img);
}

function fileCheck(img){
    if( (picfile.width!=0) && (picfile.height!=0) ){
        makeWindow(img);
    }else{
        funzione="fileCheck('"+img+"')";
        intervallo=setTimeout(funzione,50);
    }
}

function html_decode(data){
   span = document.createElement('span');
   span.innerHTML = data;
   return span.innerHTML;
}

function makeWindow(img){
   ht = picfile.height;
   wd = picfile.width;

   var args= "height=" + ht + ",innerHeight=" + ht;
   args += ",width=" + wd + ",innerWidth=" + wd;
   if (window.screen){
      var avht = screen.availHeight;
      var avwd = screen.availWidth;
      var xcen = (avwd - wd) / 2;
      var ycen = (avht - ht) / 2;
      args += ",left=" + xcen + ",screenX=" + xcen;
      args += ",top=" + ycen + ",screenY=" + ycen + ",resizable=yes";
   }
   popwin=window.open("","_blank",args)
   popwin.document.open()
   popwin.document.write('<html><head><title>'+img+'</title></head><body bgcolor=white scroll=no topmargin=0 leftmargin=0 rightmargin=0 bottomargin=0 marginheight=0 marginwidth=0><div style="position: absolute; top:0px;left:0px"><a href="javascript:window.close()"><img src="'+img+'" width="'+wd+'" height="'+ht+'" border="0"></a></div></body></html>')
   popwin.document.close()
}

function enviar_form_login(v){
	var form_login = document.getElementById("form_login");
   var _focus = null;
   var msg = '';
   if( isNaN(v) ){

      if( form_login.usuario.getAttribute('valor_inicial') == form_login.usuario.value ){
         form_login.usuario.value = '';
      }

      if( form_login.password.getAttribute('valor_inicial') == form_login.password.value ){
         form_login.password.value='';
      }

      if( ! form_login.usuario.value ) { msg += 'Debe ingresar el usuario.\n';_focus=form_login.usuario;}
      if( ! form_login.password.value ) { msg += html_decode('Debe ingresar la contrase&ntilde;a.'); if( ! _focus ) { _focus=form_login.password;} }

   }
   else if( !isNaN(v) ){

      if( $('usuario_field').getAttribute('valor_inicial') == $('usuario_field').value ){
          $('usuario_field').value = '';
      }

      if( $('password_field').getAttribute('valor_inicial') == $('password_field').value ){
          $('password_field').value='';
      }

      if( ! $('usuario_field').value ) { msg += 'Debe ingresar el usuario.\n';_focus=$('usuario_field');}
      if( ! $('password_field').value ) { msg += html_decode('Debe ingresar la contrase&ntilde;a.'); if( ! _focus ) { _focus=$('password_field');} }

   }

   if( msg ){
      alert(msg);
      _focus.focus();
   }else{
      form = isNaN(v) ? document.form_login : document.login_registro;
      form.submit();
   }
}

 function get_url(url,cb,extra){
     var xml = null;
     try{
         xml = new ActiveXObject('Microsoft.XMLHTTP');
     }catch(exception){
        xml = new XMLHttpRequest();
     }

     xml.open('GET',url , true);
     xml.send(null);
     xml.onreadystatechange = function(){
        if( 4 == xml.readyState  ){
           if(cb){
              cb(xml,extra);
           }
        }
     }
  }

 /*##################################################################################
 ## CARRITO                                                                          #
 ###################################################################################*/
 function onkeypress_cantidad(id){
    if( ID_TIMEOUT_CARRITO ){
       clearTimeout( ID_TIMEOUT_CARRITO ); // Si hay un proceso en espera entonces se cancela.
    }
    ID_TIMEOUT_CARRITO = setTimeout('set_cantidad('+id+')', TIMEOUT_CARRITO);
 }

 function set_cantidad( id ){
    cantidad = parseInt( document.getElementById('cantidad_' + id).value );
    if( ! isNaN( cantidad ) && cantidad > 0 ){
       get_url('carrito.php?act=update_cantidad&id=' + id + '&cantidad=' + cantidad,cb_set_cantidad, id);
    }
 }

 function cb_set_cantidad(response,id){
    try {
       //Actualizo todos los totales.
       var resp = eval('('+response.responseText + ')'); //Los parentesis son necesarios.
       $('total_producto_' + id).innerHTML = resp.total_producto;
       $('total').innerHTML = resp.total_con_iva;
       $('total_sin_iva').innerHTML = resp.total_sin_iva;
       $('iva').innerHTML = resp.iva;
       $('cantidad_productos').innerHTML = resp.cantidad_productos;
       $('envio').innerHTML = resp.envio;
    }catch(exception){}
 }

 /*
Se le pasa el ID del elemento que se quiere verificar.
Devuelve TRUE o FALSE
*/
function isMail(id){

   var s = document.getElementById(id);
   var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_.-]+\.[A-Za-z0-9]+[A-za-z]$/;
   if (s.length == 0 ) return true;

   if (filter.test(s.value)){
      return true;
   }else{
      return false;
   }

}
