var FG = FCG.field.good;
var FL = FCG.field.loading;
var FB = FCG.field.bad;
function enableSubmit(checkbox) {
  $('submit_and_create').disabled = !checkbox.checked;
}

function placeOrder() {
  $('submit_and_create_div').hide();
  $('ordering').show();
}

function update_url(){
  var cdi = $("company_domain_id");
  $('url_name').update($F('company_url') + "." + cdi.options[cdi.selectedIndex].text);
}

function fixUrl() {
  var el = $('company_url');
  el.value = $F(el).replace(/^www\./i, "").toLowerCase();
  return el;
}

function companyNameToUrl() {
  $('company_url').value = $F('company_name').replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
}

function checkDomainAndUrl() {
  var el =  fixUrl();
  FL("company_url_status");
  if ($F(el).length > 1){
    var callback = {
      success: function(r){
        update_url();
        FG($(el).id+"_status");
      },
      failure: function(r){
        if (parseInt(r.status) == 400){
            if (r.responseText == "unavailable"){
              FB($(el).id+"_status", "Unavailable. Please try another name.");
              el.focus();
            } else if (r.responseText == "reserved"){
              FB($(el).id+"_status", "Reserved. Please try another name.");
              el.focus();
            }
        } else {
          FCG.message.set('The site is busy.', 'bad');
        }
      }
    }; 
    var cObj = $C.asyncRequest('GET', "/account/domain_available?url="+$F(el)+"&domain="+$F("company_domain_id"), callback);    
  }
}

function checkEmail() {
  var el =  $("account_email");
  FL(el.id+"_status");
  var callback = {
    success: function(r){
      FG($(el).id+"_status");
    },
    failure: function(r){
      if (parseInt(r.status) == 400){
          if (r.responseText == "unavailable"){
            FB($(el).id+"_status", "Taken. Try another.");
          }
      } else {
        FCG.message.set('The site is busy.', 'bad');
      }
    }
  }; 
  var cObj = $C.asyncRequest('GET', "/account/email_available?email="+$F(el), callback);
}


function checkLogin() {
  var el =  $("account_login");
  FL(el.id+"_status");
  var callback = {
    success: function(r){
      FG($(el).id+"_status");
    },
    failure: function(r){
      if (parseInt(r.status) == 400){
        if (r.responseText == "unavailable"){
          FB($(el).id+"_status", "Taken. Try another.");
        }
      } else {
        FCG.message.set('The site is busy.', 'bad');
      }
    }
  }; 
  var cObj = $C.asyncRequest('GET', "/account/login_available?login="+$F(el), callback);
}

function isAlphaNum(el){
  var value = el.value.replace(/[. ]/g, "");
  if (FCG.validate.alphanum(value)){
    FG(el.id+"_status");
  } else {
    FB(el.id+"_status", "must be alphanumeric");
  }
}

function isAlpha(element){
  var value = element.value.replace(/[. ]/g, "");
  if (FCG.validate.alpha(value)){
    return true;
  } else {
    return false;
  }
}

function isEmail(element){
  if (FCG.validate.email(element.value)){
    return true;
  } else {
    return false;
  }
}

function validateForm(){
  var spanVal = $$("span[rel=validate]");
  var spans = $$("span.fieldGood[rel=validate]");
  if (spans.length == spanVal.length){
    return true;
  } else {
    return false;
  }
}

FCG.page = function () {
  var start = function(){
    $('company_name').focus();
    
    var borderReason = RUZEE.ShadedBorder.create({ corner:12, shadow:24,  border:1 });
    borderReason.render($$('div#bd div.reasons'));
  };
  
  return{
    init:function(){
      $E.onDOMReady(start);
      
      $E.on('company_name', 'change', function(e) {
        companyNameToUrl();
        update_url();
      });

      $E.on('company_time_zone', 'change', function(e) {
        var el = $E.getTarget(e);
        var value = el.value;
        if(value.length > 3){
          FG(el.id+"_status");
        } else {
          FB(el.id+"_status", "Select time zone.");
        }
      });

      $E.on('company_name', 'blur', function(e) {
        companyNameToUrl();
        update_url();
        var el = $E.getTarget(e);
        var value = el.value.replace(/[^a-zA-Z0-9]/g, "");
        if(value.length >= 3){
          checkDomainAndUrl();
          FG(el.id+"_status");
        } else {
          FB(el.id+"_status", "Not valid");
        }
      });

      $E.on(['account_first_name','account_last_name'], 'blur', function(e) {
        var el = $E.getTarget(e);
        var value = el.value.strip();
        if(value.length >= 3){
          FG(el.id+"_status");
        } else {
          FB(el.id+"_status", "Not valid (3-30 Characters)");
        }
        $E.preventDefault(e);
      });

      $E.on('company_url', 'blur', function(e) {
        fixUrl();
        update_url();
        var el = $E.getTarget(e);
        var value = el.value.strip().replace(/[^a-zA-Z0-9]/g, "");
        if(value.length >= 3){
          FG(el.id+"_status");
        } else {
          FB(el.id+"_status", "Not valid (3-24 Characters)");
        }
        $E.preventDefault(e);
      });

      $E.on('account_login', 'blur', function(e) {
        var el = $E.getTarget(e);
        var value = el.value.strip().replace(/[^a-zA-Z0-9]/g, "");
        if(value.length >= 3 && value.length <= 40){
          checkLogin();
        } else {
          FB(el.id+"_status", "Not valid (3-40 Characters)");
        }
        $E.preventDefault(e);
      });

      $E.on('account_password', 'blur', function(e) {
        var el = $E.getTarget(e);
        var value = el.value.strip();
        if(value.length >= 4 && value.length <= 40){
          if ($("account_password_confirmation").value.length >= 4){
            if(value === $("account_password_confirmation").value ){
              FG(el.id+"_status");
            } else {
              FB(el.id+"_status", "Don't match");
            }
          }
        } else {
          FB(el.id+"_status", "Not valid (4-40 Characters)");
        }
        $E.preventDefault(e);
      });

      $E.on('account_password_confirmation', 'blur', function(e) {
        var el = $E.getTarget(e);
        var value = el.value.strip();
        if(value.length >= 4 && value.length <= 40){
          if(value === $("account_password").value ){
            FG("account_password_status");
          } else {
            FB("account_password_status", "Don't match");
          }
        } else {
          FB("account_password_status", "Not valid (4-40 Characters)");
        }
        $E.preventDefault(e);
      });

      $E.on('account_email', 'blur', function(e) {
        var el = $E.getTarget(e);
        if (isEmail(el)){
          checkEmail();
        } else {
          FB(el.id+"_status", "Not valid");
        }
        $E.preventDefault(e);
      });

      $E.on('company_domain_id', 'change', function(e) {
        update_url();
        checkDomainAndUrl();
      });

      $E.on('company_url', 'change', function(e) {
        fixUrl();
        checkDomainAndUrl();
      });


      $E.on("account_form",'submit', function(e){
        //validate time zone
        if($F('company_time_zone').length > 3){
          FG("company_time_zone_status");
        } else {
          FB("company_time_zone_status", "Select time zone.");
        }
        //validate the rest
        if (!validateForm()){
          FB("account_form_status", "The form is incomplete.");
          $E.stopEvent(e);
        } else {
          placeOrder();
        }
      });
    }
  };
}();
FCG.page.init();