$(document).ready(function(){

	$('#ask_text').bind('keypress', function(e) {
	
		var c = $('#ask_text').val();
		if(c.length <= 140)
		{
			$('#ask_counter').val(140 - c.length);
			return true;
		}
		else
		{
			if(e.which != 8)
				return false;
		}
		
	});

	$('#ask_text').focus(function()
	{
		var c = $('#ask_text').val();
		if(c == 'Submit your question here, or tweet @globomaestro')
			$('#ask_text').val('');
	});

	$('#ask_text').blur(function()
	{
		var c = $('#ask_text').val();
		if(c == '')
			$('#ask_text').val('Submit your question here, or tweet @globomaestro');
	});

	$('#term').focus(function(){
		var t = $('#term').val();
		if(t == 'Enter city, country, keyword')
			$('#term').val('');
	});

	$('#term').blur(function(){
		var t = $('#term').val();
		if(t == '')
			$('#term').val('Enter city, country, keyword');
	});

});

function gradient(id, level)
{
	var box = document.getElementById(id);
	box.style.opacity = level;
	box.style.MozOpacity = level;
	box.style.KhtmlOpacity = level;
	box.style.filter = "alpha(opacity=" + level * 100 + ")";
	box.style.display="block";
	return;
}


function fadein(id) 
{
	var level = 0;
	while(level <= 1)
	{
		setTimeout( "gradient('" + id + "'," + level + ")", (level* 1000) + 10);
		level += 0.01;
	}
}

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
}

function isUrl(s) {
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}


function subscribeForm()
{
	$('#reg_messages').hide("medium");

	var un = $('#reg_username').val();
	var pw = $('#reg_pw').val();
	var pwc = $('#reg_pwconfirm').val();
	var email = $('#reg_email').val();
	var country = $('#reg_country').val();
	var zip = $('#reg_zip').val();
	var website = $('#reg_website').val();
	var age = $('#reg_age').val();
	var gen1 = $('#reg_gen1').is(':checked');
	var gen2 = $('#reg_gen2').is(':checked');
	var sub = $('#reg_sub').is(':checked');
	var terms = $('#reg_terms').is(':checked');
	
	var errors = 0;
	var html = '';

	if(un == '')
	{
		html += 'Please enter a username.<br />';
		errors += 1;
	}
	else
	{
		$.ajax({
			  type: "POST",
			  url: '/home/validateUser',
			  data: ({username:un}),
			  success: function(data) {
				  if(data == 'false')
				  {
					html += 'That username is already in use. Please try another one.<br />';
					errors += 1;
				  }
			  },
			  async: false
		});	
	}

	if(pw == '')
	{
		html += 'Please enter a password.<br />';
		errors += 1;
	}

	if(pwc == '')
	{
		html += 'Please confirm your password.<br />';
		errors += 1;
	}

	if(pw != '' && pwc != '' && (pw != pwc))
	{
		html += 'Please make sure your password matches your confirm password.<br />';
		errors += 1;
	}

	if(email == '' || validateEmail(email) == false)
	{
		html += 'Please enter a valid email address.<br />';
		errors += 1;
	}
	else
	{
		$.ajax({
			  type: "POST",
			  url: '/home/validateEmail',
			  data: ({email:email}),
			  success: function(data) {
				  if(data == 'false')
				  {
					html += 'That email address is already registered.<br />';
					errors += 1;
				  }
			  },
			  async: false
		});	
	}

	if(website != '' && !isUrl(website))
	{
		html += 'Please make sure your website is a valid. ex: http://www.globomaestro.com';
		errors += 1;
	}

	if(country == 'null')
	{
		html += 'Please select a valid country.<br />';
		errors += 1;
	}

	if(zip == '' && country == 'United States')
	{
		html += 'Please enter your zip code.<br />';
		errors += 1;
	}

	if(!terms)
	{
		html += 'Please review the terms and privacy policy, then click the box to verify you have read them.<br />';
		errors += 1;
	}

	if(errors == 0)
	{
		var gen = 0;
		if(gen2 == true)
			gen = 1;
		
		var newsletter = 0;
		if(sub == true)
			newsletter = 1;

		$.ajax({
			  type: "POST",
			  url: '/home/registerUser',
			  data: ({username:un, password:pw, email:email, country:country, zip:zip, website:website, age:age, gender:gen, newsletter:newsletter}),
			  success: function(data) {
				$('#reg_form').fadeOut("medium");
				$('#reg_messages').html('Thank you for registering with GloboMaestro. A message has been sent to the email address you provided. When you click the link in the email, your account will be activated and you can participate on the site.');
				$('#reg_messages').css({color:'#1c9300'});
				setTimeout(function(){
					$('#reg_messages').fadeIn("slow");
				}, 800);
			  },
			  async: false
		});
	}
	else
	{
		$('#reg_messages').html(html);
		$('#reg_messages').css({color:'#ff0000'});
		$('#reg_messages').show("medium");
	}
}

function tryLogin()
{
	var un = $('#login_username').val();
	var pw = $('#login_password').val();
	var log = $('#keep-me').is(':checked');
	var keep = 'false';
	if(log == true)
		keep = 'true';


	$.ajax({
		  type: "POST",
		  url: '/home/validateLogin',
		  data: ({username:un, password:pw, keep:keep}),
		  success: function(data) {
			  if(data == 'false')
			  {
				$('#login_errors').fadeIn("medium");
			  }
			  else
			  {
				window.location = data;
			  }	
		  },
		  async: false
	});	
	
}

function trySubLogin()
{
	var un = $('#sub_login_username').val();
	var pw = $('#sub_login_password').val();

	$.ajax({
		  type: "POST",
		  url: '/home/validateLogin',
		  data: ({username:un, password:pw}),
		  success: function(data) {
			  if(data == 'false')
			  {
				$('#sub_login_errors').fadeIn("medium");
			  }
			  else
			  {
				window.location = data;
			  }	
		  },
		  async: false
	});	
	
}


function submitQuestion()
{
	$('#ask_errors').html("Please make sure your email is valid and you submit a question.");

	var em = $('#ask_email').val();
	var content = $('#ask_text').val();

	if(validateEmail(em) && content != '')
	{
		$.ajax({
			  type: "POST",
			  url: '/home/askConcierge',
			  data: ({email:em, content:encodeURIComponent(content)}),
			  success: function(data) {
				if(data == "success")
				{
					$('#lightbox04').css('display', 'block');
					$('#filter').css('display', 'block');

					$('#lightbox05').css('display', 'none');
					$('#filter01').css('display', 'none');
				}
				else
				{
					$('#ask_errors').html("Error. Please try again.");
					$('#ask_errors').fadeIn("medium");
					setTimeout(function(){
						$('#ask_errors').fadeOut("medium");	
					}, 3000);
				}
			  }
		});
	}
	else
	{
		$('#ask_errors').fadeIn("medium");
	}
}

function submitForgot()
{
	$('#forgot_errors').html("Please enter a valid email address.");
	var em = $('#forgot_email').val();

	if(validateEmail(em))
	{
		$.ajax({
			  type: "POST",
			  url: '/home/sendForgot',
			  data: ({email:em}),
			  success: function(data) {
				if(data == "success")
				{
					$('#lightbox06').css('display', 'block');
					$('#filter').css('display', 'block');

					$('#lightbox07').css('display', 'none');
					$('#filter01').css('display', 'none');
				}
				else
				{
					$('#forgot_errors').html("Error. Please try again.");
					$('#forgot_errors').fadeIn("medium");
					setTimeout(function(){
						$('#forgot_errors').fadeOut("medium");	
					}, 3000);
				}
			  }
		});
	}
	else
	{
		$('#forgot_errors').fadeIn("medium");
	}
}

function openJoin(type)
{
	if(type == 'new')
		$('#join_header').html('Why Join the Conversation?');
	else
		$('#join_header').html('Success! Now Join the Conversation.');
	
	openbox01();
	updateZip();
}

$(document).ready(function(){
	$('#reg_country').change(function(){
		updateZip();
	});	

	updateZip();
});

function updateZip()
{
	var country = $('#reg_country').val();

	if(country == 'United States')
	{
		$('#join_zcr').fadeIn('medium');
	}
	else
	{
		$('#join_zcr').fadeOut('medium');
	}
}

// Open the lightbox

function subscribePrecheck(type)
{
	var em = $('#' + type + '_subemail').val();

	if(validateEmail(em))
	{
		$('#reg_email').val(em);

		$.ajax({
			  type: "POST",
			  url: '/home/blindSubscribe',
			  data: ({email:em}),
			  success: function(data) {
				if(data == 'success')
				{
					$('html,body').animate({scrollTop: 0}, 500);
					openJoin(type);
				}
				else if(data == 'updated')
				{
					$('#' + type + '_suberr').html('Your account has been updated.');
					$('#' + type + '_suberr').fadeIn("medium");
					setTimeout(function(){
						$('#' + type + '_suberr').fadeOut("medium");
					}, 3000);
					setTimeout(function(){
						$('#' + type + '_suberr').html('Please enter a valid email address.');
					}, 3500);
				}
				else if(data == 'login')
				{
					$('#' + type + '_suberr').html('Please login to update your account.');
					$('#' + type + '_suberr').fadeIn("medium");
					setTimeout(function(){
						$('#' + type + '_suberr').fadeOut("medium");
					}, 3000);
					setTimeout(function(){
						$('#' + type + '_suberr').html('Please enter a valid email address.');
					}, 3500);
				}
			  }
		});

		
	}
	else
	{
		$('#' + type + '_suberr').show("medium");
	}
}

function openAcct()
{
	$('#acct_messages').html('');
	$('#acct_messages').hide("medium");

	$.ajax({
		  type: "POST",
		  url: '/home/fetchUserAcct',
		  data: ({}),
		  dataType: 'json',
		  success: function(data) {
	
				$('#acct_id').val(data.id);
				$('#acct_un').val(data.username);
				$('#acct_em').val(data.email);
				$('#acct_zip').val(data.zipcode);
				$('#acct_website').val(data.website);

				$('#acct_country option').each(function(i){
					if($(this).val() == data.country)
						$(this).attr('selected', true);
				});

				$('#acct_age option').each(function(i){
					if($(this).val() == data.age)
						$(this).attr('selected', true);
				});

				if(data.gender == 0)
				{
					$('#acct_gm').attr('checked', true);
					$('#acct_gf').attr('checked', false);
				}
				else
				{
					$('#acct_gm').attr('checked', false);
					$('#acct_gf').attr('checked', true);
				}

				if(data.newsletter == 0)
				{
					$('#acct_nl').attr('checked', false);
				}
				else
				{
					$('#acct_nl').attr('checked', true);
				}


				$('#lightbox03').css('display', 'block');
				$('#filter').css('display', 'block');
			  
		  },
		  async: false
	});	

}

function updateAcct()
{
	$('#acct_messages').html('');
	$('#acct_messages').hide("medium");

	var id = $('#acct_id').val();
	var un = $('#acct_un').val();
	var em = $('#acct_em').val();
	var zip = $('#acct_zip').val();

	var pw = $('#acct_pw').val();
	var npw = $('#acct_npw').val();
	var npwc = $('#acct_npwc').val();

	var country = $('#acct_country').val();
	var website = $('#acct_website').val();
	var age = $("#acct_age").val();
	var nl = 0;
	var gender = 0;
	var errors = 0;
	var html = "";

	var update_pw = 'false';

	if($('#acct_nl').is(':checked'))
	{
		nl = 1;
	}

	if($('#acct_gf').is(':checked'))
	{
		gender = 1;
	}

	if(id == '')
	{
		html += "There is no record of this account.<br />";
		errors += 1;
	}

	if(em == '' || !validateEmail(em))
	{
		html += "Please enter a valid email address.<br />";
		errors += 1;
	}

	if(errors == 0)
	{
		if(npw != '' && npwc != '')
		{
			if(npw != npwc)
			{
				errors += 1;
				html += "Please make sure your new password matches the confirm password field.<br />";
			}
			else
			{
				update_pw = 'true';
			}
		}
		
		if(errors == 0)
		{
			$.ajax({
				  type: "POST",
				  url: '/home/updateUserAcct',
				  data: ({password:pw, id:id, username:un, email:em, zip:zip, country:country, update_pw:update_pw, new_password:npw, website:website, age:age, nl:nl, gender:gender}),
				  success: function(data) {
					if(data == "success")
					{
						$('#acct_message strong').html("Thank you for updating your account!");
					}
					else
					{
						$('#acct_message strong').html("There was an error updating your account. Please check the form and try again.");
					}

					$('#acct_message').fadeIn("medium");
					setTimeout(function(){
						$('#acct_message').fadeOut("medium");
					}, 4000);
					  
				  },
				  async: false
			});
		}
	}

	if(errors > 0)
	{
		$('#acct_messages').html(html);
		$('#acct_messages').show("medium");
	}
}


function openbox(formtitle, fadin)
{
  var box = document.getElementById('light-box'); 
  document.getElementById('filter').style.display='block';

  

  if(fadin)
  {
	 gradient("light-box", 0);
	 fadein("light-box");
  }
  else
  { 	
    box.style.display='block';
  }
  
}


// Close the lightbox

function closebox()
{
   document.getElementById('light-box').style.display='none';
   document.getElementById('filter').style.display='none';
}


// Open the lightbox01


function openbox01(formtitle, fadin)
{
  var box = document.getElementById('lightbox01'); 
  document.getElementById('filter01').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox01", 0);
	 fadein("lightbox01");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox01

function closebox01()
{
   document.getElementById('lightbox01').style.display='none';
   document.getElementById('filter01').style.display='none';
}

// Open the lightbox03


function openbox03(formtitle, fadin)
{
  var box = document.getElementById('lightbox03'); 
  document.getElementById('filter').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox03", 0);
	 fadein("lightbox03");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox03

function closebox03()
{
   document.getElementById('lightbox03').style.display='none';
   document.getElementById('filter').style.display='none';
}



// Open the lightbox04


function openbox04(formtitle, fadin)
{
  var box = document.getElementById('lightbox04'); 
  document.getElementById('filter').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox04", 0);
	 fadein("lightbox04");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox04

function closebox04()
{
   document.getElementById('lightbox04').style.display='none';
   document.getElementById('filter').style.display='none';
}



// Open the lightbox05


function openbox05(formtitle, fadin)
{
  var box = document.getElementById('lightbox05'); 
  document.getElementById('filter01').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox05", 0);
	 fadein("lightbox05");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox05

function closebox05()
{
   document.getElementById('lightbox05').style.display='none';
   document.getElementById('filter01').style.display='none';
}











// Open the lightbox06


function openbox06(formtitle, fadin)
{
  var box = document.getElementById('lightbox06'); 
  document.getElementById('filter').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox06", 0);
	 fadein("lightbox06");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox06

function closebox06()
{
   document.getElementById('lightbox06').style.display='none';
   document.getElementById('filter').style.display='none';
}



// Open the lightbox07


function openbox07(formtitle, fadin)
{
  var box = document.getElementById('lightbox07'); 
  document.getElementById('filter01').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox07", 0);
	 fadein("lightbox07");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox07

function closebox07()
{
   document.getElementById('lightbox07').style.display='none';
   document.getElementById('filter01').style.display='none';
}

// Open the lightbox08


function openbox08(formtitle, fadin)
{
  var box = document.getElementById('lightbox08'); 
  document.getElementById('filter').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox08", 0);
	 fadein("lightbox08");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox08

function closebox08()
{
   document.getElementById('lightbox08').style.display='none';
   document.getElementById('filter').style.display='none';
}



// Open the lightbox09


function openbox09(formtitle, fadin)
{
  var box = document.getElementById('lightbox09'); 
  document.getElementById('filter01').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox097", 0);
	 fadein("lightbox09");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox09

function closebox09()
{
   document.getElementById('lightbox09').style.display='none';
   document.getElementById('filter01').style.display='none';
}


























// Open the lightbox10


function openbox10(formtitle, fadin)
{
  var box = document.getElementById('lightbox10'); 
  document.getElementById('filter').style.display='block';

  
  
  if(fadin)
  {
	 gradient("lightbox10", 0);
	 fadein("lightbox10");
  }
  else
  { 	
    box.style.display='block';
  }  	
}


// Close the lightbox10

function closebox10()
{
   document.getElementById('lightbox10').style.display='none';
   document.getElementById('filter').style.display='none';
}
