// word functions

var maxWordCount = 65;
var word_count = 0;
function CountWords (this_field, showfield) 
{
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var email_rExp = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
var initial_whitespace_rExp = /^[^\-A-Za-z0-9]+/gi;
var telephone_number_rExp = /\(?\d{3}\)?([\-\s\.])\d{3}\1\d{4}/g;
var non_alphanumerics_rExp = /[^\-A-Za-z0-9]+/gi;
// tokenize email addresses
var tStrippedStr = fullStr.replace(email_rExp, "NNN");
// tokenize phone numbers
tStrippedStr = tStrippedStr.replace(telephone_number_rExp, "NNN");
// remove lead/trail white space
tStrippedStr = tStrippedStr.replace(initial_whitespace_rExp, "");
// tokenize remaining word delimiters
var cleanedStr = tStrippedStr.replace(non_alphanumerics_rExp, " ");
cleanedStr = cleanedStr.replace(/\s+$/g,'');
var splitString = cleanedStr.split(" ");
var tt = "";
var j = 0;
	word_count = splitString.length;

	if (fullStr.length <2) 
		word_count = 0;
	
	if (word_count >= maxWordCount)
	{
		var t = 0,i = 0;
		for (i = 0; i < splitString.length-1; i++)
		{
			t += splitString[i].length+1;
			if (i ==  maxWordCount)
				break;
		}
		
		cleanedString = this_field.value;
		
		for (j = t; j < cleanedString.length-1;j++)
		{
			tt = cleanedString.substring(j,j+1);
			if (non_alphanumerics_rExp.test(tt))
				break;
		}
		cleanedString = cleanedString.substr(0,j);
		this_field.value = cleanedString;
	}

	showfield.value=word_count;
	calcCost(showfield.form);
	return word_count;
}

function calcCost(frm)
{
var t;
var ex_words = 0;
var cost = 0;
var reserve = 0;
var numissues = parseInt(frm.numissues.value);
var numplacements = parseInt(frm.numplacements.value);

	if (frm.name == "Commercial_Reader_Ad") reserve = 20;
	ex_words = word_count-20;
	if (ex_words < 0) ex_words = 0;
	cost = ex_words;
	if (frm.extras[0].checked) cost += 0;
	if (frm.extras[1].checked) cost += 10;
	if (frm.extras[2].checked) cost += 15;
	if (frm.extras[3].checked) cost += 20;
	if (word_count > 0) cost += reserve; else cost = 0;
	cost *= numissues;
	cost *= numplacements;
	frm.cost.value = cost;
	frm.base_price.value = cost;
}



















function CountWords2 (this_field, showfield) 
{
var char_count = this_field.value.length;
var fullStr = this_field.value + " ";
var email_rExp = /([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi;
var initial_whitespace_rExp = /^[^\-A-Za-z0-9]+/gi;
var telephone_number_rExp = /\(?\d{3}\)?([\-\s\.])\d{3}\1\d{4}/g;
var non_alphanumerics_rExp = /[^\-A-Za-z0-9]+/gi;
// tokenize email addresses
var tStrippedStr = fullStr.replace(email_rExp, "NNN");
// tokenize phone numbers
tStrippedStr = tStrippedStr.replace(telephone_number_rExp, "NNN");
// remove lead/trail white space
tStrippedStr = tStrippedStr.replace(initial_whitespace_rExp, "");
// tokenize remaining word delimiters
var cleanedStr = tStrippedStr.replace(non_alphanumerics_rExp, " ");
cleanedStr = cleanedStr.replace(/\s+$/g,'');
var splitString = cleanedStr.split(" ");
var tt = "";
var j = 0;
	word_count = splitString.length-1;

	if (fullStr.length <2) 
		word_count = 0;
	
	if (word_count >= maxWordCount)
	{
		var t = 0,i = 0;
		for (i = 0; i < splitString.length-1; i++)
		{
			t += splitString[i].length+1;
			if (i ==  maxWordCount)
				break;
		}
		
		cleanedString = this_field.value;
		
		for (j = t; j < cleanedString.length-1;j++)
		{
			tt = cleanedString.substring(j,j+1);
			if (non_alphanumerics_rExp.test(tt))
				break;
		}
		cleanedString = cleanedString.substr(0,j);
		this_field.value = cleanedString;
	}

	showfield.value=word_count;
	calcCost2(showfield.form);
	return word_count;
}
function calcCost2(frm)
{
var t;
var ex_words = 0;
var cost = 0;
var reserve = 0;
var numissues = parseInt(frm.numissues.value);
var numplacements = parseInt(frm.snumplacements.value);
	if (frm.name == "Commercial_Reader_Ad") reserve = 20;
	ex_words = word_count-20;
	if (ex_words < 0) ex_words = 0;
	cost = ex_words;
	if (frm.extras[0].checked) cost += 0;
	if (frm.extras[1].checked) cost += 10;
	if (frm.extras[2].checked) cost += 15;
	if (frm.extras[3].checked) cost += 20;
	if (word_count > 0 || frm.name == "Commercial_Reader_Ad") cost += reserve; 
	cost *= numissues;
	cost *= numplacements;
	frm.cost.value = cost;
	frm.base_price.value = cost;
	//alert('Cost:'+cost+' reserve:'+reserve+' ex_words:'+ex_words+' numissues:'+numissues+' numplacement:'+numplacements+" " + frm.name);
}























function doPreview(txt,fmt)
{
var t = txt.value;
var w;

	if (fmt[0].checked) t =  "<center><table width='200'><tr><td>" + t + "</td></tr></table></center>";
	if (fmt[1].checked) t =  "<center><table width='200'><tr><td><b>" + t + "</b</td></tr></table></center>";
	if (fmt[2].checked) 
	{	
		t = "<center><table width='200'style='border:thick solid black'><tr><td><b>" + t + "</b</td></tr></table></center>";
	}
	if (fmt[3].checked) 
	{
		t = "<center><table width='200'bgcolor='#000000' style='color:#ffffff';font-weight:bold'><tr><td><b>" + t + "</b</td></tr></table></center>";
	}
	w = window.open('',null,"height=300,width=250,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,bReplace=true");
	w.document.write("");
	w.document.close();
	w.document.write(t);
}

/*function showSample(txt)
{
var w;
	w= window.open('',null,'height=300,width=400,scrollbars=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,bReplace=true');
	w.document.write(txt);
}*/