
$(document).ready(function()
{
	
	$(document).on("click","#find_user",function()
	{
		$.ajax({
			type: "POST",
			async: false,
			url: GLOBAL_BASE_PATH+"/order/ajaxSearchUser/",
			data: {billingEmail: $('#email').val(),Name: $('#Name').val()},
			success: function(data)
			{
				var x = jQuery.parseJSON(data);
				if(x.isError=='Yes')
				{
					userFound ='No';
					$("#Name").val('');
					$("#email").val('');
				}
				else
				{
					userFound ='Yes';
					$("#usersFound").val('');
					$("#usersFound").html(x.html);
				}
			}
		});
	});
	$(document).on("click","#select_user",function()
	{
		if($("input[type='radio'].selected_user_id").is(':checked')) 
		{
			var userID = $("input[type='radio'].selected_user_id:checked").val();
			$.ajax({
				type: "POST",
				async: false,
				url: GLOBAL_BASE_PATH+"/order/ajaxUpdateCartUser/",
				data: {userID: userID},
				success: function(data)
				{
					
					var x = jQuery.parseJSON(data);
					if(x.isError=='Yes')
					{
						userFound ='No';
						$("#Name").val('');
						$("#billingEmail").val('');
					}
					else
					{
						userFound ='Yes';
						
						$("#usersFound").val('');
						$("#usersFound").html(x.html);
						userFound ='Yes';
						$("#billingEmail").val(x.userEmail);
						$("#billingFirstName").val(x.userFirstName);
						$("#billingLastName").val(x.userLastName);
						$("#billingAddress").val(x.userAdress1);
						$("#billingAddress1").val(x.userAdress2);
						$("#billingCountry").val(x.countryID);
						$("#billingCountry [value=" + x.countryID + "]").attr("selected",
						"selected");
						listStates(x.countryID);
						if(x.stateID >0)
						{
							$("#stateInput").show();
							$("#stateProvinceInput").hide();
							$("#billingState").val(x.stateID);
							$("#stateSelectedType").val("state");
							$("#billingProvince").val('');
						}
						else
						{
							$("#stateInput").hide();
							$("#stateProvinceInput").show();
							$("#billingState").val('');
							$("#stateSelectedType").val("province");
							$("#billingProvince").val(x.userProvince);
						}
						$("#billingCity").val(x.userCity);
						$("#billingZip").val(x.userZip);
						$("#billingPhone").val(x.userPhone);
						$("#usersFound").val('');
						$(window).colorbox.close();
					}
				}
			});
		}
		else
		{
			jNotify(errUser, 'user_message','message_error');	
		}
	});
	$(document).on('click','#reset_user',function()
	{
		$("#Name").val('');
		$("#email").val('');
		$("#usersFound").empty();
	});
});
function listStates(countryID, stateID) {
    if (countryID) {
        $("#loadProcessImage").append('<img src="' + GLOBAL_ROOT_PATH + '/public/default/manage/standard/images/ajax-loader.gif" id="loading_gif2"  />');
    }
    $('#statehidValue').val(stateID);
    $.ajax({
        async: false,
        type: "POST",
        url: GLOBAL_BASE_PATH + "/city/ajaxState/",
        data: {
            "countryID": countryID,
            "stateID": stateID
        }
    }).done(function (msg) {
        $("#loadProcessImage").empty();
        if (msg != '') {
            $("#stateInput").show();
            $('#billingState').empty();
            $("#stateProvinceInput").hide();
            $("#billingState").append('<option value="">Select</option>' + msg);
            $("#stateSelectedType").val("state");
            $("#billingProvince").val('');
            $("#div_err_formOrderAdd_provincehidden").css("display", "none");
        } else {
            $("#stateInput").hide();
            $("#stateProvinceInput").show();
            $("#billingState").val('');
            $("#stateSelectedType").val("province");
            $("#div_err_formOrderAdd_statehidden").css("display", "none");
        }
    });
}