jQuery(document).ready(function() {
   //Required Fields
   var required = new Array('member_name','address','city','country','province','postal_code','home_phone');
   var message = '';
   if(jQuery('.cf-ol').length){
   	var required = new Array();
   	for(var i=0;i<jQuery('.cf-ol').length;i++){
   		var c=i+1;
   		required.push('member_name_'+c,'address_'+c,'city_'+c,'country_'+c,'province_'+c,'postal_code_'+c);
   	}
   }
   var error = jQuery('#error');
   
    jQuery("#email").keyup(function(){
		var email = jQuery("#email").val();
		if(email != 0 && email != '') {
			if(isValidEmailAddress(email)) {
				
				jQuery("#validEmail").css({ "background": "url('/wp-content/themes/lowdpoa/images/validyes.png') no-repeat" });
			} else {
				
				jQuery("#validEmail").css({ "background": "url('/wp-content/themes/lowdpoa/images/validno.png') no-repeat" });
			}
		} else {
			jQuery("#validEmail").css({ "background-image": "none" });
		}
	});
	
	jQuery('#postal_code').keyup(function(){
		var code = jQuery("#postal_code").val();
		if(code != 0 && code != '') {
			if(isValidPostalCode(code)) {
				
				jQuery("#validCode").css({ "background": "url('/wp-content/themes/lowdpoa/images/validyes.png') no-repeat" });
			} else {
				
				jQuery("#validCode").css({ "background": "url('/wp-content/themes/lowdpoa/images/validno.png') no-repeat" });
			}
		} else {
			jQuery("#validCode").css({ "background-image": "none" });
		}
	});
	
	
       
   // add markup to container and apply click handlers to anchors
   var obj = jQuery("#submitbutton").click(function(e){
   //Gather Form Inputs
   var inputs = new Array();
   var ids = new Array();
   jQuery('input').each(function(i){
   		if(jQuery(this).attr("id") != 'submitbutton' && jQuery(this).attr("id") != 'form-submitted' && jQuery(this).attr("id") != 's'){
    		ids.push(this.id)
     		inputs.push(jQuery(this).val());
     	}
    });
   
   //Gather Selects 
     jQuery('select').each(function(i){
     	ids.push(jQuery(this).attr("id"))
     	inputs.push(jQuery(this).val());
     });
   
   	//check for required fields
    var missing = new Array();
    jQuery.each(ids, function(){
     //alert(jQuery('#'+this).val());
     if(jQuery.inArray(this.toString(), required) != -1 && jQuery('#'+this).val() == '')
     	missing.push(this.toString());
     	if(this.indexOf('postal_code')+1){
			if(!isValidPostalCode(jQuery('#'+this).val())){
				missing.push(this.toString());
				message += "<br />You have entered an invalid Postal Code";
			}
		}
		if(this.indexOf('email')+1 && jQuery('#'+this).val() != ''){
			if(!isValidEmailAddress(jQuery('#'+this).val())){
				missing.push(this.toString());
				message += "<br />You have entered an invalid Email Address";
			}
		}
		if(this.indexOf('how_many_gift_memberships')+1 && jQuery('#add_gift_membership-1').attr("checked")==true){			
			if (isNaN(jQuery('#how_many_gift_memberships').val()) || jQuery('#how_many_gift_memberships').val()==''){				
				missing.push(this.toString());
				message += "<br />You must enter a number between 1 - 9 for the gift memberships you require.";
			}
		}
		if(this.indexOf('how_many_seedling_bundles')+1 && jQuery('#purchase_seedlings-1').attr("checked")==true){			
			if (isNaN(jQuery('#how_many_seedling_bundles').val()) || jQuery('#how_many_seedling_bundles').val()==''){				
				missing.push(this.toString());
				message += "<br />You must enter a valid number for the red pine seedlings you require.";
			}
		}
		if(this.indexOf('environment_fund')+1 && jQuery('#make_a_donation-1').attr("checked")==true){
			if ((isNaN(jQuery('#environment_fund').val()) || jQuery('#environment_fund').val()=='') && (isNaN(jQuery('#taxation_fund').val()) || jQuery('#taxation_fund').val()=='')){
				missing.push(this.toString());
				message += "<br />You must enter a valid number for ONE of the donation funds.";
			}
		}
     });
    
    //alert(missing); 
    // send request
    if(missing.length < 1){
		if(jQuery("#add_gift_membership-1").attr("checked")=="checked")
			jQuery("#member-form").attr("action", "/membership/membership-online/?step=2");
		else
			jQuery("#member-form").attr("action", "/membership/membership-online/?step=3");
		jQuery('#member-form').submit();
	}
    else {
    	jQuery('html, body').animate({scrollTop:0}, 'slow');
    	error.empty();
       	error.html("<span>You have an Error:</span><br /><br />Please enter the required fileds highlighted in red:<br />");
       	error.append(message);
    	 jQuery.each(ids, function(){
    	 	if(this=="province")
    	 		jQuery('#prov').removeClass("error");
    	 	else
    	 		jQuery('#'+this).prev('label').removeClass("error");
    	 });
    	jQuery.each(missing, function(){    		
    		if(this=="province")
    			jQuery('#prov').addClass("error");
    		else
    			jQuery('#'+this).prev('label').addClass("error");
    	});
    	error.show("normal");
    }
  
  });
  
 //Show or Hide the Other Input box
	jQuery('#where_is_your_property_located-2').click(function(){
			jQuery('#li--19items').hide("fast");
	});
	jQuery('#where_is_your_property_located-1').click(function(){
			jQuery('#li--19items').show("slow");
	});
	
	if(jQuery('#where_is_your_property_located-1').attr("checked"))
		jQuery('#li--19items').show("slow");
	
	//Show or Hide the Gift Memberships Input box
	jQuery('#add_gift_membership-1').click(function(){		
			jQuery('#li--24').show("slow");
	});
	jQuery('#add_gift_membership-2').click(function(){		
			jQuery('#li--24').hide("fast");
	});
	
	if(jQuery('#add_gift_membership-1').attr("checked"))
		jQuery('#li--24').show("slow");
		
	//Show or Hide the Donations Input box
	jQuery('#make_a_donation-1').click(function(){		
			jQuery('#li--33').show("slow");
	});
	jQuery('#make_a_donation-2').click(function(){		
			jQuery('#li--33').hide("fast");
	});
	
	if(jQuery('#make_a_donation-1').attr("checked"))
		jQuery('#li--33').show("slow");
	
	//Show or Hide the Seedlings Input box
	jQuery('#purchase_seedlings-1').click(function(){		
			jQuery('#li--26').show("slow");
			jQuery('#li--27items').show("slow");
	});
	jQuery('#purchase_seedlings-2').click(function(){		
			jQuery('#li--26').hide("fast");
			jQuery('#li--27items').hide("fast");
	});
	
	if(jQuery('#purchase_seedlings-1').attr("checked")){
		jQuery('#li--26').show("slow");
		jQuery('#li--27items').show("slow");
	}
	
	//Show or Hide the Taxes Input box
	jQuery('#i_pay_taxes_to-1').click(function(){		
			jQuery('#i_pay_taxes_to').hide("fast");
	});
	jQuery('#i_pay_taxes_to-2').click(function(){		
			jQuery('#i_pay_taxes_to').hide("fast");
	});
	jQuery('#i_pay_taxes_to-3').click(function(){		
			jQuery('#i_pay_taxes_to').hide("fast");
	});
	jQuery('#i_pay_taxes_to-4').click(function(){		
			jQuery('#i_pay_taxes_to').hide("fast");
	});
	jQuery('#i_pay_taxes_to-5').click(function(){		
			jQuery('#i_pay_taxes_to').show("slow");
	});	
	
	if(jQuery('#i_pay_taxes_to-5').attr("checked"))
		jQuery('#i_pay_taxes_to').show("slow");
	
	//Show or hide the other LOTW Area
	jQuery('#lake_of_the_woods_location-1').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-2').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-3').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-4').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-5').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-6').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-7').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-8').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});
	jQuery('#lake_of_the_woods_location-9').click(function(){		
			jQuery('#other_lake_of_the_woods_location').hide("fast");
	});	
	jQuery('#lake_of_the_woods_location-10').click(function(){		
			jQuery('#other_lake_of_the_woods_location').show("slow");
	});	
	
	if(jQuery('#lake_of_the_woods_location-10').attr("checked"))
		jQuery('#other_lake_of_the_woods_location').show("slow");
	
	
	 // add markup to container and apply click handlers to anchors
   var obj = jQuery("#submitbutton2").click(function(e){
   //Gather Form Inputs
   var inputs = null;
   var missing = null;   
   var inputs = new Array();
   var ids = new Array();
   jQuery('input').each(function(i){
   		if(jQuery(this).attr("id") != 'submitbutton2' && jQuery(this).attr("id") != 'form-submitted-2' && jQuery(this).attr("id") != 's'){
    		ids.push(jQuery(this).attr("id"))
     		inputs.push(jQuery(this).val());
     	}
    });
   
   //Gather Selects 
     jQuery('select').each(function(i){
     	ids.push(jQuery(this).attr("id"))
     	inputs.push(jQuery(this).val());
     });
   
   	//check for required fields
    var missing = new Array();
    jQuery.each(ids, function(){
     //alert(jQuery('#'+this).val());
     if(jQuery.inArray(this.toString(), required) != -1 && jQuery('#'+this).val() == '')
     	missing.push(this.toString());
     });
    
    //alert(missing); 
    // send request
    if(missing.length < 1){		
		jQuery('#gift-membership-form').submit();
	}
    else {
    	jQuery('html, body').animate({scrollTop:0}, 'slow');
    	error.empty();
       	error.html("<span>You have an Error:</span><br /><br />Please enter the required fileds highlighted in red:<br />");
    	jQuery.each(ids, function(){
    	 	if(this.indexOf("province")+1){
    	 		var n = this.substr(this.indexOf('_'),1);
    	 		jQuery('#prov_'+n).removeClass("error");
    	 	}
    	 	else {
    	 		jQuery('#'+this).prev('label').removeClass("error");
    	 	}
    	 });
    	jQuery.each(missing, function(){    		
    		if(this.indexOf("province")+1){
    	 		var n = this.substr(this.indexOf('_')+1,1);
    	 		jQuery('#prov_'+n).addClass("error");
    	 	}
    	 	else {
    	 		jQuery('#'+this).prev('label').addClass("error");
    	 	}
    	});
    	error.show("normal");
    }
  
  });
});
