function validate_form ( )
{
    if (document.pressed == "Update basket")
    {   
       return true;
    }
    
    var card_valid = true;
    var instring = "";
    
    if ( document.form1.elements["recs[delivery][billtype]"][0].checked )
    {
    	card_valid = testCreditCard();
    	if ( card_valid == true )
    	{
    	   instring = trim(document.form1.elements["recs[payment][name_on_card]"].value);
    	   if ( instring == "" )	
    	   {
		alert ( "Please enter the name on the credit card." );
		return false;
	   }
	   
            instring = trim(document.form1.elements["recs[payment][credit_card_addr1]"].value);
            if ( instring == "" )	
    	   {
		alert ( "Please enter the address for the credit card." );
		return false;
	   }

            instring = trim(document.form1.elements["recs[payment][credit_card_city]"].value);
            if ( instring == "" )	
    	   {
		alert ( "Please enter the city for the credit card." );
		return false;
	   }

             
            if ( eval(form1.elements["selector_cc_static_info_zone"]) ) 
            {    
                instring = trim(document.form1.elements["recs[payment][credit_card_state]"].value);
               if ( instring == "" )	
    	      {
		alert ( "Please enter the state for the credit card." );
		return false;
	      }

            }
            
            instring = trim(document.form1.elements["recs[payment][credit_card_zip]"].value);
            if ( instring == "" )	
    	   {
		alert ( "Please enter the zip code for the credit card." );
		return false;
	   }

             instring = trim(document.form1.elements["recs[payment][credit_card_cvv]"].value);
            if ( instring == "" )	
    	   {
		alert ( "Please enter the cvv for the credit card." );
		return false;
	   }

         }
    }
    
    valid = true;
    var collect_element = document.getElementById('collect-select'); 
    
	if (card_valid == true)
	{         
		if (  document.form1.elements["recs[delivery][shiptype]"][0].checked  )
		{
			var selected_billme = form1.elements["shipping-select"].selectedIndex;
                           	if ( selected_billme == 0 )
			{
				alert ( "Please select a shipping method for Bill Me for shipping" );
				valid = false;
			}
		}
		else
		{       
			if (  document.form1.elements["recs[delivery][shiptype]"][1].checked  )
			{
				var selected_index = form1.elements["collect-select"].selectedIndex;

				if ( selected_index == 0 )
				{
					alert ( "Please select a carrier for Collect On My Account." );
					valid = false;
				}
				else
				{
					instring = trim(document.form1.collect1.value);
					if ( instring == "" )
					{
						alert ( "Please fill in the account number for Collect On My Account." );
						valid = false;
					}
				}
			}
		}
		
		if (  document.form1.elements["recs[delivery][shiptype]"][2].checked  )
		{
		      instring = trim(document.form1.elements["recs[delivery][alternate_delivery_instructions]"].value);
		      if ( instring == "" )
		      {
		          alert ( "Please fill in the shipping information for Other." );
		          valid = false;
		      }
		 }
		 
	}
	else
	{
		valid = false;
	}

    return valid;
}

function shiptype_billme( )
{
	document.forms[2]['recs[tt_products][collect]'].selectedIndex = 0;
	document.forms[2]['recs[delivery][collect_ship_acct_no]'].value = "";
	document.forms[2]['recs[delivery][alternate_delivery_instructions]'].value = "";
	document.forms[2].submit();
}

function shiptype_collect( )
{
	document.forms[2]['recs[tt_products][shipping]'].selectedIndex = 0;
	document.forms[2]['recs[delivery][collect_ship_acct_no]'].value = "";
	document.forms[2]['recs[delivery][alternate_delivery_instructions]'].value = "";
	document.forms[2].submit();
}

function shiptype_other( )
{
	document.forms[2]['recs[tt_products][shipping]'].selectedIndex = 0;
	document.forms[2]['recs[delivery][collect_ship_acct_no]'].value = "";
	document.forms[2]['recs[tt_products][collect]'].selectedIndex = 0;
	document.forms[2]['recs[delivery][collect_ship_acct_no]'].value = "";
	document.forms[2].submit();
}

function billtype_creditcard( )
{
	document.forms[2].submit();
}

function billtype_onaccount( )
{
	document.forms[2]['recs[tt_products][payment]'].selectedIndex = 0;
	document.forms[2]['recs[payment][name_on_card]'].value = "";
	document.forms[2]['recs[payment][credit_card_zip]'].value = "";
	document.forms[2]['recs[payment][credit_card_no]'].value = "";
	document.forms[2]['recs[payment][cc_exp_month]'].selectedIndex = 0;
	document.forms[2]['recs[payment][cc_exp_year]'].selectedIndex = 0;
	document.forms[2].submit();
}

function checkterms()
{
   
if (!document.placeorder.terms.checked) 
{
	alert ( "You must agree to the Terms and Conditions" );
	return false;
} else
{   
	return true; 
}

}

function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

