//new u can put in separate js file
var main_url = "http://www.herotopia.com/";
var main_url_https = "https://www.herotopia.com/";


// wait for the DOM to be loaded
$(document).ready(function() {
    var options = {
        target:'#errorBox',
        dataType: 'json',
        beforeSubmit:  showLoading,
        success: showResponse
    };

    //clear default text from fields on focus
    $("input[type='text']").focus(function() {
        if( this.value == this.defaultValue ) {
            this.value = "";

            if (this.className=="inputFieldMedium pass"){
                this.type='password';
            }
        }

    }).blur(function() {
        if( !this.value.length ) {
            this.value = this.defaultValue;

            if (this.className=="inputFieldMedium pass"){
                this.type='text';
            }
        }
    });

    //this forms are using ajax submit
    $('#promo').ajaxForm(options);
    $('#register').ajaxForm(options);
    $('#login').ajaxForm(options);
    $('#lostpassword').ajaxForm(options);
    $('#changepassword').ajaxForm(options);
    $('#resetpassword').ajaxForm(options);
    $('#captchafrm').ajaxForm(options);
    $('#contact').ajaxForm(options);
    $('#cc').ajaxForm(options);
    $('#pbc').ajaxForm(options);
    $('#pay').ajaxForm(options);


    $("#errorBox").click(function(){
        $('#errorBox').hide();
    });
	$(".contactOk").click(function(){
        $(this).parent().hide();
    });

   //can we and shoul we listen on this class always!?
//           $(".buttonContinue").click(function(){
//              return false;
//           });

    //close popup
    $(".closePopup").click(function(){
        $('.popupWin').hide('slow');
    });

});

function goPay(){
    $("#pay").submit();
}

function checkUsername(){
    var username = $("#username").val();

    $.getJSON(main_url_https+"members/isavailable", { u: username },
      function(data){
        if(data.status){
          $("#errorBox").addClass("Okbox");
        }
        else {
          $("#errorBox").removeClass("Okbox");
        }
        $("#errorBoxMSG").html(data.message);
        $('#errorBox').show();
      });
}

function countryChanged(){
    var c = $("#country").val();
    if (c=='US'){
        $("#lbstate").show();
        $("#stateProvince").show();
    }
    else{
       $("#stateProvince").hide();
       $("#lbstate").hide();
    }
}

function showResponse(responseText, statusText){
$("#waitLoader").hide();
    //clear previous error classes
    $("#lbusername").removeClass("errorLabel");
    $("#lbgirl").removeClass("errorLabel");
    $("#lbboy").removeClass("errorLabel");
    $("#lbpass").removeClass("errorLabel");
    $("#lbpass2").removeClass("errorLabel");
    $("#lbpass3").removeClass("errorLabel");
    $("#lbemail").removeClass("errorLabel");
    $("#lbemail2").removeClass("errorLabel");
    $("#lbcap_input").removeClass("errorLabel");

    //contact forms
    $("#lbname").removeClass("errorLabel");
    $("#lbphone").removeClass("errorLabel");
    $("#lbinvoice").removeClass("errorLabel");
    $("#lbsuperheroname").removeClass("errorLabel");
    $("#lbusrmessage").removeClass("errorLabel");

    //cc form
    $("#lbsurname").removeClass("errorLabel");
    $("#lbccno").removeClass("errorLabel");
    $("#lbcvv").removeClass("errorLabel");
    $("#lbaddress").removeClass("errorLabel");
    $("#lbcity").removeClass("errorLabel");
    $("#lbstate").removeClass("errorLabel");
    $("#lbzip").removeClass("errorLabel");
    $("#lbcountry").removeClass("errorLabel");


    $("#errorBox").hide();

    var status = responseText.status;
    var msg = responseText.message;
    var url = responseText.url;
    var redir = responseText.redirect; //1 or 0

    //alert('Status '+status+' MSG: '+msg);

    if (!status){
        if(typeof(msg)=='object'){
            fieldErrors(msg);
        }
        else {
            $("#errorBox").removeClass("Okbox");
            $("#errorBoxMSG").html(msg);
            $("#errorBox").show();
            $.scrollTo("#errorBox",1000, {axis:'y'} );
        }
    }
    else {
        if (redir==0){
            $("#errorBox").addClass("Okbox");
            $("#errorBoxMSG").html(msg);
            $("#errorBox").show();
            $.scrollTo("#errorBox",1000, {axis:'y'} );
        }
        if (redir==1){
            window.location = main_url+url;
        }
    }
}

//invoked when some field isn't correctly filed'
function fieldErrors(msg){
$("#waitLoader").hide();
    var username = msg.username;
    var gender = msg.gender;
    var pass = msg.pass;
    var pass2 = msg.pass2;
    var pass3 = msg.pass3; //in password change
    var email = msg.email;
    var email2 = msg.email2;
    var message = msg.message;

    //contact forms
    var name = msg.name;
    var phone = msg.phone;
    var invoice = msg.invoice;
    var superheroname = msg.superheroname;
    var usrmessage = msg.usrmessage;

    //cc form
    var surname = msg.surname;
    var ccno = msg.ccno;
    var cvv = msg.cvv;
    var address = msg.address;
    var city = msg.city;
    var state = msg.state;
    var zip = msg.zip;
    var country = msg.country;

    var cap_input = msg.cap_input; //for captcha

    var st = 'Type here';


    //$("#errorBox").html("CORRECT FIELDS MARKED IN RED");
    $("#errorBox").removeClass("Okbox");
    $("#errorBoxMSG").html(message);
    $("#errorBox").show();

    if (username=='' || username==null || username=='Your superhero name'){
        $("#lbusername").addClass("errorLabel");
    }

    if (gender=='' || gender==null){
        $("#lbboy").addClass("errorLabel");
        $("#lbgirl").addClass("errorLabel");
    }

    if (pass=='' || pass==null || pass==st){
        $("#lbpass").addClass("errorLabel");
    }

    if (pass2=='' || pass2==null || pass2==st ){
        $("#lbpass2").addClass("errorLabel");
    }

    if (pass3=='' || pass3==null || pass3==st){
        $("#lbpass3").addClass("errorLabel");
    }

    if (email=='' || email==null){
        $("#lbemail").addClass("errorLabel");
    }

    if (email2=='' || email2==null){
        $("#lbemail2").addClass("errorLabel");
    }

    if (cap_input=='err'){
        $("#lbcap_input").addClass("errorLabel");
        refreshCaptcha(); //call the function to refresh captcha
    }

    //contact forms
    if (name=='' || name==null){
        $("#lbname").addClass("errorLabel");
    }
    if (phone=='' || phone==null){
        $("#lbphone").addClass("errorLabel");
    }
    if (invoice=='' || invoice==null){
        $("#lbinvoice").addClass("errorLabel");
    }
    if (usrmessage=='' || usrmessage==null){
        $("#lbusrmessage").addClass("errorLabel");
    }
    if (superheroname=='' || superheroname==null){
        $("#lbsuperheroname").addClass("errorLabel");
    }

    //cc forms
    if (surname=='' || surname==null){
        $("#lbsurname").addClass("errorLabel");
    }
    if (ccno=='' || ccno==null){
        $("#lbccno").addClass("errorLabel");
    }
    if (cvv=='' || cvv==null){
        $("#lbcvv").addClass("errorLabel");
    }
    if (address=='' || address==null){
        $("#lbaddress").addClass("errorLabel");
    }
    if (city=='' || city==null){
        $("#lbcity").addClass("errorLabel");
    }
    if (state=='' || state==null){
        $("#lbstate").addClass("errorLabel");
    }
    if (zip=='' || zip==null){
        $("#lbzip").addClass("errorLabel");
    }
    if (country=='' || country==null){
        $("#lbcountry").addClass("errorLabel");
    }

    $.scrollTo("#errorBox",1000, {axis:'y'} );
}

function showLoading(){
    //$("#errorBoxMSG").html("Please wait!");
	//$("#errorBoxMSG").show();
$("#waitLoader").show();
}




//age editing
function agechange(){
        $("#age").hide();
        $("#ageinput").show();

        $("#agedit").hide();
        $("#agesave").show();
}

function agesave(){
    $("#accupd").submit();
}

function genderchange(){
        $("#gender").hide();
        $("#genderinput").show();

        $("#genderedit").hide();
        $("#gendersave").show();
}

function gendersave(){
    $("#accupd").submit();
}

//ajax captcha refresh fix the absoulute path problem!
function refreshCaptcha(){
     $.getJSON(main_url+"members/activation", { aj: 'yes' },
      function(data){
        //alert(data.captcha);
        $("#capid").val(data.captcha);
        $("#cappic").attr("src",main_url+"captcha/"+data.captcha+".png"); //captcha image
      });
}


