/***********************************
*  Beliefnet Newsletter JS
*  Created on: 07/30/08
*  Rich Rudzinski
***********************************/
function resetFields() {
	//var inputArray = document.getElementsByTagName('input');
	var inputArray = $("#newsletterForm input, #manageNewsletters input");
	for(j=0;j<inputArray.length;j++){
		if(inputArray[j].type == 'text') {
			inputArray[j].onfocus = function() {
				if (this.value == this.defaultValue) this.value = '';
			}
			inputArray[j].onblur = function() {
				if(this.value == '') this.value = this.defaultValue;
			}
		}
	}
	//var textareaArray = document.getElementsByTagName('textarea');
	var textareaArray = $("#newsletterForm textarea, #manageNewsletters textarea");
	for(j=0;j<textareaArray.length;j++){
		textareaArray[j].onfocus = function() {
			if (this.value == this.defaultValue) this.value = '';
		}
		textareaArray[j].onblur = function() {
			if(this.value == '') this.value = this.defaultValue;
		}
	}		
}
var checkTally = 0;
function monitorChecks() {
	var checkArr = [];
	//var inputArr = document.getElementsByTagName('input');
	var inputArr = $("#newsletterForm input, #manageNewsletters input");
	for(var i=0; i<inputArr.length; i++) {
		if(inputArr[i].type == 'checkbox' && !inputArr[i].className.match('noTally')) checkArr.push(inputArr[i]);
	}
	for(var m=0; m<checkArr.length; m++) {
		if(checkArr[m].checked) checkTally++;
		checkArr[m].onclick = function() {
			if(this.checked) {
				if(checkTally + 1 > 10) {
					this.checked = '';
					if(!this.parentNode.className.match('error')) {
						this.parentNode.className = this.parentNode.className + ' error';
						createError(this.parentNode);
					}
				} else {
					checkTally++;
				}
			} else if(checkTally != 0) {
				checkTally--;
				var errors = $('.errorDiv');
				if(errors.length > 0) removeError(errors);
			}
		}
	}
}
function createError(cont) {
	var errorDiv = document.createElement('div');
	errorDiv.className = 'errorDiv';
	var par = document.createElement('p');
	par.innerHTML = 'You can only subscribe to 10 newsletters at the same time. Please uncheck another newsletter if you want to receive this one.'
	errorDiv.appendChild(par);
	cont.appendChild(errorDiv);
}
function removeError(errors) {
	for(var n=0; n<errors.length; n++) {
		errors[n].parentNode.className = errors[n].parentNode.className.replace('error', '');
		errors[n].parentNode.removeChild(errors[n]);
	}
}
function footerCalc() {
	var total = 0;
	$('#footerColumns div').each(function(i) {
		total = total + $(this).width() + parseInt($(this).css('padding-right')) + parseInt($(this).css('padding-left'));
	});
	$('#footerColumns').width(total + 'px');
}

