
var list = new PartsList();
var categories = new CategoryList();

function Part(categoryid, partid, part, price, def, pic){
	this.partid = partid;
	this.part = part;
	this.price = price;
	this.categoryid = categoryid;
	this.def = def;
	this.pic = pic;
}

Part.prototype.getPartId = function(){
	return(this.partid);
}

Part.prototype.getPart = function(){
	return(this.part);
}

Part.prototype.getPrice = function(){
	return(this.price);
}

Part.prototype.getCategoryId = function(){
	return(this.categoryid);
}

Part.prototype.getDef = function(){
	return(this.def);
}

Part.prototype.getPic = function(){
	return(this.pic);
}

function PartsList(){
	this.parts = new Array();
}

PartsList.prototype.addPart = function(categoryid, partid, part, price, def, pic){
	this.parts.push(new Part(categoryid, partid, part, price, def, pic));
}

PartsList.prototype.getPartInfo = function(partid){
	for(i=0; i<this.parts.length; i++){
		if(this.parts[i].getPartId() == partid){
			return(this.parts[i]);
		}
	}
}

PartsList.prototype.getCategoryParts = function(categoryid){
	var cats = new Array();
	for(i=0; i<this.parts.length; i++){
		if(this.parts[i].getCategoryId() == categoryid){
			cats.push(this.parts[i]);
		}
	}
	return(cats);
}

PartsList.prototype.getTotal = function(elem){
	
	var tot = 0.00;
	for(i=0; i<this.parts.length; i++){
		if(this.parts[i].getDef() == 1){
			tot += parseFloat(this.parts[i].getPrice());
		}
	}
	return(tot);
	
}

function CategoryList(){
	this.cats = new Array();
}

CategoryList.prototype.addCategory = function(categoryid){
	this.cats.push(categoryid);
}

CategoryList.prototype.getCategory = function(index){
	return(this.cats[index]);
}

function changeConfig(elem){
	
	var part = list.getPartInfo(elem);
	var cats = list.getCategoryParts(part.getCategoryId());
	
	
	
	
//	var part = list.getPartInfo(elem);
//	var cats = list.getCategoryParts(part.getCategoryId());
//	
//	// UPDATE INDIVIDUAL ITEMS
	for(i=0; i<cats.length; i++){
		var price_elem = document.getElementById('price_'+cats[i].getPartId());
		cats[i].def = 0;
		if(part.getPartId() == cats[i].getPartId()){
			price_elem.innerHTML = '<strong>[Included in Price]</strong>';
			cats[i].def = 1;
		}else{
			if(cats[i].getPrice() < part.getPrice()){
				var new_price = cats[i].getPrice() - part.getPrice();
				if(new_price < 0){
					price_elem.innerHTML = '<strong>[Subtract $' + Math.abs(new_price) + ']</strong>';		
				}else{
					price_elem.innerHTML = '<strong>[Add $' + new_price + ']</strong>';	
				}
			}else{
				var new_price = part.getPrice() - cats[i].getPrice();
				if(new_price < 0){
					price_elem.innerHTML = '<strong>[Add $' + Math.abs(new_price) + ']</strong>';		
				}else{
					price_elem.innerHTML = '<strong>[Subtract $' + Math.abs(new_price) + ']</strong>';	
				}
			}
		}
	}
	
	var total = list.getTotal();
	document.getElementById('custom_price_1').innerHTML = "$" + total;
	document.getElementById('custom_price_2').innerHTML = "$" + total;
	
//	
//	// UPDATE PIC
//	document.getElementById(part.getCategoryId()+"_picture").src = part.getPic();
//	
//	
//	// UPDATE LIST
//	var cat_list = document.getElementById('list_'+part.getCategoryId());
//	cat_list.innerHTML = part.getPart();
//	
//	var total = list.getTotal();
//	document.getElementById('price_total').innerHTML = "$" + total;
}

var cur_config = 0;

function loadConfig(step){
	
	var old_cat = categories.getCategory(cur_config);	
	cur_config = step;
	
	var new_cat = categories.getCategory(step);
	document.getElementById('prev_comp').className = 'prev_comp_show';
	document.getElementById('details_'+old_cat).className = 'hide_details';
	document.getElementById('details_'+new_cat).className = 'show_details';
		
}

function nextConfig(){
	
	var old_cat = categories.getCategory(cur_config);
	cur_config++;
	
	//if(cur_config >= categories.cats.length){
		document.myform.submit();
	//}else{
	//	var new_cat = categories.getCategory(cur_config);
	//	document.getElementById('prev_comp').className = 'prev_comp_show';
	//	document.getElementById('details_'+old_cat).className = 'hide_details';
	//	document.getElementById('details_'+new_cat).className = 'show_details';
	//}
	
}

function prevConfig(){
	var old_cat = categories.getCategory(cur_config);
	cur_config--;

	if(cur_config < 0){
		cur_config = 0;
		document.getElementById('prev_comp').className = 'prev_comp_hide';	
	}else{
		var new_cat = categories.getCategory(cur_config);
		document.getElementById('prev_comp').className = 'prev_comp_show';
		document.getElementById('details_'+old_cat).className = 'hide_details';
		document.getElementById('details_'+new_cat).className = 'show_details';	
	}
	
	if(cur_config == 0){
		document.getElementById('prev_comp').className = 'prev_comp_hide';		
	}
}

function fullConfig(id){
	document.getElementById('config_'+id+'_small').style.display = 'none';
	document.getElementById('config_'+id+'_full').style.display = 'block';
}

function smallConfig(id){
	document.getElementById('config_'+id+'_full').style.display = 'none';
	document.getElementById('config_'+id+'_small').style.display = 'block';
}

function updateCart(){
	document.myform.submit();
}

function help_window(categoryid){
	window.open("help.php?categoryid="+categoryid+"", "mywindow", "status=0,toolbar=0,location=0,menubar=0,directories=0,scrollbars=1,width=400,height=250");		
}