// utility function to retrieve an expiration date in proper
// format; pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire (or negative
// values for a past date); all three parameters are required,
// so use zeros where appropriate
function getExpDate(days, hours, minutes) {
    var expDate = new Date( );
    if (typeof days == "number" && typeof hours ==  "number" && 
        typeof hours == "number") {
        expDate.setDate(expDate.getDate( ) + parseInt(days));
        expDate.setHours(expDate.getHours( ) + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
        return expDate.toGMTString( );
    }
}
   
// utility function called by getCookie( )
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}
   
// primary function to retrieve cookie by name
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 "";
}
   
// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
   
// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function trim(str) {
  while (str.charAt(str.length - 1)==" ")
    str = str.substring(0, str.length - 1);
  while (str.charAt(0)==" ")
    str = str.substring(1, str.length);
  return str;
}
function deleteAllCookies()
{
var strCookie='';
strCookie=document.cookie;
var arrCookie;
arrCookie=document.cookie.split(';');
var i=0;
var strTrpCookie;
strTrpCookie='';
for (i=0; i<arrCookie.length; i++) 
	{
      var strSingleCookie = trim(arrCookie[i]);
	if (strSingleCookie.substring(0,4)=='trp_')
		{
			//get cookie name
			var arCookieName = strSingleCookie.split('=');
			deleteCookie(trim(arCookieName[0]),'/','');
		}
	}
	
}
function deleteCookiesFromStep()
{
x = document.getElementsByName('StepName');
//there is only one element on the page with the name 'StepName'
if (x[0].value == 'step1')
   stepCookie = 'activities'
else if (x[0].value == 'step2')
    stepCookie = 'regions'
else if(x[0].value == 'step3')
     stepCookie == 'accomodation';
//get cookies that need to be deleted
strCookie=document.cookie;
var arrCookie;
arrCookie=document.cookie.split(';');
var i=0;
var strTrpCookie;
strTrpCookie='';
var subCookieValues = new Array();
//loop through the cookie array
for (i=0; i<arrCookie.length; i++) 
	{
      var strSingleCookie = trim(arrCookie[i]);
	//remove trp_group_    prefix from cookie name, and attempt to find a match
	if (strSingleCookie.substring(10,strSingleCookie.indexOf('='))==stepCookie)
		{ 
			//get cookiename, seperate cookevalue
			var arCookieName = strSingleCookie.split('=');
			
			//delete main cookie
			deleteCookie(trim(arCookieName[0]),'/','');
			//delete all spss cookies related to stepCookie
                                                //extract the value to be used for the related cookie
			//split the stepCookie results if more than one was selected by the user
			//e.g. multiple activities selected for Step 1	
			var arCookieNameSub = arCookieName[1].split('|');
			//loop through sub cookie name strings, eliminating last one
			//which is just a pipe delimiter character
			for (k=0;k<arCookieNameSub.length-1;k++)
			{ 
			  var arCookieValue = arCookieNameSub[k].split('~');
			  //add values to javascript array - which will be used to 	
			  //assemble the sub cookie names
			   subCookieValues[k] = arCookieValue[1];
			}
		
	//loop through the document cookies again and delete the associated cookie
	for (j=0;j<arrCookie.length;j++)
	{
	     var strCookieRelative = trim(arrCookie[j]);
	    //loop through all related cookies
	    //remove trp_spss_
                    for (n=0;n<subCookieValues.length;n++)
	    {
	      if (strCookieRelative.substring(9,strCookieRelative.indexOf('='))==subCookieValues[n])
		{
                                  //delete the cookie based on it's name
                                   deleteCookie(trim(strCookieRelative.split('=')[0]),'/','');
		}
	    }
 
	}
			
	}//end if, find match of StepCookie in cookiename
}//end main for
}
