(function($) { 
	$(document).ready(function(){
	    $('.login_input').focus(function(){
	        $(this).val("");
	    });
	    $('.cls_login_area').hint();
	
	    $(".banner").addClass("hand");
	    $(".banner").click(function() {
	        document.location = index();
	    });
	    $("#search_products").keyup(function(evt){
	        if(evt.keyCode==13) searchSubmission();
	    });
	});
})(jQuery); 

function get_form_forgot_password(detail)
{
	(function($) { 
		$.ajax(
	    {
	        url: index() + "callback/modules.register.forgot_password",
	        async: false,
	        type: "POST",
	        data: ({
	            'detail':detail
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            $('#forgot').html(data);
	            open_block_with_id('forgot',323);
	        //show_forgot();
	        }
	    });
	})(jQuery);
}

function get_form_checkpoint()
{
	(function($) { 
		$.ajax(
	    {
	        url: index() + "callback/modules.register.form_checkpoint",
	        async: false,
	        type: "POST",
	        data: ({
	            'title': ''
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            $('#checkpoint').html(data);
	            open_block_with_id('checkpoint',323);            
	        }
	    });
	})(jQuery);
}

function do_login()
{
	(function($) { 
	    var email = $('#username').val();
	    var pass = $('#password').val();
	    $.ajax(
	    {
	        url: index() + "callback/modules.register.ajax",
	        async: false,
	        type: "POST",
	        data: ({
	            'action':'login',
	            'email': email,
	            'pass':pass
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            if(data=="OK")
	                document.location=site_url_login;
	            else if(data == "MULTI_USERS")
	            	show_popup_list_users_login(email);
	           	else if(data=="ACCOUNT_INACTIVE") 
					alert('Your account is inactive.');
	            else
	                alert('Your email or password is incorrect. Please re-enter or register above.');
	        }
	    });
	})(jQuery);
}
function do_logout()
{
	(function($) { 
	    $.ajax(
	    {
	        url: index() + "callback/modules.register.ajax",
	        async: false,
	        type: "POST",
	        data: ({
	            'action':'logout'
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            if(data=="OK"){
	            	site_url_login = index();
	                document.location=site_url_login;
	            }
	        }
	    });
	})(jQuery);
}

function do_forgot_password(detail)
{
	(function($) { 
		if($('#email_forgot_address').val() == '')
		{
			alert('Please enter your email.');
			$('#email_forgot_address').focus();
			return false;
		}
	    $.ajax(
	    {
	        url: index() + "callback/modules.register.ajax",
	        async: false,
	        type: "POST",
	        data: ({
	            'security_code':$("#security_txt_forgot").val(),
	            'action':'forgot_password',
	            'email' : $('#email_forgot_address').val()
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            data = $.trim(data);          
	            if(data.match('OK')){
	                alert("Email has been sent!!");
	                document.location=site_url_login;
	            }
	            else if(data.match("SECURITY_NOT_MATCH")) {
	                alert("Please enter correct Security Code.");
	                return false;
	            }
	            else if(data.match("USER_NAME_NOT_EXISTS")) {
	                alert("Your username (email address) does not exist.");
	                return false;
	            }else if(data.match("ACCOUNT_INACTIVE")) {
	                alert("Your account is inactive.");
	                return false;
	            }
	            else {
	                alert("Error forgot password. Please try again.");
					if(detail!=1)
		                document.location=site_url_login;
					else
						document.location= index();
	            }
	        }
	    });
	})(jQuery);
}


function do_get_point()
{
	(function($) { 
	    var email_checkpoint = $('#email_forgot_address').val();
	    $.ajax(
	    {
	        url: index() + "callback/modules.register.ajax",
	        async: false,
	        type: "POST",
	        data: ({
	            //'security_code':$("#checkpoint_security_txt_point").val(),
	            'action':'get_point',
	            'email': email_checkpoint
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            data = $.trim(data);      
	            var data_array = data.split(":::::");
	            if(data_array.length==2) {
	                var tmpPart1 = data_array[0];
	                var tmpPart2 = data_array[1];
	                if(tmpPart1=="OK") {
	                    $('#img_checkpoint').html('Your Wicked Points:'+tmpPart2+".");
	                    $.unblockUI();
	                }
	                else {
	                    if(tmpPart2=="SECURITY_NOT_MATCH") {
	                        alert("Please enter correct Security Code.");
	                        return false;
	                    }
	                    else if(tmpPart2=="USER_NAME_NOT_EXISTS") {
	                        alert("Your username (email address) does not exist.");
	                        return false;
	                    }
	                    else {
	                        alert("Cannot check your Wicked Points.");
	                        $.unblockUI();
	                    }
	                }
	            } 
	            else {
	                alert("Cannot check your Wicked Points.");
	                $.unblockUI();
	            }
				
	        }
	    });
	})(jQuery);
}
	
function handleEnter(e)
{
	    var charCode;
	    if(e && e.which){
	        charCode = e.which;
	    }else if(window.event){
	        e = window.event;
	        charCode = e.keyCode;
	    }
		
	    if(charCode == 13) {
	        do_login();
	    }
}

function open_block_with_id(id, width, height)
{
	(function($) { 
	    var viewportWidth = $(window).width();
	    //var viewportHeight = $(window).height();
	    //viewportHeight = $(window).innerHeight ? $(window).innerHeight : $(window).height();
	
	    var lefts = (viewportWidth - width)/2;
	
	    $.blockUI({
	        message: $('div#'+id),
	        css:{
	            'top':  '135px',
	            'left': lefts + 'px',
	            'width': width + 'px'
	        }
	    });
	})(jQuery);
}

function close_block()
{
	(function($) { 
	    $.unblockUI();
	    //document.location=site_url_login;
	})(jQuery);
}

//added by vietnguyen
function do_login_checkout()
{
	(function($) { 	
		var email = $('#user_name').val();
		var pass = $('#pwd').val();
		$.ajax(
		{
			url: index() + "callback/modules.register.ajax",
			async: false,
			type: "POST",
			data: ({'action':'login',
					 'email': email, 
					 'pass':pass	
							    }),
			dataType: "html",
			success: function(data)
			{
				if(data=="OK")
					document.location= index()+'shop.checkout_order';	
				else if(data == "MULTI_USERS")
	            	show_popup_list_users_login(email);
				else if(data=="ACCOUNT_INACTIVE") 
					alert('Your account is inactive.');
				else
					alert('Your email or password is incorrect. Please re-enter or register above.');
			}
		});
	})(jQuery);
}

function open_block()
{
	(function($) { 
	    var w = screen.width;
	    var lefts = (w - 560)/2;
	    $.blockUI({message: $('div#address_edit'),css:{
	            top:  '135px',
	            left: lefts + 'px',
	            width: '500px'}});
	})(jQuery);
}
    
function close_edit(){
	(function($) { 
		$.unblockUI();
	})(jQuery);
}

function show_edit(id,level,address,suburb,state,postcode,button)
{
	(function($) { 
		$('#address_edit_id').val(id);
		$('#new_level').val(level);
		$('#new_address').val(address);
		$('#new_suburb').val(suburb);
		$('#new_code').val(postcode);
		
		$('#button_add').hide();
		$('#button_edit').show();
		 
		//$('#new_level').val(level);
		$('#tr_state').hide();
		get_form_state(state);
		open_block();
	})(jQuery);
}


function get_form_state(state_id)
{
	//alert('aaa');
	
	(function($) { 
	    $.ajax(
	    {
	        url: index() + "callback/modules.register.form_state",
	        async: false,
	        type: "POST",
	        data: ({
	            'state':state_id
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	    		$('#demoTarget').html(data);
				
	        }
	    });
	})(jQuery);
}


	function show_add()
	{
		(function($) { 
			$('#address_edit_id').val('');
			$('#new_level').val('');
			$('#new_address').val('');
			$('#new_suburb').val('');
			$('#new_code').val('');
			
			$('#button_add').show();
			$('#button_edit').hide();
			$('#tr_state').show();
			get_form_state('');
			open_block()
		})(jQuery);
	}

	function do_edit()
	{
		(function($) { 
			var address_edit_id = $('#address_edit_id').val();
			var level = $('#new_level').val();
			var address = $('#new_address').val();
			var suburb = $('#new_suburb').val();
			var postcode = $('#new_code').val();
			var state = $('#type_state').val();
		
			var flag = true;
	        $('.cls_input_new').each(function(){
	            if($(this).val() == '')
	            {
	                alert("Please fill full information");
	                $(this).focus();
	                flag = false;
	                return false;
	            }
	        });
			if(flag){
				$.ajax(
				{
					url: index() + "callback/modules.register.ajax",
					async: false,
					type: "POST",
					data: ({'action':'edit',
							 'address_edit_id': address_edit_id, 
							 'level':level,
							 'address':address,
							 'suburb':suburb,
							 'postcode':postcode,
							 'state':state
									    }),
					dataType: "html",
					success: function(data)
					{
						if(data=="OK"){					
							alert('Your address has been updated');
							document.location=site_url_login;	
						}
					}
				});
			}
		})(jQuery);
	}

	function do_add()
	{
		(function($) {
			var address_edit_id = $('#address_edit_id').val();
			var client_id = $('#client_id').val(); 
			var level = $('#new_level').val();
			var address = $('#new_address').val();
			var suburb = $('#new_suburb').val();
			var postcode = $('#new_code').val();
			var state = $('#type_state').val();
			
			var flag = true;
	        $('.cls_input_new').each(function(){
	            if($(this).val() == '')
	            {
	                alert("Please fill full information");
	                $(this).focus();
	                flag = false;
	                return false;
	            }
	        });
			if(flag){
				$.ajax(
				{
					url: index() + "callback/modules.register.ajax",
					async: false,
					type: "POST",
					data: ({'action':'add',
							 'client_id':client_id,
							 'address_edit_id': address_edit_id, 
							 'level':level,
							 'address':address,
							 'suburb':suburb,
							 'postcode':postcode,
							 'state':state
									    }),
					dataType: "html",
					success: function(data)
					{
						if(data=="OK"){					
							alert('Your address has been inserted');
							document.location=site_url_login;	
						} 
					}
				});
			}
		})(jQuery);
	}
	
function do_delete(id)
	{
		(function($) {
			var address_edit_id = id;
				$.ajax(
				{
					//alert("aaaaa");
					url: index() + "callback/modules.register.ajax",
					async: false,
					type: "POST",
					data: ({'action':'delete',
							 'address_edit_id': address_edit_id
									    }),
					dataType: "html",
					success: function(data)
					{
						//alert(data);
						if(data=="OK"){	
											
							alert('Your address has been deleted');
							document.location=site_url_login;	
						}
						if(data.match('FALSE')){
							alert('There must be 1 delivery address for the account');
							return false;
						}
					}
				});
		})(jQuery);
	} 

   function do_submit()
	{
		(function($) {
			var flag = true;
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			 $('.cls_input_edit_detail').each(function(){
			  if($(this).val() == ''&&this.name != 'register_repass'&&this.name != 'register_pass') 
			  {
				   alert("Please fill full information");
				   $(this).focus();
				   flag = false;
				   return false;
			  }
			  if(this.name == 'register_email' && reg.test($(this).val()) == false)
			  {
			  		alert('Email invalid');
				    $(this).focus();
				    flag = false;
				    return false;
			  }
			  if($('#register_pass').val()!=$('#register_repass').val())
				{
					alert('Please type correct your password');
					$('#register_pass').val('');
					$('#register_repass').val('');
					$('#register_pass').focus();
					flag = false;
					return false;
				}
			 });
			if(flag)
			{
				//serc = $('#security').val().toLowerCase();
				//cap = captcha_word.toLowerCase();
				if($('#register_pass').val()!=$('#register_repass').val())
				{
					alert('Please type correct your password');
					$('#register_pass').val('');
					$('#register_repass').val('');
					$('#register_pass').focus();
					return false;
				}
				else
					__dopostback('1','');
				/*if(serc != cap ){
					alert('Please type security code correct!');		
					return false;
				}
				__dopostback('1','');*/
			}	
		})(jQuery);
	}

function do_remove_favourites(id) {
	(function($) {
		$.ajax( {
			url : index() + "callback/modules.register.ajax",
			async : false,
			type : "POST",
			data : ( {
				'id_fav':id,
				'action' : 'remove_favourites'
			}),
			dataType : "html",
			success : function(data) {
				if (data == "OK") {
						document.location = site_url_login;
				}else{
					alert('Cannot delete this item!');
					return false;
				}
			}
			});
	})(jQuery);
}	

function do_remove_favourites_order(id) {
	(function($) {
		$.ajax( {
			url : index() + "callback/modules.register.ajax",
			async : false,
			type : "POST",
			data : ( {
				'id_order':id,
				'action' : 'remove_favourites_order'
			}),
			dataType : "html",
			success : function(data) {
				if (data == "OK") {
						document.location = site_url_login;
				}else{
					alert('Cannot delete this order!');
					return false;
				}
			}
			});
	})(jQuery);
}
	
//process account customer signup 
//new request 0005489
function do_account_customer_signup_submit()
{
	(function($) {
		 var flag = true;
		 var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		 $('.cls_acc_customer').each(function(){
			  if($(this).val() == '') 
			  {
				   alert("Please fill full information");
				   $(this).focus();
				   flag = false;
				   return false;
			  }
			  if((this.name == 'your_email' || this.name == 'accounts_dept_email') && reg.test($(this).val()) == false)
			  {
			  		alert('Email invalid');
				    $(this).focus();
				    flag = false;
				    return false;
			  }
			  /*if(this.name == 'anticipated_monthly_catering_expenditure' && !IsNumeric($(this).val()))
			  {
			  		alert('Please input anticipated monthly catering expenditure is numeric');
				    $(this).focus();
				    flag = false;
				    return false;
			  }*/
		 });
	 	if(flag)
			__dopostback('1','account_customer_signup');
		else
			return false;
	})(jQuery);
}

function IsNumeric(strString)
   //  check for valid numeric strings	
{
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
	     strChar = strString.charAt(i);
	     if (strValidChars.indexOf(strChar) == -1)
         {
	         blnResult = false;
         }
      }
   return blnResult;
}

function toggle_account_customer(div_id)
{
	(function($) {
    	$('#'+div_id).toggle('fast');
    })(jQuery);
}

function do_user_login()
{
	(function($) { 
	    var client_id = $('#client_id_login').val();
	    if(!client_id)
	    {
	    	alert("please choose user to login");
	    	return false;
	    }
	    $.ajax(
	    {
	        url: index() + "callback/modules.register.ajax",
	        async: false,
	        type: "POST",
	        data: ({
	            'action':'do_user_login',
	            'client_id': client_id
	        }),
	        dataType: "html",
	        success: function(data)
	        {
	            if(data=="OK")
	                document.location=site_url_login;
	           	else if(data=="ACCOUNT_INACTIVE") 
					alert('This user is inactive.');
	            else
	                alert('This user doesn\'t exist.');
	        }
	    });
	})(jQuery);
}
