// JavaScript Document
function updateDOBDays(theForm) {
  var monthDays = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
  var daysThisMonth = Number(monthDays[theForm.dobmonth.options[theForm.dobmonth.options.selectedIndex].value]);
  theForm.dobday.options.length = 0;
  for (var i=1; i<=daysThisMonth; i++) {theForm.dobday[i-1] = new Option(i,i);}
  theForm.dobday.options[0].selected = true;
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}		

function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){expires = expires * 1000 * 60 * 60 * 24;}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function oldEnough(){
	/* the minumum age you want to allow in */
	var min_age = 13;

	/* change "age_form" to whatever your form has for a name="..." */
	var year = parseInt(document.forms["tcRegForm"]["dobyear"].value);
	var month = parseInt(document.forms["tcRegForm"]["dobmonth"].value) - 1;
	var day = parseInt(document.forms["tcRegForm"]["dobday"].value);

	var theirDate = new Date((year + min_age), month, day);
	var today = new Date;

	if ( (today.getTime() - theirDate.getTime()) < 0) {
		Set_Cookie( "tcTooYoung", "y", "1/1/2010", "/");
		window.location = "tooYoung.html";
		return false;
	}
	else {
		return true;
	}
}

function validateName(fld) {
    var error = "";
    var legalChars = /^([1-zA-Z0-1@.\s]{1,255})$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
 
    if (fld.value == "") {
        error = "You didn't enter a username.\n";
    } else if (!legalChars.test(fld.value) || illegalChars.test(fld.value) ) {
        error = "The username entered contains not-allowed characters.\n";
    }
    return error;
}

function validateCode(fld) {
    var error = "";
    var legalChars = /^([1-zA-Z0-1@.\s]{1,255})$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
 
    if (fld.value == "") {
        error = "You didn't enter the security code.\n";
    } else if (!legalChars.test(fld.value) || illegalChars.test(fld.value) ) {
        error = "The security code entered contains not-allowed characters.\n";
    }
    return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);
    var emailFilter = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {
        error = "You entered an invalid email address.\n";
    }
    return error;
}

function validateFormOnSubmit(theForm) {
	var reason = "";
	  reason += validateName(theForm.name);
	  reason += validateEmail(theForm.email);
	  reason += validateCode(theForm.sec);
	 
	  if (reason != "") {
		alert("Your form could not be submitted because:\n" + reason);
		return false;
	  } 
	  
	  return oldEnough();
}

function validateRemoveFormOnSubmit(theForm) {
	var reason = "";
	  reason += validateEmail(theForm.email);
	  reason += validateCode(theForm.sec);
	 
	  if (reason != "") {
		alert("Your form could not be submitted because:\n" + reason);
		return false;
	  } 
	  
	  return true;
}

function validateContactUs(theForm) {
	var reason = "";
	  reason += validateEmail(theForm.email);
	  reason += validateCode(theForm.sec);
	  if (reason != "") {
		alert("Your form could not be submitted because:\n" + reason);
		return false;
	  } 
	  
	  return true;
}
