
$(document).ready(function() {
	$.ajaxSetup({
		beforeSend: function(){ $("#modal").show(); },
		complete: function(){ $("#modal").hide(); }
	});
	
	$("#table_auto tbody tr:not([th]):even").addClass("alt");
	
	$("#table_auto tbody tr:not([th])").bind("mouseover", function(){
        var color  = $(this).css("background");
        $(this).children().css("background-color", "#C6E6EF");
        $(this).bind("mouseout", function(){
            $(this).children().css("background", color);
        });
	});
});


function dateDiff(date1,date2,interval) {
	
    var second=1000, minute=second*60, hour=minute*60, day=hour*24, week=day*7;
    date1 = new Date(date1);
    date2 = new Date(date2);
    var timediff = new Date();
    timediff = date2 - date1;
    if (isNaN(timediff)) return NaN;
    switch (interval) {
        case "years": return date2.getFullYear() - date1.getFullYear();
        case "months": return (
            ( date2.getFullYear() * 12 + date2.getMonth() )
            -
            ( date1.getFullYear() * 12 + date1.getMonth() )
        );
        case "weeks"  : return Math.floor(timediff / week);
        case "days"   : return Math.floor(timediff / day); 
        case "hours"  : return Math.floor(timediff / hour); 
        case "minutes": return Math.floor(timediff / minute);
        case "seconds": return Math.floor(timediff / second);
        default: return undefined;
    }
}

function jsleep(s){
	s=s*1000;
	var a=true;
	var n=new Date();
	var w;
	var sMS=n.getTime();
	while(a){
		w=new Date();
		wMS=w.getTime();
		if(wMS-sMS>s) a=false;
	}
}

function openInvoicePrintPopup( invoice_id )
{
	var popup_width = 600;
	var popup_height = 400;
	day = new Date();
	id = day.getTime();
	eval("page" + invoice_id + " = window.open('/invoice/admin/print/id/"+ invoice_id +"', '', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+popup_width+',height='+popup_height+'');");
}

var popup_opened;
function openPopup( _url, _id, _title, _width )
{
	popup_opened = false;
	// create
	$.ajax({ url: _url , success: function( data ){
		createPopup( data, _id, _title, _width );
      }});
      
}

function createPopup( div_content, div_id , div_title, div_width )
{
	var div_element = null;
	if( $("#"+div_id) ){
		$("#"+div_id).remove();
	}
	
	div_element = document.createElement('div');
	
	if( !div_width )
	{
		div_width = 400;
	}
	
	div_element.setAttribute('id', div_id);
	div_element.setAttribute('title', div_title);
//	div_element.setAttribute('class', 'modal');
	div_element.innerHTML = div_content;

	document.body.appendChild(div_element);

	$( "#" + div_id ).dialog({
		modal: true,
		autoOpen: true,
		width: div_width,
		//resizable: false,
		position: 'center',
		close: function(event, ui){
			//window.location.reload();
			popup_opened = false;
		}
	});
	popup_opened = true; 		
	
	div_element.setAttribute('style', div_element.getAttribute('style') + ';max-height:800px;overflow:auto;');
	popup_opened = true;
}

