﻿/**
 * AJAX : get price, form, price_list and so on...
 * @param	string	code		product code
 * @param	string	description	product description
 * @param	string	type		type of return value
 */
function get_form(code, description, type) {
    var url = 'get_form.php';
	var back_url = encodeURIComponent(location.href);
    $.get(url, {"code":code, "description":description, "type":type, "back_url":back_url},
        function(data){
            $('#span_' + code + '_' + type).html(data);
        }
    );
	if (type == 'price') {
		$('#span_' + code + '_price').attr('primary_code', code);
	}
}

function del_error_qty_number(obj) {
    var qty = obj.value;
    var reg = /\d/;
    for (var i = 0; i < qty.length; i++) {
        if (!reg.test(qty.charAt(i))) {
            qty = qty.substr(0, i);
        }
    }
	qty = parseInt(qty) >= 1 ? qty : 1;
	obj.value = qty;
}

function select_color(obj) {
	var des = $('#item_name').attr('des');
	var color = obj.value;
	$('#item_name').val(des + ' - Color: ' + color);
}

function select_capacity(obj) {
	var code = obj.value;
	var capacity = obj.options[obj.selectedIndex].text;
	var primary_code = $("span[id*='_price']").attr('primary_code');
	var url = $('#this_request_uri').val(); 
	
	var pos_1 = url.indexOf('code=');
	var url_1 = url.substr(0, pos_1 + 5);

	var pos_2 = url.substr(pos_1 + 5).indexOf('&');
	var url_2 = url.substr(pos_1 + 5).substr(pos_2);

	var new_url = url_1 + code + url_2;

	$('#span_' + primary_code + '_form').html('<table cellspacing="0" cellpadding="0" border="0" style=" width:230px;"><tr><td><img src="images/loading.gif" /></td></tr></table>');
	$('#span_' + primary_code + '_price').html(' ');
	
    $.get(new_url + '&type=price', function(data){
            $('#span_' + primary_code + '_price').html(data);
        }
    );

	$.get(new_url + '&type=form', function(data){
            $('#span_' + primary_code + '_form').html(data);
        }
    );

	$('#big_images').attr('src', 'images/batteries/' + code + '.jpg');
	$('#big_images').attr('title', capacity);
	$("li:contains('Capacity')").html('Capacity: <strong>' + capacity + '</strong>');
}

function bookmark(title, url){
	// Firefox
	if (window.sidebar && !document.all) {
		window.sidebar.addPanel(title, url, "");
	// Opera
	} else if(window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	// IE
	} else if(document.all) {
		window.external.AddFavorite(url, title);
	}
}

function subscribe() {
	var email = $('#email').val();
	var reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if (email.length > 0 && reg.test(email)) {
		var url = 'subscribe.php';
		$.get(url, {"email":encodeURIComponent(email)},
			function(data){
				alert(data);
			}
		);
		
	} else {
		alert('Please enter a Validated email.');
		$('#email').focus();
	}
}

function update_top_cart_info() {
	var url = 'http://www.salebattery.co.uk/cart/cart_info.php';
	$.get(url,
		function(data){
			var temp_arr = data.split('|');
			$('#items_nums').html(temp_arr[0]);
			$('#items_total').html(temp_arr[1]);
		}
	);
}