function clearOldCookie()
{
     //return;
     var expDate = new Date();
     expDate.setTime(expDate.getTime()-24*3600*1000);//hoping this will give us date in the past

     //check if there are more than one entry in cookies for a given cookie name 
     //and clear wrong one:
     if(document.cookie.length)
     {
       tmp=new String;
       tmp=document.cookie;
       //alert(tmp);
     //with(tmp)
     {
       if(tmp.lastIndexOf("CAMPWD") != tmp.indexOf("CAMPWD")){
         Set_Cookie("CAMPWD","", expDate); //This is old cookie - without path and host specified
	 alert('Deleting old password');
	} 
       if(tmp.lastIndexOf("CAMUSR") != tmp.indexOf("CAMUSR")) {
         Set_Cookie("CAMUSR","", expDate); //This is old cookie - without path and host specified
	 alert('Deleting old user');
	} 
       if(tmp.lastIndexOf("CAMSPWD") != tmp.indexOf("CAMSPWD")) {
         Set_Cookie("CAMSPWD","", expDate); //This is old cookie - without path and host specified
	 alert('Deleting old save flag');
	} 
     }
     }
     
     //else
     //{
        //alert('No cookie property');
	//m=new String;
        //for(i in document)
	//   m=m+' '+i;
	//alert(m);
     //}
}

// Ted41
// Need to provide storage of UserName & SavePwd cookies between sessions.
// For that make these cookies "unexpired".
// UserPassword cookie need to be stored only in case SavePwd flag is true
function onND_Logon()
{
     clearOldCookie()
     
     if(!ND_Check_Logon_Data())
         return;

     var pwdExpDate = null
     var expDate = new Date(2020, 11, 31);
     
     if(document.forms['logonForm'].savePwd.checked)
         pwdExpDate = new Date(2020, 11, 31);

     //alert(location.hostname);
     //m="";
     //for(i in location)
     //  m=m+i+" ";
       
     //alert(m);  
     Set_Cookie("CAMUSR",document.forms['logonForm'].user.value, expDate, "/artserver");
     //cg031126
     if(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/.test(location.hostname))
     {
      Set_Cookie("CAMPWD",document.forms['logonForm'].pwd.value, pwdExpDate, "/artserver"); 
     }
     else
     {
      Set_Cookie("CAMPWD",document.forms['logonForm'].pwd.value, pwdExpDate, "/artserver" , location.hostname);
     }
     Set_Cookie("CAMSPWD",document.forms['logonForm'].savePwd.checked, expDate, "/artserver");
     document.location="ARTIndex.html";
}

//--------------------------------------------------------------------------------------  
// Ted41
function ND_Check_Logon_Data()
{
    var sUsrName = document.forms['logonForm'].user.value;
    var sUsrPwd = document.forms['logonForm'].pwd.value;
    
    if(sUsrName==null || sUsrName=="")
    {
        alert("User name is not specified.");
	return false;
    }

    if(sUsrPwd==null || sUsrPwd=="")
    {
        alert("Password is not specified.");
	return false;
    }
    return true;
}

//--------------------------------------------------------------------------------------  
//--------------------------------------------------------------------------------------  
function clearfCookie(p)
{
     clearOldCookie();

     var expDate = new Date();
     expDate.setTime(expDate.getTime()-24*3600*1000);//hoping this will give us date in the past

     Set_Cookie("CAMPWD","", expDate, "/artserver",location.hostname);
     if(p)
     {
       location.replace(p+"?skip_logon="); //param added because Explorer for Mac won't reload page if the URL is the same
     }
}

