$(function() {
	$("#tabs").tabs();
	$("#tabs").show();
	
	$("input").blur(function(){ 
		$("#tip").remove();
		$('input').removeClass('errorInput');
	});
	
	$('.hIcon').click(function(){
		showDialog( $('#'+($(this).attr('id')+'Dialog')) );
	});
	
	$('input').numeric({allow:"."});
	
	$('#calcGravity, #calcFluid, .reset').hover(function(){$(this).addClass('ui-state-hover')}, function(){$(this).removeClass('ui-state-hover')});
	
	$('#calcFluid').click(function(){
		var patt = /^\d{0,}(\.\d)?$/;
		$("#tip").remove();
		$('input').removeClass('errorInput');
		
		if( $('#txtCollarCount').val() == '' ) requrTip($('#txtCollarCount'), 18, 0);
		
		else if( !patt.test($('#txtCollarCount').val()) ) requrTip($('#txtCollarCount'), 3, 1);
		
		else if( $('#txtLengthCollars_mm').val() == '' ) requrTip($('#txtLengthCollars_mm'), 50, 0);
		
		else if( !patt.test($('#txtLengthCollars_mm').val()) ) requrTip($('#txtLengthCollars_mm'), 35, 1);
		
		else if( $('#txtFL_mm').val() == '' ) requrTip($('#txtFL_mm'), 82, 0);
		
		else if( !patt.test($('#txtFL_mm').val()) ) requrTip($('#txtFL_mm'), 67, 1);
		
		else if( $('#txtAvgLenCollar_f').val() == '' ) requrTip($('#txtAvgLenCollar_f'), 114, 0);
		
		else if( !patt.test($('#txtAvgLenCollar_f').val()) ) requrTip($('#txtAvgLenCollar_f'), 99, 1);
		
		else {
			$('#txtVelocityAvg').val( Math.round(((2 * $('#txtAvgLenCollar_f').val() * $('#txtCollarCount').val()) / ($('#txtLengthCollars_mm').val() / 9.4))*10) / 10 );
			$('#txtFluidLevel').val( Math.round((($('#txtFL_mm').val() / 9.4) * $('#txtVelocityAvg').val() / 2)*10) / 10 );
		}
		
	});
	
	$('#calcGravity').click(function(){
		$("#tip").remove();
		$('input').removeClass('errorInput');
		var patt = /^\d{0,}(\.\d)?$/;
		
		if( $('#txtAPIgravT').val() == '' ) requrTip($('#txtAPIgravT'), 18, 0);
			
		else if( !patt.test($('#txtAPIgravT').val()) ) requrTip($('#txtAPIgravT'), 2, 1);
			
		else if( $('#txtObservedTemp').val() == '' ) requrTip($('#txtObservedTemp'), 50, 0);
						
		else if( !patt.test($('#txtObservedTemp').val()) ) requrTip($('#txtObservedTemp'), 35, 1);
			
		else if( $('#txtObservedTemp').val() < 45 ) requrTip($('#txtObservedTemp'), 35, "Observed temperature should be 45&#176;F or higher");
			
		else {
			var tempVal = $('#txtObservedTemp').val() - 60
			$('#txtAPIgrav60').val(  Math.round((($('#txtAPIgravT').val() - 0.059175 * tempVal) / (1 + 0.00045 * tempVal))*10) / 10 );
		}
	});
	$('.reset').click(function(){
		$("#tip").remove();
		$('input').removeClass('errorInput').val('');
	});
});

function showDialog(elem) {
elem.dialog({
    bgiframe: true,
    autoOpen: false,
    //height: 510,
    //width: 700,
    modal: true,
    resizable: false,
	buttons: { 'Close': function() { elem.dialog('close'); } }
	});
elem.show();
elem.dialog('open');
}

function requrTip(el, dist, mes) {
	if (mes == 0) var message = 'Required field';
	else if (mes == 1) var message = 'Should be one decimal point';
	else var message = mes;
	el.addClass('errorInput').focus();
	showTip(dist, message);
}

function showTip(topPos, mess) {
	var position = topPos + "px";

	var tipDiv = '<div id="tip"><table cellpadding="0" cellspacing="0" width="100%"><tr><td id="tipBody"></td><td id="tipLeft">&nbsp;</td></tr></table></div>';

	$("#tabs").append(tipDiv);
	$("#tipBody").html(mess);
	$("#tip").css({top:position});
}
