var RivalMap = {
  init: function() {
     Setup.common();
     Setup[document.body.id] && Setup[document.body.id]();     
  }
}

var Setup = {
	common: function() {
		$('#cancel-link').click(function(e) {
			$('#login-modal').trigger('reveal:close');
			return false;
		});
		LoginPage.init();
	},
	home: function() {

	},
	features: function() {      
	}
}

$(function(){
  RivalMap.init();
});

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.phone,'- Phone');
  error_message += validateEmpty(theForm.company_name,'- Company Name');
  error_message += validateEmpty(theForm.title,'- Title');
  error_message += validateEmpty(theForm.employees,'- Number of employees');    
      
  if (error_message != "") {
    alert("Please fill in the following fields:\n\n" + error_message + '\nThanks!');
    return false;
  }

  return true;

}

var LoginPage = {  

  init: function() {
    if ($.query.get('r') == 't') {
      $('#login-modal').reveal();			
      $('#subdomain_not_found').show();
    }
    $('#subdomain').bind("blur, keyup", function() {
      LoginPage.toggleLoginButton();
    });
  },

  toggleLoginButton: function() {
    if ($('#subdomain').val() == '') {
     $('#login_button').attr('disabled', 'disabled');
    } else {
      $('#login_button').removeAttr('disabled');
    }
  },

  sendToSubdomain: function() {
    var subdomain = $('#subdomain').val();
    window.location = "https://" + subdomain.replace(/\.rivalmap\.com/,'') + ".rivalmap.com";
  }

}
