function get_option_price(str){
	var bindex = str.indexOf("(+$");
	var eindex = str.indexOf(")");
	var price = str.substr(bindex+3, eindex-bindex-3).replace(',','');
	return price;
};

function erp_index_for_price(price){
	if ( price > 0 && price <= 497.97)
		return 1;
	else if (price > 497.97 && price <= 997.97)
    	return 2;
	else if (price > 997.97 && price <= 1497.97)
    	return 3;
    else if (price > 1497.97 && price <= 1997.97)
    	return 4;
    else if (price > 1997.97 && price <= 2497.97)
    	return 5;
    else if (price > 2497.97 && price <= 2997.97)
    	return 6;
    else if (price > 2997.97 && price <= 3497.97)
	    return 7;
	else if (price > 3497.97 && price <= 3997.97)
    	return 8;
    else if (price > 3997.97 && price <= 4497.97)
    	return 9;
    else if (price > 4497.97 && price <= 5997.97)
    	return 10;
};

model_select_handlers = new Array()

function model_select_add_handler(model_select, handler){
	
	
	model_select_handlers[model_select_handlers.length] = handler;
	
	function trigger_handlers(){
		for (key in model_select_handlers){
			model_select_handlers[key]();
		}
	}
	
	model_select.onchange = trigger_handlers;
	
};

function applyWarranty(model_select_id, warranty_select_id, cbox_id, price_span_id){
	var cbox = document.getElementById(cbox_id);
	var model_select = document.getElementById(model_select_id);
	var warranty_select = document.getElementById(warranty_select_id);
	var price_span = document.getElementById(price_span_id);
	
	function update_warranty(){
		var price = get_option_price(model_select.options[model_select.selectedIndex].text);
		var warranty_price = get_option_price(warranty_select.options[erp_index_for_price(price)].text);
	    if(cbox.checked)
	  		warranty_select.selectedIndex = erp_index_for_price(price);
		else
	      	warranty_select.selectedIndex = 0;
	      	
		price_span.innerHTML = "$"+warranty_price;
	};
	  
	model_select_add_handler(model_select, update_warranty);
	cbox.onclick = update_warranty;
	update_warranty();
};

function applyPromo(model_select_id, select_id, cbox_id, unit_price_id){
	var cbox = document.getElementById(cbox_id);
	var model_select = document.getElementById(model_select_id);
	var select = document.getElementById(select_id);
	var unit_price = document.getElementById(unit_price_id);

	function update_promo(){
	  var price = get_option_price(model_select.options[model_select.selectedIndex].text);
	  unit_price.value = price;
	
	  if(cbox.checked)
	    select.selectedIndex = 1;
	  else
	    select.selectedIndex = 0;
	 };
	  
	 model_select_add_handler(model_select, update_promo);
	 cbox.onclick = update_promo;
	 update_promo();
};

function getMaxPrice(model_select_id){

	var model_select = document.getElementById(model_select_id);
	var max_price = 0;
	
	for (var i=0; i < model_select.options.length; i++){
		price = parseFloat(get_option_price(model_select.options[i].text));
		if(price > max_price)
			max_price = price;
	}
	
	return max_price;
};
