// become a distributor form processing GS-1219, GS-1052
jQuery(document).ready(function ()
{
    if(jQuery("#become-distributor-form").length)
    {
        jQuery("#become-distributor-form").submit(function() {
            var isValid = true;
            var reg = new RegExp("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$");
            var email= jQuery.trim(jQuery("#email").val());
            
            jQuery('#country0').attr('name','country');
            jQuery('#city1').attr('name','city');
            
            if (jQuery.trim(jQuery("#first_name").val())=="")
              {jQuery("#first_name").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#first_name").css('background-color', '#ffffd2'); }

            if (jQuery.trim(jQuery("#last_name").val())=="")
              {jQuery("#last_name").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#last_name").css('background-color', '#ffffd2'); }

            if (jQuery.trim(jQuery("#phone").val())=="")
              {jQuery("#phone").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#phone").css('background-color', '#ffffd2'); }

            if (email=="" || !reg.test(email))
              {jQuery("#email").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#email").css('background-color', '#ffffd2'); }

             if (jQuery.trim(jQuery("#street").val())=="")
              {jQuery("#street").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#street").css('background-color', '#ffffd2'); }
              
            if (jQuery.trim(jQuery("#city1").val())=="")
              {jQuery("#city1").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#city1").css('background-color', '#ffffd2'); }
              
            if (jQuery.trim(jQuery("#state").val())=="")
              {jQuery("#state").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#state").css('background-color', '#ffffd2'); }
              
            if (jQuery.trim(jQuery("#zip").val())=="")
              {jQuery("#zip").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#zip").css('background-color', '#ffffd2'); }

            if (jQuery.trim(jQuery("#country0").val())=="")
              {jQuery("#country0").css('background-color', '#ffd2df'); isValid=false; }
 else
              {
                  var country = jQuery.trim(jQuery("#country0").val()).toLowerCase();
                  if(country == 'australia' || country == 'israel')
                  {
                    jQuery("#country0").css('background-color', '#ffd2df'); isValid=false;
                    alert('Dear Sir/Madam, at this time we are not able to ship quantities of our product to your country due to government rules there.\nWe hope that in time this will change and encourage you to petition your government in this regard.\n\nBest regards,\nGreen Smoke distribution department');
                    return false;
                  }
                  jQuery("#country0").css('background-color', '#ffffd2');
              }
              
               if (jQuery.trim(jQuery("#captcha").val())=="")
              {jQuery("#captcha").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#captcha").css('background-color', '#ffffd2'); }

             if(jQuery("#age").length) {
                var age = jQuery.trim(jQuery("#age").val());
                   
                if (age =="")
                {
                    jQuery("#age").css('background-color', '#ffd2df');
                    isValid=false;
                }
                else if(age != parseInt(age))
                {
                    alert("Please enter a correct Age");                
                    jQuery("#age").css('background-color', '#ffd2df');
                    isValid=false;
                }
                else if(parseInt(age) < 18)
                {
                    alert("Age under 18 cannot be allowed");                
                    jQuery("#age").css('background-color', '#ffd2df');
                    isValid=false;
                }  
                else
                {
                    jQuery("#age").css('background-color', '#ffffd2');
                }
            }
            
            if (jQuery.trim(jQuery("#description").val())=="")
              {jQuery("#description").css('background-color', '#ffd2df'); isValid=false; }
            else
              {jQuery("#description").css('background-color', '#ffffd2'); }

    jQuery.ajax({
        type: "POST",
        url: "/catalog/become-distributor-form.php",
        data: jQuery("#become-distributor-form").serializeArray(),
        async: false,
        success: function(dd) {
            jQuery('#loader').hide();
                var res =  dd.split('!!!!!');
                if(res[0]=='err') {
                   var d = new Date();
                    jQuery('#secur_image').attr('src',jQuery('#secur_image').attr('src')+ '&time='+ d.getTime());
                    jQuery('.txtError').remove();
                    jQuery('#captcha').val('');
                    jQuery("#captcha").css('background-color', '#ffd2df');
                    isValid=false;
        } 
        } 
        });
   
            if (!isValid) {
                jQuery("p:first", document.forms[0]).html("<strong>Become a </strong><strong class='addfriend'>Green Smoke Distributor </strong><br><p class='txtError'>Please make sure all required fields are completed.</p>");
                return false;
            }
            else
            {                
                jQuery("#description").val(jQuery("#description").val() + "\n\n" + jQuery("#aff_info").html()+ "\n\nAge: " + age);
                return true;
            }
        });

    }
});

