
var formSubmitted = false;

var timerStarted=false;
var startTime=0, stopTime=0, timeTaken=0;

var cname = "TLCustomerComplaintForm" // 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] 


function SubmitForm()
{
  //check if the form has been submitted before
  if( formSubmitted || ExistsCookie(cname) )
  {
     alert("You have already submitted this form!");
     formSubmitted = true;
     document.ComplaintSubmit.SB.disabled=true; //CHANGE
     return false;
  }  
  // Form has not been submitted before
  //Check if it was filled completely
  if( verifyForm() == false )
  {
    //alert ("Please complete the form before you submit it.");  // provide more detail
    return false; // Don't submit
  }
  // The form has been filled in completely. Submit it

    StopTimer();  // Set the time value in the hidden field  
    formSubmitted = true;    // Set the formSubmitted flag

    // 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);  
    // Show message to the user - disabled commented out
    //alert("Submitting Form....\nThank you!"); 
  return true;
}


function verifyForm()
{
  var error = '';

  // Use this function to ensure that all the
  // required data has been entered in the form     
   
	if (ComplaintSubmit.complaintType.value=='')
	{
	    error = 'Please select a report type';
	}		
	if (ComplaintSubmit.Incident.value=='')
	{
	    error = 'Please enter a description of the report';
	}
	if (ComplaintSubmit.FirstName.value=='')
	{	
	    error = 'Please enter your first name';
	}
	if (ComplaintSubmit.HomePhoneArea1.value=='')
	{		 
	    error = 'Please enter area code for your phone number';
	}
	if (ComplaintSubmit.HomePhoneArea2.value=='')
	{		 
	    error = 'Please enter your complete phone number';
	}
	if (ComplaintSubmit.HomePhoneNumber.value=='')
	{		 
	    error = 'Please enter your phone number';
	}   
   
	// return true if the form is complete
	// return false if its not

	if (error =='')
	{    		
	   return true;
	}
	   else
	{
	   alert(error);
	   return false;
	}  
}


function StartTimer()
{
  if( !timerStarted)
  {  
     startTime = new Date();
     startTime=startTime.getTime();// get time in milliseconds
     timerStarted = true; //Timer Enabled
  }
}


function StopTimer()
{
  if( startTime ==0 || formSubmitted )
   {     
     window.document.ComplaintSubmit.myhiddenfield.value=0; //CHANGE
   }
  else
   {
     stopTime = new Date();
     stopTime=stopTime.getTime();
     timeTaken = (stopTime - startTime)/(1000);     
     document.ComplaintSubmit.myhiddenfield.value= timeTaken; //CHANGE
   }
  
   //Reset the Timer
   ResetTimer();
}


function ResetTimer()
{
  //Reset Timer Info
  startTime=0;
  timerStarted=false; // Disable Timer
}


// Checks whether the cookie exists or not 
// If it exists, it display a specified message on the page 
function CheckForCookie() 
{ 
  if( ExistsCookie(cname) || formSubmitted )  
   { 
     // Show message - commented out message on page - provide alert instead 
     // document.write("Thank you. You have already provided...");
     //window.location = "http://www.translink.bc.ca/About_TransLink/Contact_Us/customer_relations.asp"
     //alert("Thank you. You have already provided...");
   } 
} 
 

// 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():"") // comment out to disable the expiry date/time.  Expires when window closed.
        + ((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; 
} 