/*
+----------------------------------------------------------------------+
| BCSE Reward Points Mod                                               |
+----------------------------------------------------------------------+
| Copyright (c) 2004-2010 BCSE LLC. dba BCS Engineering                |
+----------------------------------------------------------------------+
|                                                                      |
| BCSE Reward Points Mod is subject for version 2.0 of the BCSE        |
| proprietary license. That license file can be found bundled with     |
| this package in the file BCSE_LICENSE. A copy of this license can    |
| also be found at                                                     |
| http://www.bcsengineering.com/license/BCSE_LICENSE_2.0.txt           |
|                                                                      |
+----------------------------------------------------------------------+
*/

function get_points_from_price(price){

	if(ef_points == 'Y')
		return product_points;

	if(points_rate_type == '$')
		var points = price * points_rate;
	else
		var points = price * points_rate / 100;

	return points;
}

function update_points(price){
	if(document.getElementById('product_points')){
		points = get_points_from_price(Math.max(price, 0));

		if(typeof points_floats != 'undefined' && points_floats == 'Y'){
			points = price_format(points);
		}else{
			points = Math.floor(points);
		}

		document.getElementById('product_points').innerHTML = points;
	}
}

function set_up_points_change(){
	if(typeof check_options == 'function'){
		window._bcse_check_options = window.check_options;
		window.check_options = function(){
			window._bcse_check_options();
			if(document.getElementById('product_price'))
				update_points(points_fix_price(document.getElementById('product_price').firstChild.nodeValue));
		}

		check_options();
	}

	if(typeof check_wholesale == 'function'){
		window._bcse_check_wholesale = window.check_wholesale;
		window.check_wholesale = function(qty){
			window._bcse_check_wholesale(qty);
			if(document.getElementById('product_price'))
				update_points(points_fix_price(document.getElementById('product_price').firstChild.nodeValue));
		}
	}
}

function points_fix_price(p){

	var _p = p;

	if(typeof(number_format_th) != 'undefined' && number_format_th != ''){
		var regex = new RegExp((number_format_th == '.'?'\\.':number_format_th));
		while(_p.match(regex))
			_p = _p.replace(regex,'');
	}

	if(typeof(number_format_dec) != 'undefined' && number_format_dec != '.')
		_p = _p.replace(number_format_dec,'.');

	return _p;
}

function addEventToElement(target, eventType, func, useCapture){
	var result = false;

	if (target.addEventListener){
		target.addEventListener(eventType, func, useCapture);
		result = true;
	}else if (target.attachEvent){
		result = target.attachEvent("on" + eventType, func);
	}

	return result;
}

addEventToElement(window,'load',set_up_points_change,false);

