/***********************************
   Functions for handling cookies
***********************************/
// Heinle's function for retrieving a cookie.
function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length >0 ) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    } 
  }
  return null;
}

// An adaptation of Dorcht's function for setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu,01 -Jan-00 70:00: 01GMT";
  }
}

//set expiration date 1 year ahead
var expiration = new Date();
expiration.setTime(expiration.getTime() +31536000000 );

/**********************************************
   Check our cookie. Set it to no if it's null.
**********************************************/
var regflag = getCookie('registered');
if (regflag == null) {
	setCookie('registered', 'no', expiration, "/centers-institutes/iwsaw/", null, null);
}

/**********************************************
   Now let's redirect the user to either the
   registration page, or the requested file
**********************************************/
var x = '';

function redirUser(filepath) {
	if (filepath.indexOf('main-with') == -1) { // if they requested a PDF, as opposed to an index page
		x = 'http://inhouse.lau.edu.lb/iwsaw/' + filepath;
	}
	else { // if they requested an index page
		x = '/centers-institutes/iwsaw/' + filepath;
	}
	setCookie('requestedFile',x,null,'/centers-institutes/iwsaw/',null,null);
	if (regflag == "yes") { document.location.href = x; }
	else { document.location.href = "/centers-institutes/iwsaw/raidareg.html"; }
}
