// JavaScript Document// Test whether the user has cookies enabled by setting a cookie
	//  and then trying to retrieve it back.  If a "true" argument is passed
	//          then this function attempts to test whether persistant cookies
	//          (i.e. non session cookies) are enabled.

	function testClientCookies() {
            
            var testPersistant = false;            
            if (arguments.length > 0) testPersistant = arguments[0];            
            if (testPersistant) {

                        // save a new cookies with an expiration date set to 5 seconds from now
                        var now = new Date();
                        var cookiedate = new Date(now.valueOf() + 1000*5);
                        document.cookie = "cookieTest=persist;expires=" + cookiedate.toGMTString();
            }

            else {
                        // save a session cookie (no expiration specified = lives for the
                        //          lifetime of the browser) so that we check for any kind of cookies
                        document.cookie = "cookieTest=session"
            }

            

            if ((document.cookie).indexOf("cookieTest") == -1) 
            {
                  // cookie not found; all cookies must be disabled
                  return false;
            }
            else 
            {
                   return true;
            }

	}  // testClientCookies

 

	// function of client-side check(s) that are made when the user tries to log in

	function doLoginTimeChecks() {

            // test if Session-cookies (at least) are enabled
            if (testClientCookies()) {

                        document.forms['loginForm'].cookieTest.value = '1';

            }
            
            // we cannot test if JavaScript is enabled (because the test itself would
            //          have to be written in JavaScript) but we can set a variable that will
            //          be looked at by login.asp when the user submits the form

            document.forms['loginForm'].JavaScriptTest.value = '1';


            var loginbox = document.getElementById('login');
            var chkremember = document.getElementById('rememberMe');
            if ((loginbox != null) &&  (chkremember  != null))
            { 
                    var theDate = new Date();                                   

                if (document.forms['loginForm'].rememberMe.checked == true)
                {                     
                     // set cookie expires 3 months later
                     var threeMonthsLater = new Date( theDate.getTime() + 7776000000 );  
                     var expiryDate = threeMonthsLater.toGMTString();
                     document.cookie = 'login=' + loginbox.value + '; expires=' + expiryDate;
                }                 

                else

                { 
                     var passDate  =  new Date( theDate.getTime() - 7776000000 );
                     var expiryDate2 =  passDate.toGMTString();
                     document.cookie = 'login=' + loginbox.value + '; expires=' + expiryDate2;                    
                }                              
            } 


            return true;

	}

     // Cookie reader
    function readCookie(name)
    {
       var cookieValue = "";
       var search = name + "=";
       if(document.cookie.length > 0)
       { 
          offset = document.cookie.indexOf(search);
          if (offset != -1)
          { 
             offset += search.length;
             end = document.cookie.indexOf(";", offset);
             if (end == -1) end = document.cookie.length;
             cookieValue = unescape(document.cookie.substring(offset, end))
           }
       }

       return cookieValue;
     }

     
     // Querystring reader
     function getQuerystring(key, default_)
     {
       if (default_==null) default_=""; 
       key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
       var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
       var qs = regex.exec(window.location.href);
       if(qs == null)
         return default_;
       else
         return qs[1];
      } 



	//  If you use "Remember Me" checkbox along with login / password, this section is needed.
	//  This function will be called 
	//

	function rememberMeClick() {
		if (document.forms['loginForm'].rememberMe.checked == true) {
			// first check if cookies are enabled at all so that we don't
			//	inaccurately claim that "only session cookies are enabled"
			if (!testClientCookies) {
				window.location = '/yourloginpagehere.xxx?m=cookies_off&browser=' + browserCode;
				return;
			}
			
			// check for non-session cookie (pass true as argument)
			if (!testClientCookies(true)) {
				alert('In order to take advantage of Alarm.com\'s "Remember My Login" feature, you must allow cookies to be stored on your computer.  Visit the Alarm.com FAQ for more information.');
				document.forms['loginForm'].rememberMe.checked = false;
			}
		}
	}
  
   
    //  If you use "Remember Me" checkbox along with login / password, this section is needed.
	//  This function will be called 
	//
   function LoadRememberedLoginName()
   {

      var login = ''
      login = readCookie('login')
    
	  if (login != '') 
	  {	   
	    var loginbox = document.getElementById('login')
	    if (loginbox != null) 
	    { 
	     loginbox.value = login;
	    } 
	    
	    var chkRemember = document.getElementById('rememberMe')
	    if (chkRemember != null)
	    {
	     chkRemember.checked = true; 
	    }
	}
   }
       

   //
   // Error message
   //  
   // !! You can customize your own error message here.
   //   

   function showAlarmLoginMessage()
   {          
     var Qm = getQuerystring("m");      
     if (Qm == null) return;
     
     var msg, errorReason;    
     msg = "";
     switch(Qm)
     {
        case "bad_permission":
          msg = "I'm sorry, you tried to reach a page or an object that you do not have permission to access. If you reached this page via a bookmark, please delete your bookmark, navigate to the page that you are trying to reach, and then re-bookmark the page. If you reached this page by manually typing in a URL, you may have entered an incorrect number or the number may have changed in our records. We encourage users to use the Alarm.com site links rather than typing in URLs for this reason."
          break; 
        case "cookies_off":
		  msg = "Cookies are not enabled. Your browser must have cookies enabled to maintain your secured identity while you are logged in." 
		  break;     
        case "logout":
          msg = "You have successfully logged out."
          break;          
        case "no_session":
		  msg = "I'm sorry, your session has automatically timed out as a security precaution - please log in again."
	      break; 		
       	case "scripting_off":
		  msg = "Your browser's current settings prevent JavaScript from working correctly. Please enable JavaScript."
		  break;	
		case "sensors_cmd_sent":
		  msg = "A request has been sent to retrieve an up-to-date sensor list. After a few moments, please try to log in again."
		  break;	
		case "web_error":
		  msg = "An unexpected error occurred. You have been automatically logged out. If the problem persists, please contact us for help."
		  break;
		case "login_fail":
		  	errorReason = getQuerystring("r")										
			if (errorReason == "lockout")
			{ 
			  msg = "Your account has been locked due to too many incorrect password attempts. Request a new password by clicking <a href='https://www.alarm.com/forgot_logininfo.asp'>here."
			}			
			else if (errorReason == "terminated") 
			{
              msg = "Your account has been terminated. If this is an error, or if you would like to re-activate your account, please contact us."
			}
			else if (errorReason == "no_panel")
			{	
		      msg = "Your control panel hasn't communicated with Alarm.com. Turn your control panel on then try again. For more help, please contact us."
			}
			else if (errorReason == "error")						
			{
			  msg = "Your account is unavailable. A message has been sent to Technical Support. We will contact you when the issue has been resolved. " 
            }
            else if (errorReason == "custom")
            {
			  msg =  getQuerystring("cm")
			}  
			else if (errorReason == "no_match")
			{
			  msg = "Incorrect username/password"
			}		 
		  break;	  	  
        default:         
          break;   
      }    
      var divMsg = document.getElementById("divAlarmMessage");
      if (divMsg != null)
      {
           divMsg.innerHTML = msg;
      }   
   }
   
    function submitenter(myfield,e)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;

		if (keycode == 13)
   		{
   			doFormSubmit();
   			return false;
  		 } else {
   			return true;
		 }
	}
   
   function doFormSubmit() {
	   doLoginTimeChecks();
	   document.loginForm.submit();
   }