// JavaScript Document

function valid_email()
{
	var apos=document.proposal.email.value.indexOf("@")
	var dotpos=document.proposal.email.value.lastIndexOf(".")
	if (apos<1||dotpos-apos<2) return false;
	return true;
}
function CheckFields()
{
	var alerts = new String();
	if(WithoutContent(document.proposal.firstname.value))
	{
		alerts += "\n\nPlease provide your first name";
	}
	if(WithoutContent(document.proposal.lastname.value))
	{
		alerts += "\n\nPlease provide your last name";
	}
	if(WithoutContent(document.proposal.email.value))
	{
		alerts += "\n\nPlease provide your e-mail address";
	}
	if(!WithoutContent(document.proposal.email.value))
	{
		if(!valid_email())
		{
			alerts += "\n\nPlease provide a valid e-mail address";
		}
	}
	if(WithoutContent(document.proposal.companyname.value))
	{
		alerts += "\n\nPlease provide a company name";
	}
	if(document.proposal.industrytype.value == "")
	{
		alerts += "\n\nPlease select an industry type";
	}
	if(!WithoutContent(document.proposal.zipcodefirstfive.value) || !WithoutContent(document.proposal.zipcodelastfour.value))
	{
		if(!valid_zip_first_five(document.proposal.zipcodefirstfive.value) || !valid_zip_last_four(document.proposal.zipcodelastfour.value))
		{
			alerts += "\n\nPlease enter a valid zip code if providing one";
		}
	}
	if(WithoutContent(document.proposal.phoneareacode.value))
	{
		alerts += "\n\nPlease provide a phone area code";
	}
	if(!WithoutContent(document.proposal.phoneareacode.value))
	{
		if(!valid_area_code(document.proposal.phoneareacode.value))
		{
			alerts += "\n\nPlease provide a 3-digit phone area code";
		}
	}
	if(WithoutContent(document.proposal.phoneexchange.value))
	{
		alerts += "\n\nPlease provide a phone exchange";
	}
	if(!WithoutContent(document.proposal.phoneexchange.value))
	{
		if(!valid_exchange(document.proposal.phoneexchange.value))
		{
			alerts += "\n\nPlease provide a 3-digit phone exchange";
		}
	}
	if(WithoutContent(document.proposal.phonelastfour.value))
	{
		alerts += "\n\nPlease provide the last four digits of the phone number";
	}
	if(!WithoutContent(document.proposal.phonelastfour.value))
	{
		if(!valid_last_four(document.proposal.phonelastfour.value))
		{
			alerts += "\n\nPlease provide a valid last four digits to your phone number";
		}
	}
	if(document.proposal.faxareacode.value.length > 0 || document.proposal.faxexchange.value.length > 0 || document.proposal.faxlastfour.value.length > 0)
	{
		if(document.proposal.faxareacode.value.length != 3 || document.proposal.faxexchange.value.length != 3 || document.proposal.faxlastfour.value.length != 4)
		{
			alerts += "\n\nPlease provide a valid fax number if providing one";
		}
	}
	if(document.proposal.contactme.value == "")
	{
		alerts += "\n\nPlease select a way for us to contact you";
	}
	if(document.proposal.arrivalmonth.value == "")
	{
		alerts += "\n\nPlease select an arrival month";
	}
	if(document.proposal.arrivalday.value == "")
	{
		alerts += "\n\nPlease select an arrival day";
	}
	if(document.proposal.arrivalyear.value == "")
	{
		alerts += "\n\nPlease select an arrival year";
	}
	if(document.proposal.arrivaltime.value == "")
	{
		alerts += "\n\nPlease enter your arrival time";
	}
	if(document.proposal.departuremonth.value == "")
	{
		alerts += "\n\nPlease select an departure month";
	}
	if(document.proposal.departureday.value == "")
	{
		alerts += "\n\nPlease select an departure day";
	}
	if(document.proposal.departureyear.value == "")
	{
		alerts += "\n\nPlease select an departure year";
	}
	if(document.proposal.departuretime.value == "")
	{
		alerts += "\n\nPlease enter your departure time";
	}
	if(!radio_button(document.proposal.flexible))
	{
		alerts += "\n\nPlease choose whether you can be flexible or not";
	}
	if(alerts.length > 2) 
	{
		alert('ERROR:' + alerts);
		return false;
	}
	capitalizeName()
	capitalizeCity()
	capitalizeReferenceName()
	return true;
}

function capitalizeCity()
{
	var message = document.proposal.city.value;
	if(message != "")
	{
		var format=message.toUpperCase();
		var str;
		for(i=0;i<message.length;i++)
		{
			if(i==0)
			{
				str = format.charAt(i);
			}
			else
			{
				str += format.charAt(i).toLowerCase();
			}
		}
		document.proposal.city.value = str;
	}
	else
	{
		document.proposal.city.value = "";
	}
}

function capitalizeReferenceName()
{
	capitalizereffname();
	capitalizereflname();
}

function capitalizereffname()
{
	var message = document.proposal.referencefirstname.value;
	if(message != "")
	{
		var format=message.toUpperCase();
		var str;
		
		for (i=0;i<message.length;i++)
		{
			if(i==0)
			{
				str = format.charAt(i);
			}
			else
			{
				str += format.charAt(i).toLowerCase();
			}
		}
		document.proposal.referencefirstname.value = str;
	}
	else
	{
		document.proposal.referencefirstname.value = "";
	}
}

function capitalizereflname()
{
	var message = document.proposal.referencelastname.value;
	if(message != "")
	{
		var format=message.toUpperCase();
		var str;
		
		for (i=0;i<message.length;i++)
		{
			if(i==0)
			{
				str = format.charAt(i);
			}
			else
			{
				str += format.charAt(i).toLowerCase();
			}
		}
		document.proposal.referencelastname.value = str;
	}
	else
	{
		document.proposal.referencelastname.value = "";
	}
}

function capitalizeName()
{
	capitalizefName();
	capitalizelName();
}

function capitalizefName()
{
	var message = document.proposal.firstname.value;
	var format=message.toUpperCase();
	var str;
	
	for (i=0;i<message.length;i++)
	{
		if(i==0)
		{
			str = format.charAt(i);
		}
		else
		{
			str += format.charAt(i).toLowerCase();
		}
	}
	document.proposal.firstname.value = str;
}

function capitalizelName()
{
	var message = document.proposal.lastname.value;
	var format=message.toUpperCase();
	var str;
	
	for (i=0;i<message.length;i++)
	{
		if(i==0)
		{
			str = format.charAt(i);
		}
		else
		{
			str += format.charAt(i).toLowerCase();
		}
	}
	document.proposal.lastname.value = str;
}

function radio_button(ss)
{
	for(var a=0;a<ss.length;a++)
	{
		if(ss[a].checked) return true;
	}
	return false;
}

function valid_area_code(ss)
{
	if(ss.length != 3) return false;
	return true;
}

function valid_exchange(ss)
{
	if(ss.length != 3) return false;
	return true;
}

function valid_last_four(ss)
{
	if(ss.length != 4) return false;
	return true;
}

function valid_zip_first_five(ss)
{
	if(ss.length != 5) return false;
	return true;
}

function valid_zip_last_four(ss)
{
	if(ss.length != 4) return false;
	return true;
}

function WithoutContent(ss)
{
	if(ss.length > 0) return false;
	return true;
}

function WithoutSelectionValue(ss)
{
	if(ss[0].selected) return false;
	return true;
}

function WithCheck(ss)
{
	for(var a=1; a < ss.length; a++)
	{
		if(ss[a].checked) return true;
	}
	return false;
}

function autotab(original,destination){
	if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
	destination.focus();
}
