// Travis Palmer
// This function checks the shipping address, and verifies that it does not
// contain a P.O. Box, if Free Economy Ground shipping is selected.
function checkForPOBox(form) {	
    // Test the address field against our regular expression.
    re = /.*(p|post)\.?\s*(o|office)\.? \s*(box)?/i;    
    var freeground = false;
    var usmail = false;

    // Iterate through the shipping method array of radio buttons.
    for (var i=0; i < form.method.length; i++)
    {
        if (form.method[i].checked)
        {
            var rad_val = form.method[i].value;
        }
	
	// As we run through the array of radio buttons, we set the freeground
	// and US Mail flags.
	if (form.method[i].value == "Free Economy Ground &lt;b&gt;(No P.O. Boxes)&lt;/b&gt; (3-6 bus. days)")
	{
    		freeground = true;
	}
	if (form.method[i].checked && form.method[i].value == 'US Mail (AK, AS, GU, VI, AA, AE, AP, PO Boxes only)' )
	{		
		usmail = true;
	}
    }
    
    // The user has selected US Mail, and a bonus item (meaning Free Shipping is not in the list).
    if (freeground == false && usmail == true) {
	    
	    // If the user has a PO Box, then ignore the restrictions.
	    if ( re.test(form.shiptoaddress.value) ) {
		    form.submit();
		    return true;
	    }	    
	    // Otherwise, restrict them to a valid choice.
	    else {		    
		    // Get the reference to the ship to state dropdown.
		    var shipto_ref = document.getElementById('shiptostate');
		    
		    // For readability, assign the inner text of the current option to a
		    // variable.
		    var txt = shipto_ref.options[shipto_ref.selectedIndex].text;
		    
		    // Test the state to make sure it's one of the following:
		    // AK, AS, GU, VI, AA, AE, AP.
		    if (txt == 'AK' || txt == 'AS' || txt == 'GU' || txt == 'VI' || txt == 'AA' || txt == 'AE' || txt == 'AP') {
			    form.submit();
			    return true;
		    }
		    // If one of the above states isn't selected we block the submit, and
		    // notify the user.
		    else {
			    alert("US Mail can only be used when shipping to AK, AS, GU, VI, AA, AE or AP.");
			    return false;	
		    }
	    }	
    }    
    
    // If the user changes their address to no longer be a po box, then restrict
    // them to the seven valid options.
    if ( usmail == true && freeground == true ) {
	  // Get the reference to the ship to state dropdown.
	var shipto_ref = document.getElementById('shiptostate');
	
	// For readability, assign the inner text of the current option to a
	// variable.
	var txt = shipto_ref.options[shipto_ref.selectedIndex].text;
	
	// Test the state to make sure it's one of the following:
	// AK, AS, GU, VI, AA, AE, AP.
	if (txt == 'AK' || txt == 'AS' || txt == 'GU' || txt == 'VI' || txt == 'AA' || txt == 'AE' || txt == 'AP') {
		form.submit();
		return true;
	}
	// If one of the above states isn't selected we block the submit, and
	// notify the user.
	else {
		alert("US Mail can only be used when shipping to AK, AS, GU, VI, AA, AE or AP.");
		return false;	
	}	  
    }
        
    // Test against our regular expressions.
    if ( re.test(form.shiptoaddress.value)) {
        // There is a P.O. Box present, and the user selected Free
        // Economy Ground shipping.
        alert("Please note that FedEx does not deliver to P. O. Boxes. You have identified a P. O. Box as your Ship To address.  U S mail is the only shipping option to P. O. Boxes unless you change your Ship To address to a physical residence address, in which case Express shipping options would then be available.");
        
	// Enable the US Mail option radio button.
	for (var i=0; i < form.method.length; i++)
	{		
        	if (form.method[i].value == 'US Mail (AK, AS, GU, VI, AA, AE, AP, PO Boxes only)')
		{
			form.method[i].disabled = false;			
		}
		
	}
		
	return false;
    }
    else {
        // There is no P.O. Box, and no free shipping - we can continue.
	form.submit();
        return true;
    }    
}

// Travis Palmer
// This function is run on step 1 of the cart, and sets the US Mail option to its appropriate state.
function checkUSMail() {
	
	// Get the reference to the shipto form.
	var form = document.getElementById('shipto');

	// Iterate through the shipping method array of radio buttons.
	for (var i=0; i < form.method.length; i++) {
		if (form.method[i].value == 'US Mail (AK, AS, GU, VI, AA, AE, AP, PO Boxes only)' && form.method[i].checked == true)
		{
			form.method[i].disabled = false;
		}
		
		if ((parseInt(document.getElementById(form.method[i].value).innerHTML.substr(document.getElementById(form.method[i].value).innerHTML.search(/\$/) + 1, document.getElementById(form.method[i].value).innerHTML.length)) == 0) && !(form.method[i].value.match("free") || form.method[i].value.match("Free")))
		{
			form.method[i].disabled = true;	
		}
	}
}

