/* check the form for missing or invalid input
-------------------------------------------------------------------------*/
function checkForm(f, errorMsgName){
	allIsGood = true;
	errorStr = 'Please correct: ';

	// check for training program
	if(f.training_program.value.length < 1 || f.training_program.selectedIndex == 0){
		if ('\v' == 'v') {
			highlightBg(f, 'training_program', true);
		} else {
			highlight(f, 'training_program', true);
		}
		allIsGood = false;
		errorStr += 'course, ';}
	else {
		if ('\v' == 'v') {
			highlightBg(f, 'training_program', false);
		} else {
			highlight(f, 'training_program', false);
		}
	}

	// check for first name
	if(f.first_name.value.length < 1){
		highlight(f, 'first_name', true);
		allIsGood = false;
		errorStr += 'first name, ';}
	else{
		highlight(f, 'first_name', false);}

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

	if (f.sendmethod_mail[f.sendmethod_mail.selectedIndex].value == 'Email'){
		// check for email
		if(!isEmail(f.email.value)){
			highlight(f, 'email', true);
			allIsGood = false;
			errorStr += 'email address, ';}
		else{
			highlight(f, 'email', false);}
		highlight(f, 'street_address', false);
		highlight(f, 'city', false);
		if ('\v' == 'v') {
			highlightBg(f, 'state', false);
		} else {
			highlight(f, 'state', false);
		}
	} else if (f.sendmethod_mail[f.sendmethod_mail.selectedIndex].value == 'Mail'){
		// check for email
		if(!isAddress(f.street_address.value)){
			highlight(f, 'street_address', true);
			allIsGood = false;
			errorStr += 'street address, ';}
		else{
			highlight(f, 'street_address', false);}

		if(!isCity(f.city.value)){
			highlight(f, 'city', true);
			allIsGood = false;
			errorStr += 'city, ';}
		else{
			highlight(f, 'city', false);}
			
		if(f.state.selectedIndex == 0){
			if ('\v' == 'v') {
				highlightBg(f, 'state', true);
			} else {
				highlight(f, 'state', true);
			}
			allIsGood = false;
			errorStr += 'state, ';}
		else {
			if ('\v' == 'v') {
				highlightBg(f, 'state', false);
			} else {
				highlight(f, 'state', false);
			}
		}
		highlight(f, 'email', false);
	}
	
	// check for zip code
	if(!isZip(f.zip.value)){
		highlight(f, 'zip', true);
		errorStr += 'zip code, ';
		allIsGood = false;}
	else{
		highlight(f, 'zip', false);}

	if(!isPhoneNumber(f.phone_number.value)){
		highlight(f, 'phone_number', true);
		errorStr += 'phone, ';
		allIsGood = false;}
	else{
		highlight(f, 'phone_number', false);}
					
	if(allIsGood){	
		return true;
	}else{
		errorStr = errorStr.replace(/, $/, '.');
		writeError(f, errorStr, errorMsgName);
		return false;
	}
}

/* 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 isCity(city){
	city = stripSpaces(city);
	regCity = /^.+$/;
	is = regCity.test(city);
	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){
		return true;
	}else{
		return false;}
}

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

function writeError(myForm, str, errorMsgName){
	document.getElementById(errorMsgName).innerHTML = str;
	document.getElementById(errorMsgName).style.display = 'block';
	document.getElementById(errorMsgName).style.border = '1px solid red';
}

function highlight(myForm, inputName, show){
	if(show){
		eval("document.getElementById('"+myForm.id+"')."+inputName+".style.border = 'Solid 1px Red'");
	}else{
		eval("document.getElementById('"+myForm.id+"')."+inputName+".style.border = 'Solid 1px #666666'");
	}
}
function highlightBg(myForm, inputName, show){
	if(show){
		eval("document.getElementById('"+myForm.id+"')."+inputName+".style.backgroundColor = '#ff9999'");
	}else{
		eval("document.getElementById('"+myForm.id+"')."+inputName+".style.backgroundColor = '#ffffff'");
	}
}

/* shows the extra text boxes for "How did you hear about Career Step?"
-------------------------------------------------------------------------*/
function showExtra(theCombo) 
{
	//show or hide #referral div
	/*---------------------------------------------------*/
	var myText = document.getElementById("referral");
	//references to the dropdown options
	if (theCombo.options[theCombo.selectedIndex].value == "Industry Organization" ||
	theCombo.options[theCombo.selectedIndex].value == "CS Student or Graduate" ||
	theCombo.options[theCombo.selectedIndex].value == "Personal Referral" ||
	theCombo.options[theCombo.selectedIndex].value == "Professional Referral" ||
	theCombo.options[theCombo.selectedIndex].value == "Health Care Facility" ||
	theCombo.options[theCombo.selectedIndex].value == "Employer") {
		myText.style.display = "";
		if (theCombo.selectedIndex == 10) { //If this is a company website:
			document.getElementById('rFirstName').style.display = 'none';
			document.getElementById('rLastName').style.display = 'none';
			document.getElementById('rEmail').style.display = 'none';
			document.getElementById('rPhone').style.display = 'none';
			document.getElementById('rAddress').style.display = 'none';
			document.getElementById('rCity').style.display = 'none';
			document.getElementById('referralUSAstateInput').style.display = 'none';
			document.getElementById('rZip').style.display = 'none';
		} else {
			document.getElementById('rFirstName').style.display = 'block';
			document.getElementById('rLastName').style.display = 'block';
			document.getElementById('rEmail').style.display = 'block';
			document.getElementById('rPhone').style.display = 'block';
			document.getElementById('rAddress').style.display = 'block';
			document.getElementById('rCity').style.display = 'block';
			document.getElementById('referralUSAstateInput').style.display = 'block';
			document.getElementById('rZip').style.display = 'block';
		}
		if (theCombo.selectedIndex == 13) { //If this is a personal referral:
			document.getElementById('rOrganization').style.display = 'none';
			//document.getElementById('rId').style.display = 'none';
		} else {
			document.getElementById('rOrganization').getElementsByTagName('input')[0].value = '';
			document.getElementById('rOrganization').style.display = 'block';
			//document.getElementById('rId').style.display = 'block';
		}
		if (theCombo.options[theCombo.selectedIndex].value == 'Industry Organization') { //If this is a company website:
			if (theCombo.selectedIndex == 16) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'AHDI';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 17) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'AAPC';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 18) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'AHIMA';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 19) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'MTIA';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 20) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'Operation Life Transformed';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 21) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'CWJC';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 22) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'Women For Hire';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 23) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'Homeschool';
				document.getElementById('rOrganization').style.display = 'none';
			} else if (theCombo.selectedIndex == 24) {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'Uninversiy of Phoenix';
				document.getElementById('rOrganization').style.display = 'none';
			} else {
				document.getElementById('rOrganization').getElementsByTagName('input')[0].value = 'Not Specified';
				document.getElementById('rOrganization').style.display = 'block';
			}
		} else {
			
		}
	} else {
		myText.value="";
		myText.style.display = "none";
	}
	//show or hide #advertising div
	var myText = document.getElementById("advertising");
	//references to the dropdown options
	if (theCombo.options[theCombo.selectedIndex].value == "TV Ad" ||
	theCombo.options[theCombo.selectedIndex].value == "Infomercial" ||
	theCombo.options[theCombo.selectedIndex].value == "Radio Ad" ||
	theCombo.options[theCombo.selectedIndex].value == "Print Ad" ||
	theCombo.options[theCombo.selectedIndex].value == "PR") {
		myText.style.display = "";
	} else {
		myText.value="";
		myText.style.display = "none";
	}
	
	//show or hide #Tradeshow div
	var myText = document.getElementById("tradeshow");
	//references to the dropdown options
	if (theCombo.options[theCombo.selectedIndex].value == "Convention" ||
	theCombo.options[theCombo.selectedIndex].value == "Seminar") {
		myText.style.display = "";
	} else {
		myText.value="";
		myText.style.display = "none";
	}
	
	//show or hide #website div
	var myText = document.getElementById("website");
	//references to the dropdown options
	if (theCombo.options[theCombo.selectedIndex].value == "Social Network" ||
	theCombo.selectedIndex == 10 ||
	theCombo.options[theCombo.selectedIndex].value == "Organic Search Engine") {
		myText.style.display = "";
	}
	else {
		myText.value="";
		myText.style.display = "none";
	}
}

