/* 1/20/07 CMW
these scripts control some aspects of the UI for the SCC site

showHide = shows and hides a collapsible section, e.g. the signup section of the calendar
showHelp = makes a floating div to display popup information

*/


//expand/collapse a page element based on its current state
function showHide(theEl){
	theEl = document.getElementById(theEl);
	theEl.style.display = theEl.style.display==''?'none':'';
	
}

//add information for a volunteer set
function addVolunteer(containerId) {
	theEl = document.getElementById(containerId);
	

	//first go through all of the table rows of the container and figure out the highest ID
	//we'll add 1 to that and use that as the basis for naming the new row ID and field names
	siblingRows = theEl.childNodes;
	
	rowId = 0;
	
	for(i=0;i < siblingRows.length; i++) {
		//parse the id of the row
		parsedId = siblingRows[i].id.split("_");
		//the prepended + is type coercion to convert the string to a number
		parsedId = +parsedId[1];
		//if it's equal or higher than rowId, set rowId to that plus 1
		rowId = parsedId >= rowId? parsedId+1 : rowId
	}
	
	/* the below works, but not in IE6. I will not go into my feelings on this. Let's do it via the DOM
	theHTML = "<tr id = 'volunteerRow_"+rowId+"'><td><small><b>Number</b> <input type = 'text' name = 'number_"+rowId+"' size = '3'/></small></td>";
	theHTML+= "<td><small><b>Type</b> <input type = 'text' name = 'type_"+rowId+"' size = '25'/></small></td>";
	theHTML+= "<td><input type = 'button' class = 'button' value = 'Remove Set' onClick = 'removeVolunteer(\"volunteerRow_"+rowId+"\");'/></td></tr>";
	
	//now add to the container
	theEl.innerHTML+=theHTML;
	
	*/
	
	theRow = document.createElement("tr");
	theRow.setAttribute("id","volunteerRow_"+rowId);
	
	oneCell = document.createElement("td");
	oneCell.innerHTML = "<small><b>Number</b> <input type = 'text' name = 'volunteer_number_"+rowId+"' size = '3'/></small>";
	twoCell = document.createElement("td");
	twoCell.innerHTML = "<small><b>Type</b> <input type = 'text' name = 'volunteer_type_"+rowId+"' size = '25'/></small>";
	threeCell = document.createElement("td");
	threeCell.innerHTML = "<input type = 'button' class = 'button' value = 'Remove Set' onClick = 'removeVolunteer(\"volunteerRow_"+rowId+"\");'/>";
	
	theEl.appendChild(theRow);
	theRow.appendChild(oneCell);
	theRow.appendChild(twoCell);
	theRow.appendChild(threeCell);
	
	return true;
	
	
}

//remove information for a volunteer set
function removeVolunteer(volunteerId) {
	targetEl = document.getElementById(volunteerId);
	targetEl.parentNode.removeChild(targetEl);
	
	return true;
}

//quick client side validation for volunteers
function checkVolunteerForm(theForm) {
	
	//first check that we are indeed using volunteers
	if(theForm.enableVolunteers.checked) {
		//loop through the form elements and check that all volunteer numbers are numbers and all volunteer types have text
		for (i=0; i < theForm.elements.length; i++) {
			
			//numbers
			if(theForm.elements[i].name.indexOf("volunteer_number_") == 0) {
				//the regex here uses anchors to match the whole string, and it must be a positive integer
				if(!theForm.elements[i].value.match(/^\d+$/) || +theForm.elements[i].value <= 0){
					alert("Please ensure that all volunteer sets have a number of volunteers. If you wish to delete the volunteer set, do so by clicking the 'Remove Set' button next to the associated row");
					return false;
				}
			}
			
			//types
			if(theForm.elements[i].name.indexOf("volunteer_type_") == 0) {
				//just make sure there's something there
				if(theForm.elements[i].value.length <= 0) {
					alert("Please ensure that all volunteer types are filled in. If you wish to delete the volunteer set, do so by clicking the 'Remove Set' button next to the associated row");
					return false;
				}
			}
			
		}
	}
	
	return true;

}