var responseCount = 0;

function o( strId ) {
	if(document.getElementById( strId )) {
		return document.getElementById( strId );
	} else return false;
}
function submit31CSNForm() {
	var strMsg = '';
	
	if( (o('br31csn_in2').value == '' ) || (o('br31csn_in1').value == '') ) {
		display31CsnValidationError('- Please complete all the required fields.');
	} else {
		// validate email
		if(!isValidEmail(o('br31csn_in2').value)) {
			strMsg += ('- The email address you entered is not a valid format. Please try again. (Emails should be in the proper format XX@XX.XX)'+"<br>");
		}

		// validate name
		if( !isValidName(o('br31csn_in1').value)) {
			strMsg += ('- Please enter a valid name'+"<br>");
		}

		if( strMsg == '') {
			// submit form, check for response
			o('br31csnForm').submit();

			br31CsnGetResponse();
		} else {
			display31CsnValidationError( strMsg );
		}
	}
}

function display31CsnValidationError( strMsg ) {
	o('br31csn_validerr').innerHTML = strMsg;
}

function br31CsnCheckResponseVar() {
	if(responseCount > 120) { // set one minute time out
		// display server failure
		display31CsnValidationError('The server is not responding. Please try again later.');
	}
	else {
		responseCount++;
		if(window.br31csnIframe.registerResponse != undefined) {
			// we have response
			if(window.br31csnIframe.registerResponse == '1') { // server responded with failure
				display31CsnValidationError('Your email cannot be sent at this time. Please try again later.');
			} else if(window.br31csnIframe.registerResponse == '0') {

				// display success
				br31CsnDisplaySuccess();
			}
			responseCount = 0;
			o('br31csnIframe').src = 'emails/blank.html';
		} else {	
			// no response yet, check again
			br31CsnGetResponse();
		}
	}
}
function br31CsnGetResponse() {
	setTimeout("br31CsnCheckResponseVar();",500); // check frame every 1/2 second
}

function br31CsnDisplaySuccess() {
	
	o('br31csn_formBox').className = 'br31csn_viaEmail_success';
	
	o('br31csn_formBox').innerHTML = '<p>A reminder for 31 Cent Scoop Night will be emailed to you on the day of the event.<br><br>Join the Birthday Club today for FREE ice cream on your birthday and more!  <a href="/BDayClub/RegisterInfo1.aspx?icid=bdc_000066">Join Now</a>.</p>';
}

/* xtra stuff */
 function isValidEmail(val){
 	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
 	if (!val.match(re)) {
 		return false;
 	} else {
 		return true;
 	}
 }
 
  function isValidName(val){
 	var re = /[^\d\w\s]/;
 	if (val.match(re)) {
 		return false;
 	} else {
 		return true;
 	}
 }
 
 String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

