
function get_item_price(item_id) {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_item_price&item_id=" + item_id + "&rand=" + Math.random()
				}).responseText;
}

function get_item_total_price(item_id) {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_item_total_price&item_id=" + item_id + "&rand=" + Math.random()
				}).responseText;
}

function get_item_total_qty(item_id) {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_item_total_qty&item_id=" + item_id + "&rand=" + Math.random()
				}).responseText;
}

function get_mini_shopping_cart_item_HTML_(item_id) {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_mini_shopping_cart_item_HTML_&item_id=" + item_id + "&rand=" + Math.random()
				}).responseText;
}

function get_subtotal_price() {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_subtotal_price&rand=" + Math.random()
				}).responseText;
}

function get_shipping_price(country_id) {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_shipping_price&rand=" + Math.random()
				}).responseText;
}

function get_total_price(country_id) {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_total_price&rand=" + Math.random()
				}).responseText;
}

function get_total_qty() {
	return	$.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					async: false,
					data: "action=get_total_qty&rand=" + Math.random()
				}).responseText;
}

function get_shipping_country_name(country_id) {
	return $.ajax({
					type: "GET",
					url: "/ajax/ajax_shopping_cart.php",
					data: "action=get_shipping_country_name&country_id=" + country_id + "&rand=" + Math.random()
				}).responseText;
}

function update_item(item_id, item_qty) {
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_shopping_cart.php",
		data: "action=update_item&item_id=" + item_id + "&item_qty=" + item_qty + "&rand=" + Math.random()
	});
}

function add_to_cart(item_id, item_qty) {
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_shopping_cart.php",
		data: "action=add_item&item_id=" + item_id + "&item_qty=" + item_qty + "&rand=" + Math.random()
	});
}

///////////////// Mini Shopping Cart begin//////////////////////

function update_mini_shopping_cart_item_(item_id, item_qty) {
	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/ajax/ajax_shopping_cart.php",
		async: true,
		data: "action=update_shopping_cart_item_&cart=mini_shopping_cart&item_id=" + item_id + "&item_qty=" + item_qty + "&rand=" + Math.random(),
		success: function(json){

				switch (json.status){
					case 'OK':{
						$('#items_in_cart').html(json.items_total_qty);
						$('#items_price').html('$' + json.subtotal_price);
                        document.location.replace('/shoppingcart.html');
					}
				}

		}
	});
}

////////////////// Mini Shopping Cart end///////////////////////


//////////////////// Shopping Cart begin/////////////////////////


function set_shopping_cart_shipping_(shipping_country_id, shipping_state_code) {
	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/ajax/ajax_shopping_cart.php",
		async: true,
		data: "action=set_shopping_cart_shipping_&shipping_country_id=" + shipping_country_id + "&shipping_state_code=" + shipping_state_code + "&rand=" + Math.random(),
		success: function(json){

				switch (json.status){
					case 'OK':{

							if (json.shipping_country_name == '') {
								$('#checkout_order_shipping_country_name').html('Select shipping country');
							} else {
								$('#checkout_order_shipping_country_name').html(json.shipping_country_name);
							}

						$('#checkout_shipping_price').html('$' + json.shipping_price);
						$('#checkout_order_shipping_price').html('$' + json.shipping_price);
						$('#checkout_total_price').html('$' + json.total_price);
						$('#checkout_order_total_price').html('$' + json.total_price);
						$('#price').val(json.total_price);
						$('#desc').val(json.desc);
					}
				}

		}
	});
}

function set_shopping_cart_item_(item_id, item_qty) {
	$.ajax({
		type: "GET",
		dataType: "json",
		url: "/ajax/ajax_shopping_cart.php",
		async: true,
		data: "action=set_shopping_cart_item_&cart=shopping_cart&item_id=" + item_id + "&item_qty=" + item_qty + "&rand=" + Math.random(),
		success: function(json){

				switch (json.status){
					case 'OK':{

							switch (json.action){
								case 'update':{
									$('#shopping_cart_item_total_price_' + item_id).text(json.item_total_price);
									$('#' + item_id + '_qty').text(json.item_tatal_qty);
									$('#shopping_cart_order_item_total_qty_' + item_id).text(json.item_tatal_qty);
									$('#checkout_subtotal_price').text('$' + json.subtotal_price);
									$('#checkout_total_price').text('$' + json.total_price);
									$('#price').val(json.total_price);
									$('#desc').val(json.desc);
									break;
								}
								case 'remove':{
									$('#shopping_cart_item_' + item_id).remove();
									$('#checkout_subtotal_price').text('$' + json.subtotal_price);
									$('#checkout_total_price').text('$' + json.total_price);
									$('#price').val(json.total_price);
									$('#desc').val(json.desc);

										if (json.items_total_qty == 0) {
											alert('Shopping cart is empty.');
											document.location.replace('/index.html');
										}

								}
							}

					}
				}

		}

	});
}
/*
function delete_shopping_cart_item(item_id){
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_shopping_cart.php",
		data: "action=delete_item&item_id=" + item_id + "&rand=" + Math.random(),
		success: function(){
			update_shopping_cart_subtotal_price($('#shipping_country').val());
			update_shopping_cart_total_price($('#shipping_country').val());
			$('#shopping_cart_item_' + item_id).hide(1000);
			$('#shopping_cart_order_item_' + item_id).hide(1000);
		}
	});
}
/*
function update_shopping_cart_subtotal_price(){
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_shopping_cart.php",
		data: "action=get_subtotal_price&rand=" + Math.random(),
		success: function(data){
			$('#checkout_subtotal_price').html(data);
			$('#checkout_order_subtotal_price').html(data);
		}
	});
}
/*
function update_shopping_cart_shipping_price(country_id){
	$.ajax({
		type: "GET",
		url: "/ajax/ajax_shopping_cart.php",
		data: "action=get_shipping_price&country_id=" + country_id + "&rand=" + Math.random(),
		success: function(data){
			$('#checkout_shipping_price').html(data);
			$('#checkout_order_shipping_price').html(data);
		}
	});
}
/*
function update_shopping_country(country_id){
	update_shopping_cart_shipping_price(country_id);
	update_shopping_cart_total_price(country_id);
	update_shopping_cart_shipping_country_name(country_id);
}

function update_shopping_cart_shipping_country_name(country_id){
	$.ajax({
				type: "GET",
				url: "/ajax/ajax_shopping_cart.php",
				data: "action=get_shipping_country_name&country_id=" + country_id + "&rand=" + Math.random(),
				success: function(data){
					$('#checkout_order_shipping_country_name').text(data);  
				}
	});
}
*/
//////////////////// Shopping Cart end////////////////////////

///////////////////// Chexkout begin //////////////////////////

function checkout_shipping_address_validation() {
  var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var error_log = '';
  var err = false;

	if ($('#Customer_First_Name').val().length < 2){
		$('#Customer_First_Name_error_log').html('First Name is too short.').show(500);
		err = true;
	}

	if ($('#Customer_Last_Name').val().length < 2){
		$('#Customer_Last_Name_error_log').html('Last Name is too short.').show(500);
		err = true;
	}

	if (!$('#Customer_E_Mail').val().match(email_reg)){
		$('#Customer_E_Mail_error_log').html('Email is not valid.').show(500);
		err = true;
	}
/*
	if ($('#Customer_Country').val() == 0){
		$('#Customer_Address_error_log').html('Billing/Shipping Country is not selected.').show(500);
		err = true;
	}
*/
	if ($('#Customer_Address').val().length < 2){
		$('#Customer_Address_error_log').html('Billing/Shipping Address is too short.').show(500);
		err = true;
	}

	if ($('#Customer_City').val().length < 2){
		$('#Customer_City_error_log').html('City name is too short.').show(500);
		err = true;
	}

	if ($('#Customer_Zip').val().length < 2){
		$('#Customer_Zip_error_log').html('Zip Code is too short.').show(500);
		err = true;
	}

	if ($('#Customer_Phone').val().length < 5){
		$('#Customer_Phone_error_log').html('Phone Number is too short.').show(500);
		err = true;
	}

	if (err == true) {
		return false;
	} else {
		return true;
	}

}

function clear_error_logs() {
	$('.error').hide().html('');
}

function accordion_(step_id) {
	$('#step_one').hide(500);
	$('#step_two').hide(500);
	$('#step_three').hide(500);
	$('#step_four').hide(500);
	$('#step_one_head').removeClass('titleCartOn').removeClass('titleCart').addClass('titleCart');
	$('#step_two_head').removeClass('titleCartOn').removeClass('titleCart').addClass('titleCart');
	$('#step_three_head').removeClass('titleCartOn').removeClass('titleCart').addClass('titleCart');
	$('#step_four_head').removeClass('titleCartOn').removeClass('titleCart').addClass('titleCart');
	$('#' + step_id + '_head').removeClass('titleCartOn').removeClass('titleCart').addClass('titleCartOn');
	$('#' + step_id).show(500);
}

function accordion(step) {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	switch (step) {
		case '1': {
			clear_error_logs();
			accordion_('step_one');
			break;
		}
		case '2': {
			clear_error_logs();
			accordion_('step_two');
			break;
		}
		case '3': {
			clear_error_logs();
				///////// Checking Step 2 ///////////////
				if (is_logged_in() == 'false') {

						if ($('#first_time_email').val() == '') {
							$('#step_two_error_log').html('Log In Please or Enter a valid "First time user" email.').show(700);
							accordion_('step_two');
						} else {

								if ($('#first_time_email').val().match(email_reg)) {

									$.ajax({
										type: "GET",
										url: "/ajax/ajax_my_account.php",
										data: "action=check_email&email=" + $('#first_time_email').val() + "&rand=" + Math.random(),
										success: function(data){

												if (data == 'true'){
													accordion_('step_three');
												}else{
													accordion_('step_two');
												}

										}
									});


								} else {
									$('#first_time_email_error_log').html('"First time user" email is not valid.').show(700);
									accordion_('step_two');
								}

						}

				} else {
					accordion_('step_three');
				}
			///////// /Checking Step 2 ///////////////
			break;
		}
		case '4': {
			clear_error_logs();
			$('#checkout_order_shipping_address_preview_box').html('<h3>Billing/Shipping address </h3>' + $('#Customer_First_Name').val() + ' ' + $('#Customer_Last_Name').val() + ',<br />' + $('#Customer_Country option:selected').text() + '<br />' + $('#Customer_Address').val() + '<br />' + $('#Customer_Zip').val() + '<br />' + $('#Customer_Phone').val());

				if (is_logged_in() == 'false') {
//$('#first_time_email').val().match(email_reg)
					$('#first_time_user_password_box').show();

						if ($('#first_time_email').val() == '') {
							$('#step_two_error_log').html('Log In Please or Enter a valid "First time user" email.').show(700);
							accordion_('step_two');
						} else {

								if ($('#first_time_email').val().match(email_reg)) {

									$.ajax({
										type: "GET",
										url: "/ajax/ajax_my_account.php",
										data: "action=check_email&email=" + $('#first_time_email').val() + "&rand=" + Math.random(),
										success: function(data){

												if (data == 'true'){

														if (checkout_shipping_address_validation() == true) {
															accordion_('step_four');
														} else {
															accordion_('step_three');
														}

												}else{
													$('#first_time_email_error_log').html('Email address already registred.').show(700);
													accordion_('step_two');
												}

										}
									});


								} else {
									$('#first_time_email_error_log').html('"First time user" email is not valid.').show(700);
									accordion_('step_two');
								}

						}

				} else {
					$('#first_time_user_password_box').hide();

						if (checkout_shipping_address_validation() == true) {
							accordion_('step_four');
						} else {
							accordion_('step_three');
						}

				}

			break;
		}
		default : {}
	}

	return false;
}

function total_checkoutvalidator() {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	clear_error_logs();

				if (is_logged_in() == 'false') {

						if ($('#first_time_email').val() == '') {
							$('#step_two_error_log').html('Log In Please or Enter a valid "First time user" email.').show(700);
							accordion_('step_two');
						} else {

								if ($('#first_time_email').val().match(email_reg)) {

									check_email = $.ajax({
																	type: "GET",
																	url: "/ajax/ajax_my_account.php",
																	async: false,
																	data: "action=check_email&email=" + $('#first_time_email').val() + "&rand=" + Math.random()
																}).responseText;

										if (check_email == 'true'){

												if (checkout_shipping_address_validation() == true) {

														if ($('#first_time_user_password1').val().length < 6) {
															$('#first_time_user_password1_error_log').html('Your password is too short. Should be 6 characters or more. Please try again.').show(700);
															return false;
														} else {

																if ($('#first_time_user_password1').val() != $('#first_time_user_password2').val()) {
																	$('#first_time_user_password2_error_log').html('Wrong password. Please retype again.').show(700);
																	return false;
																} else {
																	$.ajax({
																				type: "GET",
																				url: "/ajax/ajax_my_account.php",
																				async: false,
																				data: "action=register_account&page=checkout&user_email=" + $('#first_time_email').val() + "&user_password1=" + $('#first_time_user_password1').val() + "&user_password2=" + $('#first_time_user_password1').val() + "&user_first_name=" + $('#Customer_First_Name').val() + "&user_last_name=" + $('#Customer_Last_Name').val() + "&rand=" + Math.random()
																			});
																}

														}

													return true;
												} else {
													accordion_('step_three');
												}

										}else{
											$('#first_time_email_error_log').html('Email address already registred').show(700);
											accordion_('step_two');
										}

								} else {
									$('#first_time_email_error_log').html('"First time user" email is not valid.').show(700);
									accordion_('step_two');
								}

						}

				} else {

						if (checkout_shipping_address_validation() == true) {
							return true;
						} else {
							accordion_('step_three');
						}

				}

}
///////////////////// Chexkout end ////////////////////////////















