function adRun(adField)
{
	this.adField = document.getElementById(adField);
	this.papers = new Object();
	this.paperCount = 0;
	this.wdCount = 0;
	this.totalCirc = 0;
	this.totalPapers = 0;
	this.process = adRun_process;
	this.isPaper = adRun_isPaper;
	this.procPrice = adRun_procPrice;
	this.calculate = adRun_calculate;
	this.wordCount = adRun_wordCount;
	this.getTotal = adRun_getTotal;
	this.store = adRun_store;
}

function adRun_store()
{
	if(this.paperCount > 0)
	{
		var field = document.getElementById("runData");
		var totField = document.getElementById("totalData");
		var str = "Paper\t25/w Rate\tadd/wrd Rate\tWord Count\tSub Total\r\n";
		var tot = 0;
		var temp;
		for(paper in this.papers)
		{
			str += this.papers[paper].paper + "\t";
			str += this.papers[paper].initialCost + "\t";
			str += this.papers[paper].addCost + "\t";
			str += this.wdCount + "\t";
			temp = this.papers[paper].calculate(this.wdCount);
			tot = tot + temp;
			str += temp + "\n";
		}
		totField.value = parseFloat(tot).toFixed(2);
		field.value = str.substring(0,(str.length - 1));
	}
}

function adRun_getTotal()
{
	if(this.adField.value.length > 0)
	{
		//if statement won't work with onchange on textarea
		/*if(this.paperCount > 0)
		{*/
			var display = document.getElementById("display");
			var output = this.calculate() + "&nbsp;|&nbsp;<strong>Words: </strong>";
			output += this.wdCount + "&nbsp;|&nbsp;<strong>Total Circulation: </strong>";
			output += this.totalCirc + "&nbsp;|&nbsp;<strong>Number of newspapers: </strong>";
			output += this.totalPapers;
			display.innerHTML = output;
		/*}
		else
		{
			alert("You must select some papers to run in before getting a total.");
		}*/
	}
	else
	{
		alert("Please supply the text for your ad in the Ad Text field");
	}/**/
}

function adRun_isPaper(name)
{
	if(this.paperCount != 0)
	{
		if(typeof this.papers[name] != "undefined")
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function adRun_wordCount(text)
{
	if(text != "")
	{
		var ret = text.split(" ");
		this.wdCount = ret.length;
		return this.wdCount;
	}
	else
	{
		return 0;	
	}
}

function adRun_procPrice(price)
{
	price = price.replace("$","");
	if(price == "" || price == "&nbsp;")
	{
		price = 0;
	}
	return price;
}

function adRun_process(ckObj)
{
	if(typeof ckObj != "undefined")
	{
		var row = ckObj.parentNode.parentNode;
		var rowID = row.id
		if(ckObj.checked)
		{
			var paper = ckObj.name;
			var rowElm = row.getElementsByTagName("td");
			var initialCost = this.procPrice(rowElm.item(2).innerHTML);
			var addCost = this.procPrice(rowElm.item(3).innerHTML);
			var totalCirc = rowElm.item(4).innerHTML;
			var publications = rowElm.item(5).innerHTML;
			this.papers[rowID] = new newsPaper(rowID,paper,initialCost,addCost,totalCirc,publications);
			this.paperCount++;
			this.getTotal();
		}
		else
		{
			if(this.isPaper(rowID))
			{
				delete this.papers[rowID];
				this.paperCount--;
				this.getTotal();
			}
		}
	}
}

function adRun_calculate()
{
	var txt = this.adField.value;
	var wordCount = this.wordCount(txt);
	if(wordCount > 0)
	{
		var total = 0;
		this.totalCirc = 0;
		this.totalPapers = 0;
		for(paper in this.papers)
		{
			if(typeof total == 'string')
			{
				total = parseFloat(total);
			}
			total = total + parseFloat(this.papers[paper].calculate(wordCount));
			this.totalCirc = this.totalCirc + parseInt(this.papers[paper].totalCirc);
			this.totalPapers = this.totalPapers + parseInt(this.papers[paper].publications);
		}
		return total.toFixed(2);
	}
}
//*******************************************************************
function newsPaper(id,paper,initialCost,addCost,totalCirc,publications)
{
	this.id = id;
	this.paper = paper;
	this.initialCost = initialCost;
	this.addCost = addCost;
	this.filterNumber = paper_filterNumber;
	this.totalCirc = this.filterNumber(totalCirc);
	this.publications = this.filterNumber(publications);
	this.total = 0;
	this.calculate = paper_calculate;
}

function paper_filterNumber(number)
{
	number = number.replace(/,/g,"");
	if(number.replace(/\s/g,'') == "" || number == "&nbsp;")
	{
		number = 0
	}
	return number;
}

function paper_calculate(wordCount)
{
	if(wordCount <= 25)
	{
		this.total = parseFloat(this.initialCost);
		return this.total;
	}
	else
	{
		if(this.addCost > 0)
		{
			var dif = wordCount - 25;
			var add = dif * this.addCost;
			this.total = add + parseFloat(this.initialCost);
			return this.total;
		}
		else
		{
			this.total = this.initialCost;	
			return this.total;
		}
	}
}
