//<!-- begin script

function getCookieVal (offset) {
     var endstr = document.cookie.indexOf (";", offset);
     if (endstr == -1)
         endstr = document.cookie.length;
     return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
     var arg = name + "=";
     var alen = arg.length;
     var clen = document.cookie.length;
     var i = 0;
     while (i < clen) {
         var j = i + alen;
         if (document.cookie.substring(i, j) == arg)
             return getCookieVal (j);
         i = document.cookie.indexOf(" ", i) + 1;
         if (i == 0) break;
     }
    return null;
}

function SetCookie (name, value) {
     var argv = SetCookie.arguments;
     var argc = SetCookie.arguments.length;
     var expires = (argc > 2) ? argv[2] : null;
     var path = (argc > 3) ? argv[3] : null;
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
         ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
         ((path == null) ? "" : ("; path=" + path)) +
         ((domain == null) ? "" : ("; domain=" + domain)) +
         ((secure == true) ? "; secure" : "");
}

//function which sets values of the cookie if none exist. Resets the
//expiry date if the cookie already exists.
function SetOrUpdateCookie(intNoOfDays){
  infoValue = GetCookie("info");
  if (infoValue==null){             //no cookie already set
     FileNumber =Math.round(Math.random()*100000000);
     //now get the user name
     var msg = 'Please enter your name as it should appear in the shortlist';
     do
        userName = prompt (msg, 'Name')
     while (userName == null || userName == 'Name' || userName == "");
     userName = escape (userName);  //get rid of any spaces.
     expiryDate = new Date(new Date().getTime()+(intNoOfDays*24*60*60*1000));
     infoValue = FileNumber  + '#' + expiryDate + '$' + userName;
     SetCookie("info",infoValue,expiryDate); //set with the new File Name
     return "Add";
  }
  else{
     // A cookie is already set. Check to find out if the user wants to add to
     // the shortlist or just to view it.
     // Note that we're not going to change the expiry date of the cookie if
     // the user just wants to view it.
     msg = "Do you want to add the property you were viewing to your";
     msg += " shortlist or just view your current shortlist?\n\n";
     msg += "To add to your shortlist please click OK otherwise use the";
     msg += " Cancel button.";
     if ( confirm (msg) ) {
       //user wants to add therefore reset cookie text with new expiry date as well as
       //resetting the expiry date of the cookie
       expiryDate = new Date(new Date().getTime()+(intNoOfDays*24*60*60*1000));
       infoValue = GetFileNumber() + '#' + expiryDate + '$' + GetUserName();
       SetCookie("info",infoValue,expiryDate); //reset with new Expiry Date
       return "Add";
     } else {
       return "";
     }
  }
}

//function appendName(name){
//        if (document.cookie.length==0){         //no cookie set
//           alert ("there is no cookie set");
//           return false;
//        }
//        else if(name.length == 0){                      //no name entered in the box
//                alert ("There is no name entered in the box");
//                return false;
//        }
//        else{
//                cookieValue = GetCookie("info");
//                $Index = cookieValue.indexOf("$");
//                //check whether there is a name appended
//                 if ($Index > 0){                                                       //if a name is appended then
//                        cookieValue= cookieValue.substring(0,$Index);                   //delete the existing name
//                        cookieValue = cookieValue +"$" + name;                          //and then add the new name
//                        SetCookie("info", cookieValue, window.opener.expiryDate);       //resets with new name and amended expiry date
//                }
//		//append name
//                else{                                                                   //no name appended as yet
//                         cookieValue = cookieValue + "$" + name;                        //append the name
//                         SetCookie("info", cookieValue, window.opener.expiryDate);      //and set cookie with name and amended expiry date
//                }
//        window.close();
//        return true;
//        }
// }
//
//function resetName(){
//        window.open("NameInput.html","myName","top=300,width=400,height=150");
//}

function GetFileNumber(){
        strCookie = GetCookie("info");
        return strCookie.substring(0, strCookie.indexOf("#"));
}

function GetUserName(){
        strCookie = GetCookie("info");
        // Check that user allows cookies.
        if ( strCookie == null ) {
          var msg = 'You must allow Cookies for shortlists to be available.\n\n';
          msg += "For more information about Cookies please see your browser's Help File.";
          alert (msg);
          return 0;
        }
        return strCookie.substring((strCookie.indexOf("$") + 1), strCookie.length);
}

function GetExpiryDate(){
          strCookie = GetCookie("info");
          return strCookie.substring((strCookie.indexOf("#") + 1), strCookie.indexOf("$"));
}

function showDate(expiryDate){
        Months = new Array(12);
        Months[0] = "January";
        Months[1] = "February";
        Months[2] = "March";
        Months[3] = "April";
        Months[4] = "May";
        Months[5] = "June";
        Months[6] = "July";
        Months[7] = "August";
        Months[8] = "September";
        Months[9] = "October";
        Months[10] = "November";
        Months[11] = "December";
        with (expiryDate) {
          numMonth = getMonth();
          textDate = getDate() + ' ' + Months[numMonth] + ', ' + getFullYear();
        }
        return textDate;
}

//--> end script
