$(document).ready(function(){
	$('.checkbox .imgCheckbox').click(function()
	{
		var state = !$(this).siblings('input').attr('checked');
		var prefix = '';
		if(state === false){ prefix = 'un'; }
		
		//mutually exclusive checkbox logic
		if($(this).siblings('input').attr('data-none') == 'none'  && state)
		{
			$('.checkbox input').attr('checked','').siblings('img').attr('src','/kiosk/assets/img/unchecked.png');
		}
		else
		{
			$('#doNotContact').attr('checked','').siblings('img').attr('src','/kiosk/assets/img/unchecked.png');
		}
		
		//check or uncheck what we clicked
		$(this).attr('src','/kiosk/assets/img/'+prefix+'checked.png').siblings('input').attr('checked',state);
	});
	
	$('#imgClear').click(resetForm);
	$('#imgSubmit').click(submitForm);
	$('#closeClick').click(function(){ counter = 0; ticDown(0); });
	
	$('.numbersOnly').keydown(function(e)
	{
		var key = e.charCode || e.keyCode || 0;
		// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
		return (
			key == 8 || 
			key == 9 ||
			key == 46 ||
			(key >= 37 && key <= 40) ||
			(key >= 48 && key <= 57) ||
			(key >= 96 && key <= 105));
	});
	
	$('#closeForm').click(function(){$('#hoverForm').fadeOut(500)});
	
	$('#phone').mask("(999) 999-9999");
});

function resetForm()
{
	$('.text input').attr('value','');
	$('.checkbox input').attr('checked','').siblings('img').attr('src','/kiosk/assets/img/unchecked.png');
	$('.checkbox input.defaultChecked').attr('checked','checked').siblings('img').attr('src','/kiosk/assets/img/checked.png');;
}

function submitForm()
{
	if(validateForm())
	{
		$.ajax({
			url:'/kiosk/submit.php',
			type:'POST',
			data:$('form').serialize(),
			success: function(data)
			{
				thankYou();
			},
			error: function()
			{
				clearInterval(counterInterval);
				$('#tyWrap').fadeOut(1,function()
				{
					alert("Sorry,\nA connection to the server could not be made.  Please request assistance to reset the internet conenction.");						
				});
			}
		});
		
	}
	
}





var counterTime = 5
var counter = counterTime;
var counterInterval;
function thankYou()
{
	$('#kioskForm').hide();
	$('#tyWrap').fadeIn(500,function()
	{
		counterInterval = setInterval(ticDown,1000);
	});
}

function ticDown()
{
	counter--;
	$('#thankYou #counter').html(counter);
	if(counter < 1)
	{
		clearInterval(counterInterval);
		resetForm();
		$('#thankYou').fadeOut(500,function()
		{
			counter = counterTime;
			$('#thankYou #counter').html('5');
		});
		$('#hoverForm').fadeOut(500);
	}
}


function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function validateForm()
{
	var isValid = true;
	$('.requiredField').each(function()
	{
		if($(this).attr('value').length < 2)
		{
			alert('A Required Field ('+$(this).attr('name')+') was not completed');
			isValid = false;
		}
	});
	
	if(!isValidEmailAddress($('#email').attr('value')))
	{
		alert('Email is not valid format');
		isValid = false;
	}
	
	//checkboxes
	var checked = false;
	$('.checkbox input').each(function()
	{
		if($(this).attr('checked'))
		{	
			checked = true;
		}
	});
	if(!checked)
	{
		alert('You must check at least one box above.');
		isValid = false;
	}
	return isValid;
}
