function intlPurchaseAgreementForm()
{
	var data = '';
	var h_country = document.getElementById('country');
	var h_state_label = document.getElementById('buyerState_label');
	var h_state_input = document.getElementById('buyerState_input');
	var h_zip_label = document.getElementById('buyerZip_label');
	var h_zip_input = document.getElementById('buyerZip_input');
    //alert("h_country: " + h_country + "\n" + "h_state_label: " + h_state_label + "\n");
	//alert("h_state_input: " + h_state_input + "\n" + "h_zip_label: " + h_zip_label + "\n" + "h_zip_input: " + h_zip_input);
	if( h_country && h_state_label && h_state_input && h_zip_label && h_zip_input )
	{	
		// if the selected country is the United States
		if( h_country.value == 'US|United States' )
		{
			h_state_label.innerHTML = '<span class="required">*</span> <strong>State:</strong>';
			h_state_input.innerHTML = generateUSStates('buyerState', 'buyerState');
			h_zip_label.innerHTML = '<span class="required">*</span> <strong>Zip Code:</strong>';
			h_zip_input.innerHTML = generateCanadianZip('buyerZip', 'buyerZip');	
		}
		// else if it is Canada
		else if( h_country.value == 'CA|Canada' )
		{
			h_state_label.innerHTML = '<span class="required">*</span> <strong>Province:</strong>';
			h_state_input.innerHTML = generateCanadianProvinces('buyerState', 'buyerState');
			h_zip_label.innerHTML = '<span class="required">*</span> <strong>Postal Code:</strong>';
			h_zip_input.innerHTML = generateCanadianZip('buyerZip', 'buyerZip');
			
		}
		// else it is a different country
		else
		{
			h_state_label.innerHTML = '<strong>State/Province/Region:</strong>';
			h_state_input.innerHTML = generateOtherState('buyerState', 'buyerState');
			h_zip_label.innerHTML = '<span class="required">*</span> <strong>Postal Code:</strong>';
			h_zip_input.innerHTML = generateOtherZip('buyerZip', 'buyerZip');
		}
	}
	else
	{
		alert( 'Unable to find a country input field.' );
	}
	
	return;
}


function intlCheckoutForm()
{
	var data = '';
	
	var bcountry = document.getElementById('bill_country');
	
	var bstate_label = document.getElementById('bill_state_label');
	var bstate_input = document.getElementById('bill_state_input');
	
	var bzip_label = document.getElementById('bill_zip_label');
	var bzip_input = document.getElementById('bill_zip_input');
	
	var scountry = document.getElementById('ship_country');
	
	var sstate_label = document.getElementById('ship_state_label');
	var sstate_input = document.getElementById('ship_state_input');
	
	var szip_label = document.getElementById('ship_zip_label');
	var szip_input = document.getElementById('ship_zip_input');
	
	
	if( bcountry && bstate_label && bstate_input && bzip_label && bzip_input &&
		scountry && sstate_label && sstate_input && szip_label && szip_input 
	){	
		
	    //alert( "bcountry: " + bcountry.value + "\nscountry: " + scountry.value );
	    
	    // billing ------------------------------------
		// if the selected country is the United States
		if( bcountry.value == 'US|United States' || bcountry.value == '' )
		{
			bstate_label.innerHTML = '<span class="required">*</span> State:';
			bstate_input.innerHTML = generateUSStates('bill_state', 'bill_state');
			bzip_label.innerHTML = '<span class="required">*</span> Zip Code:';
			bzip_input.innerHTML = generateUSZip('bill_zip', 'bill_zip');	
		}
		// else if it is Canada
		else if( bcountry.value == 'CA|Canada' )
		{
			bstate_label.innerHTML = '<span class="required">*</span> Province:';
			bstate_input.innerHTML = generateCanadianProvinces('bill_state', 'bill_state');
			bzip_label.innerHTML = '<span class="required">*</span> Postal Code:';
			bzip_input.innerHTML = generateCanadianZip('bill_zip', 'bill_zip');
			
		}
		// else it is a different country
		else
		{
			bstate_label.innerHTML = 'State/Province/Region:';
			bstate_input.innerHTML = generateOtherState('bill_state', 'bill_state');
			bzip_label.innerHTML = '<span class="required">*</span> Postal Code:';
			bzip_input.innerHTML = generateOtherZip('bill_zip', 'bill_zip');
		}
		
		// shipping -----------------------------------
		// if the selected country is the United States
		if( scountry.value == 'US|United States' || scountry.value == '' )
		{
			sstate_label.innerHTML = 'State:';
			sstate_input.innerHTML = generateUSStates('ship_state', 'ship_state');
			szip_label.innerHTML = 'Zip Code:';
			szip_input.innerHTML = generateUSZip('ship_zip', 'ship_zip');
		}
		// else if it is Canada
		else if( scountry.value == 'CA|Canada' )
		{
			sstate_label.innerHTML = 'Province:';
			sstate_input.innerHTML = generateCanadianProvinces('ship_state', 'ship_state');
			szip_label.innerHTML = 'Postal Code';
			szip_input.innerHTML = generateCanadianZip('ship_zip', 'ship_zip');			
		}
		// else it is a different country
		else
		{
			sstate_label.innerHTML = 'State/Province/Region:';
			sstate_input.innerHTML = generateOtherState('ship_state', 'ship_state');
			szip_label.innerHTML = 'Postal Code:';
			szip_input.innerHTML = generateOtherZip('ship_zip', 'ship_zip');
		}
	}
	else
	{
		//alert( 'Unable to find a country input field.' );
	}
	
	return;
}



function generateUSStates( name, id )
{
	var data = ''+
	'<select name="'+name+'" id="'+id+'">'+
	'<option value=""></option>'+
	'<option value="AL">Alabama (AL)</option>'+
	'<option value="AK">Alaska (AK)</option>'+
	'<option value="AZ">Arizona (AZ)</option>'+
	'<option value="AR">Arkansas (AR)</option>'+
	'<option value="CA">California (CA)</option>'+
	'<option value="CO">Colorado (CO)</option>'+
	'<option value="CT">Connecticut (CT)</option>'+
	'<option value="DE">Delaware (DE)</option>'+
	'<option value="DC">District of Columbia (DC)</option>'+
	'<option value="FL">Florida (FL)</option>'+
	'<option value="GA">Georgia (GA)</option>'+
	'<option value="HI">Hawaii (HI)</option>'+
	'<option value="ID">Idaho (ID)</option>'+
	'<option value="IL">Illinois (IL)</option>'+
	'<option value="IN">Indiana (IN)</option>'+
	'<option value="IA">Iowa (IA)</option>'+
	'<option value="KS">Kansas (KS)</option>'+
	'<option value="KY">Kentucky (KY)</option>'+
	'<option value="LA">Louisiana (LA)</option>'+
	'<option value="ME">Maine (ME)</option>'+
	'<option value="MD">Maryland (MD)</option>'+
	'<option value="MA">Massachusetts (MA)</option>'+
	'<option value="MI">Michigan (MI)</option>'+
	'<option value="MN">Minnesota (MN)</option>'+
	'<option value="MS">Mississippi (MS)</option>'+
	'<option value="MO">Missouri (MO)</option>'+
	'<option value="MT">Montana (MT)</option>'+
	'<option value="NE">Nebraska (NE)</option>'+
	'<option value="NV">Nevada (NV)</option>'+
	'<option value="NH">New Hampshire (NH)</option>'+
	'<option value="NJ">New Jersey (NJ)</option>'+
	'<option value="NM">New Mexico (NM)</option>'+
	'<option value="NY">New York (NY)</option>'+
	'<option value="NC">North Carolina (NC)</option>'+
	'<option value="ND">North Dakota (ND)</option>'+
	'<option value="OH">Ohio (OH)</option>'+
	'<option value="OK">Oklahoma (OK)</option>'+
	'<option value="OR">Oregon (OR)</option>'+
	'<option value="PA">Pennsylvania (PA)</option>'+
	'<option value="RI">Rhode Island (RI)</option>'+
	'<option value="SC">South Carolina (SC)</option>'+
	'<option value="SD">South Dakota (SD)</option>'+
	'<option value="TN">Tennessee (TN)</option>'+
	'<option value="TX">Texas (TX)</option>'+
	'<option value="UT">Utah (UT)</option>'+
	'<option value="VT">Vermont (VT)</option>'+
	'<option value="VA">Virginia (VA)</option>'+
	'<option value="WA">Washington (WA)</option>'+
	'<option value="WV">West Virginia (WV)</option>'+
	'<option value="WI">Wisconsin (WI)</option>'+
	'<option value="WY">Wyoming (WY)</option>'+
	'</select>';
	return( data );
}
function generateUSZip( name, id )
{
	var data = '<input type="text" name="'+name+'" id="'+id+'" size="10" maxlength="10" />';
	return( data );
}


function generateCanadianProvinces( name, id )
{
	var data = ''+
	'<select name="'+name+'" id="'+id+'">'+
	'<option value=""></option>'+
	'<option value="AB">Alberta (AB)</option>'+
	'<option value="BC">British Columbia (BC)</option>'+
	'<option value="MB">Manitoba (MB)</option>'+
	'<option value="NB">New Brunswick (NB)</option>'+
	'<option value="NL">Newfoundland and Labrador (NL)</option>'+
	'<option value="NS">Nova Scotia (NS)</option>'+
	'<option value="NT">Northwest Territories (NT)</option>'+
	'<option value="NU">Nunavut (NU)</option>'+
	'<option value="ON">Ontario (ON)</option>'+
	'<option value="PE">Prince Edward Island (PE)</option>'+
	'<option value="QC">Quebec (QC)</option>'+
	'<option value="SK">Saskatchewan (SK)</option>'+
	'<option value="YT">Yukon (YT)</option>'+
	'</select>';
	return( data );
}
function generateCanadianZip( name, id )
{
	var data = '<input type="text" name="'+name+'" id="'+id+'" size="7" maxlength="7" />';
	return( data );
}


function generateOtherState( name, id )
{
	var data = '<input type="text" name="'+name+'" id="'+id+'" size="30" maxlength="255" />';
	return( data );
}
function generateOtherZip( name, id )
{
	var data = '<input type="text" name="'+name+'" id="'+id+'" size="20" maxlength="255" />';
	return( data );
}

//param "mode" refers to which address' country dropdown menu has triggered the onchange.
//i.e. whether it is the billing or shipping address that has triggered it.
//Values can be "billing", "shipping", or "onload". 
function filterShipOptions( mode, show_US_as_bill_country )
{
     var isset_bcountry = 0;
     var isset_scountry = 0;
     
     var bcountry_val = document.getElementById("bill_country").value;
     var scountry_val = document.getElementById("ship_country").value;
     
     if( bcountry_val != "" ) { isset_bcountry = 1; }
     if( scountry_val != "" ) { isset_scountry = 1; }
     
     //each of these vars correspond to a dropdown menu on "checkout.php"
     //The US dropdown shows the shipping options available to US residents
     //CA dropdown shows shipping options available to Canadian residents
     //OTHER dropdown shows shipping option available to all other countries
     var ups_ship_US = document.getElementById("ups_shipping_US");
     var ups_ship_CA = document.getElementById("ups_shipping_CA");
     var ups_ship_OTHER = document.getElementById("ups_shipping_OTHER");
     
     ups_ship_US.value = '';
     ups_ship_CA.value = '';
     ups_ship_OTHER.value = '';
    
     //ONLOAD 
     if( mode == "onload" ) {
        
         //if a billing country has been selected, but not a shipping country
        if( isset_bcountry == 1 && isset_scountry == 0 ) {
            
            if( bcountry_val == 'US|United States' ) {
                ups_ship_US.style.display = 'block';  
                ups_ship_CA.style.display = 'none';
                ups_ship_OTHER.style.display = 'none';         
            }
            else if( bcountry_val == 'CA|Canada' ) {
                ups_ship_US.style.display = 'none';  
                ups_ship_CA.style.display = 'block';
                ups_ship_OTHER.style.display = 'none';      
		    }
		    else {
		        ups_ship_US.style.display = 'none';  
                ups_ship_CA.style.display = 'none';
                ups_ship_OTHER.style.display = 'block';      
		    }
        }
        //else if both a billing country and a shipping country have been selected
        else if ( isset_bcountry == 1 && isset_scountry == 1 ) {
            
            //closes a gap in the code that would allow someone to place an order 
                //from a foreign country using the US shipping options
                var ship_address1 = document.getElementById("ship_address1").value;
                var ship_city = document.getElementById("ship_city").value; 
                var ship_state = document.getElementById("ship_state").value;
                var ship_zip = document.getElementById("ship_zip").value;  
                
                //if the billing and shipping countries are different, then
                //check to see if the person is entering a custom shipping address,
                //or if the country dropdown menu is merely set to the default value, "US". 
                //if they're entering a custom address, they should enter at least:
                //ship_address1, ship_city, ship_state, ship_zip               
                if( scountry_val != bcountry_val ) {
                    if( ship_address1 == '' || ship_city == '' || ship_state == '' || ship_zip == '' ) {
                        //USA
                        if( bcountry_val == 'US|United States' ) {
                            ups_ship_US.style.display = 'block';  
                            ups_ship_CA.style.display = 'none';
                            ups_ship_OTHER.style.display = 'none';   
                        }
                        //CANADA
                        else if( bcountry_val == 'CA|Canada' ) {
                            ups_ship_CA.style.display = 'block';
                            ups_ship_US.style.display = 'none';  
                            ups_ship_OTHER.style.display = 'none'; 
            		    }
            		    //OTHER
            		    else {
            		        ups_ship_OTHER.style.display = 'block';
            		        ups_ship_US.style.display = 'none';  
                            ups_ship_CA.style.display = 'none';
                        }
                    } else {
                        //USA
                        if( scountry_val == 'US|United States' ) {
                            ups_ship_US.style.display = 'block';  
                            ups_ship_CA.style.display = 'none';
                            ups_ship_OTHER.style.display = 'none';   
                        }
                        //CANADA
                        else if( scountry_val == 'CA|Canada' ) {
                            ups_ship_CA.style.display = 'block';
                            ups_ship_US.style.display = 'none';  
                            ups_ship_OTHER.style.display = 'none';
            		    }
            		    //OTHER
            		    else {
            		        ups_ship_OTHER.style.display = 'block';
            		        ups_ship_US.style.display = 'none';  
                            ups_ship_CA.style.display = 'none';
            		    }   
                    }
                } else {
                    //USA
                        if( bcountry_val == 'US|United States' ) {
                            ups_ship_US.style.display = 'block';  
                            ups_ship_CA.style.display = 'none';
                            ups_ship_OTHER.style.display = 'none';   
                        }
                        //CANADA
                        else if( bcountry_val == 'CA|Canada' ) {
                            ups_ship_CA.style.display = 'block';
                            ups_ship_US.style.display = 'none';  
                            ups_ship_OTHER.style.display = 'none'; 
            		    }
            		    //OTHER
            		    else {
            		        ups_ship_OTHER.style.display = 'block';
            		        ups_ship_US.style.display = 'none';  
                            ups_ship_CA.style.display = 'none';
                        }   
                }
        }
        //else neither a billing nor shipping country have been selected
        else {
           if( show_US_as_bill_country ) {
               ups_ship_US.style.display = 'block'; 
               ups_ship_CA.style.display = 'none';
               ups_ship_OTHER.style.display = 'none';        
           } else {
               ups_ship_US.style.display = 'none';
               ups_ship_CA.style.display = 'none';
               ups_ship_OTHER.style.display = 'block';
           } 
        } 
     }
     //REGULAR, i.e. NOT onload
     else {
         
        //if a billing country has been selected, but not a shipping country
        if( isset_bcountry == 1 && isset_scountry == 0 ) {
            //USA
            if( bcountry_val == 'US|United States' ) {
                ups_ship_US.style.display = 'block';  
                ups_ship_CA.style.display = 'none';
                ups_ship_OTHER.style.display = 'none';   
            }
            //CANADA
            else if( bcountry_val == 'CA|Canada' ) {
                ups_ship_CA.style.display = 'block';
                ups_ship_US.style.display = 'none';  
                ups_ship_OTHER.style.display = 'none'; 
		    }
		    //OTHER
		    else {
		        ups_ship_OTHER.style.display = 'block';
		        ups_ship_US.style.display = 'none';  
                ups_ship_CA.style.display = 'none';
            }
        }
        //else both a billing country and a shipping country have been selected
        else if ( isset_bcountry == 1 && isset_scountry == 1 ) {
            
                    if( mode == "billing" ) { 
                        //USA
                        if( bcountry_val == 'US|United States' ) {
                            ups_ship_US.style.display = 'block';  
                            ups_ship_CA.style.display = 'none';
                            ups_ship_OTHER.style.display = 'none';   
                        }
                        //CANADA
                        else if( bcountry_val == 'CA|Canada' ) {
                            ups_ship_CA.style.display = 'block';
                            ups_ship_US.style.display = 'none';  
                            ups_ship_OTHER.style.display = 'none'; 
            		    }
            		    //OTHER
            		    else {
            		        ups_ship_OTHER.style.display = 'block';
            		        ups_ship_US.style.display = 'none';  
                            ups_ship_CA.style.display = 'none';
                        }
                    } else if( mode == "shipping" ) {
                        //USA
                        if( scountry_val == 'US|United States' ) {
                            ups_ship_US.style.display = 'block';  
                            ups_ship_CA.style.display = 'none';
                            ups_ship_OTHER.style.display = 'none';   
                        }
                        //CANADA
                        else if( scountry_val == 'CA|Canada' ) {
                            ups_ship_CA.style.display = 'block';
                            ups_ship_US.style.display = 'none';  
                            ups_ship_OTHER.style.display = 'none';
            		    }
            		    //OTHER
            		    else {
            		        ups_ship_OTHER.style.display = 'block';
            		        ups_ship_US.style.display = 'none';  
                            ups_ship_CA.style.display = 'none';
            		    }   
                    }
         }
        //neither a billing nor shipping country have been selected
        else {
            ups_ship_US.style.display = 'none';  
            ups_ship_CA.style.display = 'none';
            ups_ship_OTHER.style.display = 'none';
        }
     }
}