function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function loadCitiesByStateCode(state_code, wrap_elem)
{	
	startAnimation();
	$('#city_ID').html('<option value="">Loading...</option>');
	
	if (!wrap_elem)
	{
		var wrap_elem = 'place_cities';
	}

	$.post(
	  	SUBDIR + '/cities/loadCitiesByStateCode/' + state_code + '/' + wrap_elem,
	  	{},
	  	onAjaxSuccess
	);
	
	function onAjaxSuccess(data)
	{
		$('#' + wrap_elem).html(data);
		finishAnimation();		
	}
}

function loadCoursesByCityName(city_name, wrap_elem)
{	
	startAnimation();
	$('#course_ID').html('<option value="">Loading...</option>');
	
	if (!wrap_elem)
	{
		var wrap_elem = 'place_courses';
	}

	$.post(
	  	SUBDIR + '/courses/loadCoursesByCityName/' + city_name + '/' + $('#stateSelect').val() + '/' + wrap_elem,
	  	{},
	  	onAjaxSuccess
	);
	
	function onAjaxSuccess(data)
	{
		$('#' + wrap_elem).html(data);
		finishAnimation();		
	}
}

function startAnimation()
{
	return true;
}

function finishAnimation()
{
	return true;
}

$(document).ready(function() {
	$('#select_country').bind('change', function(){
		if ($(this).val() == '0')
		{
			$('#select_country').selectOptions('223');
		}

		if ($(this).val() == 223)
		{
			$('#select_state').attr('disabled', false);
		}
		else
		{
			$('#select_state').attr('disabled', true);
			$('#select_state').selectOptions('0');
		}
	}).change();    
});

function isInteger(s)
{
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}
$(document).ready(function() {
	$('tr.link').bind('click', function(){
		window.location = $(this).attr('link');
	});
});
$(document).ready(function() {
	$('a.deleteConfirm').bind('click', function(){
		$this = $(this);
		$.modaldialog.custom('Are you sure you want to delete?', {
			'title': 'Warning!',
			'width': 300,
			'buttons': {
				'btnOK': {
					'caption': 'OK',
					'click': function(){
						window.location = $this.attr('href');
					}	
				},
				'btnCancel': {
					'caption': 'Cancel'
				}
			}
		});
		
		return false;
	});
});
$(document).ready(function() {
//	$('.customPanel').css('opacity', 0.85);
});
function calculateScore() 
{
	var total;
	var strokes = new Array();
	
  	var stroketypes = new Array("eag","bir","par","bog","dou","tri");

	for (var i=0;i<stroketypes.length;i++) 
	{	
		if ($("#" + stroketypes[i]).val()) 
		{		
			strokes[i] = parseInt($("#" + stroketypes[i]).val());
		} 
		else 
		{	
			strokes[i] = 0;
		}
	}
	total = (strokes[0]) + (strokes[1]) + (strokes[2]) + (strokes[3]) + (strokes[4]) + (strokes[5]); 

//	alert(total);
	if (total != 18)
	{
		alert('Try again. The combination needs to be 18 holes total!');			
		
		return false;
	}

	if (!$('#course_ID').val())
	{
		alert('You should select a Course!');

		return false;
	} 

	var weight = $('#course_ID option:selected').attr('weight');

	if (!weight)
	{
		alert('This Golf Course will be available soon!');

		return false;
	} 

	//eagle ka value 61/1000  birdies ka 49/1000  par 37/1000 bogey 25/1000 double 13/1000, triple 1/1000    textbox values * these values ka sumation

	var eag_score = ((62/1000) * strokes[0]);
	var bir_score = ((50/1000) * strokes[1]);
	var par_score = ((38/1000) * strokes[2]);
	var bog_score = ((26/1000) * strokes[3]);
	var dou_score = ((14/1000) * strokes[4]);
	var tri_score = (( 2/1000) * strokes[5]);
	
	var spl_score = eag_score + bir_score + par_score + bog_score + dou_score + tri_score;
	var result = Math.round(spl_score*1000)/1000;			

	//weight = 126; result = 0.470;//test
	result = result * 1.153 * 72 / weight + 129.04 / 90 - 1.792 * 72 / weight;
	
	if (result > 0.950)
	{
		result = '0.950';
	}
	else if (result < 0.318)
	{
		result = '0.318';
	}
	
	var sc_res = getSplashieCounter(result);
	var score_value = format_score(result);

	$('#sc_res').html(sc_res);
	$('#score_value').val(score_value);

	return true;
}
function getSplashieCounter(score_value)
{
	var value = $.sprintf('%01.3f', score_value).toString();
	var html = '';

	for (var i = 0; i < value.length; i++)
	{
		 if (i == 0 && value[i] == '0')
		 {
		 	continue;
		 }
		 
		 html += '<span class="';
		 
		 if (isInteger(value.charAt(i)))
		 {
			 html += 'c' + value.charAt(i);
		 }
		 else
		 {
			 html += 'dot';
		 }
		 
		 html += '"></span>';
	}

	return html;
}

format_score = function(score_value) {
	return score_value.toString().substr(1, 4);
}

fixFbPicture = function(e)
{
	$(e).attr('src', $(e).attr('__src'));
}

$(document).ready(function() {
	// To prevent Par edits from non-figures
	$('.par').keypress(function(e){
		return isNumberKey(e)
	});

	// To prevent Round Date edits from non-figures	
	$('#round_date').keypress(function(e){
		return isNumberKey(e)
	});

	$("table.zebra").find("tr:even").addClass('even');
	$("table.zebra").find("tr:odd").addClass('odd');
});

