var parts = [];
var rm_count = 0;
var script_loaded = [];
script_loaded.wp_script_loaded = false;

fmtMoney = function(n, c, d, t){
	var m = (c = Math.abs(c) + 1 ? c : 2, d = d || ",", t = t || ".",
			/(\d+)(?:(\.\d+)|)/.exec(n + "")), x = m[1].length > 3 ? m[1].length % 3 : 0;
	return (x ? m[1].substr(0, x) + t : "") + m[1].substr(x).replace(/(\d{3})(?=\d)/g,
			"$1" + t) + (c ? d + (+m[2] || 0).toFixed(c).substr(2) : "");
};

function include_js(file) {
    var html_doc = document.getElementsByTagName('head').item(0);
    js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', file);
    html_doc.appendChild(js);
		return false;
}

function check_script_loaded(script_loaded_val, prices, waittime, interval) {
	if (!script_loaded[script_loaded_val]) {
		rm_count++;
		if ((rm_count*interval) == waittime) {
			setPricing(prices);
			return false;
		}
		setTimeout(function() {check_script_loaded(script_loaded_val, prices, waittime, interval)}, interval);
	} else {
		setPricing(prices);
		return false;
	}
}

function setPricing(price_class) {
	$("form." + price_class).each(function() {
		var el_id 		= this.id.value;
		var el_pnum 	= this.part_number.value;
		var el_bc 		= this.brand_code.value;
		var el_price 	= this.price.value;
		var el_core 	= this.core.value;
		
		if (parts[el_pnum + "|||||" + el_bc] != undefined) {
			el_price 				= parts[el_pnum + "|||||" + el_bc].price;
			el_core 				= parts[el_pnum + "|||||" + el_bc].core;
			this.bpp.value 	= parts[el_pnum + "|||||" + el_bc].key;
			this.price.value = el_price;
			this.core.value	 = el_core;
		}
			
		$("#price_" + el_id).html('$' + fmtMoney(el_price,2,'.',','));
		if (el_core > 0) {
			$("#core_" + el_id).append('$' + fmtMoney(el_core,2,'.',','));
			$("#core_" + el_id).css('display', 'block');
		}
	});
}