function isBlank(val) {
	if(val==null){return true;}
	var b = /\S/;
	var match = b.test(val);
	return !match;
}

function checkZip(zip) {
	if(zip==null || zip.length<5){return false;}
	var z = /[\d-]/;
	var match = z.test(zip);
	return match;
}

function checkemail(email) {
	/* This RegExp requires an address of the form xxx@xxx.xxx where xxx is one or more alphanumeric characters.  The last xxx can't have any numbers.  */
	var address= /[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}/;
	var match = address.test(email);
	return match;
}


function cardval() {
//alert("checking...");

// set error handler to default
   var ok = true;
// creat error message leader
   var errmsg="Sorry, this form could not be submitted for the following reasons: \n";
// create error list number
	var n="0"	


// vars for both forms
	var femail = document.forms.postcard.elements.email.value;
	var fcomments = document.forms.postcard.elements.comments.value;

// short for var
	var fname = document.forms.postcard.elements.name.value;

// long form vars	
	var ffirstname = document.forms.postcard.firstname.value;
	var flastname = document.forms.postcard.lastname.value;
	var fzip = document.forms.postcard.zip.value;
	var fgender = document.forms.postcard.gender.value;
	var fbmonth = document.forms.postcard.bmonth.value;
	var fbday = document.forms.postcard.bday.value;
	var fbyear = document.forms.postcard.byear.value;


// check the forms for required values and data types	
	//check email
	if (!checkemail(femail)) {
		ok = false;
		errmsg += " - You haven't entered a valid email address \n";
	}


	if (document.forms.postcard.signup.checked) { // long form checks
		if (isBlank(ffirstname)) {
			errmsg += " - You haven't told us your first name \n";
			ok = false;
		}
		if (isBlank(flastname)) {
			errmsg += " - You haven't told us your last name \n";
			ok = false;
		}
		if (!checkZip(fzip)) {
			errmsg += " - You haven't entered a valid zip code \n";
			ok = false;
		}
		if (isBlank(fgender)) {
			errmsg += " - You haven't selected a gender \n";
			ok = false;
		}
	} else { // short form checks
		if (isBlank(fname)) {
			errmsg += " - You haven't told us your name \n";
			ok = false;
		}
	}
	
	//check comments
	if (isBlank(fcomments)) {
		ok = false;
		errmsg += " - You haven't entered any comments\n";
	}
	
	// check final validation
	if (!ok) {
		alert(errmsg);
		//errmsg="Sorry, this form could not be submitted for the following reasons: \n";
		return false;
		
	} else {
		return true;
	}
} // end cardval()


function setdays(month){
	//alert('yes');
	if (month=="02") {
		l=29;
	} else if (month =="04" || month=="06" || month=="09" || month=="11" ) {
		l=31;
	} else {
		l=32;
	}
	
	document.postcard.bday.options.length = l;
	
	for (var i=1; i<=l ; i++){
			n = i.toString();
			if (n.length ==1) n="0" + n;
			
			document.postcard.bday.options[i].text = n;
			document.postcard.bday.options[i].value = n;
		}
}

function switchform(box) {
	//alert();
	var postcard = document.postcard;
	if (box.checked) { // box checked, we're expanding
		bigDisplay="block";
		simpleDisplay="none";
		
		
		
		// split name into first and last parts
		if(postcard.name.value) {
			postcard.name.value = postcard.name.value.replace(/\s+/gi," ");
			allnames = postcard.name.value.split(" ");
			postcard.firstname.value = allnames.shift();
			postcard.lastname.value = allnames.toString(" ");
		}
		
	} else { // box is unchecked, we are switching back
		bigDisplay="none";
		simpleDisplay="block";
		
		// if we have split names, recombine into one
		if((postcard.firstname.value.length) || (postcard.lastname.value.length)) {
			postcard.name.value = postcard.firstname.value + " " + postcard.lastname.value;
			postcard.name.value = postcard.name.value.replace(/\s+/gi," ");
		}
		
	} //endif
	
	bigps = getElementsByClass('bigform');
	for (i=0;i<bigps.length;i++) {
		bigp = bigps[i];
		bigp.style.display=bigDisplay;
	}
	
	simpleps = getElementsByClass('simpleform');
	for (i=0;i<simpleps.length;i++) {
		simplep = simpleps[i];
		simplep.style.display=simpleDisplay;
	}
	
} //end switchform()
