var mySelect = '<div id="selectComboBox"><p id="selectReminderReplace" class="SelectReplace"></p><ul id="selectReminderList" class="SelectReplace"></ul><div>';

$(document).ready( function() {
	//Showoff checkbox
	$('#reminderMe').addClass('Disappear');
	$('#reminderMeLabel').addClass('CheckReplace');
	
	//Showoff select
	$('#selectReminder').append(mySelect);
	$('#reminder').addClass('Disappear');
	
	var myList = '';
	var firstValue = '';
	var count = 0;
	$('#reminder option').each(function(){
		var temp = $(this).text();
		if (count == 0) firstValue = temp;
		myList += '<li>'+temp+'</li>';
		count++;		
	});
	$('#selectReminderList').html(myList);
	//Handler my checkbox
	if ($('#reminderMe').attr('checked') == true) $('#reminderMeLabel').addClass('CheckReplaceActive');
	$('#reminderMeLabel').click(checkMe);
	
	//Handler my select
	$('#selectReminderReplace').text($('#reminder :selected').text().substring(0,21));
	$('#selectComboBox').hover(function(){
		$('#selectReminderReplace').click(function(){$('#selectReminderList').fadeIn('fast');});
	},function(){
		$('#selectReminderList').fadeOut('fast');
	});
	
	$('#selectReminderList li').hover(function(){
		$(this).addClass('OnSelect');
	}, function() {
		$(this).removeClass('OnSelect');
	});
	
	$('#selectReminderList li').click(function(){
		var hixhix = $(this).text();
		$('#selectReminderReplace').text(hixhix.substring(0,21));
		$('#reminder option:contains("'+hixhix+'")').attr('selected',true);
		$('#selectReminderList').hide();
	});
	
	//Handler reset button
	$('#reset').click(function(){
		$('#reminder option[@value="'+firstValue+'"]').attr('selected',true);
		$('#selectReminderReplace').text(firstValue);
		
		$('#yourNameError').removeClass('Error')
		$('#yourName').val('');
		$('#yourName').focus();
		
		$('#yourEmailError').removeClass('Error')
		$('#yourEmail').val('');
		
		$('#recipientNameError').removeClass('Error')
		$('#recipientName').val('');
		
		$('#recipientEmailError').removeClass('Error')
		$('#recipientEmail').val('');
		
		if ($('#reminderMe').attr('checked') != true) {
			checkMe();
			$('#reminderMe').attr('checked',true);
		}
		
		return false;
	});
	
	//Lets validate
	$('#reminderForm').submit(function(){
		
		if($('#yourName').val().length < 2) {
			$('#yourNameError').addClass('Error');
			$('#yourName').focus();
			return false;
		} else {
			$('#yourNameError').removeClass('Error');
		}
		
		if(! $('#yourEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#yourEmailError').addClass('Error');
			$('#yourEmail').focus();
			return false;
		} else {
			$('#yourEmailError').removeClass('Error');
		}
		
		if($('#recipientName').val().length < 2) {
			$('#recipientNameError').addClass('Error');
			$('#recipientName').focus();
			return false;
		} else {
			$('#recipientNameError').removeClass('Error');
		}
		
		if(! $('#recipientEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#recipientEmailError').addClass('Error');
			$('#recipientEmail').focus();
			return false;
		} else {
			$('#recipientEmailError').removeClass('Error');
		}
		
	});

	$('#yourName').bind('blur',function(){
		if($('#yourName').val().length < 2) {
			$('#yourNameError').addClass('Error');
		} else {
			$('#yourNameError').removeClass('Error');
		}					   
	});
	$('#yourEmail').bind('blur',function(){
		if(! $('#yourEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#yourEmailError').addClass('Error');
		} else {
			$('#yourEmailError').removeClass('Error');
		}								
	});
	$('#recipientName').bind('blur',function(){
		if($('#recipientName').val().length < 2) {
			$('#recipientNameError').addClass('Error');
		} else {
			$('#recipientNameError').removeClass('Error');
		}							 
	});
	$('#recipientEmail').bind('blur',function(){
		if(! $('#recipientEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#recipientEmailError').addClass('Error');
		} else {
			$('#recipientEmailError').removeClass('Error');
		}										 
	});
	
});

function checkMe() {
	$('#reminderMeLabel').toggleClass('CheckReplaceActive');
}
