
function hasnumber(s)
    // sees if a string has a number in it
	{ret=0;
	j=0;
	i=s.indexOf(" "); 
	if (i>1) for (i; i<s.length; ++i) for (j=0; j<=9; ++j) if ((j==s.charAt(i)) && (s.charAt(i)!=" ")) ++ret;
	return ret;
	}

function reset_envelopes()
    // resets envelop area and form
	{document.getElementById("innerenv").innerHTML = " &nbsp; ";
	document.getElementById("outerenv").innerHTML = " &nbsp; ";
	document.getElementById("explain").innerHTML = " &nbsp; ";
	document.envelopes.reset();
	return false;
	}
function show_envelopes() 
    // takes input and calculates envelopes
	{gtype=3;						// 0--no information at all, 1--single person, no 2nd info, 2--same last names, 3 different last names   
	ein="";						// message for inner envelope
	eout="";						// message for outer envelope
	ex="";						// message for explanation
	at=document.envelopes.atitle.value;
	af=document.envelopes.a1name.value;
	al=document.envelopes.a2name.value;
	bt=document.envelopes.btitle.value;
	bf=document.envelopes.b1name.value;
	bl=document.envelopes.b2name.value;
	a=document.envelopes.addr.value;
	c=document.envelopes.city.value;
	s=document.envelopes.state.value;	
	z=document.envelopes.zip.value;
	married=document.envelopes.married.checked;

	if (a.length<2) a="123 Example Street";
	if (c.length<2) c="Sampletown";
	if (s.length!=2) s="WA";
	if (z.length!=5) z="12345";
    // sets default information for address if none is present

	if (bl==al) 
		{gtype=2;
		married=true;
		}
	if (bl=="") gtype=1;
	if (al=="") gtype=0; 
    // sets type of guest 


	if ((gtype>=2) && (married==true))
    // if 2 people are submitted and they say they are married, makes sure		
		{if (bt==at)
    // if both have same personal title, doesn't let them be married 
			{if (at=="Mrs.") married=false;
			if (at=="Miss") married=false;
			if (at=="Mr.") married=false;
			}
		if (((bt=="Miss") || (bt=="Mrs.")) && ((at=="Miss") || (at=="Mrs."))) married=false;
    // if both women with different titles won't let them be married
		if (married)
			{if (bt=="Miss") bt="Mrs.";
			if (at=="Miss") at="Mrs.";
			}
		else gtype=3;
		}	

	if (gtype==0)
    // blank submission
		{eout="";
		ein="";
		ex+="<p>Unable to generate envelope information because the first person's last name is blank. Enter a last name and resubmit. ";
		}
	if (gtype==1)
    // single person
		{eout=at + " " + af + " " + al;
		ein=at + " " + al + " And Guest";
		ex+="<p>For a single guest's invitation, It is not necessary to reference bringing a guest .  That is at your discretion.";      
		}
	if (gtype==2)
    // married--have same last name
		{eout=at + " and " + bt +" " + af + " " + al;
		ein=at + " and " + bt +" " + al;
		if ((at=="Doctor") && (bt=="Doctor")) 
			{eout="Doctors " + af + " and " + bf + " " + al;
			ein="The Doctors " + al;
			} 
		}
	if (gtype==3)
    // 2 people with different last names
		{if ((married) && (at=="Mrs."))
    // if married and woman is first person she gets top billing
			{eout=at + " " + af + " " + al + "<br>" + bt  + " " + bf + " " + bl;
			ein=at + " " + al + "<br>" + bt  + " " + bl;	
			}
		if ((married) && (bt=="Mrs."))
    // if married and second person is woman she gets top billing
			{eout=bt  + " " + bf + " " + bl + "<br>" + at + " " + af + " " + al;
			ein=bt  + " " + bl + "<br>" + at + " " + al;
			}
		else if (al<bl)
    // if unable to find married woman lists people alphabetically by last name, this is if 1st person is first 
			{eout=at + " " + af + " " + al + "<br>" + bt  + " " + bf + " " + bl;
			ein=at + " " + al + "<br>" + bt  + " " + bl;	
			}
		else 
    // if unable to find married woman lists people alphabetically by last name, this is if 2nd person is first 
			{eout=bt  + " " + bf + " " + bl + "<br>" + at + " " + af + " " + al;
			ein=bt  + " " + bl + "<br>" + at + " " + al;
			}
		ex+="<p>When addressing invitations to an unmarried couple with different last names, the person who's last name is alphabetically first goes ";
		ex+="on top. If a couple is married but has different last names, then the wife's title and name appears above the husband's. If a couple has ";
		ex+="different addresses, then an invitation should be sent individually to both people, or to the person who you are more closely associated ";
		ex+="and allow them a guest.";     
		} 	
	if (eout!="") eout+="<br>"+ a + "<br>" + c + ", " + s + " " + z;	
    // adds address to outer envelope
	if ((at=="Doctor") || (bt=="Doctor")) 
		{ex+="<p>Only medical doctors have the word 'Doctor' spelled out in their title. If the person is a Ph.D., veterinary, chiropractor or dentist then ";
		ex+="their title is the abbreviated 'Dr.'<br>"; 
		}
	if (((at=="Miss") || (bt=="Miss") || (at=="Mrs.") || (bt=="Mrs.")) && (gtype==3))  
		ex+="<p>For a married woman who does not share her husband's last name, 'Mrs.' is still her correct title.<br>";
	if (((at=="Miss") || (at=="Mrs.")) && (gtype==1)) 
		ex+="<p>Also, a divorced or widowed woman who still uses her husband's last name should be addressed as 'Mrs.'.<br>"; 
	if ((a.indexOf(".")>=0) || (c.indexOf(".")>=0)) 
		ex+="<p>Abbreviations in addresses should be avoided.  The proper way is to spell out the entire word.<br>";  
	if (hasnumber(a)>0)  
		ex+="<p>You should spell out all numbers in the address that are not the actual street number, i.e. Street Name, Apartment Numbers, etc.<br>";
    // below writes all info to page 
	if (ein.length<2) ein="&nbsp;";
	if (eout.length<2) eout="&nbsp;";
	if (ex.length<2) ex="&nbsp;";

	document.getElementById("innerenv").innerHTML = ein;
	document.getElementById("outerenv").innerHTML = eout;
	document.getElementById("explain").innerHTML = ex;
	window.location="#envelopes";
	}

