$(document).ready(function() {
	try{$( "#tabs" ).tabs();}catch(e){}
});


function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
		
function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
		
$(document).ready(function() {
	$('.custom_select[name="day"]').parent().parent().css({width:"28%",float:"left",margin:"0 0 0 5px"});
	$('.custom_select[name="month"]').parent().parent().css({width:"28%",float:"left",margin:"0 0 0 5px"});
	$('.custom_field[name="year"]').parent().parent().css({width:"28%",float:"left",margin:"0 0 0 5px"});
	$('.custom_field[name="childs_birthdate"]').hide();

	$('.custom_select[name="day"]').change(function(){
		$('.custom_field[name="childs_birthdate"]').val($('.custom_select[name="day"]').val()+"/"+$('.custom_select[name="month"]').val()+"/"+$('.custom_field[name="year"]').val());
	});

	$('.custom_select[name="month"]').change(function(){
		$('.custom_field[name="childs_birthdate"]').val($('.custom_select[name="day"]').val()+"/"+$('.custom_select[name="month"]').val()+"/"+$('.custom_field[name="year"]').val());
	});

	$('.custom_field[name="year"]').change(function(){
		$('.custom_field[name="childs_birthdate"]').val($('.custom_select[name="day"]').val()+"/"+$('.custom_select[name="month"]').val()+"/"+$('.custom_field[name="year"]').val());
	});
	
	// Register box show/hide
	$('#register').click(function(e) {
		$('.box_reg_hider').show();
		$('.box_reg').show();
		e.stopPropagation();
	});
	$(document).click(function() {
		// Hide register box
		$('.box_reg_hider').hide();
		$('.box_reg').hide();
	});
	$('.box_reg').click(function() {
		return false;
	});
	
	// Update register form fields to swap the default values on focus
	$('.reg_formfields .reg_text').focusout(function() {
		if ($(this).val() == '')
			$(this).val($(this).attr('placeholder'));
	});
	$('.reg_formfields .reg_text').focusin(function() {
		if ($(this).val() == $(this).attr('placeholder'))
			$(this).val('');
	});
	
	// Update register form passwords to swap
	$('.reg_formfields .reg_password2').focusout(function() {
		if ($(this).val().length === 0) {
			$(this).hide();
			$(this).prev().show();
		}
	});
	$('.reg_formfields .reg_password').focusin(function() {
		$(this).hide();
		$(this).next().show();
		$(this).next().focus();
	});
	
	// Select the gender because it is unselectable in HTML for register form
	$('.male_gender').click(function() {
		$('#male_gender').attr('checked', 'checked');
		$(this).attr('checked', 'checked');
	});
	$('.female_gender').click(function() {
		$('#female_gender').attr('checked', 'checked');
	});
	$('#male_gender').focus(function() {
		$(this).attr('checked', 'checked');
	});
	$('#female_gender').focus(function() {
		$(this).attr('checked', 'checked');
	});
	
	// Toggle monitor on/off for register form
	$('.monitor_on').click(function() {
		$('.monitor_off').css('color', '#999999');
		$('.monitor_off').removeClass('selected');
		
		$(this).css('color', '#ffbbb6');
		$(this).addClass('selected');
	});
	$('.monitor_off').click(function() {
		$('.monitor_on').css('color', '#999999');
		$('.monitor_on').removeClass('selected');
		
		$(this).css('color', '#ffbbb6');
		$(this).addClass('selected');
	});
	
	// Submit and register with register form
	var monitor = 'On';
	$('#register_submit').click(function(){
		user_login = $('#user_login').attr('value');
		user_email = $('#user_email').attr('value');
		if(document.getElementsByName('register_gender')[0].checked) { gender = 'Male'; } else { gender = 'Female'; }
		childs_birthdate_month = document.getElementsByName('birth_month')[0].value;
		childs_birthdate_day = document.getElementsByName('birth_day')[0].value;
		childs_birthdate_year = document.getElementsByName('birth_year')[0].value;
		childs_birthdate = childs_birthdate_month + '/' + childs_birthdate_day + '/' + childs_birthdate_year;
		pass1 = $('#register_password').attr('value');
		pass2 = $('#register_password2').attr('value');
		
		// Determine monitor status based on color
		if ($('.monitor_on').hasClass('selected'))
			monitor = 'On';
		else
			monitor = 'Off';
		
		error = $('.box_reg_error');
		error.html('').hide();
		
		if(!validateEmail(user_email)) {
			error.html('Please enter a valid email address.').show();
			return false;
		}
		
		if(is_empty(user_login)) {
			error.html('Please enter a username.').show();
			return false;
		}
		
		if(pass1.length < 6) {
			error.html('Password must be at least 6 characters in length.').show();
			return false;
		} else {
			if(pass1 != pass2) {
				error.html('Passwords do not match.').show();
				return false;
			}
		}
		
		$.ajax({
			type: 'POST',
			url: 'wp-login.php?action=register',
			data: {
				'user_login':user_login,
				'user_email':user_email,
				'gender':gender,
				'childs_birthdate':childs_birthdate,
				'monitor':monitor,
				'pass1':pass1,
				'pass2':pass2,
				//'redirect_to':'index.php',
				'wp-submit':'Register'
			},
			async: true,
			success: function(result) {
				if($(result).find('#login_error').html()) {
					error.html($(result).find('#login_error').html()).show();
				} else{
					// Hide register box
					$('.box_reg_hider').hide();
					$('.box_reg').hide();
					
					show_success_box('Your account has been created!', 'Account Created');
					//$('.show_error_box').html('Your account has been created!', 'Account Created').show();
				}
			}
		});
	});
});

function is_empty(field) {
	value = white_space(field);
	if(value == null || value == '' || value == 'Username'){ return true; }
	return false;
}
		
function white_space(field) {
	return (field).replace(/^\s*|\s*$/g,'');
}

function validateEmail(elementValue) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(elementValue);
}

function updateCartHeader() {
	// Update cart header
	$.ajax({
		url: '/store/index.php?route=checkout/cart/getheader',
		type: 'get',
		dataType: 'json',
		success: function(json) {
			if (json != null) {
				if (json['quantity'] != null && json['quantity'] != 0) {
					// Update price in cart
					$('#cart_price span').text(' (' + json['total'] + ')');
					
					// Update number of products in cart
					$('.cartnum').text(json['quantity']);
					
					// Show checkout option if there are items to checkout with
					$('#cart_checkout').show();
				}
				else {
					// Update price
					$('#cart_price span').text('');
					
					// Update number of products in cart
					$('.cartnum').text('');
					
					// Hide checkout option if there is nothing to checkout with
					$('#cart_checkout').hide();
				}
			}
		}
	});
}

// Reset the popup box
function reset_store_box() {
	// Hide box
	$('.box_growl_helper').hide();
	$('.box_growl h3').text('');
	$('#link_view_cart').text('View your cart here');
	$('.box_growl_helper').removeClass('cartadd');
}

// Show the popup box regarding item added
function show_item_added_box(message) {
	$('.box_growl h2').hide();
	$('.box_growl h3').html(message);
	$('.box_growl_helper').addClass('cartadd');
	$('.box_growl_helper').show();
}

// Show the popup box with an error
function show_error_box(error_message) {
	$('.box_growl h2').text('warning');
	
	$('.box_growl h2').show();
	$('.box_growl h3').html(error_message);
	$('.box_growl_helper').removeClass('cartadd');
	$('.box_growl_helper').show();
}

// Show the popup box with a success
function show_success_box(error_message, title) {
	$('.box_growl h2').text(title);
	$('.box_growl h2').show();
	$('.box_growl h3').html(error_message);
	$('.box_growl_helper').removeClass('cartadd');
	$('#link_view_cart').text('');
	$('.box_growl_helper').show();
}
