// name     - name of the cookie
// value    - value of the cookie
// [expires]- days until the cookie expires (defaults to end of current session)
// [path]   - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
// args to this function are sent in this sequence:
//  name, value, expiredate, path, domain, secure

function getexpirydate(nbrOfDays)
{
    var UTCstring;
    Today       = new Date();
    nomilli     =Date.parse(Today);
    Today.setTime(nomilli+nbrOfDays*24*60*60*1000);
    UTCstring   = Today.toUTCString();
    return UTCstring;
}

function setCookie (cookieName, cookieValue) 
{ 
    var args    = setCookie.arguments; 
    var arglen  = setCookie.arguments.length; 

    var expires = (arglen > 2) ? args[2] : null; 
    var path    = (arglen > 3) ? args[3] : null; 
    var domain  = (arglen > 4) ? args[4] : null; 
    var secure  = (arglen > 5) ? args[5] : false; // true or false not null
    
    //alert("name="+name);
    //alert("value="+value);
    //alert("expires="+expires);
    //alert("path="+path);
    //alert("domain="+domain);
    //alert("secure="+secure);

    document.cookie = cookieName + "=" + escape (cookieValue)+
    ((expires == null) ? ""         : ("; expires = " + getexpirydate(expires))) +
    ((path    == null) ? ""         : ("; path=   "   + path))   + 
    ((domain  == null) ? ""         : ("; domain=   " + domain)) + 
    ((secure  == true) ? "; secure" : ""); 
    
} 

// name - name of the desired cookie
// * return value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc        = document.cookie;
  var prefix    = name + "=";
  var begin     = dc.indexOf("; " + prefix);
  
  if (begin == -1) 
  { begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } 
  else
  { begin += 2;
  }
  
  var end = dc.indexOf(";", begin);
  
  if (end == -1)
  { end = dc.length;
  }
  
  return unescape(dc.substring(begin + prefix.length, end));
}

// name     - name of the cookie
// [path]   - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
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";
  }
}


// ********************************************************************************************
// when calling this function, value stringSpecialCharsAllowed like this "%+-@" in the call
// ********************************************************************************************
function isNumeric(stringToCheck,  stringSpecialCharsAllowed)
{
   var strValidChars = "0123456789"+stringSpecialCharsAllowed;
   var strChar;
   var bIsValid = true;

   if (stringToCheck.length == 0) return false;

   stringToCheck = stringToCheck.toLowerCase();
   
    for (i = 0; i < stringToCheck.length; i++)
    {
        strChar = stringToCheck.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            bIsValid = false;
            break;
        }
    }

   return bIsValid;
}

// ********************************************************************************************
// when calling this function, value stringSpecialCharsAllowed like this "%+-+@" in the call
// ********************************************************************************************
function isAlpha(stringToCheck, stringSpecialCharsAllowed)
{
   var strValidChars = " abcdefghijklmnopqrstuvwxyz"+stringSpecialCharsAllowed;
   
   var strChar;
   var bIsValid = true;

   if (stringToCheck.length == 0) return false;

   stringToCheck = stringToCheck.toLowerCase();
   
    for (i = 0; i < stringToCheck.length; i++)
    {
        strChar = stringToCheck.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            bIsValid = false;
            break;
        }
    }

   return bIsValid;
}

// ********************************************************************************************
// when calling this function, value stringSpecialCharsAllowed like this "=-+@" in the call
// ********************************************************************************************
function isAlphaNumeric(stringToCheck, stringSpecialCharsAllowed)
{
   var strValidChars = " abcdefghijklmnopqrstuvwxyz0123456789"+stringSpecialCharsAllowed;
   var strChar;
   var bIsValid = true;

   if (stringToCheck.length == 0) return false;
   
   stringToCheck = stringToCheck.toLowerCase();
   
    for (i = 0; i < stringToCheck.length; i++)
    {
        strChar = stringToCheck.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            bIsValid = false;
            break;
        }
    }

   return bIsValid;

}
