// Purpose:			NetAssociates Javascript SendMail Lib
// Author:			Kieran O'Toole - NetAssociates Limited.
// Note:			

// All below this line this generic, i.e. the functions are re-use able and not specific to this page
// -----------------------------------------------------------------------------
var IE4 = (document.all) ? 1 : 0;									// IE4+	
var IE5 = document.all&&document.getElementById;					// IE5+
var IE6 = (navigator.appVersion.indexOf("MSIE 6.")!=-1) ? 1 : 0;	// IE6
var IE7 = (navigator.appVersion.indexOf("MSIE 7.")!=-1) ? 1 : 0;	// IE7
var NS4 = (document.layers) ? 1 : 0;								// Netscape ver 4-6
var NS5 = (navigator.appName == "Netscape" && navigator.appVersion.substring(0,1) >= 5) ? 1 : 0; 
var bOK = (NS5 || document.getElementById) ? 1 : 0;			// Must be true to use this DHTML page

var sF1="action='http://www.netassoc.co.nz/tools/sendmail.asp'";
var sF2=" method='post'";

// Purpose:			Returns a js object, holding all the required details to use the 
//					..SendMail facitlity
function getSendMailFormObj (nxRspnFormat,nxRspnPage,nxRspnTemplate,nxRspnTitle,nxRspnEmailFrom,nxRspnEmailFromName,nxRspnEmailTo,nxRspnOptions){
	this.RspnFormat=nxRspnFormat;
	this.RspnPage=nxRspnPage;
	this.RspnTemplate=nxRspnTemplate;
	this.RspnTitle=nxRspnTitle;
	this.RspnEmailFrom=nxRspnEmailFrom;
	this.RspnEmailFromName=nxRspnEmailFromName;
	this.RspnEmailTo=nxRspnEmailTo;
	this.RspnOptions=nxRspnOptions;
}

// Purpose:			returns the HTML containing the hidden form fields required by SendMail from NetAssociates
//					Hides code that may be parsed i.e. the input form field tags
function getSendMailFormHeader(nxRspnFormat,nxRspnPage,nxRspnTemplate,nxRspnTitle,nxEmailFrom,nxEmailFromName,nxEmailTo,nxRspnOptions) {
	var sI="<input ";
	var sT="type='hidden' ";
	var sX=sI+sT;
	var sH="";
	sH+=sX+"name='nxResponseFormat' value='"+nxRspnFormat+"'>";
	sH+=sX+"name='nxResponsePage' value='"+nxRspnPage+"'>";
	sH+=sX+"name='nxResponseTemplate' value='"+nxRspnTemplate+"'>";
	sH+=sX+"name='nxResponseOptions' value='"+nxRspnOptions+"'>";
	sH+=sX+"name='nxResponseTitle' value='"+nxRspnTitle+"'>";
	sH+=sX+"name='nxEmailFrom' value='"+nxEmailFrom+"'>";
	sH+=sX+"name='nxEmailFromName' value='"+nxEmailFromName+"'>";
	sH+=sX+"name='nxEmailTo' value='"+nxEmailTo+"'>";
	return sH;
}

// Purpose:			returns the HTML containing the hidden form fields required by SendMail from NetAssociates
function getSendMailFormHeaderByObj(oSMForm) {
	return getSendMailFormHeader(oSMForm.RspnFormat,oSMForm.RspnPage,oSMForm.RspnTemplate,oSMForm.RspnTitle,oSMForm.RspnEmailFrom,oSMForm.RspnEmailFromName,oSMForm.RspnEmailTo,oSMForm.RspnOptions)
}

// Purpose:			Returns a Save String
// 					Returns the value of an element, but replaces the ' char
function getSafeString(sThis) {
	if(sThis=='') return sThis;
	var sFind="'";
	var sReplace="&rsquo;";
	var sReturn="" + sThis;
	while (sReturn.indexOf(sFind)>-1) {
		iPos = sReturn.indexOf(sFind);
		sReturn = "" + (sReturn.substring(0, iPos) + sReplace + sReturn.substring((iPos + sFind.length), sReturn.length));
	}
	return sReturn;
}

// purpose:			returns an HTML DOM element by ID
// Scope:			cross browser function
function getE(sID) {
	if (bOK) return document.getElementById(sID);
	if (IE4) return document.all(sID);
	if (NS4) return document.layers(sID);
}

// Purpose:			Handles the 'Submit' button click, which does the folloowing:
//						Validate the for input and Warns user on error via MSGBOX
//						Creates a hidden form in the page and fills it will the user input
//						 .. note only fields specified in the sFieldList arguement will be included
// 						Submits the hidden form which then sends the message via email
// Design:			The form tag never appears in the HTML and is therefore not visible to spider
//					.. wishing to automate it.. i.e. its form spam proof
//					This design avoids bugging the user with additional inputs i.e. enter what you see here type stuff
//					Also the form fields are completely separated from the form itself allowing a sub-set of fields
//					.. displayed to be submitted.. i.e. building the form on the fly
// Args:			item					buttom element
//					sFormID					the id of the new form tag
//					oForm					as returned by getSendMailFormObj()
//					sFieldList				list of form field element ids, the field to include in the post
// Note:			requires a <div id=dvSendMail_1></div> to be included in the HTML
// Debug:			<input onclick="fnSubmitSendMail(item,'ContactForm',moForm,'EmailAddress;FirstName;LastName;Comments')">
//						..where moForm has been defined at the page level via a call to getSendMailFormObj()
function fnSubmitSendMail(item,sFormID,oForm,sFieldList,sSubmittedCaption) {
	var sI="<input ";
	var sT="type='hidden' ";
	var sS="/";
	var sHTML="<"+"for"+"m "+sF1+sF2;
	sHTML+=" name='"+sFormID+"' id='"+sFormID+"' style='display:none;visibility:hidden;'>";
	if (oForm.constructor==String) {
	
	} else {
		sHTML+=getSendMailFormHeaderByObj(oForm);
	}
	var sFormFields=sFieldList.split(";");
	for (n=0;n<=sFormFields.length;n++) {
		eX=getE(sFormFields[n]);
		if (eX) {
			// must check if this form element is a checkbox or option box... 
			// .. if so only return their value if its selected !!
			if ((eX.type=="text") || (eX.type=="textarea") || (eX.type=="select-one") || (eX.type=="hidden")) {
				sHTML+=sI+sT+"name='"+sFormFields[n]+"' value='"+getSafeString(eX.value)+"'>";
			}
			if ((eX.type=="checkbox") || (eX.type=="radio")) {
				if (eX.checked==true) {
					sHTML+=sI+sT+"name='"+sFormFields[n]+"' value='"+getSafeString(eX.value)+"'>";
				}
			}
		}
	}
	sHTML+="<"+sS+"f"+"orm"+">";
	var eP=getE('dvSendMail_1');
	if (!eP) {
		document.body.insertAdjacentHTML('AfterBegin',sHTML);
	} else {
		eP.innerHTML=sHTML;								// insert the new form tags into the HTML page, as hidden
	}
	if (sSubmittedCaption=='') {
		sSubmittedCaption='Submitted...';
	}
	item.value=sSubmittedCaption;						// update the button caption
	item.disabled=true;									// disable the button
	var eForm=getE(sFormID);
	eForm.submit();										// submit the hidden form
	return;
}


// (nxRspnFormat,nxRspnPage,nxRspnTemplate,nxRspnTitle,nxRspnEmailFrom,nxRspnEmailFromName,nxRspnEmailTo,nxRspnOptions)
function fnSubmitThisSendMail(item,sItemCaption,sFormID,sFieldList,sRspnFormat,sRspnPage,sRspnTemplate,sRspnTitle,sRspnEmailFrom,sRspnEmailFromName,sRspnEmailTo,sRspnOptions){
	var oForm = new getSendMailFormObj(sRspnFormat,sRspnPage,sRspnTemplate,sRspnTitle,sRspnEmailFrom,sRspnEmailFromName,sRspnEmailTo,sRspnOptions);
	return fnSubmitSendMail(item,sFormID,oForm,sFieldList,sItemCaption);
}