$(document).ready(function(){

	$("a#login-form-link").fancybox({
		'scrolling':'no'
	});
	$('.zebra tr:even, .zebra1 tr:even').css('background','#d9d9d9');
	$('.user_type').click(function(){
		val = $(this).val();
		window.location = 'register.php?user_type='+val;
	});
	$('.req').focus(function(){
		$(this).removeClass('form-error');
		$('#ch-msg').html('');
	});
	
	if($("#first_email_hint").html()!=''){
		
		$("#first_email_hint").css("color","red");
		$("#first_email_hint").show();
	}
	$("#first_email").focus(function(){
		$("#first_email_hint").hide();
	});

	$('#copy_address').click(function(){
		if($(this).is(':checked')){
			delivery_address = $('#first_delivery_address').val();
			delivery_address2 = $('#first_delivery_address2').val();
			delivery_city = $('#first_delivery_city').val();
			delivery_state = $('#first_delivery_state').val();
			delivery_postal_code = $('#first_delivery_postal_code').val();

			if(delivery_address == ''){
				$('#first_delivery_address').addClass('form-error');
			}
			else{
				$('#first_billing_address').removeClass('form-error');
				$('#first_billing_address').val(delivery_address);
			}
			if(delivery_address2 !=''){
				$('#first_billing_address2').val(delivery_address2);
			}
			if(delivery_city == ''){
				$('#first_delivery_city').addClass('form-error');
			}
			else{
				$('#first_billing_city').removeClass('form-error');
				$('#first_billing_city').val(delivery_city);
			}
			if(delivery_state == ''){
				$('#first_delivery_state').addClass('form-error');
			}
			else{
				$('#first_billing_state').removeClass('form-error');
				$('#first_billing_state').val(delivery_state);
			}
			if(delivery_postal_code == ''){
				$('#first_delivery_postal_code').addClass('form-error');
			}
			else{
				$('#first_billing_postal_code').removeClass('form-error');
				$('#first_billing_postal_code').val(delivery_postal_code);
			}

		}
		else{
			$('#first_billing_address').val('');
			$('#first_billing_address2').val('');
			$('#first_billing_city').val('');
			$('#first_billing_state').val('');
			$('#first_billing_postal_code').val('');
		}
	});
	$("#credential, #password").focus(function(){
		$("#login-error").hide();
	});
	$("#first_password").focus(function(){
		$('#first_password_hint').fadeOut(2000);
	});
	$('#login-form').bind('submit',function(){
		if ($("#credential").val().length < 1 || $("#password").val().length < 1) {
	    $("#login-error").show();
	    $.fancybox.resize();
	    return false;
		}
		$.ajax({
			type		: "POST",
			cache	: false,
			url		: "/inc/ajax/login.php",
			data		: $(this).serializeArray(),
			success: 	function(data) {
				if(data == true){
					window.location="user.php";
				}
				else{
					$("#login-error").show();
				}
			}
		});
		return false;
		});

	$('#register-form').bind('submit', function(){
		var req_flag = 0;
		var email_flag = 0;
		var email = $('#first_email').val();
		var pwd1 = $('#first_password').val();
		var pwd2 = $('#confirm_password').val();
		var retailer_code = $('#retailer_code').val();
		var re = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/;
		/*var repwd = /^\w+$/; */

		$('#form-msg').html('');

//		$('.req').each(function(){
//			if($(this).val()==''){
//				$(this).addClass('form-error');
//				req_flag += 1;
//			}
//		});

		if($('#first_password').val().length < 5){
			$('#first_password').addClass('form-error');
			$('#first_password_hint').css({'display':'block'});
			msgh = $('#first_password').attr('class');
			if(msgh=="req form-error"){
				$('#first_password_hint').html('<img src="assets/images/pwd-hint.png" alt="" title="" />');
			}
			
			req_flag += 1;
		}
		if($('#first_password').val().length > 24){
			$('#first_password').addClass('form-error');
			req_flag += 1;
		}
		$('.req').each(function(){
			if($(this).val().length == 0){
				$(this).addClass('form-error');
				req_flag += 1;
			}
		});
		

		if(re.test(email) == false){
			$('#first_email').addClass('form-error');
			$('#first_email_hint').html('Invalid email address');
			$('#first_email_hint').css('padding-left',"10px");
			$("#first_email_hint").css("color","red");
			$('#first_email_hint').show();
			email_flag += 1;
			req_flag += 1;
		}

		if(pwd1 != pwd2){
			$('#first_password, #confirm_password').addClass('form-error');
			req_flag += 1;
		}
		if(retailer_code){
			$.post("/inc/ajax/register.php",{'code':retailer_code},function(data){
				if(data == 0){
					$('#retailer_code').addClass('form-error');
					var req_flag = 1;
				}
			});
		}
		if(email_flag == 0 && email!=''){
			$.post("/inc/ajax/register.php",{'email':email},function(data){
				if(data == 0){
					$('#first_email').addClass('form-error');
					var req_flag = 1;
				}
			});

		}
		if(req_flag > 0){
			return false;
		}
	});

	$('#edit-user-form').bind('submit', function(){
		var req_flag = 0;
		var email_flag = 0;
		var email = $('#first_email').val();
		var re = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/;
		var user_id = $('#user_id').val();

		$('#form-msg').html('');
		$('.req').each(function(){
			if($(this).val()==''){
				$(this).addClass('form-error');
				req_flag += 1;
			}
		});

		if(re.test(email) == false){
			$('#first_email').addClass('form-error');
			email_flag += 1;
			req_flag += 1;
		}
		if(email_flag == 0 && email!=''){
			$.post("/inc/ajax/register.php",{'email':email,'user_id':user_id},function(data){
				if(data == 0){
					$('#first_email').addClass('form-error');
					var req_flag = 1;
				}
			});

		}
		if(req_flag > 0){
			return false;
		}
	});

	$('#change-pwd-user-form').bind('submit', function(){

		var req_flag = 0;

		var user_id = $('#user_id').val();

		var pwd1 = $('#first_password').val();

		var pwd2 = $('#confirm_password').val();

		$('#form-msg').html('');

		$('.req').each(function(){
			if($(this).val().length == 0){
				$(this).addClass('form-error');
				req_flag += 1;
			}
		});

		if(pwd1 != pwd2){
			$('#first_password, #confirm_password').addClass('form-error');
			req_flag += 1;
		}

		if(req_flag > 0){
			return false;
		}
	});

	$('li#cart_update').click(function(){
		$('#cart-overview-form').submit();
	});
	$('#payment_type').change(function(){
		mode_url = $(this).val();
		window.location = mode_url;
	});
	$('#payment-submit').click(function(){
		$('#cart-overview-form').submit();
	});
});

function remove_item_from_cart(item_id,from_flag){
	var from_page = 'cart.php';
	if(confirm('The selected product will be removed from your cart. Click OK to continue or Cancel to abort.')){
		if(from_flag == 0){
			from_page = 'cart.php';
		}
		if(from_flag == 1){
			from_page = 'checkout.php';
		}
		window.location = '/'+from_page+'?action=remove&cart_id='+item_id;
	}
}
