﻿/**

 * 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_list') {

		$('#span_' + code + '_price_list').attr('primary_code', code);

	}

}

/**

 * Update price

 * @param	string	code		product code

 * @param	string	type		action type: sub-, add+, and so on...

 */

function update_price(code, type) {

    // format qty number

    var obj_qty_input = $('#qty_' + code);

    var reg = /\d/;

    var qty = obj_qty_input.val();

    for (var i = 0; i < qty.length; i++) {

        if (!reg.test(qty.charAt(i))) {

            qty = qty.substr(0, i);

        }

    }

    

    if (type == 'sub') {

        qty = parseInt(qty) - 1;

    } else if (type == 'add') {

        qty = parseInt(qty) + 1;

    }

    qty = parseInt(qty) >= 1 ? qty : 1;

    obj_qty_input.val(qty);



    // update total price

    var preferential_price = $('#preferential_price_' +  code).val();

    var unit_price = $('#base_price_' + code).val();

    var times = 0;

    if (qty == 1) {

        times = 0;

    } else if (qty >=2 && qty <= 5) {

        times = 1;

    } else if (qty >=6 && qty <= 9) {

        times = 2;

    } else if (qty >=10 && qty < 14) {

        times = 3;

    } else {

        times = 4;

    }

	$('#unit_price_' + code).val(Math.round(parseFloat(unit_price - times * preferential_price) * 100, 2) / 100);

    var total_price = Math.round(parseInt(qty) * (unit_price - times * preferential_price)*100, 2)/100;

    $('#total_price_' + code).html(total_price);

}



function select_currency(currency, code, description, type) {

	var url = '/get_form.php';

	var back_url = encodeURIComponent(location.href);

	var primary_code = $("span[id*='_price_list']").attr('primary_code');
		primary_code = primary_code != code ? code : primary_code;
		
	if (type == 'form') {

		$('#span_' + primary_code + '_' + type).html('<table cellspacing="0" cellpadding="0" border="0" style=" width:230px;"><tr><td><img src="/img/loading.gif" /></td></tr></table>');

	} else {

		$('#span_' + primary_code + '_' + type).html(' ');

	}

	

    $.get(url, {"code":code, "description":description, "currency":currency, "type":type, "back_url":back_url},

        function(data){

            $('#span_' + primary_code + '_' + type).html(data);

        }

    );

}



function select_color(obj) {

	var des = $('#item_name').attr('des');

	var color = obj.value;

	$('#item_name').val(des + ' - Color: ' + color);

	

	var src = $('.product-left img:first').attr('src');

	var dir = src.substr(0, src.indexOf('/') + 1);

	var temp_src = src.substr(src.indexOf('/') + 1);

	

	var point_pos = temp_src.lastIndexOf('_');

	if (point_pos > -1) {

		var new_src = color + '_' + temp_src.substr(point_pos + 1);

	} else {

		var new_src = color + '_' + temp_src;	

	}

	$('.product-left img:first').attr('src', dir + new_src);

}



$(function(){

	$("div[type='selected_currency']").bind('mouseenter', function(){

		$(this).next().slideDown().bind('mouseleave', function(){

			$(this).slideUp();

		}).css({'left':$(this).offset().left, 'top':$(this).offset().top});

	})	   

})



function currency_drop_list() {

	$('#selected_currency').bind('mouseenter', function(){

		$(this).next().slideDown().bind('mouseleave', function(){

			$(this).slideUp();

		}).css({'left':$(this).offset().left, 'top':$(this).offset().top});

	})

}

currency_drop_list();



function select_capacity(obj) {

	var code = obj.value;

	var primary_code = $("span[id*='_price_list']").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="/img/loading.gif" /></td></tr></table>');

	$('#span_' + primary_code + '_price_list').html(' ');

	

    $.get(new_url + '&type=price_list', function(data){

            $('#span_' + primary_code + '_price_list').html(data);

        }

    );



	$.get(new_url + '&type=form', function(data){

            $('#span_' + primary_code + '_form').html(data);

        }

    );

}

