﻿var cngFormSubmitButtonObjectOne;
var cngFormSubmitButtonObjectTwo;
var cngFormSubmitButtonOneLabel;
var cngFormSubmitButtonTwoLabel;
var cngCountdownStartTime;
var cngCountdownDuration;


function StartOkCountdown(okButtonObjectOne, okButtonObjectTwo, countdownDuration) {
	
	// Register the relevant data
	cngFormSubmitButtonObjectOne = okButtonObjectOne;
	cngFormSubmitButtonObjectTwo = okButtonObjectTwo;
	cngFormSubmitButtonOneLabel = okButtonObjectOne.value;
	cngFormSubmitButtonTwoLabel = okButtonObjectTwo.value;
	cngCountdownDuration = countdownDuration;
	var cngDateObject = new Date();
	cngCountdownStartTime = cngDateObject.getTime();
	
	// Disable the buttons
	cngFormSubmitButtonObjectOne.disabled = true;
	cngFormSubmitButtonObjectTwo.disabled = true;
	
	// Set the labels to countdown duration
	cngFormSubmitButtonObjectOne.value = countdownDuration;
	cngFormSubmitButtonObjectTwo.value = countdownDuration;
	
	// Start the countdown
	setTimeout("UpdateCountdown();", 500);
}


function ClickMessageButton(formObject, buttonObject) {
	formObject.cngwhichbutton.value = buttonObject.value;
	formObject.cngnotnowbutton.disabled = true;
	formObject.cngmorebutton.disabled = true;
	formObject.submit();
}


function UpdateCountdown() {
	
	// Calculate duration remaining
	var cngDateObject = new Date();
	var cngDurationRemaining = Math.round(cngCountdownDuration - ((cngDateObject.getTime() - cngCountdownStartTime) / 1000));
	if (cngDurationRemaining < 0) {
		cngDurationRemaining = 0;
	}
	
	// If there is some remaining time
	if (cngDurationRemaining > 0) {
	
		// Display the remaining time and callback
		cngFormSubmitButtonObjectOne.value = cngDurationRemaining;
		cngFormSubmitButtonObjectTwo.value = cngDurationRemaining;
		setTimeout("UpdateCountdown();", 500);
		
	} else {
		
		// Revert back to the original labels
		cngFormSubmitButtonObjectOne.value = cngFormSubmitButtonOneLabel;
		cngFormSubmitButtonObjectTwo.value = cngFormSubmitButtonTwoLabel;
		
		// Enable the buttons again
		cngFormSubmitButtonObjectOne.disabled = false;
		cngFormSubmitButtonObjectTwo.disabled = false;
	}
}