// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var LoginPage = {  
  initialize: function() {
    var params= window.location.href.toQueryParams();    
    if (params['r'] == 't') {
      $('login').show();
      $('subdomain_not_found').show();
      $('subdomain_message').hide();
    } 
    Event.observe($('subdomain'), 'blur', this.toggleLoginButton.bind(this));
    Event.observe($('subdomain'), 'keyup', this.toggleLoginButton.bind(this));
  },
  
  toggleLoginButton: function() {
    if ($F('subdomain') == '') {
     $('login_button').disabled = true; 
    } else {
      $('login_button').disabled = false; 
    }
  },
  
  sendToSubdomain: function() {
    var subdomain = $F('subdomain');
    window.location = "http://" + subdomain.gsub(/\.setstr\.com/,'') + ".setstr.com"
  }
}


var SignupPage = {  
  setupSubdomainPreview: function() {
    Event.observe($('subdomain'), 'blur', this.updateAccountSubdomain.bind(this));
    Event.observe($('subdomain'), 'keyup', this.updateAccountSubdomain.bind(this));
  },
  
  updateAccountSubdomain: function() {
    var subdomain = $F('subdomain').replace(/^www\./i, "");;
    $('subdomain').value = subdomain;
    if(subdomain.match(/^\s*$/)) subdomain = "YOURURL";
    $('account_subdomain').innerHTML = subdomain;
  },
  
  toggleSubmit: function(checkbox) {
    $('submit_button').disabled = !checkbox.checked;
  }
}

function validateEmpty(fld,name) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#FFECEC'; 
        error = name + "\n"
    } else {
        fld.style.background = '#FFFFFF';
    }
    return error;  
}

function validate_contact_sales_form(theForm) {
  var error_message = "";

  error_message += validateEmpty(theForm.first_name,'First Name');
  error_message += validateEmpty(theForm.last_name,'Last Name');
  error_message += validateEmpty(theForm.email,'Email');
  error_message += validateEmpty(theForm.company_name,'Company Name');
  error_message += validateEmpty(theForm.title,'Title');
  error_message += validateEmpty(theForm.phone,'Phone');
      
  if (error_message != "") {
    alert("Please fill in the following fields:\n" + error_message);
    return false;
  }

  return true;

}