$(document).ready(function() {
	$('#add').click(function() {
		jQuery('#lineItems').append('<div><input type="text" name="invoice[new_item_attributes][description][]" class="ui-corner-all text invDesc" /> <input type="text" name="invoice[new_item_attributes][qty][]" class="ui-corner-all text invQty" /> <input type="text" name="invoice[new_item_attributes][amount][]" class="ui-corner-all text invAmount" title="Amount" /> <input type="hidden" name="invoice[new_item_attributes][rowTotal][]" class="row-total-input" /></div>');
	});
});
 
// Label inside inputs

$(document).ready(function(){
	$(":text").labelify();
	$(":password").labelify();
});

// Button Style
$(document).ready(function() {
    $('.ui-button').hover(function() {
        $(this).addClass("ui-state-hover");
    },
                function() {
                    $(this).removeClass("ui-state-hover");
                }
        ).mousedown(function() {
            $(this).addClass("ui-state-active");
        })
        .mouseup(function() {
            $(this).removeClass("ui-state-active");
        });
});

// Alert Effect
$(document).ready(function() {
	$("#flash-message").fadeOut(3000);
});


// TABS
$(function(){
	$('#tabs').tabs();
});

// DATE
$(function() {
	$("#datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
});


// INVOICE MATHS
function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
};

function calcProdSubTotal() {
    
    var prodSubTotal = 0;

    $(".row-total-input").each(function(){
    
        var valString = $(this).val() || 0;
        
        prodSubTotal += parseFloat(valString);
                    
    });
        
    $("#product-subtotal").val(prodSubTotal.toFixed(2));

};

function calcTaxTotal() {

	var taxTotal = 0;
	//var taxAmount = 10;	
	var taxAmount = $("#salesTaxAmount").val() || 0;
		
    var productSubtotal = $("#product-subtotal").val() || 0;
        
    var taxTotal = parseInt(productSubtotal) * parseInt(taxAmount) / 100;
    var taxTotalNice = taxTotal;
	$("#product-tax").val(taxTotalNice.toFixed(2));
	
};

function calcOrderTotal() {

    var orderTotal = 0;

    var productSubtotal = $("#product-subtotal").val() || 0;
    var productTax = $("#product-tax").val() || 0;
        
    var orderTotal = parseFloat(productSubtotal) + parseFloat(productTax);
    
    $("#order-total").val(orderTotal.toFixed(2));
        
};



$(function(){
	$('.row-total-input').each(
		function( intIndex ){
			$('.invAmount').livequery('blur', function() {
					var $this = $(this);
					var amount = $this.val();
					
					var qty = $this.parent().find('.invQty').val();	
		
					if ( (IsNumeric(amount)) && (amount != '') ) {           
						var rowTotal = qty * amount;   
						$this.css("background-color", "white").parent().find(".row-total-input").val(rowTotal);	
					} else {        
						$this.css("background-color", "#ffdcdc");                     
			        };	        					
					calcProdSubTotal(); 
					calcTaxTotal()
				    calcOrderTotal();
			});
		}
	);
});

// VALIDATION
$(document).ready(function() {
	$("#signupForm").validate();
});

// PUBLIC Screenshots

$(document).ready(function() {
	$("a.zoom").fancybox({
		'overlayOpacity'	:	0.9,
		'overlayColor'		:	'#111'
	});
});