 window.onload=StartUp;
 
 var UNITIDLENGTH=4;
 
 
 var UnitID=[];
 var LeaseName=[];
 var SessionID="";
 var CookieIn;
 
 
 
 
     
  ////////////////////////////////////////////////////////////////////////////
  
  function Trim(str) {
  
     var out = str.replace(/^\s+|\s+$/g, '') ;
     return out;
 
 }
  
 
  /////////////////////////////////////////////////////////////////////////////
      
      function CreateGUID()
      {
         var guid, i, j;
         guid = "";
         for(j=0; j<32; j++){
            if( j == 8 || j == 12|| j == 16|| j == 20) guid+= "-";
            i = Math.floor(Math.random()*16).toString(16).toUpperCase();
            guid += i;
         }
         
        return guid;      
 } 
 
 
 
//////////////////////////////////////////////////////////////////////////////

   function GetParamFromThisURL(ParameterIn)
      {
        var name;
        var ParameterOut;
        var ParamList;

        name=ParameterIn.toLowerCase();
        ParamList= window.location.href.toLowerCase();

        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( ParamList );
        if( results == null) {
          ParameterOut="";
        } else {
          name=results[1];
          ParameterOut=CleanUpParameterIn(name);
        }

        return ParameterOut;
    }


 ///////////////////////////////////////////////////////////////////////////

   function CleanUpParameterIn(ParamIn) {
       	if (ParamIn =="") {
       	  return("");
       	}
       	ParamIn=ParamIn.replace(/\%20/g," ");
       	ParamIn=ParamIn.replace(/\%22/g,"");
       	return (ParamIn);
   }
   

/////////////////////////////////////////////////////////////////////////////
function AddUnit() {
     var UnitIDIn="";
     var LeaseNameIn="";
    
     
     UnitIDIn=document.AddForm.NUID.value;
     LeaseNameIn=document.AddForm.NLN.value;


   if (UnitIDIn.length==UNITIDLENGTH-1) UnitIDIn="0"+UnitIDIn;

     if (UnitIDIn.length<UNITIDLENGTH) {
         alert("Unit ID MUST be "+ UNITIDLENGTH + " digits long.");
         RefreshListDisplay();
         return;
     }
     
  
     var i=UnitID.length;
     UnitID[i]=UnitIDIn;
     LeaseName[i]=LeaseNameIn;
     WriteCookie();
     RefreshListDisplay();
     

}


//////////////////////////////////////////////////////////////////////////
function RefreshListDisplay() {
    var ListOut="";
    var i;
    
    if (UnitID.length<1) {
       document.getElementById("UnitList").innerHTML="<b>No Items Found.</b>";
        document.AddForm.NUID.value="";
        document.AddForm.NLN.value="";
        return;
    }
    
    for (i=0;i<UnitID.length;i++) {
        ListOut += "<input type=\"checkbox\" name=\"UnitIDList\" value=\"" + UnitID[i] + "\"/><b>Unit " + UnitID[i] + ":</b>  " + LeaseName[i] + "<br>";    
    }

    document.getElementById("UnitList").innerHTML=ListOut;
    document.AddForm.NUID.value="";
    document.AddForm.NLN.value="";
    
}


///////////////////////////////////////////////////////////////////////////////
function DeleteUnits() {
    var i;
    var ListLength=1;
    
    
    ListLength=UnitID.length;
    
    if (ListLength==0) return;
    
    if ((ListLength==1) &&(document.DelForm.UnitIDList.checked)) {
        UnitID=[];
        LeaseName=[];
        DeleteCookie();
       // console.log("Last Item Deleted.  Cookie Cleared.")
        RefreshListDisplay();
        return;
     }
    
    
    //start at end and work backwards to keep the index values
    //of individual items from changing 
    for (i=ListLength-1; i>=0; i--){
        if (document.DelForm.UnitIDList[i].checked){
            UnitID.splice(i,1);
            LeaseName.splice(i,1);
        }
    }
     WriteCookie();
     RefreshListDisplay();

}


///////////////////////////////////////////////////////////////////////////////

function WriteCookie() {
    var CookieOut="";
    var ExpireDate;
    var CookieList=[];
    var i;

    
    if (UnitID.length==0){
       DeleteCookie();
       SessionID="";
       return;
    }
    SessionID=CreateGUID();
    
    
    //set new cookie to expire 6 months from now.
    ExpireDate=new Date();
    ExpireDate.setMonth(ExpireDate.getMonth() + 6);
    

    for (i=0;i<UnitID.length;i++) {
        CookieList[i]=UnitID[i] + "=" + LeaseName[i];
    }

    CookieList.sort();


    for (i=0;i<UnitID.length;i++) {
        CookieOut += CookieList[i];
        if (i<UnitID.length-1) CookieOut += ";";
    }
    

    CookieOut = "UnitList=" + escape(CookieOut);
    CookieOut += ";expires=" + ExpireDate.toGMTString();
    document.cookie=CookieOut;
    
}


/////////////////////////////////////////////////////////////////////////////
function DeleteCookie() {
  var cookie_name= "UnitList";
  var cookie_date = new Date ( ); 
  //set expire date to 1 sec in the past.
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();

}


//////////////////////////////////////////////////////////////////////////////
function ParseCookie() {
    
   var Split0=[]; 
   var strTemp;
   var SplitList=[];
   var Split2=[];


   //Ovearall cookie consists of name=value.
   //the name is a constant -- "UnitList"
   //the value part is the SessionID and a list of unitIDs and lease name pairs
   //each pair divided by ;  
   //The individual items in each pair seperated by =
   Split0=CookieIn.split("=");
   strTemp=unescape(Split0[1]);
   

   
   
   SplitList=strTemp.split(";");
   
   
      SessionID="";

   
   //now grab the UnitID = LeaseName pairs.
   for (var i=0;i<SplitList.length;i++) {
        SplitList[i]=Trim(SplitList[i]);
        Split2=SplitList[i].split("=");
        if (Split2.length==2) {
           if (Split2[0].length>=UNITIDLENGTH)  {
              UnitID[i]=Split2[0];
              LeaseName[i]=Split2[1];
           }
           
        }
   }

}


function BackToMultiview() {
    location.replace("multiview.html")
}


///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
function StartUp() {
    
    //clear out the "Please Wait" message.
    document.getElementById("WaitMessage").innerHTML="<br>";
         
   
    UnitID=[];
    LeaseName=[];
    
    CookieIn=Trim(document.cookie);
    
    if (CookieIn.indexOf("undefined")>-1 ) {
      DeleteCookie();
      CookieIn="";
    }
 
    if (CookieIn==null || CookieIn=="") {
       document.getElementById("UnitList").innerHTML="<b>No Items Found.</b>";
       document.AddForm.NUID.value="";
       document.AddForm.NLN.value="";
       return;
    } else {
        ParseCookie();
        RefreshListDisplay();
    }
    
}

