
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 set_mini_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=mini_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 'insert':{
									$('#mini_shopping_cart_body').append(json.HTML);
									$('#items_total_qty').text(json.items_total_qty);
									$('#mini_shopping_cart_items_subtotal_price').text(json.subtotal_price);
									$('#items_subtotal_price').text(json.subtotal_price);
									break;
								}
								case 'update':{
									$('#item_qty_' + item_id).val(json.item_tatal_qty);
									$('#mini_shopping_cart_item_total_price_' + item_id).text(json.item_total_price);
									$('#items_total_qty').text(json.items_total_qty);
									$('#mini_shopping_cart_items_subtotal_price').text(json.subtotal_price);
									$('#items_subtotal_price').text(json.subtotal_price);
									break;
								}
								case 'remove':{
									$('#mini_shopping_cart_item_' + item_id).remove();
									$('#items_total_qty').text(json.items_total_qty);
									$('#mini_shopping_cart_items_subtotal_price').text(json.subtotal_price);
									$('#items_subtotal_price').text(json.subtotal_price);
								}
							}

					}
				}

		}

	});
}

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':{

							switch (json.action){
								case 'insert':{
									$('#mini_shopping_cart_body').append(json.HTML);
									$('#items_total_qty').text(json.items_total_qty);
									$('#mini_shopping_cart_items_subtotal_price').text(json.subtotal_price);
									$('#items_subtotal_price').text(json.subtotal_price);
									break;
								}
								case 'update':{
									$('#item_qty_' + item_id).val(json.item_tatal_qty);
									$('#mini_shopping_cart_item_total_price_' + item_id).text(json.item_total_price);
									$('#items_total_qty').text(json.items_total_qty);
									$('#mini_shopping_cart_items_subtotal_price').text(json.subtotal_price);
									$('#items_subtotal_price').text(json.subtotal_price);
									break;
								}
								case 'remove':{
									$('#mini_shopping_cart_item_' + item_id).remove();
									$('#items_total_qty').text(json.items_total_qty);
									$('#mini_shopping_cart_items_subtotal_price').text(json.subtotal_price);
									$('#items_subtotal_price').text(json.subtotal_price);
								}
							}

					}
				}

		}
	});
}

////////////////// 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);
							}

              if (json.shipping_price == 0) {
                  $('#checkout_shipping_price').html('<b style="color:red;">Free</b>');
              } else if (json.shipping_price == 'NOT SET') {
                  $('#checkout_shipping_price').html('<span class="notset">NOT SET</span>');
              } else {
                  $('#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);
									$('#shopping_cart_order_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_order_subtotal_price').text(json.subtotal_price);
									$('#checkout_total_price').text(json.total_price);
									$('#checkout_order_total_price').text(json.total_price);
									$('#price').val(json.total_price);
									$('#desc').val(json.Desc);
									break;
								}
								case 'remove':{
									$('#shopping_cart_item_' + item_id).remove();
									$('#shopping_cart_order_item_' + item_id).remove();
									$('#checkout_subtotal_price').text(json.subtotal_price);
									$('#checkout_order_subtotal_price').text(json.subtotal_price);
									$('#checkout_total_price').text(json.total_price);
									$('#checkout_order_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');
										}

								}
							}

					}
				}

		}

	});
}
//////////////////// 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'){
													$('#Customer_E_Mail').val($('#first_time_email').val());
													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 {
					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() + "&shipping_address=" + $('#Customer_Address').val() + "&shipping_city=" + $('#Customer_City').val() + "&shipping_country=" + $('#Customer_Country').val() + "&shipping_state=" + $('#shipping_state').val() + "&shipping_zip=" + $('#Customer_Zip').val() + "&shipping_phone=" + $('#Customer_Phone').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');
						}

				}

}

function clear_sessions() {
	$.ajax({
		type: "POST",
		async: true,
		url: "/ajax/ajax_shopping_cart.php?action=clear_sessions",
		data: "rand=" + Math.random()
	});
}

function total_checkoutvalidator2() {
	var email_reg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	clear_error_logs();
	
	var country = $('#Customer_Country').val();
	var state = $('#shipping_state').val();
	var province = $('#shipping_province').val();
	
	if (country == 0) {
		alert('Please choose your Country');
		return false;
	} else if (country == 1) {
		if (state == 0) {
			alert('Please choose your State.');
			return false;
		}
	} else if (country == 39) {
		if (province == 0) {
			alert('Please choose your Province.');
			return false;
		}
	}

        if (is_logged_in() == 'false') {
/*
                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()) {
                    $.ajax({
                        type: "POST",
                        url: "/ajax/ajax_my_account.php?action=register_account1&page=checkout1&rand=" + Math.random(),
                        async: false,
                        data: $("#checkout_frm").serialize(),
                        success: function(data){
                            clear_sessions();
                            $("#checkout_frm").submit();
                        }
                    });
                }

        } else {

                if (checkout_shipping_address_validation()) {
                    doCreate_checkout_log(6, 'success - <font style="color:green">Checkout Success</font>.');
                    clear_sessions();
                    return true;
                }

        }
                
    return false;
}

function total_checkoutvalidator1() {
	
	var country = $('#Customer_Country').val();
	var state = $('#shipping_state').val();
	var province = $('#shipping_province').val();
	
	if (country == 0) {
		alert('Please choose your Country');
		return false;
	} else if (country == 1) {
		if (state == 0) {
			alert('Please choose your State.');
			return false;
		}
	} else if (country == 39) {
		if (province == 0) {
			alert('Please choose your Province.');
			return false;
		}
	}

}

function checkout_shipping_country_onchange(thisval) {
	set_shopping_cart_shipping_(thisval, $('#shipping_state').val());
	get_state(thisval);
	$('#shipping_country').val(thisval);
}

///////////////////// Chexkout end ////////////////////////////














