// ================================================================== FILE:   filecabinet.js DESCR:   ================================================================== 
// global variables
fcWindow = new Object();
fcMailWindow = new Object();
mainWindowPage = new Object();

//-----------------------------------------//
//--- opens new window(assisting function)-//
//-----------------------------------------//
function openNewWindow(windowURL){  
// CHECK IF NEW WINDOW IS ALREADY OPEN // 
if (fcWindow.closed != false){
 fcWindow=window.open(windowURL, 'FCWindow','toolbar=yes,resizable=yes,scrollbars=yes,width=735,height=485,screenX=55,screenY=30');
 fcWindow.focus();}
else {
 fcWindow.location=windowURL; }
fcWindow.moveTo(55,30); 
fcWindow.focus();
}
//-----------------------------------------//
//--- opens new mail window(assisting function)-//
//-----------------------------------------//
function openNewMailWindow(windowURL){  
// CHECK IF NEW WINDOW IS ALREADY OPEN // 
if (fcMailWindow.closed != false){
  fcMailWindow=window.open(formURL, 'FCmailWindow','toolbar=no,resizable=yes,scrollbars=yes,width=640,height=380,screenX=100,screenY=80'); }
else {
 fcMailWindow.location=windowURL;}
fcMailWindow.moveTo(100,80); 
fcMailWindow.focus();
}

//-------------------------------------//
//--- saves a link in file cabinet---//
//-------------------------------------//
function filePageLinkInFC(isWindowOpen,pname,link){  
 if (document.fcform.UserID.value =="" || document.fcform.UserID.value =="File Cabinet") {
   //-- can result if user did not login or page did not finish loading
   //-- if page did not load get cookie
   var username=getCookie('file_cabinet_user');
   if (username=='' || username==null) {
     //-- ask to enter userName
     alert('To save this page to your file cabinet, there is a one-time registration. Please go to our File Cabinet Section from our home page. It is on the right navigation section". \r  \rOr if you are accessing this site from a computer other than your own, type in your log-in name in the field provided, on our File Cabinet Section');
     return; }
   else {
     document.fcform.UserID.value=username }
 }
 var uname=document.fcform.UserID.value;
 openNewWindow('/filecabinet.nsf/webFileLink?OpenAgent&UserID='+escape(uname)+'&LinkAlias='+escape(pname)+'&Link='+ escape(link)); 
}

//-----------------------------------------//
//--- runs onLoad event procedures      ---//
//-----------------------------------------//
function onLoadPage(){ 
 mainWindowPage=window;
 var username=getCookie('file_cabinet_user');
 if (username=='' || username==null) {
   document.fcform.UserID.value='File Cabinet'; }
 else {
   document.fcform.UserID.value=username; }
}
//-----------------------------------------//
//--- opens a fc link in mainWindowPage   -//
//-----------------------------------------//
function openLinkPage(pageLink){
   if (mainWindowPage.closed==false) {
     mainWindowPage.location=pageLink;
     mainWindowPage.focus();}
   else {
   mainWindowPage=window.open(pageLink, 'mainWindowPage'); }
}

//-----------------------------------------//
//--- runs onUnload event procedures      -//
//-----------------------------------------//
function onUnloadPage() {
 if (fcMailWindow.closed == false) {
   fcMailWindow.close();
 }
}
//--------------------------------------------//
//----Opens file cabinet----------------------//
//--------------------------------------------//
function openFC(isWindowOpen){ 
 if (document.fcform.UserID.value =="" || document.fcform.UserID.value =="File Cabinet") {
   //-- can result if user did not login or page did not finish loading
   //-- if page did not load get cookie
   var username=getCookie('file_cabinet_user');
   if (username=='' || username==null) {
     // ask to enter userName
     alert('To save this page to your file cabinet, there is a one-time registration. Click "create file cabinet". \r  \rOr if you are accessing this site from a computer other than your own, type in your log-in name in the field provided.');
     return; }
   else {
     document.fcform.UserID.value=username }
}
if (isWindowOpen==false) {
   openNewWindow('/filecabinet.nsf/webOpenFileCabinet?OpenAgent&UserID='+escape(document.fcform.UserID.value));}
else {
   window.location ='/filecabinet.nsf/webOpenFileCabinet?OpenAgent&UserID='+escape(document.fcform.UserID.value);}
}

//------------------------------------//
//--- Creates user profile -----------//
//------------------------------------//
function openUserProfile(isWindowOpen){ 
 if (isWindowOpen==false) {
  openNewWindow('/filecabinet.nsf/userProfile?OpenForm');}
 else {
  window.location ='/filecabinet.nsf/userProfile?OpenForm';}
}
//-----------------------------------------------------//
//----- opens mail entry form--------------------------//
//-----------------------------------------------------//
function openMailEntryForm(pname,link,PageTitle){ 
 if (window.name!="FCWindow") { // the fuction was called from general page
    // check if user entered login name
    //if (document.fcform.UserID.value =="" || document.fcform.UserID.value =="File Cabinet") {
    //  alert('Please enter your file cabinet login name.') }
    //else {
     qString='&LinkAlias='+escape(pname)+'&Link='+ escape(link)+'&PageTitle='+ escape(PageTitle); 
     formURL='/filecabinet.nsf/mailFriend?OpenForm' + qString; 
 } 
 else { // the function called from fc page
    formURL='/filecabinet.nsf/mailFriend?OpenForm'; 
 }
 openNewMailWindow(formURL);
}

//------------------------------------//
//--- Retrieves user id --------------//
//------------------------------------//
function forgotUserName(isWindowOpen){  
 if (isWindowOpen==false) {
  openNewWindow('/filecabinet.nsf/forgotUserID?OpenForm');}
 else {
  window.location ='/filecabinet.nsf/forgotUserID?OpenForm';}
}

//----------------------------------------------------------//
// 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");
}
//--------------------------------------------//
// 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 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-70 00:00:01 GMT";
  }
}
