//called on submission

function insertInfo()
{	
	var businessFlag=document.catalogRequest.businessFlag;
	var businessCkbox=document.catalogRequest.businessCkbox;
	var company=document.catalogRequest.company;
	var firstName=document.catalogRequest.firstName;
	var lastName=document.catalogRequest.lastName;
	var address1=document.catalogRequest.address1;
	var address2=document.catalogRequest.address2;
	var city=document.catalogRequest.city;
	var stateSelect=document.catalogRequest.stateSelect;
	var email=document.catalogRequest.email;
	var zip=document.catalogRequest.zip;
	var zip4=document.catalogRequest.zip4;
	var postal=document.catalogRequest.postal;
	var provinceSelect=document.catalogRequest.provinceSelect;
	var countrySelect=document.catalogRequest.countrySelect;
	var catalogBtn=document.catalogRequest.catalogBtn;
	
	//trim leading and trailing spaces
	firstName.value=firstName.value.replace(/^\s+|\s+$/g,"");
	lastName.value=lastName.value.replace(/^\s+|\s+$/g,"");
	address1.value=address1.value.replace(/^\s+|\s+$/g,"");
	address2.value=address2.value.replace(/^\s+|\s+$/g,"");
	city.value=city.value.replace(/^\s+|\s+$/g,"");
	email.value=email.value.replace(/^\s+|\s+$/g,"");

	//first name check
	if (firstName.value.length < 1)
	{
		document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
		firstName.focus();
		return false;
	}
	
	//last name check
	if (lastName.value.length < 1)
	{
		document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
		lastName.focus();
		return false;
	}
	
	businessFlag.value=businessCkbox.checked;
	
	//if business flag isn't checked, clear company field
	if (businessFlag.value=='false')
	{
		company.value=" ";
		company.disabled=false;
	}
	
	//else check for entry in company field
	else
	{
		var company=document.catalogRequest.company;
		company.value=company.value.replace(/^\s+|\s+$/g,"");
		if (company.value.length < 1)
		{
			document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
			company.focus();
			return false;
		}
	}
	
	//address check
	if (address1.value.length < 1)
	{
		document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
		address1.focus();
		return false;
	}

	//city check
	if (city.value.length < 1)
	{
		document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
		city.focus();
		return false;
	}

	//state check
	if (document.getElementById('stateSelectDiv').style.display!="none")
	{
		if (stateSelect.value == 'XX')
		{
			document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
			stateSelect.focus();
			return false;
		}
	}
	
	//province check
	if (document.getElementById('provinceDiv').style.display!="none")
	{
		if (provinceSelect.value == 'XX')
		{
			document.getElementById('errorText').innerHTML = '*Please fill out all required fields';
			provinceSelect.focus();
			return false;
		}
	}
	
	//postal check
	if (document.getElementById('postalInputDiv').style.display!="none")
	{
		if (postal.value.length!=6)
		{
			document.getElementById('errorText').innerHTML = '*Please provide a valid postal code.';
			postal.focus();
			return false;
		}
	}
	
	//zip check
	if (document.getElementById('zipInputDiv').style.display!="none")
	{	
		if (zip.value.length != 5 || isNaN(zip.value)==true)
		{
			document.getElementById('errorText').innerHTML = '*Please provide a valid zip code.';
			zip.focus();
			return false;
		}

		//zip4 check
		if ((zip4.value.length != 4 && zip4.value.length!=0) || isNaN(zip4.value)==true)
		{
			document.getElementById('errorText').innerHTML = '*Please provide a valid zip code.';
			zip4.focus();
			return false;
		}
	}

	//email check
	if (email.value.length > 0 && (email.value.indexOf("@") == -1 || email.value.indexOf(".") == -1))
	{
		document.getElementById('errorText').innerHTML = '*Please provide a valid email address.';
		email.focus();
		return false;
	}
	var updateFlag=document.catalogRequest.updateFlag;
	updateFlag.value="updated";
}

//enable or disable company fields based on checkbox
function businessChange()
{	
	var businessCkbox=document.catalogRequest.businessCkbox;
	var company=document.catalogRequest.company;

	if (businessCkbox.checked==true)
	{
		document.catalogRequest.company.disabled=false;
		document.getElementById('companyCell').innerHTML = 'Company*';
	}
	
	else
	{
		company.value='';
		document.catalogRequest.company.disabled=true;
		document.getElementById('companyCell').innerHTML = 'Company';
	}
}

function canadianChange()
{
	var countrySelect=document.catalogRequest.countrySelect;
	var stateSelect=document.catalogRequest.stateSelect;
	var provinceSelect=document.catalogRequest.provinceSelect;
	var zip=document.catalogRequest.zip;
	var zip4=document.catalogRequest.zip4;
	var postal=document.catalogRequest.postal;
	
	if (countrySelect.value=='US')
	{
		provinceSelect.value='XX';
		postal.value='';
		document.getElementById('zipDiv').innerHTML='ZIP Code*';
		document.getElementById('stateDiv').innerHTML='State*';
		
		document.getElementById('zipInputDiv').style.display='';
		document.getElementById('stateSelectDiv').style.display='';
		document.getElementById('provinceDiv').style.display='none';
		document.getElementById('postalInputDiv').style.display='none';
	}
		
	if (countrySelect.value=='CA')
	{
		stateSelect.value='XX';
		zip.value='';
		zip4.value='';
		document.getElementById('zipDiv').innerHTML='Postal Code*';
		document.getElementById('stateDiv').innerHTML='Province*';
		
		document.getElementById('zipInputDiv').style.display='none';
		document.getElementById('stateSelectDiv').style.display='none';
		document.getElementById('provinceDiv').style.display='';
		document.getElementById('postalInputDiv').style.display='';
	}
	document.getElementById('errorText').innerHTML = '';
}

function selectEnter(e)
{ 
	if(e.keyCode==13)
	{
		insertInfo();
	}
}

function disableBtn()
{
	var updateFlag=document.catalogRequest.updateFlag;
	if (updateFlag.value=="updated")
	{
		var catalogBtn=document.catalogRequest.catalogBtn;
		catalogBtn.disabled=true;
	}
}

function enableBtn()
{
		var catalogBtn=document.catalogRequest.catalogBtn;
		catalogBtn.disabled=false;
}

