/*
	Module:			EmbeddedEmailOrLinkedQuestionnaire_v1.js
	Description:	Javascript for EmbeddedEmailOrLinkedQuestionnaire.xslt

	Change History
	
		Created:	Alex Moses		26 Jun 2007
		Modified:	Alex Moses		30 Oct 2007		Updated browser information
	
*/


function submitEmbeddedEmailQuestionnaire()
{
	var oIframeDocument;
	var oEmail = document.frmEmbeddedQ.inpEmail;
	var strEmail = '';
	var aQWebContentIDStatus;
	var numIndex, numWebContentIDUnanswered, numQWebContentIDStatus;
	var bAlreadyInCookie = false;
		
	if (oEmail) {
		strEmail = oEmail.value;
	}

	if ((oEmail && strEmail.length == 0) || strEmail == gstrQEmailText)
	{
		alert(gstrQEmailText);
	}
	else if (oEmail && (checkEmail(strEmail) == false))
	{
		alert('Please check you have entered your address correctly');
	}
	else
	{
		if (document.getElementById('iframeQuestionnaire').contentDocument) {
			oIframeDocument = document.getElementById('iframeQuestionnaire').contentDocument; // Firefox/Opera/Safari
		} else {
			oIframeDocument = document.frames('iframeQuestionnaire').document; // IE
		}
		if (document.frmEmbeddedQ.inpReceiveEmail && document.frmEmbeddedQ.inpReceiveEmail.checked) {
			oIframeDocument.frmQuestionnaire.elements[gstrQPermitEMailField].value = 'True';
		}
		oIframeDocument.frmQuestionnaire.elements[gstrQEmailField].value = (strEmail.length==0 ? 'DEFAULT' : strEmail);
		oIframeDocument.frmQuestionnaire.submit();
		document.getElementById('divInvite').style.display = 'none';
		document.getElementById('divThank').style.display = 'block';

		/*		
		N.B. Questionnaire Web Content ID is stored in the RMSession cookie with
				a binary suffix denoting whether it has been answered.
		   If cookie value ends with 1, Q is answered. To flag Q123456 as answered:
		       123456 * 10 + 1 = 1234561
		   If cookie value ends with 0, Q is unanswered. To flag Q654321 as unanswered:
		       654321 * 10 = 6543210
		*/

		aQWebContentIDStatus = getCookie('RMSession','EmQ').split(',');
		// Assuming that Questionnaire content references are formatted as Q######...
		numWebContentIDUnanswered = oIframeDocument.frmQuestionnaire.inpContentref.value.substring(1) * 10;
		for (numIndex in aQWebContentIDStatus)
		{
			numQWebContentIDStatus = aQWebContentIDStatus[numIndex];
			if (numQWebContentIDStatus == numWebContentIDUnanswered)
			{
				// Amend WebContentIDStatus in array as answered
				aQWebContentIDStatus[numIndex] = numWebContentIDUnanswered + 1;
				bAlreadyInCookie = true;
				break;
			}
		}
		if (!bAlreadyInCookie)
		{
			// Append WebContentIDStatus to array as answered
			numIndex = aQWebContentIDStatus.length - 1;
			if (numIndex==0 && aQWebContentIDStatus[0].length>0 && aQWebContentIDStatus[0]!='undefined') {
				// Array has a single entry
				numIndex++
			}
			aQWebContentIDStatus[numIndex] = numWebContentIDUnanswered + 1;
		}
		// Save array back into cookie
		setCookie('RMSession',aQWebContentIDStatus.join(','),null,'/',null,null,'EmQ')		
	}
}


function InputFocus (pstrID, pstrBackground)
{
	if (document.all[pstrID].value == pstrBackground)
	{
		document.all[pstrID].value = '';
	}
	document.all[pstrID].style.color = '#000';
}


function InputBlur (pstrID, pstrBackground)
{
	if (document.all[pstrID].value == '')
	{
		document.all[pstrID].style.color = '#B1B1B1';
		document.all[pstrID].value = pstrBackground;
	}
}


function windowOnload ()
{
	var oIframeDocument;

	if (document.getElementById('iframeQuestionnaire') && document.getElementById('iframeQuestionnaire').contentDocument) {
		oIframeDocument = document.getElementById('iframeQuestionnaire').contentDocument; // Firefox/Opera/Safari
	} else if (window.frames['iframeQuestionnaire']) {
		oIframeDocument = window.frames['iframeQuestionnaire'].document; // IE
	}
	if (oIframeDocument) {
		// Set Questionnaire content HTML
		oIframeDocument.body.innerHTML = gstrBodyHTML;
	}
}

window.onload = windowOnload;