// Cost of Poor Writing functions
var calcMe=true;
function PoorWritingCost() {
	for (i=0; i<document.copw.length-1; i++) {
		if (document.copw[i].value=="") { 
			document.copw[i].style.background='pink';
			alert('Please correctly fill out all of the fields below.');
			calcMe=false; 
		}
	}

	if (calcMe==true) {
		var emp=removeCommas(document.copw.employees.value), sal=removeCommas(document.copw.salary.value), msg=removeCommas(document.copw.messages.value), rcp=removeCommas(document.copw.recipients.value); 
		
		
		var costBWTK="", costWFB="", costWFW="";
		
		
		
		if (emp<50) { costBWTK=74.99; } else if (emp>49 && emp<200) { costBWTK=67.49; } else if (emp>199 && emp<500) { costBWTK=59.99; } else { costBWTK=52.99; } 
		
		if (emp<50) { costWFW=48.99; } else if (emp>49 && emp<100) { costWFW=45.99; } else if (emp>99 && emp<500) { costWFW=42.99; } else { costWFW=40.99; } 	
		
		
		
		if (emp<10) { costWFB=29.95; } else if (emp>9 && emp<25) { costWFB=26.96; } else if (emp>24 && emp<100) { costWFB=23.96; } else { costWFB=20.97; } 
		
		
		
		
		var cost=Comma(Math.round(emp*sal*msg*rcp*0.05/2080/6*250));
		var displayBWTK= addCommaToDecimal(Math.round(emp*costBWTK).toFixed(0)), displayWFB=addCommaToDecimal(Math.round(emp*costWFB).toFixed(0)), displayWFW=addCommaToDecimal(Math.round(emp*costWFW).toFixed(0));
		if (cost=='NaN') { document.copw.total.value="";  }
		else { 
		
// Print functions sent back to the webpage
		document.getElementById('calcresult').innerHTML="$"+cost;
		document.getElementById('BWTK_cost').innerHTML= displayBWTK;
		document.getElementById('WB_cost').innerHTML= displayWFB;
		document.getElementById('WFW_cost').innerHTML= displayWFW;
		document.getElementById('BWTK_unitnumber').innerHTML=emp;
		document.getElementById('WB_unitnumber').innerHTML=emp;
		document.getElementById('WFW_unitnumber').innerHTML=emp;
		
		// This is the field that sends back data to the hidden field
		document.getElementById('CAT_Custom_108418').value="<table><tr><td class='desc'><p>Number of Employees</p></td><td class='cost'>"+emp+"</td></tr><tr><td class='desc'><p>Average Salary per Employee</p></td><td class='cost'>"+sal+"</td></tr><tr><td class='desc'><p>Number of E-Mail Messages Send per Day</p></td><td class='cost'>"+msg+"</td></tr><tr><td class='desc'><p>Recipients per Message</p></td><td class='cost'>"+rcp+"</td></tr><tr><td class='desc'><p>What Could Poor Writing Cost You in a Year?</p></td><td class='cost'><div class='poorcost'>$"+cost+"</div></td></tr></table>";
		document.getElementById('CAT_Custom_107960').value="<table><tr><td class='desc'><p><a href='http://www.upwritepress.com/CatalogueRetrieve.aspx?CatalogueID=37352&ProductID=354437' target='_blank'>Business-Writing Training Kits</a></p></td><td class='cost'><div class='uwpcost'>$"+displayBWTK+ "<span class='units'>for "+emp+" units</span></div></td></tr><tr><td class='desc'><p><a href='http://www.upwritepress.com/CatalogueRetrieve.aspx?CatalogueID=37268&ProductID=247292' target='_blank'>Write for Business</a></p></td><td class='cost'><div class='uwpcost'>$"+displayWFB +"<span class='units'>for "+emp+" units</span></div></td></tr><tr><td class='desc'><p><a href='http://www.upwritepress.com/_product_37268/Write_for_Work' target='_blank'>Write for Work</a></p></td><td class='cost'><div class='uwpcost'>$"+displayWFW +"<span class='units'>for "+emp+" units</span></div></td></tr></table>";		
		}
	}
}

function removeCommas(myNum) {
	var stripDol=myNum.replace(/\$/,'');
	var striptNum=stripDol.replace(/\,/g,'');
	return (striptNum);
}
function Comma(number) {
	number='' + number;
	if (number.length > 3) {
		var mod=number.length % 3;
		var output=(mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length / 3); i++) {
			if ((mod==0) && (i==0))
			output +=number.substring(mod+ 3 * i, mod + 3 * i + 3);
			else
			output+=',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
		}
	return (output);
	}
	else return number;
}
function checkIt(whichIn) {
	var stripDol;
	if (whichIn=="salary") { stripDol=eval('document.copw.'+whichIn).value.replace(/\$/,''); }
	else {stripDol=eval('document.copw.'+whichIn).value; }
	if ((stripDol=="") || (IsNumeric(stripDol)==false)) {
		eval('document.copw.'+whichIn).style.background='pink';
		alert('Please correctly fill out all of the fields below.');
	}
}
function emptyIt(whichIn) {
	eval('document.copw.'+whichIn).value="";
	eval('document.copw.'+whichIn).style.background='#ffffff';
}
function IsNumeric(sText) {
	var ValidChars="0123456789,", IsNumber=true, Char;
	for (i=0; i < sText.length && IsNumber==true; i++) { 
		Char=sText.charAt(i); 
		if (ValidChars.indexOf(Char)==-1) { IsNumber=false; }
		}
	return IsNumber;
}
function addCommaToDecimal(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;
}
// end Cost of Poor Writing functions

