/* check the form for missing or invalid input
-------------------------------------------------------------------------*/
function checkForm(f){
	allIsGood = true;
	errorStr = '<div><br /><strong>Please make the corrections listed below:</strong></div>';
	
	// check for first name
	if(f.first_name.value.length < 1){
		highlight('first_name', true);
		allIsGood = false;
		errorStr += '<li>Fill out your First Name.</li>';}
	else{
		highlight('first_name', false);}

	// check for last name
	if(f.last_name.value.length < 1){
		highlight('last_name', true);
		allIsGood = false;	
		errorStr += '<li>Fill out your Last Name.</li>';}
	else{
		highlight('last_name', false);}

	// check for street address
	if(!isAddress(f.street_address.value)){
		highlight('street_address', true);
		errorStr += '<li>Fill out a valid address.</li>';
		allIsGood = false;
	}else{
		highlight('street_address', false);}

	// check for city
	if(f.city.value.length < 1){
		highlight('city', true);
		errorStr += '<li>Fill out the city.</li>';
		allIsGood = false;}
	else{
		highlight('city', false);}

	// check for state
	if(f.USAstateInput.selectedIndex == 0){
		highlight('USAstateInput', true);
		allIsGood = false;
		errorStr += '<li>Select your state.</li>';}
	else{
		highlight('USAstateInput', false);}

	// check for zip code
	if(!isZip(f.zip.value)){
		highlight('zip', true);
		errorStr += '<li>Fill out a valid zip code.</li>';
		allIsGood = false;}
	else{
		highlight('zip', false);}
				
	// check for email	
	if(!isEmail(f.email.value)){
		highlight('email', true);
		allIsGood = false;
		errorStr += '<li>Fill out a valid email Address.</li>';}
	else{
		highlight('email', false);}
		
	//check to see if confirm e-mail matches the e-mail
	if(f.email.value != f.emailconfirm.value || f.email.value.length < 1 && f.emailconfirm.value.length < 1){
		highlight('email', true);
		highlight('emailconfirm', true);
		allIsGood = false;
		errorStr += '<li>Your e-mail address and your confirmation e-mail address do not match.</li>';
	}else{
		highlight('email', false);
		highlight('emailconfirm', false);
	}
		
	// check for phone number
	if(!isPhoneNumber(f.phone_number.value)){
		highlight('phone_number', true);
		allIsGood = false;
		errorStr += '<li>Fill out a valid phone number. (Don\'t forget the area code)</li><br />';}
	else{
		highlight('phone_number', false);}
	
	if(allIsGood){
		f.submit();}
	else{
		writeError(errorStr);}
}

/* makes sure input is a valid format
-------------------------------------------------------------------------*/
function isEmail(email) {
	email = stripSpaces(email);
	var reg=/^[A-Za-z0-9]+([_\.-Ω][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	is = reg.test(email);
	return is;}

function isPhoneNumber(num){
	num = stripSpaces(num);
	var reg = /\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}/;
	is = reg.test(num);
	return is;}

function isZip(zip){
	zip = stripSpaces(zip);
	regZip = /(^[\d]{5}((\s|-)?[\d]{4})?$)|(^[\dA-Za-z]{3}(\s|-)?[\dA-Za-z]{3}$)/;
	is = regZip.test(zip);
	return is;}

function isAddress(address){
	regNum = /[\d]/;
	regLet = /[A-z]/;
	hasNum = regNum.test(address);
	hasLet = regLet.test(address);
	
	if(address.length >= 5){
		hasMoreThan5 = true;}
	
	if(hasNum && hasLet && hasMoreThan5){
		return true;
	}else{
		return false;}
}

function stripSpaces(str){
	newStr = '';
	for(a = 0; a < str.length; a++){
		if(str.charAt(a) != ' '){
			newStr += str.charAt(a);
		}
	}
	return newStr;
}

function writeError(str, show){
	document.getElementById('errorDiv').innerHTML = str;
	highlight('errorDiv', true);
}

function highlight(id, show){
	if(show){
		document.getElementById(id).style.border = 'Solid 1px Red';
	}else{
		document.getElementById(id).style.border = 'solid 1px #000;';
	}
}

/* pop-up windows
-------------------------------------------------------------------------*/
function popup(url, windowwidth, windowheight) {
	var y = parseInt((window.screen.availWidth - windowwidth)/2);
	var x = parseInt(((window.screen.availHeight - windowheight)/2) * .9);
	eval("detailswindow \= open\(url, \"details\", \"screenX="+x+", screenY="+y+", top="+x+", left="+y+", toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width="+windowwidth+", height="+windowheight+"\")");
}