jQuery.noConflict();

var is_submitting = false;

(function($) {
		  
	$(document).ready(function() {
		
		// append the form to the current doc
		var formhtml = $('<form>', {
			id: 'rsvpform',
			method: 'post',
			action: 'laderman-rsvp-email.asp',
			css: {
				width: '500px',
				height: '290px',
				maxheight: '290px',
				maxwidth: '500px',
				'z-index': '120000'
			}
		});
		
		var titleline = $('<div>');
		titleline.append('<h2 style="margin-top: 0;">RSVP</h2><p>Please Note: This event is for adults only</p>');
		
		var nameline = $('<div>', { css: { height: '40px' } });
		nameline.append('<span style="width: 210px; display: block; line-height: 30px; float: left; font-weight: bolder;">Full Name:</span><input type="text" name="txt_FullName" id="txt_FullName" value="" /><span style="color: red;"> *</span>');
		
		var relationshipline = $('<div>', { css: { height: '40px' } });
		relationshipline.append('<span style="width: 210px; display: block; line-height: 30px; float: left; font-weight: bolder;">Relationship to Leo Baeck:</span><select name="ddl_Relationship" id="ddl_Relationship" size="1"><option value="parent">Parent</option><option value="staff">Staff</option><option value="community-member">Community Member</option><option value="staff-parent">Staff/Parent</option></select><span style="color: red;"> *</span>');
		
		var peoplecountline = $('<div>', { css: { height: '40px' } });
		peoplecountline.append('<span style="width: 210px; display: block; line-height: 15px; float: left; font-weight: bolder;">People attending (including yourself):</span><input type="text" name="txt_People" id="txt_People" size="5" maxlength="2" value="" /><span style="color: red;"> *</span>');
		
		formhtml.append( titleline );
		formhtml.append( nameline );
		formhtml.append( relationshipline );
		formhtml.append( peoplecountline );
		formhtml.append( '<div style="color: red; line-height: 40px;">* Required Field</div>' );
		formhtml.append( '<div id="form-msg" style="color: red; margin-bottom: 10px;">&nbsp;</div>' );
		formhtml.append( '<input type="submit" value="Submit" style="padding: 4px;" />' );
		formhtml.append( '<style type="text/css"> #fancybox-wrap, #fancybox-overlay { z-index: 120000; } #fancybox-close { border: none; } #rsvpform { font-family: Geneva, Verdana, Arial; font-size: 13px; } </style>' );
		
		// Bind the onclick form popup to any link with the class 'rsvpform'		
		$('.rsvpform').fancybox({
			hideOnContentClick: false,
			padding: 20,
			overlayOpacity: 0.8,
			content: formhtml,
			onComplete: function() {
				// bind validators
				$('#rsvpform input, select').bind('change', validateForm );
				$('#rsvpform input, select').bind('keyup', validateForm );
				$("#rsvpform").bind("submit", function(event) {
					if (!is_submitting) {
						is_submitting = true;
						//alert('submitting');
						if (validateForm()) {
							$("#rsvpform").submit();
						} else {
							event.preventDefault();
						}
					}
				});	
			}
		});
	
	});
	
	function validateForm() {
		var result = true;
		
		if ( !IsNumeric($('#txt_People').val()) || $('#txt_People').val() <= 0 ) {
			result = false;
			$('#form-msg').html('Please provide a valid number of people.');
			$('#txt_People').css('background-color', '#f5dede');
		} else {
			$('#txt_People').css('background-color', '#d6e6d1');
		}	
		
		if ( !$('#txt_FullName').val() ) {
			result = false;
			$('#form-msg').html('Please provide your name.');
			$('#txt_FullName').css('background-color', '#f5dede');
		} else {
			$('#txt_FullName').css('background-color', '#d6e6d1');
		}
		
		if (result) {
			$('#form-msg').html('&nbsp;');
		}
		
		return result;
	}
	
	function IsNumeric(strString) {
		var strValidChars = "0123456789.-";
		var strChar;
		var blnResult = true;

		if (strString.length == 0) return false;

		//  test strString consists of valid characters listed above
		for (i = 0; i < strString.length && blnResult == true; i++) {
			strChar = strString.charAt(i);
			if (strValidChars.indexOf(strChar) == -1) {
				blnResult = false;
			}
		}
		return blnResult;
	}
	
})(jQuery);