$(document).ready(function() {

    if ($("#type").length) {
        $("#type").livequery("change", function() {
            if ($(this).val() == 1) {
                $("#promo").show();
            } else {
                $("#promo").hide();
            }
        });
    } else {
        $("#promo").show();
    }

    $("#terms").click(function() {
        if ($(this).is(":checked")) {
            $("#signup").removeAttr("disabled").css({opacity: '1'});
        } else {
            $("#signup").attr({disabled:"disabled"}).css({opacity: '0.3'});
        }
    });

    $("#username").blur(function(){
        var name = $(this).val();
		name = name.toLowerCase();
		$(this).val(name);
		
		if (check_email(name)) {
			alert('Username can not be an email address, please choose another.');
			$(this).val('').focus();
		}
    });

    $("#signupForm").submit(function() {
        $(".error").removeClass("error");
        var error_id  = "";
        var error_msg = "";
        var arr = ["firstname", "lastname", "email", "username", "password", "password2", "creditCardNumber", "cvv2Number", "zip", "address1", "city"];
        $.each(arr, function() {
            var value = trim($("#"+this).val());
            if (value == "") {
                error_id = this;
            } else {
                if (this == "email") {
                    if (!check_email(value)) {
                        error_id = this;
                    }
                } else if (this == "username") {
					value = value.replace(/@/gi, '');
					$("#"+this).val(value);
                    if (value.length < 6) {
                        error_id  = this;
                        error_msg = "Username should be minimum 6 characters"
                    }
                }else if (this == "password") {
                    if (value.length < 6) {
                        error_id  = this;
                        error_msg = "Password should be minimum 6 characters"
                    }
                } else if (this == "password2") {
                    var pwd1 = trim($("#password").val());
                    var pwd2 = trim($("#password2").val());
                    if (pwd1 != pwd2) {
                        error_id = this;
                    }
                } else if (this == "creditCardNumber") {
                	myCardType = document.getElementById('creditCardType').value;
                	if (!checkCreditCard(value,myCardType)) {
                		 error_id  = this;
                         error_msg = ccErrors[ccErrorNo];
                	}
                }
            }
            if (error_id != "") {
                return false;
            }
        });
        
        if (error_id != "") {
            $("#"+error_id+"_msg").addClass("error");
            if (error_msg != "") {
                $("#"+error_id+"_msg").text(error_msg);
            }
            $("#"+error_id).focus();
            return false;
        } else {
            return true;
        }
    });

    $("#check_promo").click(function() {
        var code = trim($("#code").val());
        if (code != '') {
            $("#check_promo").hide();
            $("#check_promo_loader").show();

            $.ajax({
                type: "POST",
                url: "/account/check-promo",
                data: {code:code},
                dataType: 'json',
                success: function(msg){
                	if(msg){
                		var trialPeriod = msg.trial_period;
                        var trialAmt    = msg.trial_amount;
                        var allmonthAmt = msg.all_month_amount;
                        var details     = '';

                        if (parseInt(msg.times_used) < parseInt(msg.times_to_use)){
                        	if(trialPeriod != -1) {
                    			details += trialPeriod + ' day free trial';
                        	}
                        	if (trialAmt != -1) {
                                if (details.length) {
                                    details += ', ';
                                }
                                details += '$' + trialAmt + ' for trial period';
	       	                }
	       	                if (allmonthAmt != -1) {
                                if (details.length) {
                                    details += ', ';
                                }
	       	                	details += '$' + allmonthAmt + ' per month';
	       	                }
                        } else {
                        	details = "Invalid Code";
							$("#code").val('');
                        }
                	} else {
                		details = "Invalid Code";
						$("#code").val('');
                	}
                    
                    $("#promo-offer").html('<strong>'+details+'</strong>');
                    $("#check_promo_loader").hide();
                    $("#check_promo, #discount_price").show();
                }
            });
        } else {
            alert('Please enter Promotion code');
        }
    });

    $("#submit-email").click(function() {
        var useremail = trim($("#useremail").val());
        if (check_email(useremail)) {
            $("#email_loader").show();
            $(this).attr({disabled:"disabled"}).css({opacity: '0.3'});
            $.ajax({
                type: "POST",
                url: "/account/forgot-password",
                data: {useremail:useremail},
                dataType: 'json',
                success: function(msg){
					var addclass = "error";
                    $("#email_loader").hide();
                    if (msg.status) {
                        addclass = "sucess";
                    }
                    $("#useremail").val('');
                    $("#stat-msg").addClass(addclass).html(msg.message).show();
                    $("#submit-email").removeAttr("disabled").css({opacity: '1'});
                    setTimeout(function(){$("#stat-msg").hide();},5000);
                }
            });
        } else {
            alert("Please enter a valid email address");
        }
    });

});


function trim(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

function check_email(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (reg.test(email)) {
        return true;
    }
    return false;
}

