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

	// check for name
	if(f.bookingName.value.length < 1){
		highlight('bookingName', true);
		allIsGood = false;	
		errorStr += '<li>Fill out your Name.</li>';}
	else{
		highlight('bookingName', false);}
		
	// check for organization
	if(f.bookingOrganization.value.length < 1){
		highlight('bookingOrganization', true);
		allIsGood = false;	
		errorStr += '<li>Fill out your Organization.</li>';}
	else{
		highlight('bookingOrganization', false);}
		
	// check for title
	if(f.bookingTitle.value.length < 1){
		highlight('bookingTitle', true);
		allIsGood = false;	
		errorStr += '<li>Fill out your Title.</li>';}
	else{
		highlight('bookingTitle', false);}

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

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

	// check for state
	if(f.bookingState.value.length < 1){
		highlight('bookingState', true);
		allIsGood = false;
		errorStr += '<li>Select your state.</li>';}
	else{
		highlight('bookingState', false);}

	// check for zip code
	if(!isZip(f.bookingZip.value)){
		highlight('bookingZip', true);
		errorStr += '<li>Fill out a valid zip code.</li>';
		allIsGood = false;}
	else{
		highlight('bookingZip', false);}
				
	
	// check for phone number
	if(!isPhoneNumber(f.bookingPhone.value)){
		highlight('bookingPhone', true);
		allIsGood = false;
		errorStr += '<li>Fill out a valid phone number. (Don\'t forget the area code)</li>';}
	else{
		highlight('bookingPhone', false);}
		
	// check for email	
	if(!isEmail(f.bookingEmail.value)){
		highlight('bookingEmail', true);
		allIsGood = false;
		errorStr += '<li>Fill out a valid email Address.</li>';}
	else{
		highlight('bookingEmail', false);}

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

		
	if(allIsGood){
	  document.getElementById('errorDiv').style.display='none';
		f.submit();
		scrollTop();}
	else{
	  document.getElementById('errorDiv').style.display='block';
		writeError(errorStr);
		scrollTop();}	
}

/* 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 #a20401'
		document.getElementById(id).style.backgroundColor = '#f1c2c2';
	}else{
		document.getElementById(id).style.border = 'solid 1px #17be0f';
		document.getElementById(id).style.backgroundColor = '#c3fbc0';
	}
}

/* scrolls to the top of the page */
function scrollTop() {
   	window.scroll(0,0); // horizontal and vertical scroll targets
}
