// JavaScript Document for validating any page that lets a user/admin change company info
// Required fields are marked by *
// validation is in the same order as the fields on the form
// form must have id="companyInfoForm".



function addValidateCompanyInfo()
{
	//adds validation to form #companyInfoForm
	if (document.getElementById("companyInfoForm"))
	{
		var form = document.getElementById("companyInfoForm");
		form.onsubmit=function(){return(validateCompanyInfo());};
	
		// sameAddress checkbox fills in physical address fields with mailing address fields if filled in.
		var applyMailingAddress = document.getElementById("applyMailingAddress");
		applyMailingAddress.onclick = function()
		{
			removeErrorMessages(applyMailingAddress);
			removeErrorMessages(applyMailingAddress);

			if ((form.Address.value !== "") && (form.City.value !== ""))
			{
				if (applyMailingAddress.checked)
				{
					form.PhysicalAddress.value = form.Address.value;
					//form.PhysicalAddress.disabled = true;
					
					form.PhysicalCity.value = form.City.value;
					//form.PhysicalCity.disabled = true;
										
					removeErrorMessages(form.physicalAddress);
					removeErrorMessages(form.physicalCity);
				}
				else
				{
					form.PhysicalAddress.value = "";
					//form.PhysicalAddress.disabled = false;
					
					form.PhysicalCity.value = "";
					//form.PhysicalCity.disabled = false;
				}
			}
			else
			{
				addErrorMessage(form.applyMailingAddress,"Please fill in the mailing address fields first.");
				applyMailingAddress.checked = false;
			}
		}
	}
}










function validateCompanyInfo()
{
	//Must run removeErrorMessages() a couple times to get rid off all the messages for some reason...
	removeErrorMessages();
	removeErrorMessages();
	removeErrorMessages();
	removeErrorMessages();

	var form = document.getElementById("companyInfoForm");
	var isError = false
	
	// Basic Company Info Set
	// ****************************************************
	if (form.CompanyName.value === "")
	{
		addErrorMessage(form.CompanyName,"Add the company's name.");
		isError = true;
	}
	if (form.ContactName.value === "")
	{
		addErrorMessage(form.ContactName,"Add the name of the main contact in charge of this listing.");
		isError = true;
	}
	if (form.ContactTitle.value === "")
	{
		addErrorMessage(form.ContactTitle,"Add the title of the person listed above.");
		isError = true;
	}
	
	// Passwords
	if ((form.Password1) && (form.Password1.value.length < 5))
	{
		addErrorMessage(form.Password1,"Your password can not be shorter than 5 characters.");
		isError = true;
	}
	if ((form.Password2) && (form.Password2.value.length === ""))
	{
		addErrorMessage(form.Password1,"Please re-enter your password.");
		isError = true;
	}
	if ((form.Password2) && (form.Password1.value != form.Password2.value)) //compare passwords
	{
		addErrorMessage(form.Password1,"Your passwords do not match. Please try again.");
		form.Password1.value = "";
		form.Password2.value = "";
		isError = true;
	}

	// Phone and fax
	if ((form.PhoneNumber) && (form.PhoneNumber.value.length === ""))
	{
		addErrorMessage(form.PhoneNumber,"Enter a phone number that your company can be contacted with.");
		isError = true;
	}
	if ((form.PhoneNumber) && (!checkInternationalPhone(form.PhoneNumber.value)))
	{
		addErrorMessage(form.PhoneNumber,"This phone number is not a valid format.");
		isError = true;
	}
	if ((form.FaxNumber) && ((form.FaxNumber.value !== "") && (!checkInternationalPhone(form.FaxNumber.value))))
	{
		addErrorMessage(form.FaxNumber,"This fax number is not a valid format.");
		isError = true;
	}

	// email
	if (form.EmailAddress)
	{
		if(form.EmailAddress.value === "")
		{
			addErrorMessage(form.EmailAddress,"Please add an email address.");
			isError = true;
		}
	
		if (form.EmailAddress.value && ((form.EmailAddress.value === "") || (!isEmail(form.EmailAddress.value))))
		{
			addErrorMessage(form.EmailAddress,"This email is not in the proper format (name@provider.com)");
			isError = true;
		}
	}
	if (form.EmailAddress2.value && ((form.EmailAddress2.value === "") || (!isEmail(form.EmailAddress2.value))))
	{
		addErrorMessage(form.EmailAddress2,"This email is not in the proper format (name@provider.com)");
		isError = true;
	}
	if(form.EmailAddressNotRequired){
		if (form.EmailAddressNotRequired.value && ((form.EmailAddressNotRequired.value === "") || (!isEmail(form.EmailAddressNotRequired.value))))
		{
			addErrorMessage(form.EmailAddressNotRequired,"This email is not in the proper format (name@provider.com)");
			isError = true;
		}
	}



	if ((form.HomePageURL) && ((form.HomePageURL.value !== "") && (form.HomePageURL.value.indexOf("http://") == 0))) // remove http:// from all urls
	{
		form.HomePageURL.value = form.HomePageURL.value.replace("http://", "");
	}
	if ((form.HomePageURL) && ((form.HomePageURL.value !== "") && (form.HomePageURL.value.indexOf(".") == -1))) // check for "."
	{
		addErrorMessage(form.HomePageURL,"Your Home Page URL is not in the proper format (e.g. www.yoursite.com)");
		ieError = true;
	}
	
	
	if ((form.Category) && (form.Category.value === "Make a Selection"))
	{
		addErrorMessage(form.Category,"Please select a category for your company from the drop down menu.");
		isError = true;
	}
	if ((form.CompanySize) && (form.CompanySize.value === "Make a Selection"))
	{
		addErrorMessage(form.CompanySize,"Please select the number of people in you company located in the state.");
		isError = true;
	}
	if ((form.Market) && (form.Market.value === "Make a Selection"))
	{
		addErrorMessage(form.Market,"Please select your company's market from the drop down menu.");
		isError = true;
	}
	
	// Mailing Address Set
	// ****************************************************
	if ((form.Address) && (form.Address.value === ""))
	{
		addErrorMessage(form.Address,"Enter your mailing address.");
		isError = true;
	}
	if ((form.City) && (form.City.value === ""))
	{
		addErrorMessage(form.City,"Enter your city.");
		isError = true;
	}
	if (form.State) // Change to Upper Case
	{
		form.State.value = form.State.value.toUpperCase();
	}
	if ((form.State) && ((form.State.value === "") || !(form.State.value.length === 2)))
	{
		addErrorMessage(form.State,"Enter your state abbreviation.");
		isError = true;
	}
	if ((form.PostalCode) && (form.PostalCode.value === "")) 
	{
		addErrorMessage(form.PostalCode,"Enter your 5 digit ZIP Code");
		isError = true;
	}
	if ((form.PostalCode) && ((form.PostalCode.value !== "") && (form.PostalCode.value.length !== 5)))
	{
		addErrorMessage(form.PostalCode,"This ZIP Code is not in the proper format. \nPlease enter your 5 digit ZIP Code");
		isError = true;
	}
	else if (form.PostalCode) // use isNumber() to check if all characters are numeric
	{
		for (var j = 0; j < form.PostalCode.value.length; j++) 
		{
			if (! isNumber(form.PostalCode.value.charAt(j)))
			{
				addErrorMessage(form.PostalCode,"This ZIP Code is not in the proper format. \nPlease use only numbers.");
				isError = true;
			}
		}
	}
	if ((form.County) && (form.County.value === "Make a Selection"))
	{
		addErrorMessage(form.County,"Select a county");
		isError = true;
	}
	

	// Product Information Set
	// ****************************************************
	if ((form.ProductLine1) && (form.ProductLine1.value === ""))
	{
		addErrorMessage(form.ProductLine1,"Please enter your primary product(s)");
		isError = true;
	}
	if ((form.SICcode) && ((form.SICcode.value === "") || (form.SICcode.value.length < 4)))
	{
		addErrorMessage(form.SICcode,"Enter a SIC code");
		isError = true;
	}
	if ((form.NAICScode) && ((form.NAICScode.value === "") || (form.NAICScode.value.length < 5)))
	{
		addErrorMessage(form.NAICScode,"Enter a NAICS code");
		isError = true;
	}
	if ((form.Status) && (form.Status.value == "Make a Selection"))//*Status
	{
		addErrorMessage(form.Status,"Select the company's status from the drop down");
		isError = true;
	}
	
	
	
	
	
	
	
	
	
	
	//a bit of clean up
	//add any fields here to be cleaned up of weird punctuation or white space. Run through cleanOutJunk()
	form.KeyWords.value = cleanOutJunk(form.KeyWords.value);
	form.ProductLine1.value = cleanOutJunk(form.ProductLine1.value);
	form.ProductLine2.value = cleanOutJunk(form.ProductLine2.value);
	if (form.Notes){form.Notes.value = cleanOutJunk(form.Notes.value);}
	
	//end clean up

	//End checking for filled in fields
	//****************************************************************************************************

	// if there are any errors, then don't send the info...
	if (isError === true) 
	{
		document.getElementById("errorMessageSection").innerHTML = '<p class="message-box error">Some info is missing or incorrect. Please <a href="#companyInfoForm">go back through the form</a> and fix all the fields with red text below them.</p>';
		return false;
	}
	
	// form is valid! Send info on to the VBS!
	form.validated.value = "true";
	return true;
}








//Start of Generic Stuff *******************
//******************************************

//allows changes to company info to be rejected by an administrator.
function rejectForm()
{
	document.getElementById("validated").value = "reject";
	return true;
}

addLoadEvent(addValidateCompanyInfo); // run addNewCompanyValidation() onLoad 