added = 'Added!';
add2cart = 'Add To Cart';


function toggleDisplay(divObj) {

	//alert("div name = "+divObj);
	var el = document.getElementById(divObj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	} 	
	
	if (divObj == 'warranty')  {
		initializeESPCost();
	} else if (divObj == 'optional') {
		initializeOptionalCost();
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function toggleProgress(divObj) {

	var el = document.getElementById(divObj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	} 	
	
	if (divObj == 'warranty')  {
		initializeESPCost();
	} else if (divObj == 'optional') {
		initializeOptionalCost();
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function changeRequiredLabel (which) {

//	alert("in changeRequiredLabel");

	cost = document.getElementById('required_cost_'+which.value).value;
	 	
	if (which.checked) {
		 document.getElementById('label_'+which.value).innerHTML = added; 
	} else {
		 document.getElementById('label_'+which.value).innerHTML = add2cart;	
		 cost *= -1;	
	}	
	
	updateRequiredCost(cost);
	calculateRunningTotal();

}


function updateRequiredCost(fCost) {
	
	 //alert ('cost = '+ fCost);
	 //alert ('req cost total = '+ parseFloat(document.getElementById('requiredCostTotal').value));
	 final_cost =  parseFloat(document.getElementById('requiredCostTotal').value)  + parseFloat(fCost);
	 //alert('final cost = '+ final_cost);
	
	document.getElementById('requiredCostTotal').value = round(final_cost);
	document.getElementById('requiredCost').innerHTML = moneyFormat(round(final_cost));

}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function initializeESPCost() {

	//alert("espCost string = "+document.getElementById('espCost').innerHTML);
	text = document.getElementById('espCost').innerHTML;
	if (text.indexOf("-") != -1) {
		document.getElementById('espCost').innerHTML = "<strong>$0.00</strong>";
	}
		
}

function changeESPLabel (form, which) {

	//alert("which = "+which.value);
	cost = document.getElementById('esp_cost_'+which.value).value;
	 	
	if (which.checked) {
		 document.getElementById('label_'+which.value).innerHTML = added; 
	} else {
		 document.getElementById('label_'+which.value).innerHTML = add2cart;	
		 cost *= -1;	
	}	
	
	for(i=0; i<form.elements.length; i++) {
		if (form.elements[i].type=="checkbox" && (form.elements[i].name.search("esp_checkbox_") != -1) ) {
			if (form.elements[i].name.search("esp_checkbox_"+which.value) == -1)  {
				if (form.elements[i].checked) {
					document.getElementById('label_'+form.elements[i].value).innerHTML = add2cart;	
				    oldESPCost = document.getElementById('esp_cost_'+form.elements[i].value).value *-1;	    
					updateESPCost(oldESPCost);
				}
				form.elements[i].checked = false;
			}
		}	
	}


	updateESPCost(cost);
	calculateRunningTotal();

}


function updateESPCost(fCost) {
	
	 //alert ('cost = '+ fCost);
	 //alert ('esp cost total = '+ parseFloat(document.getElementById('espCostTotal').value));
	 final_cost =  parseFloat(document.getElementById('espCostTotal').value)  + parseFloat(fCost);
	 //alert('final cost = '+ final_cost);
	
	document.getElementById('espCostTotal').value = round(final_cost);
	document.getElementById('espCost').innerHTML = moneyFormat(round(final_cost));

}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function initializeOptionalCost() {

	//alert("optionalCost string = "+document.getElementById('optionalCost').innerHTML);
	text = document.getElementById('optionalCost').innerHTML;
	if (text.indexOf("-") != -1) {
		document.getElementById('optionalCost').innerHTML = "<strong>$0.00</strong>";
	}
		
}


function changeOptionalLabel (which) {

	//alert("which.value = "+ which.value);
	cost = document.getElementById('optional_cost_'+which.value).value;
	//alert("optional cost = "+ cost);
	 	
	if (which.checked) {
		 document.getElementById('label_'+which.value).innerHTML = added; 
	} else {
		 document.getElementById('label_'+which.value).innerHTML = add2cart;	
		 cost *= -1;	
	}	
	
	updateOptionalCost(cost);
	calculateRunningTotal();

}


function updateOptionalCost(fCost) {
	
	 //alert ('cost = '+ fCost);
	 //alert ('optional cost total = '+ parseFloat(document.getElementById('optionalCostTotal').value));
	 final_cost =  parseFloat(document.getElementById('optionalCostTotal').value)  + parseFloat(fCost);
	 //alert('final cost = '+ final_cost);
	
	document.getElementById('optionalCostTotal').value = round(final_cost);
	document.getElementById('optionalCost').innerHTML = moneyFormat(round(final_cost));

}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function calculateRunningTotal() {
	//alert("calculateRunningTotal");

	applCost 	= document.getElementById('applianceCostTotal').value;
	reqCost 	= document.getElementById('requiredCostTotal').value;
	wrntyCost	= document.getElementById('espCostTotal').value;
	optCost	= document.getElementById('optionalCostTotal').value;
	shipCost	= document.getElementById('shippingCostTotal').value;
	
	subTotal = parseFloat(applCost) + parseFloat(reqCost) + parseFloat(wrntyCost) + parseFloat(optCost) + parseFloat(shipCost);
	
	//alert('total = '+ subTotal);
	
	document.getElementById('subtotal').value = round(subTotal);
	document.getElementById('appliance-subtotalvalue').innerHTML = moneyFormat(round(subTotal));

}

function moneyFormat(y) {
    
    var x=y+'';
    //alert("x = "+ x);
	if (x.indexOf(".") == -1) {
		temp =  "<strong>$"+x+".00</strong>";
	} else {
	
	//	alert("x length = "+x.length);
	//	alert(". index = "+x.indexOf("."));
		charsLeft = x.length - (x.indexOf(".")+1);
	//	alert("charsLeft = "+charsLeft);
		
		if (charsLeft != 2) {
			temp =  "<strong>$"+x+"0</strong>";
		} else {
			temp = "<strong>$"+x+"</strong>";
		}
	}
	//alert ("temp = "+temp);
	return temp;
}

function round(x) {
	return Math.round(x*100)/100;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function updatePartsServicesSelectedItems() {
	var form = document.partsServicesForm;

	if(document.getElementById("editCnt")) {
		var requiredKey = "required_checkbox_";
		var espKey = "esp_checkbox_";
		var optionalKey = "optional_checkbox_";
		for(var i = 0; i < form.elements.length; i++) {
			if(form.elements[i].type == "checkbox") {
				if(form.elements[i].name.search(requiredKey) == 0) {
					if(handlePartsServicesEditCheckbox(form.elements[i])) {
						changeRequiredLabel(form.elements[i]);
					}
				}
				else {
					if(form.elements[i].name.search(espKey) == 0) {
						if(handlePartsServicesEditCheckbox(form.elements[i])) {
							changeESPLabel(form, form.elements[i]);
						}
					}
					else {
						if(form.elements[i].name.search(optionalKey) == 0) {
							if(handlePartsServicesEditCheckbox(form.elements[i])) {
								changeOptionalLabel(form.elements[i]);
							}
						}
					}
				}
			}
		}
	}
}

function handlePartsServicesEditCheckbox(checkbox) {
	var retval = false;

	var storeSkuElement = document.getElementById(checkbox.value);
	if(storeSkuElement) {
		if(!checkbox.checked) {
			checkbox.checked = true;
			retval = true;
		}
	}
	else {
		if(checkbox.checked) {
			checkbox.checked = false;
			retval = true;
		}
	}
	
	return retval;
}

