
function writeContactInfo(){
	
		// debugger;
				contactInfo  = new cookieObject("contact", null, "/", "name", "email", "clientId");
				var formName = document.entrycontact.name.value;
				var formEmail = document.entrycontact.email.value;
				var formClientId = document.entrycontact.clientid.value;
				
				contactInfo.put("name", formName);
				contactInfo.put("email", formEmail);
				contactInfo.put("clientId", formClientId);

				//Output the updated cookie
				contactInfo.expires = 500
				contactInfo.write();
			
			//alert("System check - please continue, nothing's wrong: code "+sTime.value)


}

function getContactInfo(){
	
	contactInfo  = new cookieObject("contact", null, "/", "name", "email", "clientId");
	
		// debugger;
		if (contactInfo.found) {
				
								
				var formName =  contactInfo.get("name");
				var formEmail = contactInfo.get("email");
				var formClientId = contactInfo.get("clientId");
				
				document.paymentNotify.name.value = formName;
				document.paymentNotify.from.value = formEmail;
				
				document.getElementById('clientId').value = formClientId;         //  That's the field below with space in name
				// document.paymentNotify.customclientid.value = formClientId;  // which is invalid!

				//Output the updated cookie
			
				contactInfo.write();
			
		}


}

function getContactInfoPostPay(){
	
	contactInfo  = new cookieObject("contact", null, "/", "name", "email", "clientId");
	
		// debugger;
		if (contactInfo.found) {
				
								
				var formName =  contactInfo.get("name");
				var formEmail = contactInfo.get("email");
				var formClientId = contactInfo.get("clientId");
				
				document.postPay.name.value = formName;
				document.postPay.from.value = formEmail;
				document.getElementById('confirmEmail').value = formEmail; 
				document.getElementById('clientId').value = formClientId;         //  That's the field below with space in name
				// document.paymentNotify.customclientid.value = formClientId;  // which is invalid!

				//Output the updated cookie
			
				contactInfo.write();
			
		}


}

function getContactInfoVP(){
	
	contactInfo  = new cookieObject("contact", null, "/", "name", "email", "clientId");
	
		// debugger;
		if (contactInfo.found) {
				
								
				var formName =  contactInfo.get("name");
				var formEmail = contactInfo.get("email");
				
				document.vpPayment.name.value = formName;
				document.vpPayment.email.value = formEmail;

			
		}


}

function checkClientId() {
	
	var clientIdDate = new Date();
	var	clientId = clientIdDate.getTime();
	var noVisits = 0;
	var prevVisit = ""
	
	
// Test if session cookie exists

// Create cookie object and attempt to read it from disk
	sessionCookie  = new cookieObject("session", null, "/", "sessionStarted", "qDurn");
// if cookie doesn't exist, create it

//debugger;

	sessionCookie.put("sessionStarted", "Yes"); 
	
	
if (sessionCookie.found) {

	// sessionCookie.write(); // superfluous now but left in in case we later decide to reinstate cookie with 30 min lifetime

} else {
	  sessionCookie.put("qDurn", "0"); // qDurn is total answer time for all Qs this session
      sessionCookie.write();
	  
	  
	  // Now check to see if there is a clientId cookie - 
	  // If yes, update it - they've been here before

	  
	  clientIdCookie  = new cookieObject("clientId", 500, "/", "cid", "firstVisit", "prevVisit", "lastVisit", "noVisits");
	  
	  if (clientIdCookie.found) {
       // Yes, update it - they've been here before
			prevVisit = clientIdCookie.get("lastVisit");
			clientIdCookie.put("prevVisit", prevVisit);
			clientIdCookie.put("lastVisit",clientIdDate);
			noVisits =	clientIdCookie.get("noVisits");
			++noVisits;
			clientIdCookie.put("noVisits", noVisits);

			clientIdCookie.write();
	  
	  } else {
		  	//No - never here before, create one
			clientIdCookie.put("cid", clientId);
			clientIdCookie.put("firstVisit", clientIdDate);
			clientIdCookie.put("prevVisit", clientIdDate);
			clientIdCookie.put("lastVisit",clientIdDate);
			clientIdCookie.put("noVisits",1);

			clientIdCookie.write();
		  
		  }
	}
}

function getClientId(myForm) {
	
	// Unless cookies or javascript disabled, there should always be a client cookie - created by 
	// the function checkClientId which should  have been called before this routine [most pages have onLoad="checkClientId()"] 
	// If we find a client cookie, add the necessary fields to the form on the page
	
// debugger;
	  var cid = ""
	   var firstVisit = ""
	   var prevVisit = ""
	   var lastVisit = ""
	   var noVisits = 0
	
	 clientIdCookie  = new cookieObject("clientId", null, "/", "cid", "firstVisit", "prevVisit", "lastVisit", "noVisits")
	 
	 if (clientIdCookie.found) {
       // Yes, get record details
		

			cid = clientIdCookie.get("cid")
			firstVisit = clientIdCookie.get("firstVisit")
			prevVisit = clientIdCookie.get("prevVisit")
			lastVisit = clientIdCookie.get("lastVisit")
			noVisits =	clientIdCookie.get("noVisits")
			
	  		myForm.clientid.value = cid
			myForm.firstVisit.value = firstVisit
			myForm.prevVisit.value = prevVisit
			myForm.lastVisit.value = lastVisit
			myForm.noVisits.value =	noVisits
	  }
	
}

function getClientIdVP(myForm) {
	
	// *****************      Variant just for payment page     ******************
	
	// Unless cookies or javascript disabled, there should always be a client cookie - created by 
	// the function checkClientId which should  have been called before this routine [most pages have onLoad="checkClientId()"] 
	// If we find a client cookie, add the necessary fields to the form on the page
	
	// *****************      Variant just for payment page     ******************
	
// debugger;
	
	 clientIdCookie  = new cookieObject("clientId", null, "/", "cid", "firstVisit", "prevVisit", "lastVisit", "noVisits")
	 
	 if (clientIdCookie.found) {
       // Yes, get record details
			
	  		myForm.cart_id.value = clientIdCookie.get("cid")

	  }
	  else {	// should never get here but create them an id if we do
			var clientIdDate = new Date();
			var	clientId = clientIdDate.getTime();
		  	myForm.cart_id.value = clientId
		  }
// *****************      Variant just for payment page     ******************
}



function startClockTerms() 
	
{	// Set pageViewStartTime to start time that page loads
	var pageViewStartTime = new Date();
	pageViewStartTime = pageViewStartTime.getTime();
	var noVisits = 1
	
// Test if termsRead cookie exists
// If yes, update it. If no, create a new one 

// Create cookie object and attempt to read it from disk
	termsReadCookie  = new cookieObject("termsRead", null, "/", "startTime", "endTime", "duration", "noVisits")
	
// if cookie doesn't exist, create it

// debugger;

	if (termsReadCookie.found) {
      
	  // If yes, update it - they've been here before
	  //If not, create one

			termsReadCookie.put("startTime",pageViewStartTime)
			noVisits =	termsReadCookie.get("noVisits")
			++noVisits
			termsReadCookie.put("noVisits", noVisits)
			
			termsReadCookie.expires = 500
			termsReadCookie.write()
	  
	  } else {
		  	//No - never here before, create one
			termsReadCookie.put("startTime", pageViewStartTime)
			termsReadCookie.put("endTime", 0)
			termsReadCookie.put("duration",0)
			termsReadCookie.put("noVisits",noVisits)
			
			termsReadCookie.expires = 500
			termsReadCookie.write()
		  
		  }
 
}

function stopClockTerms() 
	
{
	
// debugger;
		var pageViewStopTime = new Date()
		var pageViewStartTime
		pageViewStopTime = pageViewStopTime.getTime()
		var readingTime = 0
		
		// Create cookie object and attempt to read it from disk
		termsReadCookie  = new cookieObject("termsRead", 0, "/", "startTime", "endTime", "duration", "noVisits")
	    termsReadCookie.expires = 500
		if (termsReadCookie.found) {
      
	  // Should be one - if not, just ignore

			pageViewStartTime = termsReadCookie.get("startTime")
			termsReadCookie.put("endTime",pageViewStopTime)
			readingTime =	parseInt(termsReadCookie.get("duration"))
			 // parseInt to force back to numeric - otherwise next step 
			 // produces character string concatenation
			readingTime = readingTime + (pageViewStopTime - pageViewStartTime)
			termsReadCookie.put("duration", readingTime)

			termsReadCookie.write()
			
		}
 
}


function getDetailTerms(myForm) {
	
	// This routine puts the details of the number of times they've read the ts and cs of the competition
	// plus the time they've spent reading them into the form for Q1 and Q4 <<< NO  - Now Q3 ****.
	// We do this in form for Q1 to get the data in case they bail out before finishing the Qs
	// NO LONGER TRUE >>>>  and again in Q4 just to keep all the data in one database record with all their question answers *****
	//Instead we do this in Q3 as we've dropped the final question
	
// debugger;
		var noVisits = 0
		var readingTime = 0
		
		// Attempt to read cookie from disk
		termsReadCookie  = new cookieObject("termsRead", 0, "/", "startTime", "endTime", "duration", "noVisits")
		if (termsReadCookie.found) {
      
	  // Should be one - if not, just ignore

			readingTime =	termsReadCookie.get("duration");
			noVisits =	termsReadCookie.get("noVisits");
			myForm.termsDuration.value = readingTime;
			myForm.termsNoVisits.value = noVisits;
		}
 
}

