/*==============================================================================

    Routines written by John Gardner - 2003 - 2005

    See www.braemoor.co.uk/software for information about more freeware
    available.

/*==============================================================================

Routine to write a session cookie

    Parameters:
        cookieName        Cookie name
        cookieValue       Cookie Value
    
    Return value:
        true              Session cookie written successfullly
        false             Failed - persistent cookies are not enabled

   e.g. if (writeSessionCookie("pans","drizzle") then
           alert ("Session cookie written");
        else
           alert ("Sorry - Session cookies not enabled");
*/

function writeSessionCookie (cookieName, cookieValue) {
  if (testSessionCookie()) {
    document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
    return true;
  }
  else return false;
}

/*==============================================================================

Routine to get the current value of a cookie

    Parameters:
        cookieName        Cookie name
    
    Return value:
        false             Failed - no such cookie
        value             Value of the retrieved cookie

   e.g. if (!getCookieValue("pans") then  {
           cookieValue = getCoookieValue ("pans2);
        }
*/

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}

/*==============================================================================

Routine to see if session cookies are enabled

    Parameters:
        None
    
    Return value:
        true              Session cookies are enabled
        false             Session cookies are not enabled

   e.g. if (testSessionCookie())
           alert ("Session coookies are enabled");
        else
           alert ("Session coookies are not enabled");
*/

function testSessionCookie () {
  document.cookie ="testSessionCookie=Enabled; path=/";
  if (getCookieValue ("testSessionCookie")=="Enabled") {
    return true 
  } else {
    return false;
  }
}

function testEtiesSessionCookie (sessID) {
//  document.cookie ="testSessionCookie=Enabled; path=/";
  if (getCookieValue ("PHPSESSID")==sessID) {
//  	alert("e bravo...li accetti!!!!");
    return true 
  } else {
//  	alert("nonono....vuoi essere proprio sicuro e non accetti i cookie eh!?!?....ammaronna!!!!");
    return false;
  }
}

/*==============================================================================

Routine to see of persistent cookies are allowed:

    Parameters:
        None
    
    Return value:
        true              Session cookies are enabled
        false             Session cookies are not enabled

   e.g. if (testPersistentCookie()) then
           alert ("Persistent coookies are enabled");
        else
           alert ("Persistent coookies are not enabled");
*/

function testPersistentCookie () {
  writePersistentCookie ("testPersistentCookie", "Enabled", "minutes", 1);
  if (getCookieValue ("testPersistentCookie")=="Enabled")
    return true  
  else 
    return false;
}

/*==============================================================================

Routine to write a persistent cookie

    Parameters:
        CookieName        Cookie name
        CookieValue       Cookie Value
        periodType        "years","months","days","hours", "minutes"
        offset            Number of units specified in periodType
    
    Return value:
        true              Persistent cookie written successfullly
        false             Failed - persistent cookies are not enabled
    
    e.g. writePersistentCookie ("Session", id, "years", 1);
*/       

function writePersistentCookie (CookieName, CookieValue, periodType, offset) {

  var expireDate = new Date ();
  offset = offset / 1;
  
  var myPeriodType = periodType;
  switch (myPeriodType.toLowerCase()) {
    case "years":
      expireDate.setYear(expireDate.getFullYear()+offset);
      break;
    case "months":
      expireDate.setMonth(expireDate.getMonth()+offset);
      break;
    case "days":
      expireDate.setDate(expireDate.getDate()+offset);
      break;
    case "hours":
      expireDate.setHours(expireDate.getHours()+offset);
      break;
    case "minutes":
      expireDate.setMinutes(expireDate.getMinutes()+offset);
      break;
    default:
      alert ("Invalid periodType parameter for writePersistentCookie()");
      break;
  } 
  
  document.cookie = escape(CookieName ) + "=" + escape(CookieValue) + "; expires=" + expireDate.toGMTString() + "; path=/";
}  

/*==============================================================================

Routine to delete a persistent cookie

    Parameters:
        CookieName        Cookie name
    
    Return value:
        true              Persistent cookie marked for deletion
    
    e.g. deleteCookie ("Session");
*/    

function deleteCookie (cookieName) {

  if (getCookieValue (cookieName)) writePersistentCookie (cookieName,"Pending delete","years", -1);  
  return true;     
}


function rewriteAhref(sessione) {
	
	if ((false) && (sessione.match(/^PHPSESSID\=[A-Za-z0-9]{32}$/))) {
	    var sessID = sessione.match(/[a-z0-9A-Z]{32}$/);
		alert("sessione.match OK");
	    if (testEtiesSessionCookie(sessID)==false) {
			alert("testEtiesSessionCookie OK");
			
	        var regE = new RegExp(/^javascript(.*)/);

	        var fm=document.getElementsByTagName("a");
	        alert("nr di 'a' = "+fm.length);
	        for (i=0;i<fm.length;i++) {
	        
	             if (!regE.test(fm[i].href)) {
             		/**
             		* non sono link javascript
             		* quindi devo aggiungere il PHPSESSID
             		* se non c'è già
             		*/
             		if (fm[i].href.match(/PHPSESSID/) == null) {
             			// NON esiste già il PHPSESSID
        				if (fm[i].href.match(/\?/) == null) {
        					fm[i].href += "?" + sessione;
						} else {
							fm[i].href += "&" + sessione;
						}
					}
	             }
	        }
	        
	        var nodiForm = document.getElementsByTagName("form");
	//        alert(nodiForm);
	        for(f = 0; f < nodiForm.length; f++) {
	//        	alert(nodiForm[f]);
        		for(a = 0; a < nodiForm[f].attributes.length; a++) {
	//        		alert(nodiForm[f].attributes[a].name);
	//        		alert(nodiForm[f].attributes[a].value);
        			if (nodiForm[f].attributes[a].name.toLowerCase() == "action") {
        				if (nodiForm[f].attributes[a].value.match(/PHPSESSID/) == null) {
        					// nell'action NON ho il PHPSESSID
        					// quindi lo aggiungo io
        					if (nodiForm[f].attributes[a].value.match(/\?/) == null) {
        						nodiForm[f].attributes[a].value += "?"+sessione;
							} else {
        						nodiForm[f].attributes[a].value += "&"+sessione;
							}
						}
	//					alert(nodiForm[f].attributes[a].value);
					}
				}
	        }
		}
	}
    
    
}