/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Cookie script - Scott Andrew */

function newCookie(name,value) {
  var days = 365;   // the number of days until the cookie expires, or
                    // false if you want just a session cookie (expires upon browser exit)
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  } else var expires = "";
  
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameSG = name + "=";

  if (document.cookie.indexOf(nameSG) == -1)
    return '';

  var ca = document.cookie.split(';');
  
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') { c = c.substring(1,c.length); }
    if (c.indexOf(nameSG) == 0)
      return c.substring(nameSG.length,c.length);
  }
  return '';
}

function eraseCookie(name) {
  newCookie(name,"");
}

function toMem() {
  // add a new cookie as shown for every field
  // you wish to have the cookie remember
  newCookie('login', document.forms[0].login.value);
  newCookie('remember_me', true);
}

function delMem() {
  // make sure to add the eraseCookie function for every field
  eraseCookie('login');
  eraseCookie('remember_me');
}


function remCookie() {
  if(readCookie('login') != '') {
    document.forms[0].elements['login'].value = readCookie('login');
    document.forms[0].elements['remember_me'].checked = readCookie('remember_me');
    return true;
  }
  return false;
}


// Perform actions on window load
window.onload = function() {
  if(document.forms[0].elements['company_id'] != null) {
    // focus on the company select field if it exists
    document.forms[0].elements['company_id'].focus();
  } else if(remCookie()) {
    // skip to password field if we found the login in our cookie
    document.forms[0].elements['password'].focus();
  } else if(document.forms[0].elements['email_address'] != null) {
    // focus on the email address field if it exists
    document.forms[0].elements['email_address'].focus();
  } else if(document.forms[0].elements['login'] != null) {
    // focus on the login field
    document.forms[0].elements['login'].focus();
  }
  
  jQuery.fn.populateFormNotify();
  
}

// Save cookie on form submit (if checked)
function setCookie() {
  if(document.forms[0].remember_me.checked) {
    toMem();
  } else {
    delMem();
  }
}




jQuery(function($) {    
  $.fn.populateFormNotify = function() {
  
    $('.prepopulate').each(function() {
      $this = $(this);
      if ($this.val()=='')
      {
        $this.formNotifier({
          className: 'formNotifier'
        });
      }
    });
  };
});
  
  
  
function updateOrientation()
{
  $('.prepopulate').trigger('remove.fn');
  jQuery.fn.populateFormNotify();
}