$(document).ready(function() {
  var params = getParams();

  if(params.reset) {
    $.cookie('password', null);
    $.cookie('userName', null);
  }
  if($.cookie('userName') && $.cookie('password')) {
    $('#password').val($.cookie('password'));
    $('#rememberMe').attr('checked', 'checked');
    $('#userName').val($.cookie('userName'));
  } else {
    $('#password').replaceWith('<input class="inp" id="password" name="password" type="text" value="password" />');
    $('#userName').val('email address');
  }

  $('#userName').focus(function() {
    if($(this).val() == 'email address') {
      $(this).val('').get(0).focus();
    }
  });

  $('#password').focus(function() {
    if($(this).attr('type') != 'password') {
      $(this).replaceWith('<input class="inp" id="password" name="password" type="password" value="" />');
      $('#password').get(0).focus();
    }
  });

  var dialogOptions = {
    buttons: { 'OK': function() { $(this).dialog('close') } },
    draggable: false,
    modal: true,
    resizable: false,
    title: ''
  };

  if($('#success').length > 0) {
    dialogOptions.title = 'Success';
    $('#dialog').text($('#success').text()).dialog(dialogOptions);
  } else if($('#error').length > 0) {
    dialogOptions.title = 'Error';
    $('#dialog').text($('#error').text()).dialog(dialogOptions);
  }

  $('#contactForm').submit(validateContactUsForm);

  updateFormActions();

	
});

function getParams() {
  var search = document.location.search.replace(/\?/,'').split('&');
  var params = {};
  if(search.length > 0 && search[0] != '') {
    for(var i = 0; i < search.length; i++) {
      var keyValuePair = search[i].split('=');
      params[keyValuePair[0]] = unescape(keyValuePair[1]);
    }
  }
  return params;
}

function updateFormActions() {
	// Switch MAINTENANCE to 'true' to redirect all necessary forms to a static maintenance page while web portal is unavailable.
	var MAINTENANCE = false;

  var host = document.location.hostname;
	var pathname, port, protocol;

  if(host == 'localhost' || host == '127.0.0.1') {
		protocol = 'http://';
		port = '8080';
  } else { //if (host == 'www.ecobee.com' || host == 'ecobee.com') {
		port = window.location.port;
		protocol = (port == '' ? 'https://' : (window.location.protocol == "https:" ? 'https://' : 'http://'));
  } /*else {
		protocol = 'https://';
		port = '';  
	}*/
	
	if(MAINTENANCE) {
    $('#forgotPasswordForm, #loginForm').attr('action', '/public/update.html');
		return;
	}

	//if(host != 'www.ecobee.com' && host != 'ecobee.com') {
    if($('#forgotPasswordForm').length > 0) {
			$('#forgotPasswordForm').attr('action', protocol + host + (port != '' ? ':' + port : '') + '/home/resetPassword/');
    }
    if($('#loginForm').length > 0) {
			$('#loginForm').attr('action', protocol + host + (port != '' ? ':' + port : '') + '/home/login/');
    }
	//}
}

function toogleDisplay(obj) {
  if(obj.style.display == "block" || obj.style.display == "") {
    obj.style.display = "none";
  } else {
    obj.style.display = "block";
  }	
}

function validateLoginForm() {
  setLoginCookie();

  return true;

  var username = document.getElementById('userName').value;
  var password = document.getElementById('password').value;

  if(username == '' || password == '') {
    return false;	
  }

  return true;
}

function validateContactUsForm() {
  $('#contactForm #submitButton').before('<input id="key" name="key" type="hidden" value="ecobeeKey" />');
}
function setLoginCookie() {
  if($('#rememberMe').is(':checked')) {
    $.cookie('password', $('#password').val(), { expires: 30, path: '/' });
    $.cookie('userName', $('#userName').val(), { expires: 30, path: '/' });
  } else {
    $.cookie('password', null, { path: '/' });
    $.cookie('userName', null, { path: '/' });
  }
}

