
// Login username password fields
// don't use document.ready or $(function(){...} )  as chrome and ff mac attempt to fill in the fields with save password AFTER document.ready fires
// FIXED using autocomplete="off" attribute on form fields
$(window).load(function(){
  $("#login_email, #login_password")
    .focus(function(){
      $(this).prev().css("top", "-99em");
    })
    .blur(function(){
      if($(this).val() == "") {
        $(this).prev().css("top", "12px");
      }
    })
    .each(function(){
      this.defaultText = this.value;
      this.autocomplete="off";
      this.value = "";
      $(this).prev().css("top", "12px");
    });
});

  
  
function resize(value){
  createCookie('508',value,'30');
  setsize();
}

function setsize(){
  document.getElementById('main_wrapper').style.fontSize = readCookie('508');
}

function createCookie(name,value,days){
  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 nameEQ = name + "=";
  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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// generic multi use pop-up with pre-programmed 'type'
var newWin = null;
function popUp(strURL, strType, strWidth, strHeight, left, top) {
	if (newWin != null && !newWin.closed) newWin.close();
	var strOptions="";
	if (strType=="console") strOptions="resizable,location=0,status=0";
	if (strType=="fixed")   strOptions="location=0,status=0,height="+strHeight+",width="+strWidth;
	if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location";
	// position
  if(typeof(left) == 'undefined') {
    var left = (screen.width/2) - strWidth/2;
  }
  if(typeof(top) == 'undefined') {
    var top = (screen.height/2) - strWidth/2;
  }
  //alert(screen.width + ':' + screen.height)
 
	strOptions +=',width='+strWidth+',height='+strHeight+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	// open
	newWin = window.open(strURL, 'appWin', strOptions);
	newWin.focus();
}

		
/**
 function setCookie(name, value, expires) {
 document.cookie = name+"="+value+expires+"; path=/";
}  */

function setrawcookie (name, value, expires, path, domain, secure) {
//alert(name+' '+value+' '+typeof(expires)+' '+path+' '+domain+' '+secure);
    if (typeof expires === 'string' && (/^\d+$/).test(expires)) {
        expires = parseInt(expires, 10);
    } 
    if (expires instanceof Date) {
        expires = expires.toGMTString();
    } else if (typeof(expires) === 'number') {
        expires = (new Date(expires * 1e3)).toGMTString();    }
 
    var r = [name + '=' + value], s = {}, i = '';
    s = {expires: expires, path: path, domain: domain};
    for (i in s) {        if (s.hasOwnProperty(i)) { // Exclude items on Object.prototype
            s[i] && r.push(i + '=' + s[i]);
        }
    }
        return secure && r.push('secure'),
                    this.window.document.cookie = r.join(";"),
                        true;
}

/**
 * Delete a cookie by setting expiry to date in past
 * Requires path and domain arguments
 */
function deleteCookie(name, path, domain, secure) {
  if(typeof secure !== "undefined" && secure) {
    secure = "; secure";
  } else {
    secure = "";
  }
  //alert('Deleting cookie: name='+name+'; path='+path+'; domain='+domain+secure);
  document.cookie = name+"=; expires=Thu, 01-Jan-70 00:00:01 GMT; path="+path+"; domain="+domain+secure;
}

/**
 * On confirmation, log the user out, redirect parent window to logged out screen and close this window.
 */
function deleteLoginCookies() {
  // delete the session cookies
  for(var i = 0, j = Joomla.cookies.length; i<j; i++) {
    deleteCookie(Joomla.cookies[i].name, Joomla.cookies[i].path, Joomla.cookies[i].domain, Joomla.cookies[i].secure);
  }
}


