
// Job Cost Calculator - dynamically calculates cost and savings for number of job listings
function calculateSavings() {
	var inputNum 	= document.getElementById("job-listing-input");
	var calcBlock	= document.getElementById("cost-calc-block");
	var error		= document.getElementById("error-message-text");
	var cost		= document.getElementById("cost-amount");
	var savings		= document.getElementById("savings-amount");
	
	if (inputNum.value < 1 || inputNum.value > 99 || inputNum.value == "" || isNaN(inputNum.value) || inputNum.value == "Enter #"){
		
		calcBlock.className = calcBlock.className+" highlight-error";
		inputNum.style.color 	= "red";
		cost.innerHTML 			= "0.00";
		savings.innerHTML		= "0.00";
		if (inputNum.value == ""){
			inputNum.value = "Enter #";
		}
		if (inputNum.value < 1 || inputNum.value == "" ||  isNaN(inputNum.value) || inputNum.value == "Enter #"){
			error.innerHTML = selectErrorInvalidMsg;
		}
		else if (inputNum.value > 99){
			error.innerHTML = selectErrorTooManyMsg; 
		}
		error.style.display		= "block";
		return false;
	}
	else {
		calcBlock.className = calcBlock.className.replace("highlight-error","");
		
		error.style.display		= "none";
		inputNum.style.color 	= "black";
		
		var unitCost;
		var jobBlockCost;
		var jobBlockSavings;
		var listings = inputNum.value;
		
		for (var x = 0; x < 11; x++){
			if (listings >= job_packs[x+1][0]){
				continue;
			}
			else {
				unitCost = job_packs[x][1];
				break;
			}
		}
		jobBlockCost		= listings * unitCost;
		cost.innerHTML 		= addCommas(jobBlockCost);
		jobBlockSavings		= (listings * job_packs[0][1]) - jobBlockCost;
		savings.innerHTML	= addCommas(jobBlockSavings);
		return true;
	}
}

// Add a comma for the thousand position
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function calculateSavingsOtto() {
	var inputNum 	= document.getElementById("job-listing-input");
	var error		= document.getElementById("error-message-text");
	var cost		= document.getElementById("cost-amount");
	var savings		= document.getElementById("savings-amount");
	
	if (inputNum.value < 1 || inputNum.value > 99 || inputNum.value == "" || isNaN(inputNum.value) || inputNum.value == "Enter #"){
		cost.innerHTML 			= "0.00";
		savings.innerHTML		= "0.00";
		if (inputNum.value < 1 || inputNum.value == "" ||  isNaN(inputNum.value) || inputNum.value == "Enter #"){
			//alert(selectErrorInvalidMsg);
			showErrorOverlay("Error", selectErrorInvalidMsg)
			inputNum.value="";
			cost.innerHTML 			= "0.00";
			savings.innerHTML		= "0.00";
		} else if (inputNum.value > 99){
			//alert(selectErrorTooManyMsg);
			showErrorOverlay("Error", selectErrorTooManyMsg)
			inputNum.value="";
			cost.innerHTML 			= "0.00";
			savings.innerHTML		= "0.00";		
		}
		return false;
	} else {
		var unitCost;
		var jobBlockCost;
		var jobBlockSavings;
		var listings = inputNum.value;
		
		for (var x = 0; x < 11; x++){
			if (listings >= job_packs[x+1][0]){
				continue;
			} else {
				unitCost = job_packs[x][1];
				break;
			}
		}
		jobBlockCost		= listings * unitCost;
		cost.innerHTML 		= addCommas(jobBlockCost);
		jobBlockSavings		= (listings * job_packs[0][1]) - jobBlockCost;
		savings.innerHTML	= addCommas(jobBlockSavings);
		return true;
	}
}

function validateInputBeforeSubmittingOtto(){
		if(calculateSavingsOtto()){
			document.ProductPurchaseForm.submit();
			return true;
		}
		else{
			return false;
		}
	}

function showErrorOverlay(errTitle, errBody) {
	$j("#errorTitle").css("color","#ff0000");
	$j("#errorTitle").html(errTitle);
	$j("#errorBody").html(errBody);
	$j('#dialog').jqmShow();
}