$(document).ready(function () {
	
	$('#popInviteFriend').click(function (e) {

		e.preventDefault();		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		$('h3.inviteMessage').show();
		$('h3.inviteMessageThanks').hide();			

		//Set the popup window to center
		$('#modalInviteFriend').css('top',  winH/2-$('#modalInviteFriend').height());
		$('#modalInviteFriend').css('left', winW/2-$('#modalInviteFriend').width()/2);
	
		$('#modalInviteFriend').fadeIn('fast'); 


	});

	
	$(window).resize(function () {


		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		$('#modalInviteFriend').css('top',  winH/2-$('#modalInviteFriend').height());
		$('#modalInviteFriend').css('left', winW/2-$('#modalInviteFriend').width()/2);
		
	});
	

	$('#inviteModal').click(function (e) {
		
		e.preventDefault();		
		
		name = $('#invite-name');
		nameval = $('#invite-name').val();
		email = $('#invite-email');
		emailval = $('#invite-email').val();
		user = $('#invite-user').val();
		userid = $('#invite-id').val();
		usermail = $('#invite-usermail').val();

		
		error=0;
		
		if (nameval == '') {
			$('#invite-name').addClass('highlight');
			error=1;
		} else $('#invite-name').removeClass('highlight');

		if (emailval == '' || !isEmail(emailval)) {
			$('#invite-email').addClass('highlight');
			error=1;
		} else $('#invite-email').removeClass('highlight');

		if (error) return false;
		else {
		
			
			data =  'id=' + userid + '&user=' + user + '&usermail=' + usermail + '&name=' + nameval + '&email=' + emailval;

			$.ajax({
				type: "GET",
				url: "invite-friend.php",
				data: data,
				cache: false,
				success: function (html) {

					if (html) {

						$('#invite-name').val('');
						$('#invite-email').val('');
						$('h3.inviteMessage').hide();
						$('h3.inviteMessageThanks').show();			
			
						//t = setTimeout("$('#modalInviteFriend').fadeOut('fast')", 5000);
					} else alert('Unexpected error. Please try again later.');
				}
			});

		}
		
	});			


	$('#closeModal').click(function (e) {

		e.preventDefault();		
		$('#modalInviteFriend').fadeOut('fast');
		$('#invite-name').val('');
		$('#invite-email').val('');
	});			


	$('#doneModal').click(function (e) {

		e.preventDefault();		
		$('#modalInviteFriend').fadeOut('fast');
		clearTimeout(t);
	});			


/* Change Password */


	$('#changePassword').click(function (e) {

		e.preventDefault();		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		$('#changePassword1').show();
		$('#changePassword2').hide();			

		//Set the popup window to center
		$('#modalChangePassword').css('top',  winH/2-$('#modalChangePassword').height());
		$('#modalChangePassword').css('left', winW/2-$('#modalChangePassword').width()/2);
	
		$('#modalChangePassword').fadeIn('fast'); 


	});


	$('#resetPassword').click(function (e) {
		
		e.preventDefault();		

		password = $('#password').val();
		repeatPassword = $('#repeatPassword').val();
		userID  = $('#userID').val();
		
		
		error=0;
		
		if (password == '' ||password != repeatPassword) {
			$('#password').addClass('highlight');
			error=1;
		} else $('#password').removeClass('highlight');
		

		if (error) return false;
		else {
			$.ajax({
				type: "GET",
				url: "change-password.php",
				data: 'id=' + userID + '&pass=' + password,
				cache: false,
				success: function (html) {
					$('#changePassword1').hide();
					$('#changePassword2').show();			
					t = setTimeout("$('#modalChangePassword').fadeOut('fast')", 5000);
				}
			});
		}
		
	});			

	$('#closeModalChange').click(function (e) {

		e.preventDefault();		
		$('#modalChangePassword').fadeOut('fast');
	});			


	$('#doneModalChange').click(function (e) {

		e.preventDefault();		
		$('#modalChangePassword').fadeOut('fast');
		clearTimeout(t);
	});			




	
	$(window).resize(function () {


		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();

		//Set the popup window to center
		$('#modalMeal').css('top',  winH/2-$('#modalMeal').height());
		$('#modalMeal').css('left', winW/2-$('#modalMeal').width()/2);
		
	});
	
	$('#closeModalMeal').click(function (e) {

		e.preventDefault();		
		$('#modalMeal').fadeOut('fast');
	});			


	$('#closeModalNewWish').click(function (e) {

		e.preventDefault();		
		$('#modalNewWish').fadeOut('fast');
		$('#wishvalue').val('');
	});			

	
});


function popMenuLog() {
	
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();

	$('#invitePanel1').show();
	$('#invitePanel2').hide();			
	
	//Set the popup window to center
	$('#modalMeal').css('top',  winH/2-$('#modalMeal').height());
	$('#modalMeal').css('left', winW/2-$('#modalMeal').width()/2);

	$('#modalMeal').fadeIn('fast'); 
	
}


function popNewWish() {
	
	//Get the window height and width
	var winH = $(window).height();
	var winW = $(window).width();
	
	//Set the popup window to center
	$('#modalNewWish').css('top',  winH/2-$('#modalNewWish').height());
	$('#modalNewWish').css('left', winW/2-$('#modalNewWish').width()/2);

	$('#modalNewWish').fadeIn('fast'); 
	
}




function isNumber(s) {
	return (s.toString().search(/^-?[0-9]+$/) == 0);
}

function isEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;					
	}

