function calcPrice() {
	adText = document.adForm.adtext.value
	adText = adText.replace(/\n/g, ' ');
	adText = adText.replace(/\*/g, ' ');
	adText = adText.replace(/\s{2,}/g, ' ');
	adText = adText.replace(/^\s+/g, '').replace(/\s+$/g, '');
	document.adForm.adtext.value = adText;
	adTitle = document.adForm.adtitle.value;
	adTitle = adTitle.replace(/\n/g, ' ');
	adTitle = adTitle.replace(/\(/g, ' (');
	adTitle = adTitle.replace(/\)/g, ') ');
	adTitle = adTitle.replace(/\s{2,}/g, ' ');
	adTitle = adTitle.replace(/^\s+/g, '').replace(/\s+$/g, '');
	document.adForm.adtitle.value = adTitle;
	text = document.adForm.adtitle.value + ' ' + document.adForm.adtext.value;
	var t = text.replace(/\//g, ' ');
	t = t.replace(/\\/g, ' ');
	t = t.replace(/_/g, ' ');
	t = t.replace(/([a-z]-[a-z])/gi,' ');
	t = t.replace(/([a-z]-[0-9])/gi,' ');
	t = t.replace(/([a-z]:[0-9 a-z])/gi,' ');
	t2 = t.split(' ');
	count = t2.length;
	months = document.adForm.months.value;
	cost = 0;
	if (text.length>1 && count<=10) {
		cost = 5;
	} 
	if (text.length>1 && count>10) {
		cost = ((count - 10) * .25) + 5;
	}
	if (document.adForm.used_curriculum.checked == true) {
		totalcost = 0;
		cost = 0;
		document.adForm.months.selectedIndex = 0;
	} else {
		totalcost = cost * months;
	}
	document.adForm.cost.value = formatAsMoney(cost);
	document.adForm.totalcost.value = formatAsMoney(totalcost);
}

function calcRenew() {
	cost = document.renewForm.cost.value;
	months = document.renewForm.months.value;
	totalcost = cost * months;
	document.renewForm.totalcost.value = formatAsMoney(totalcost);
}

function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

function resetForm() {
	if (confirm('Are you sure you want to clear this form and start over?')) {
		document.location=document.location;
	}
}

function checkForm() {
	that = document.adForm;
	var msg='';
	if (that.name.value.length < 1) {
		msg += '=> Your name is missing\n';
	}
	if (that.email.value.length < 1) {
		msg += '=> Your e-mail address is missing\n';
	} else {
		if (!isValidEmail(document.adForm.email.value)) {
			msg += '=> Please enter a valid e-mail address\n';
		}
	}
	if (that.telephone.value.length < 1) {
		msg += '=> Your telephone number is missing\n';
	} else {
		if (!checkTelephone(document.adForm.telephone.value)) {
			msg += '=> Enter your full phone number including the area code\n';
		}
	}
	if (that.adtitle.value.length < 1) {
		msg += '=> Your ad title is missing\n';
	}
	if (that.adtext.value.length < 1) {
		msg += '=> Your ad text seems a little too short\n';
	}
	if (msg.length > 0) {
		msg = 'There seems to be a problem with the information you\'ve entered\n\n'+msg;
		msg += '\n\nPlease go back and fill out all of the fields properly';
		alert(msg);
		return false;
	} else {
		document.adForm.submit();
	}
}

function checkTelephone(tel) {
	tel = tel.replace(/[^0-9]/g, '');
	if (tel.length < 10) {
		return false;
	} else {
		return true;
	}
}

function isValidEmail(address) {
	if (address != '' && address.search) {
  	if (address.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
	  	return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

