var cname = "FormSubmitted" // name of the cookie
var data ="1";    // data to be stored(should not be null)
var cpath = "";   // path for which allowed [Optional]
var cdomain = ""; // domain for which allowed [Optional]


// Checks whether the cookie exists or not
// If it exists, it display a specified message on the page
function CheckForCookie()
{
  if( ExistsCookie(cname) ) // If the cookie is found
   {
     // Show message 
     document.write("<html><head><title>"
       +"The Form has already been submitted!!</title>"
       +"</head><body><center>"
       +"<h1>You have already submitted the form!!</h1><br><h3>"
       +"You cannot submit this form again, until three months "
       +"(since the day you first submitted it) have elapsed."
       +"</h3><br><h4> Thank You for your interest.</h4>"
       +"</center></body></html>");
   }
}



// Checks whether the form should be submitted or not
// If the cookie exists, the form is not submitted
// if no cookie exists, a new cookie, set to expire after
// three months ,is written and the form is submitted
function CheckForm()
{
  if( !ExistsCookie(cname) )
  {
    // Write a cookie set to expire after 90 days
    now= new Date();  // get current date and time
    expiry = new Date();
    // Set expiry Date to 90 days from now
    expiry.setTime((now.getTime() + 90*24*60*60*1000));
    
    WriteCookie(cname,data,expiry,cpath,cdomain); 
    
    return true;
  }
  
  // The user has already clicked submit once
  alert("You have already submitted this form");
  return false;
}  




// Writes the specified data to the cookie file
function WriteCookie(name,data,expiryDate,path,domain)
{  
  // The name and data arguments are compulsory  
  if( name==null || name=="" || data==null || data=="")
  {
     alert("You must provide both name & data values.");
     return false;
  }
  
  var Cookie = name + "=" + escape(data)
        +((expiryDate)?"; expires="+expiryDate.toGMTString():"")
        + ((path)? "; path=" + path : "" )
        + ((domain)?"; domain=" + domain : "" );
         
   //Set cookie
   document.cookie = Cookie;
   return true;
}



//Checks if the specified cookie exists or not
function ExistsCookie(name)
 {
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair is separated by an = sign
    var aCrumb = aCookie[i].split("=");
    if (name == aCrumb[0]) 
      return true;
  }

  // a cookie with the requested name does not exist
  return false;
}
