var sessionTimer;
var sessionDuration = 10*60*1000;

function setSessionTimeout() {
    sessionTimer = setTimeout(function() {
        document.location = 'logout';
    },sessionDuration); 
}
function resetSessionTimeout() {
    clearTimeout(sessionTimer);
}
function ReadCookie(cookieName) {
	 var theCookie=""+document.cookie;
	 var ind=theCookie.indexOf(cookieName);
	 if (ind==-1 || cookieName=="") return ""; 
	 var ind1=theCookie.indexOf(';',ind);
	 if (ind1==-1) ind1=theCookie.length; 
	 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}
$(document).ready(function() {
    
    $("a.broken-invoice-url").livequery('click', function() {
        alert("Esta factura no puede ser mostrada.");
        return false;
    });
    setSessionTimeout();
    
    
    $("ul#tabs-menu li:first").addClass('active');
    $(document).ajaxComplete(function() {
        resetSessionTimeout();
        setSessionTimeout();
    });
    $("ul#tabs-menu li a").click(function(){
        $("ul#tabs-menu li").removeClass('active');
        $(this).parents('li').addClass('active');
        
        $("div#tabs div#tabs-content").html('');
        
        $("div#tabs div#tabs-content").addClass('loading').load($(this).attr('href'), function () {
            
            $("div#tabs-content").removeClass('loading');
        })
        
        return false;
    });
    
    $('div#tabs div#tabs-content').addClass('loading').load($("ul#tabs-menu a:first").attr("href"), function() {
        $(this).removeClass('loading');    
    });
});
    
$(document).ready(function() {
    $("a#equipment-list").livequery('click', function() {
        $.fancybox({
            'padding' : 0,
            'width': '70%',
            'height': '80%',
            'autoScale'     	: false,
            'type'				: 'iframe',
            'href' : $(this).attr('href'),
            'transitionIn'	: 'elastic',
	    'transitionOut'	: 'elastic'
        });
        return false;
    });
    
    $("a.loginbox, a.fancybox").livequery('click', function(){
        $.fancybox({
            'padding' : 0,
            'width' : '50%',
            'height' : '60%',
            'autoScale'     	: true,
            'type'				: 'iframe',
            'href' : $(this).attr('href'),
            'transitionIn'	: 'elastic',
	    'transitionOut'	: 'elastic'
        });
        return false;
    });
});

$(document).ready(function(){
    $('input.datepicker').livequery(function(){
        $(this).datepicker({
            monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
            dayNamesMin: ['Do', 'Lu', 'Mar', 'Mie', 'Ju', 'Vi', 'Sa'],
            dateFormat: 'yy-mm-dd'
        });
    });
    
    /*$('form.ajax-form').livequery(function() {
        $(this).ajaxForm({
            url: $(this).attr('action'),
            target:     '#form-content'
        });
    });*/
    var aform = function() {
        $(this).ajaxForm({
            url: $(this).attr('action'),
            target:     '#form-content',
            success: function() {
                $(this).addClass('ajax-form2');
            }
        })
    }
    $("form.ajax-form").livequery(aform);
    
    var funcForm = function (){
        if ($("input#email-check").attr("checked")== false) {
            $("p.email").hide();
        }
        $("input#email-check").change(function(){
            if ($(this).is(":checked")) {
                $("p.email").show();    
            } else {
                $("p.email").hide();
            }
        });
        var myForm = this;
        $(this).ajaxForm({
            url: $(this).attr('action'),
            target:     '#form-content',
            beforeSubmit: function() {
                counter = 0;
                $("input[type=checkbox]",myForm).each(function(f) {
                    //alert($(this).attr('name'));
                    if ($(this).is(":checked")) counter++;
                });
                if (counter == 0) {
                    if ($("input#email-check").not(":checked")) {
                        $("p.email input").attr("value", "");
                    }
                    return confirm("Esta seguro que no desea recibir su factura por ningun medio?");
                    
                }
            },
            success : function() {
               $(myForm).addClass("ajax-form2");
                
            }
            
        });
        
    }
    $('form#invoices-form').livequery(funcForm);
    
    
    var reportLoss = function() {
        var myForm = this;
        $(this).ajaxForm({
            url: $(this).attr('action'),
            target:     '#form-content',
            beforeSubmit: function() {
                return confirm('Est\341 seguro de que quiere reportar su m\363vil como perdido? El n\372mero ser\341 suspendido y deber\341 visitar alguna de nuestras tiendas para reactivar el servicio.');
            },
            success : function () {
                if ("#hidden_link") {
                    $("#hidden_link").fancybox({
                        'padding' : 0,
                        'width' : '50%',
                        'height' : '60%',
                        'autoScale'     	: true,
                        'type'				: 'iframe',
                        'href' : $(this).attr('href'),
                        'transitionIn'	: 'elastic',
                        'transitionOut'	: 'elastic',
                        onClosed	:	function() {
                            window.location = 'entrar';
                        }
                    }).trigger('click');
                }
                $(this).addClass("ajax-form2")
                
            }
        });
    }
    $('form#report-loss').livequery(reportLoss);
    /*$('form#report-loss').livequery('submit', function() {
        return confirm('Est\341 seguro de que quiere reportar su m\363vil como perdido? El n\372mero ser\341 suspendido y deber\341 visitar alguna de nuestras tiendas para reactivar el servicio.');
    })*/
    
    $('input.phone').livequery(function() {
        $(this).mask("(999) 999-9999");    
    });
    
    $('input.cardnumber').livequery(function() {
        $(this).mask("9999999999999999"); 
    });
});

$(document).ready(function(){
    var services = function() {
        var myForm = this;
        $(this).ajaxForm({
            url: $(this).attr('action'),
            target: '#form-content',
            beforeSubmit: function() {
                counter = 0;
                $("input[type=checkbox]",myForm).each(function() {
                    //alert($(this).attr('name'));
                    if ($(this).is(":checked")) counter++;
                });
                if (counter == 0) {
                    alert('No ha seleccionado ningun servicio nuevo');
                    return false;
                }
                return confirm('Est\341 seguro de que quiere activar estos servicios? En su pr\363xima factura se ver\341n reflejados estos cambios');
            },
            success: function() {
                $(this).addClass("");
            }
        });
    }
    $('form#services').livequery(services);
    
    $('div.services input[type=checkbox]').livequery('change', function(){
        var total_price = 0;
        $('div.services input[type=checkbox]').each(function(){
            if ($(this).is(":checked")) {
                total_price = total_price + parseInt($(this).next("em").next('span.price').attr('title'));
            }    
        })
        $('span#total-price').html(total_price);
    });
});
/// || ($("li.ex").size() >= 1)
$(document).ready(function() {
    $("div.ex input[type=checkbox]").livequery('change', function() {
        if ($("div.ex input[type=checkbox]:checked").size() > 1 ) {
            $(this).attr("checked", false);
            
            var selected = $('div.ex input[type=checkbox]:checked').next('em').text();
            alert("No puede seleccionar este servicio: ya tiene seleccionado "+ selected);
            return false;    
        }
        if ($("li.ex").size() >= 1) {
            $(this).attr("checked", false);
            var selected = $('li.ex span').text();
            alert("No puede seleccionar este servicio: ya tiene seleccionado "+ selected);
            return false;    
        }
    }); 
});

function OuvreFenetre() {
	var width = 315;
	var height = 365;
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "width=" + width + ",height=" + height + ",status,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
	window.open('http://websms.viva.com.do/sms/index.php?q=inicio', 'Ping_Pong', windowFeatures);
}


$(document).ready(function() {
	$("a#pingpong").livequery('click', function (){
		OuvreFenetre();
		return false;
	});
});
//$(document).ready(function(){
//    $("a.read-message").click(function() {
//        $(this).toggleClass('reading');
//        $(this).next("div.message-content").toggle('slow');
//        return false;
//    })    
//})
