// JavaScript Document

function urlencode(str) {
    return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}



//Email Validation Class
function EmailCheck(email){
    this.email=email.toLowerCase();
}

function getEmail(){
    return this.email;
}

function validate(){
    var pattern=/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
    if(pattern.test(this.email)){
        return true;
    }
    else{
        return false;
    }
} 

  
EmailCheck.prototype.getEmail=getEmail;
EmailCheck.prototype.validate=validate;
    
//End Email Validation Class


function checkOfferCode(){
    //ajax call to check offer code

    var offerCode=$("#couponCode").val();

    

   
    $.ajax({
        type: "POST",
        url: "ajaxOfferCode.php",
        data: "cmd=checkOfferCode&offerCode="+urlencode(offerCode),
        async: false,
        success: function(theResponse){
            if(theResponse=="OKAY"){
                $("#offerCodeResponse").html("<div class='successMessage'>Code was found.</div>");
                $("#couponResponse").val("verified");
                
                

            }
            else if(theResponse=="ENTER CODE"){
                $("#offerCodeResponse").html("<div class='errorMessage'>Please ensure you entered an offer code.</div>");
               

            }
            else if(theResponse=="NOT FOUND"){
                $("#offerCodeResponse").html("<div class='errorMessage'>We were unable to locate the offer code you entered.  Please check your input and try again.</div>");
                
            }
            else if(theResponse=="OVER SUBSCRIBED"){
                $("#offerCodeResponse").html("<div class='errorMessage'>The offer code you entered is no longer valid.  Please select one of the membership options below.</div>");
                
            }
        }
    });

    
    




}

function checkValid(){

    var offerCode=$("#couponResponse").val();

    if(offerCode=='pending'){

        //alert(offerCode);
        return false;
    }
    else{
        //alert("TRUE: "+offerCode);
        return true;
    }
    

}





function bindEvents(){

    $(function() {
  

        //-------------------------------------------------------- handle form validation
        $('.formError').hide();
        $("#accept").click(function() {
            // validate and process form here
            $("#ajaxNotify").html("<img src='images/ajax-loader.gif' alt='Processing' />");
      
            $('.formError').hide();
            var first_name = $("#first_name").val();
            if (first_name == "") {
                $("#first_name_error").show();
                $("#first_name").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR FIRST NAME</div>");
                return false;
            }
      
            var last_name = $("#last_name").val();
            if (last_name == "") {
                $("#last_name_error").show();
                $("#last_name").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR LAST NAME</div>");
                return false;
            }
      
            var business_tel = $("#business_tel").val();
            if (business_tel == "") {
                $("#business_tel_error").show();
                $("#business_tel").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR BUSINESS TELEPHONE NUMBER</div>");
                return false;
            }
      
            var street_1 = $("#street_1").val();
            if (street_1 == "") {
                $("#street_1_error").show();
                $("#street_1").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR STREET ADDRESS</div>");
                return false;
            }
      
            var city = $("#city").val();
            if (city == "") {
                $("#city_error").show();
                $("#city").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR CITY</div>");
                return false;
            }
      
            var state = $("#state").val();
            if (state == "") {
                $("#state_error").show();
                $("#state").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR STATE</div>");
                return false;
            }
      
            var zip = $("#zip").val();
            if (zip == "") {
                $("#zip_error").show();
                $("#zip").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>YOU MUST ENTER YOUR ZIP CODE</div>");
                return false;
            }
      
      
      
            //check email
      
            var myEmail=new EmailCheck($("#email").val());
            if(!myEmail.validate()){
                $("#email_error").show();
                $("#email").focus();
                $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>PLEASE ENTER A VALID EMAIL ADDRESS</div>");
                return false;
      
            }
      
            //check password
            var passwordCheck = $("#nopass").val();
            if(passwordCheck==0){
                //password active
                var password = $("#password").val();
                var password_confirm = $("#password_confirm").val();
        
                if(password.length<6){
                    $("#password_error").show();
                    $("#password").focus();
                    $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>THE PASSWORD MUST BE AT LEAST 6 CHARACTERS IN LENGTH</div>");
                    return false;
                }
        
                if(password!==password_confirm){
                    $("#password_confirm_error").show();
                    $("#password_confirm").focus();
                    $("#ajaxNotify").html("<div class='errorMessage' style='width:70%'>THE PASSWORD AND CONFIRM PASSWORD FIELDS MUST MATCH</div>");
                    return false;
                }
            }
      
      
      
      
 
      
    
  		      
        });
    });

}



//Cufon Font Changes for theme
$(document).ready(function() {
  
    bindEvents();
  
			                            
});






