<!--

window.onload = setup_page;

// set up javascript elements on the page
function setup_page() {
	// create the 'sms notification' global checkbox
	var smsCheckbox = document.createElement('input');
	smsCheckbox.setAttribute('type', 'checkbox');
	smsCheckbox.setAttribute('id', 'smsNotificationCheckbox');
	smsCheckbox.setAttribute('class', 'check');
	smsCheckbox.setAttribute('title', 'Enable or disable SMS notification for all eligible participants');
	
	// add a handler to it, to set or clear all SMS notification
	genericAddHandler(smsCheckbox, 'click', 
		function(e) {
			setSMSNotification(genericGetEventTarget(e).checked);
		});

	// create the label for the checkbox
	var smsLabel = document.createElement('label');
	smsLabel.appendChild(document.createTextNode('SMS'));

	// change the class of the header cell, so the width can be set to stop the label from wrapping onto a new line
	smsHeaderCell = document.getElementById('smsNotification');
	smsHeaderCell.setAttribute('class', 'smsNotification');
	
	// remove the existing text from the header cell, and add the checkbox and label
	smsHeaderCell.removeChild(document.getElementById('smsNotificationTag'));
	smsHeaderCell.appendChild(smsCheckbox);
	smsHeaderCell.appendChild(smsLabel);
	
	// now attach handlers to all the individual checkboxes
	var numberRows = document.form1.numrows.value;
	for (index = 1; index <= numberRows; index++) {
		genericAddHandler(document.getElementById("row" + index + "_sms"), 'click',
			function(e) {
				smsClicked(genericGetEventTarget(e).checked);
			});
	}

	// create the 'email notification' global checkbox		
	var emailCheckbox = document.createElement('input');
	emailCheckbox.setAttribute('type', 'checkbox');
	emailCheckbox.setAttribute('id', 'emailNotificationCheckbox');
	emailCheckbox.setAttribute('class', 'check');
	emailCheckbox.setAttribute('title', 'Enable or disable email notification for all eligible participants');

	// add a handler to it, to set or clear all email notification
	genericAddHandler(emailCheckbox, 'click', 
		function(e) {
			setEmailNotification(genericGetEventTarget(e).checked);
		});
		
	// create the label for the checkbox
	var emailLabel = document.createElement('label');
	emailLabel.appendChild(document.createTextNode('Email'));
	
	// change the class of the header cell, so the width can be set to stop the label from wrapping onto a new line
	emailHeaderCell = document.getElementById('emailNotification');
	emailHeaderCell.setAttribute('class', 'emailNotification');

	// remove the existing text from the header cell, and add the checkbox and label
	emailHeaderCell.removeChild(document.getElementById('emailNotificationTag'));
	emailHeaderCell.appendChild(emailCheckbox);
	emailHeaderCell.appendChild(emailLabel);
	
	// now attach handlers to all the individual checkboxes
	for (index = 1; index <= numberRows; index++) {
		genericAddHandler(document.getElementById("row" + index + "_emailNotification"), 'click',
			function(e) {
				emailClicked(genericGetEventTarget(e).checked);
			});
	}
	
	// add 'free characters in SMS message'
	genericAddHandler(document.getElementById('includeMessage'), 'keyup', 
		function(e) {
			updateSMSCounter(genericGetEventTarget(e));
		});
	updateSMSCounter(document.getElementById('includeMessage'));
}

// update the SMS counter display
function updateSMSCounter(messageInput) {
	//alert(document.getElementById('smsCharCounter').childNodes[0]);
	document.getElementById('smsCharCounter').innerHTML = "SMS notification space: " + (Math.max(35-messageInput.value.length, 0));
}

function smsClicked(status) {
	if (! status) {
		setSMSStatus(status);
	}
}

function emailClicked(status) {
	if (! status) {
		setEmailStatus(status);
	}
}

// set the status of the global SMS checkbox
function setSMSStatus(status) {
	document.getElementById('smsNotificationCheckbox').checked = status;
}

// set the status of the global email checkbox
function setEmailStatus(status) {
	document.getElementById('emailNotificationCheckbox').checked = status;
}

// enables or disables SMS notification for all participants
function setSMSNotification(enabled) {
	if (enabled) {
		// get phone number box for each row
		// if it contains a mobile number, set SMS notification for this participant
		var numberRows = document.form1.numrows.value;
		for (index = 1; index <= numberRows; index++) {
			var field = document.getElementById("row" + index + "_phone");
			if (is_mobile_number(field.value)) {
				document.getElementById("row" + index + "_sms").checked = true;
			}
		}
	}
	else {
		var numberRows = document.form1.numrows.value;
		for (index = 1; index <= numberRows; index++) {
			document.getElementById("row" + index + "_sms").checked = false;
		}
	}
}

// enables or disables email notification for all participants
function setEmailNotification(enabled) {
	if (enabled) {
		// get email address box for each row
		// if it contains an email address, set email notification for this participant
		var numberRows = document.form1.numrows.value;
		for (index = 1; index <= numberRows; index++) {
			var field = document.getElementById("row" + index + "_email");
			if (is_email_address(field.value)) {
				document.getElementById("row" + index + "_emailNotification").checked = true;
			}
		}
	}
	else {
		var numberRows = document.form1.numrows.value;
		for (index = 1; index <= numberRows; index++) {
			document.getElementById("row" + index + "_emailNotification").checked = false;
		}
	}
}

function do_form_update() {
	// LS had to rename attribute to 'op' because the BB browser ignored action.value
    document.form1.scriptedop.value='Update';
    document.form1.submit();
    return 1;
}

function remove_participant(index) {
	var numberRows = document.form1.numrows.value;
	
	if (index <= numberRows) {
		for (sourceIndex = index + 1; sourceIndex <= numberRows; sourceIndex++) {
			targetIndex = sourceIndex - 1;
			document.getElementById("row" + targetIndex + "_name").value = 
				document.getElementById("row" + sourceIndex + "_name").value;
			document.getElementById("row" + targetIndex + "_phone").value = 
				document.getElementById("row" + sourceIndex + "_phone").value;
			document.getElementById("row" + targetIndex + "_sms").checked =
				document.getElementById("row" + sourceIndex + "_sms").checked;
			document.getElementById("row" + targetIndex + "_email").value =
				document.getElementById("row" + sourceIndex + "_email").value;
			document.getElementById("row" + targetIndex + "_emailNotification").checked =
				document.getElementById("row" + sourceIndex + "_emailNotification").checked;
			document.getElementById("row" + targetIndex + "_originalPID").value =
				document.getElementById("row" + sourceIndex + "_originalPID").value;
		}
		
		document.getElementById("row" + numberRows + "_name").value="";
		document.getElementById("row" + numberRows + "_phone").value="";
		document.getElementById("row" + numberRows + "_sms").checked = false;
		document.getElementById("row" + numberRows + "_email").value="";
		document.getElementById("row" + numberRows + "_emailNotification").checked = false;
		document.getElementById("row" + numberRows + "_originalPID").value="";
	}
	return false;	
}

function notify_sms_disabled() {
	alert("The SMS service is disabled for Try-it-Now users. You must register and log in to use SMS notification.");
}

 //-->
