var strUbicacion = 'ctl00_ContentPlaceHolder1_';
var JQstrUbicacion = '#ctl00_ContentPlaceHolder1_';
var GB_ANIMATION = true;

var addthis_config = {ui_click: true};

$(document).ready(function(){

    // tabs
   	$(".tabs").tabs();
   	
   	// maletin
   	loadMaletinUsados();

    $('#s1').cycle({
        fx: 'scrollUp', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
        timeout: 6000
    });

     // seleccion de tab
     var tab=getParameterByName('ver');
     $('#ei_tabs').tabs('select', parseInt(tab));


         // greybox
        $("a.greybox").click(function(){
          var t = this.title || this.innerHTML || this.href;
          GB_show(t,this.href,1,1);
          return false;
        });

});

function loadMaletinUsados()
{
    $('#mi-maletin ul li:not(:first)').not(':last').remove();
    $('#mis-cotizaciones ul li:not(:first)').not(':last').remove()
    
   	var random= Math.random();
   	$.get($base + 'AjaxMetodos/Maletin.aspx?action=getmimaletinfavoritos&n='+random, function(data) {
        if(data != ''){
            //$('#mi-maletin ul').html(data);
            $('#mi-maletin ul li:first').after(data).remove();
            $('.mensajeMiMaletin').show();
        }
    });

   	$.get($base + 'AjaxMetodos/Maletin.aspx?action=getmimaletincotizados&n='+random, function(data) {
        if(data != ''){
            $('#mis-cotizaciones ul li:first').after(data).remove();
        }
    });
   	
}

function _AbrirOnLine(){
    window.open('http://messenger.providesupport.com/messenger/elinmobiliario.html','','height=460,width=500,menubar=0,resizable=0');
}

function getParameterByName( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function onKPNumero(evt) {
    var key;
    if (window.event)
        key = evt.keyCode;
    else if (evt.which)
        key = evt.which;
    else {
        return true;
    }
    
    if (key == 08) return true; //  Backspace 
    if (key == 09) return true; //  Tab 
    if (key == 33) return true; //  Page Up 
    if (key == 34) return true; //  Page Down 
    if (key == 35) return true; //  End 
    if (key == 36) return true; //  Home 
    if (key == 37) return true; //  Arrow Left 
    if (key == 38) return true; //  Arrow Up 
    if (key == 39) return true; //  Arrow Right
    if (key == 40) return true; //  Arrow Down
    if (key == 45) return true; //  menos
    if (!(key >= 48 && key <= 57)) return false;
    return true;
}

function valRut(s, e)
{
    if (e.Value!=null)
    {
        e.Value = formato_rut(e.Value, true);
	    var tmpstr = "";
	    var intlargo = e.Value;
	    if (intlargo.length> 0)
	    {
		    crut = e.Value
		    largo = crut.length;
		    if ( largo <2 )
		    {
		        e.IsValid = false;
			    return;
		    }
		    for ( i=0; i <crut.length ; i++ )
		    if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
		    {
			    tmpstr = tmpstr + crut.charAt(i);
		    }
		    rut = tmpstr;
		    crut=tmpstr;
		    largo = crut.length;
    	
		    if ( largo> 2 )
			    rut = crut.substring(0, largo - 1);
		    else
			    rut = crut.charAt(0);
    	
		    dv = crut.charAt(largo-1);
    	
		    if ( rut == null || dv == null )
		    {
		        e.IsValid = false;
			    return;
    	    }
		    var dvr = '0';
		    suma = 0;
		    mul  = 2;
    	
		    for (i= rut.length-1 ; i>= 0; i--)
		    {
			    suma = suma + rut.charAt(i) * mul;
			    if (mul == 7)
				    mul = 2;
			    else
				    mul++;
		    }
    	
		    res = suma % 11;
		    if (res==1)
			    dvr = 'k';
		    else if (res==0)
			    dvr = '0';
		    else
		    {
			    dvi = 11-res;
			    dvr = dvi + "";
		    }
    	
		    if ( dvr != dv.toLowerCase() )
		    {
		        e.IsValid = false;
			    return;
		    }
	    }
	}
}

function formato_rut(texto, activo) 
{ 
    var invertido = ""; 
    var dtexto = ""; 
    var cnt = 0; 
    var i=0; 
    var j=0; 
    var largo = "";     
    if (activo && texto!=null) { 
          texto = formato_rut(texto, false) 
          largo = texto.length; 
          for ( i=(largo-1),j=0; i>=0; i--,j++ ) 
            invertido = invertido + texto.charAt(i);           
          dtexto = dtexto + invertido.charAt(0); 
          dtexto = dtexto + '-';           
          for ( i=1,j=2; i<largo; i++,j++ ) 
          { 
            if ( cnt == 3 ) 
            { 
              dtexto = dtexto + '.'; 
              j++; 
              dtexto = dtexto + invertido.charAt(i); 
              cnt = 1; 
            } 
            else 
            {  
              dtexto = dtexto + invertido.charAt(i); 
              cnt++; 
            } 
          } 
          invertido = ""; 
          for ( i=(dtexto.length-1),j=0; i>=0; i--,j++ ) 
            invertido = invertido + dtexto.charAt(i); 
          if (invertido == '-') invertido = "" 
          texto = invertido; 
    } else { 
        var tmpstr = ""; 
        if(texto!=null)
        {
            for ( i=0; i < texto.length ; i++ ) 
            if ( texto.charAt(i) != ' ' && texto.charAt(i) != '.' && texto.charAt(i) != '-' ) 
                tmpstr = tmpstr + texto.charAt(i); 
            texto = tmpstr; 
        }
    } 
    return texto; 
} 

function ResizeCenterPopup(width, height){
    // calcular left, top de acuerdo a medidas
    var left=$(window).width()/2-width/2;
    var top=$(window).scrollTop()+$(window).height()/2-height/2;
    
    if(left<=50)
        left=50;
    if(top<=$(window).scrollTop())
        top=$(window).scrollTop()+50;
    // animar
    $('#GB_window').animate({width:width, height:height, top:top, left:left},500, function(){
        $('#GB_frame').css('height', height-20);
    });
}



