$(function() {
	
		$('#jqmodal_window').jqm({
		trigger: false,
		onHide:  function(hash) {
			hash.w.fadeOut('1000',function(){ hash.o.remove(); });
			$("#jqmodal_window .content").html('Please wait... <img src="/images/busy.gif" alt="loading" />');
			//window.location.reload();
		}
	});

	$('#create_ticket, #add_nonhourly, .edit_ticket, .add_time, .view_ticket').click(function() {
		$('#jqmodal_window').jqmShow();
		$.ajax({ type: "GET",
		         url: $(this).attr('href'),
		         dataType: "html",
		         success: function(r) { $("#jqmodal_window .content").html(r); },
		         error: function(r) { alert(r); }
		});
		return false;
	});

	
	
	$('#mailCode_submit_btn').livequery('click', function() {
		var code = $('#mailCode').val();
		//alert(code);
		oldexchange = $('#exchange').html();
		$.ajax({
			type: "GET",
			url: '/index.php/home/get_app_data/'+code,
			dataType: "html",
			success: function(r) {
				$('#exchange').html(r);
				//alert(r)
			},
			error: function(xhr, desc, e) {
				var d = $('#mailCode');
				d.val('Invalid Recipient Code');
			}
		});
		return false;
	});
	
	$('#appDataForm1_cancel_btn').livequery('click', function() {
		$('#exchange').html(oldexchange);
		return false;
	});

	$('#appDataForm2').bind('submit', function() {
		$('#right_side').html();
		$.ajax({
			type: "POST",
			url: '/index.php/home/submit',
			data: $(this).serialize(),
			success: function(r) {
				$('#right_side').html(r);
			}
		});
		return false;
	});
	/*
	//$('#appDataForm2').livequery('submit',function() {
		
		
		$("[class^=validate]").validationEngine({
			
			success :	function() { successfromform() }, 
		
			failure : function() { alert('fail'); }
		
		});
	//});
	*/
	
	$('#mailCode').livequery('focus',function() {
		if($(this).hasClass('greyout')){
			$(this).removeClass('greyout').addClass('nogreyout').val('DDS-556-');
		}
	});
	
	$('#cur_debt').livequery('change', function() {
        var curdebt = $(this).val().replace(/[^0-9\.]+/g, '');
        var newdebt = Math.round((curdebt*0.6)*100)/100;
        var savings = curdebt-newdebt;
        $('#new_debt').val('$'+newdebt);
        $('#savings').val('$'+savings);
    });
	
		
});

function successfromform() {
	ajax({
				type: "POST",
				url: 'index.php/home/submit',
				data: $('#appDataForm2').serialize(),
				success: function(r) {
					$('#right_side').html(r);
				},
				error: function(r) {
					$('#form_errors').html('errors');
				}
			});
}	

function processJson(data) {
	alert(data.message);
	window.location.reload();
}

function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
	return result;
}

function toggleExchange() {
	$('#exchange').toggle('fast');
}

