/*=============================================================================

			 	 TITLE:		Main Javascript Library
			  CLIENT:		University of Kentucky Cardiovascular Research Center
		  MODIFIED:		2006.08.28
		 AUTHOR(S): 	Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:		Prototype 1.5.0 ( www.conio.com )

=============================================================================*/
var UKCVRC = window.UKCVRC || {

	CVRDRegistration: {
		
		pubAuthorCount: 1,
		
		controller: "ajax.aspx",
		
		init: function() {
			if ( $("ddlInstitutionWrapper") != null ) {
				$("ddlInstitutionWrapper").appendChild( UKCVRC.CVRDRegistration.createInstitutionDDL("ddlInstitution") );
			}
			if ( $("serializedAuthors") != null ) {
				$("serializedAuthors").value = "";
			}
		},
		
		ddlInstitutionsChanged: function(ddl) {
			var inID = Number( $F(ddl) );
			if ( inID == -1 ) {
				$( ddl.id + "_textBoxWrapper").style.display = "block";
			} else if ( inID == 0 ) {
				$( ddl.id + "_textBoxWrapper").style.display = "none";
				$( ddl.id + "_textBox").value = "N/A";
			} else {
				$( ddl.id + "_textBox").value = ddl.options[ddl.selectedIndex].text;
				$( ddl.id + "_textBoxWrapper").style.display = "none";
			}			
		},
		
		showLunchOptions: function() {
			var opt = $("ddlLunchAttendance").selectedIndex;
			if ( opt == 2 ) {
				$("lunchOptions1").style.display = "inline";
			} else {
				$("lunchOptions1").style.display = "none";
			}
		},
		
		showDinnerOptions: function() {
			var opt = $("ddlDinnerAttendance").selectedIndex;
			if ( opt == 0 ) {
				$("dinnerOptions1").style.display = "none";
				$("dinnerOptions2").style.display = "none";
			} else if ( opt == 1 ) {
				$("dinnerOptions1").style.display = "inline";
				$("dinnerOptions2").style.display = "none";
			} else {
				$("dinnerOptions1").style.display = "inline";
				$("dinnerOptions2").style.display = "inline";
			}
		},
		
		togglePosterDetails: function(cb) {
			if ( cb.checked ) {
				$("posterDetails").style.display = "block";
			} else {
				$("posterDetails").style.display = "none";
			}
			UKCVRC.CVRDRegistration.addPublicationAuthor();
		},
		
		createInstitutionDDL: function(id) {
			var ddl = document.createElement("select");
			UKCVRC.CVRDRegistration.institutionList.each( function(li) {
				var opt = document.createElement("option");
				opt.value = li[0];
				Element.update( opt, li[1] );
				ddl.appendChild(opt);
			} );
			ddl.id = id;
			Event.observe( ddl, "change", function(e) { UKCVRC.CVRDRegistration.ddlInstitutionsChanged(this); }.bind(ddl), false );
			return ddl;
		},
		
		addPublicationAuthor: function() {
			var numRows = $$("#publicationAuthors tr").length;
			if ( numRows > 7 ) {
				alert("Sorry! You may only add up to seven other co-authors.");
				return false;
			}
			var row = document.createElement("tr");
			var cFName = document.createElement("td");
			var cLName = document.createElement("td");
			var cInst = document.createElement("td");
			var cDept = document.createElement("td");
			var cRem = document.createElement("td");
			var iFName = document.createElement("input");
			var iLName = document.createElement("input");
			var ddlInst = UKCVRC.CVRDRegistration.createInstitutionDDL( "ddlInstitution" + (numRows+1) );
			var tbw = document.createElement("span");
			var iInst = document.createElement("input");
			var iDept = document.createElement("input");
			var lRem = document.createElement("a");
			Element.update( lRem, "remove" );
			Event.observe( lRem, "click", function(e) {
				var aTag = Event.element(e);
				var row = Event.findElement( e, "tr" );
				$("publicationAuthors").removeChild(row);
			}, true );
			iFName.setAttribute( "type", "text" );
			iFName.setAttribute( "class", "TextBox" );
			iLName.setAttribute( "type", "text" );
			iLName.setAttribute( "class", "TextBox" );
			iInst.setAttribute( "type", "text" );
			iInst.setAttribute( "id", "ddlInstitution" + (numRows+1) + "_textBox" );
			iInst.setAttribute( "class", "TextBox" );
			tbw.setAttribute( "id", "ddlInstitution" + (numRows+1) + "_textBoxWrapper" );
			iDept.setAttribute( "type", "text" );
			iDept.setAttribute( "class", "TextBox" );
			cFName.appendChild(iFName);
			cLName.appendChild(iLName);
			tbw.appendChild(iInst);
			tbw.style.display = "none";
			cInst.appendChild(ddlInst);
			cInst.appendChild(tbw);
			cDept.appendChild(iDept);
			cRem.appendChild(lRem);
			row.appendChild(cFName);
			row.appendChild(cLName);
			row.appendChild(cInst);
			row.appendChild(cDept);
			row.appendChild(cRem);
			$("publicationAuthors").appendChild(row);
		},
	
		submitForm: function() {
			var regForm = $("registrationForm");
			// Preliminary Validation
			var isPresenting = Boolean( $F("cbPoster") );
			var errorText = "";
			var emailRE = new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
			if ( $F("txtFirstName").length < 2 || $F("txtLastName").length < 2 ) {
				errorText += "-You must provide your first and last names.\n";
			}
			if ( !emailRE.test( $F("txtEmail") ) ) {
				errorText += "-You must provide a valid email address.\n";
			}
			if ( isPresenting ) {
				var authors = "";
				var rows = $$("#publicationAuthors tr");
				for ( var r = 0; r < rows.length; r++ ) {
					if ( r != 0 ) {	 authors += "|"; }
					var cells = $A( rows[r].cells );
					for ( var c = 0; c < cells.length; c++ ) {
						var tb = cells[c].getElementsByTagName("input")[0];
						if ( tb != null ) {
							if ( c != 0 ) { authors += ","; }
							authors += tb.value;
						}
					}
				}
				$("serializedAuthors").value = authors;
				if ( $F("ddlSubmissionType") == -1 ) {
					errorText += "-Please specify the category under which you are submitting your poster.\n";
				}
				if ( $("abstractTitle").value.length < 10 ) {
					errorText += "-The title of your abstract must be at least ten characters in length.\n";
				}
				if ( $("abstractText").value.length < 10 ) {
					errorText += "-Your abstract must be at least ten characters in length.\n";
				}
			}
			
			if ( errorText.length != 0 ) {
				alert( "Please address the following issues and try again:\n\n" + errorText);
			} else {
				// Everything checks out; send the registration
				var pars = "?action=storeRegistration";
				if ( $("rbCMECreditYes").checked ) {
					pars += "&cmeCredit=true";
				} else {
					pars += "&cmeCredit=false";
				}
				if ( $("availableForGradingYes").checked ) {
					pars += "&availableForGrading=true";
				} else {
					pars += "&availableForGrading=false";
				}
				if ( isPresenting ) {
					pars += "&isPresenting=true";					
				} else {
					pars += "&isPresenting=false";
				}
				regForm.action += pars;
				regForm.submit();
			}
		},
		
		submitAbstractForm: function() {
			var errorText = "";
			var authors = ""
			var rows = $$("#publicationAuthors tr");
			for ( var r = 0; r < rows.length; r++ ) {
				if ( r != 0 ) {	 authors += "|"; }
				var cells = $A( rows[r].cells );
				for ( var c = 0; c < cells.length; c++ ) {
					var tb = cells[c].getElementsByTagName("input")[0];
					if ( tb != null ) {
						if ( c != 0 ) { authors += ","; }
						authors += tb.value;
					}
				}
			}
			$("serializedAuthors").value = authors;
			if ( $F("ddlSubmissionType") == -1 ) {
				errorText += "-Please specify the category under which you are submitting your poster.\n";
			}
				if ( $("abstractTitle").value.length < 10 ) {
					errorText += "-The title of your abstract must be at least ten characters in length.\n";
				}
				if ( $("abstractText").value.length < 10 ) {
					errorText += "-Your abstract must be at least ten characters in length.\n";
				}
			if ( errorText.length != 0 ) {
				alert( "Please address the following issues and try again:\n\n" + errorText);
			} else {
				$("registrationForm").submit();
			}
		}
		
	}

};

