/* check the form for missing or invalid input
-------------------------------------------------------------------------*/
function checkForm(f){
	allIsGood = true;
	errorStr = '<div>Please make the corrections listed below:</div>';
	// check for course selection
	if (document.getElementById('trainingProgram') != undefined) {
		if (f.training_program.selectedIndex == 0) {
			highlight('trainingProgram', true);
			allIsGood = false;
			errorStr += '<li>Select your training program.</li>';
		} else {
			highlight('trainingProgram', false);
		}
	}

	// 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 sending method
//	if (f.sendmethod_mail.value!='Mail' && f.sendmethod_mail.value!='Email'){
	if (f.sendmethod_mail[0].checked == false && f.sendmethod_mail[1].checked == false){
		highlight('sendingOptions', true);
		errorStr += '<li>Let us know if you\'d like to receive the packet by mail or email.</li>';
		allIsGood = false;}
	else{
		highlight('sendingOptions', false);}
	
	if (f.sendmethod_mail[0].checked==true){
		// 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.state.selectedIndex == 0){
			highlight('state', true);
			allIsGood = false;
			errorStr += '<li>Select your state.</li>';}
		else{
			highlight('state', false);}
	}
	
//	if (f.sendmethod_mail[1].checked == true){
		// 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){
			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 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);}
					
	if(allIsGood){	
		return true;
	}else{
		writeError(errorStr);
		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 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){
	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);
	document.getElementById('errorDiv').style.display = 'block';
}

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

/* 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";
	}
}
