function getStr(lang, strName)
{
	var s = "";
	if ((lang == null) || (lang == "")) lang = "en";
	// TBD here will be lookup;
	return strName;
}

function ReplaceLineBreaksForDisplay(s)
{
	return s.replace(/^\n*/, "\n");
}

function TestPhoneNumber(s)
{
	if (!s) return false;
	if (IsUnsafeString(s)) return false;
	var i=0,n=0, c;
	for (i=0;i<s.length;i++)
	{
		c = s.substr(i);
		if ((c >= "0") && (c <="9"))
		{
			n++;
		}
	}
	return (n >= 7);
}

function TestEmailAddress(s)
{
	if (IsUnsafeString(s)) return false;
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return (regex.test(s))
}

function GetRadioButtonSetting(rbgrp)
{
	for (i=0;i<rbgrp.length;i++)
	{
		if (rbgrp[i].checked) return i;
	}
	return -1;
}

function SetRadioButtonSetting(rbgrp, i)
{
	if (isNaN(i)) i = parseInt(i);
	if ((!isNaN(i)) && (i >= 0) && (i < rbgrp.length)) rbgrp[i].click();
}

function SetRadioButtonValue(rbgrp, v)
{
	for (i=0;i<rbgrp.length;i++)
	{
		rbgrp[i].checked = (rbgrp[i].value == v);
		if (rbgrp[i].value == v) if (rbgrp[i].click());
	}
}

function GetCheckBoxSetting(btn)
{
	return (btn.checked);
}

function SetCheckBoxSetting(btn, how)
{
	btn.checked = (how == true);
}

function initPgData()
{
	if (gbCookiesOK)
	{
		//alert("safe cookies read")
		window.status="Safe cookies OK"
		var elems=document.forms["contactform"].elements;
		var i=0, j=0, a = new Array();
		var s = GetCookie("grp1");
		if (s != "")
		{
			a = s.split("{^}");
			i = 0;
			if (a[i]) elems["real"].value = a[i++];
			if (a[i]) elems["org"].value = a[i++];
			if (a[i]) elems["address"].value = a[i++];
			if (a[i]) elems["email"].value = a[i++];
			if (a[i]) elems["phone"].value = a[i++];
			if (a[i]) elems["mobile"].value = a[i++];
			if (a[i]) elems["fax"].value = a[i++];
			if (a[i]) elems["country"].value = a[i++];
			if (a[i]) elems["voicephone"].value = a[i++];
			if (a[i]) elems["faxphone"].value = a[i++];
			if (a[i]) elems["IsTravelAgent"].checked = (a[i++] == "true");
			if (a[i]) SetCheckBoxSetting(elems["byEmail"], a[i++]);
			if (a[i]) SetCheckBoxSetting(elems["byPhone"], a[i++]);
			if (a[i]) SetCheckBoxSetting(elems["byMobile"], a[i++]);
			if (a[i]) SetCheckBoxSetting(elems["byFax"], a[i++]);
			if (a[i]) SetCheckBoxSetting(elems["byMail"], a[i++]);
		}
		s = GetCookie("grp2");
		if (s != "")
		{
			var a = s.split("{^}");
			i = 0;
			elems["project"].value = a[i++];
			elems["questions"].value = a[i++];
		}
		/*
		s = GetCookie("tags");
		if (s != "")
		{
			var aTags = (GetCookie("tags")).split(",");
			//Not done here - done when dynamically creating checkboxes
			for (var i=0;i<elems.length;i++)
			{
				if ((elems[i].name == "interest") && (elems[i].checked))
				{
					tag = (elems[i].value.split("~"))[0];
					for (j = 0; j < aTags.length; j++)
					{
						if (tag == aTags[j])
						{
							elems[i].checked = true;
						}
					}
				}
			}

		}
		*/
		//window.status = "Preferences read from cookie"

	}
}

function sanestring(s,nMax)
{
	if (s == null) return "";
	var a = new Array("<script",".run"); //very meek, I know
	for (i=0; i<a.length;i++)
	{
		if (s.indexOf(a[i]) > -1) return "";
	}
	if (isNaN(nMax)) nMax = 2000;
	s = s.substr(0,nMax);
	return s;
}

var gbPgDataSaved = false;

function savePgData()
{
	return
	if ((!gbPgDataSaved) && (ConfirmCookiesOK()))
	{
		gbPgDataSaved = true;
		//alert("writing cookies")
		window.status = "Saving preferences in cookie. See Privacy page for details."
		var a = new Array();
		var s = "";
		var elems=document.forms["contactform"].elements;
		a[a.length] = sanestring(trim(elems["name"].value),80);
		a[a.length] = sanestring(trim(elems["email"].value),256);
		a[a.length] = sanestring(trim(elems["address1"].value),100)
		a[a.length] = sanestring(trim(elems["address2"].value),100)
		a[a.length] = sanestring(trim(elems["city"].value),100)
		a[a.length] = sanestring(trim(elems["state"].value),40)
		a[a.length] = sanestring(trim(elems["zip"].value),20)
		a[a.length] = sanestring(trim(elems["country"].value),40)
		a[a.length] = sanestring(trim(elems["voicephone"].value),30)
		a[a.length] = sanestring(trim(elems["faxphone"].value),30)
		a[a.length] = elems["IsTravelAgent"].checked
		a[a.length] = GetRadioButtonSetting(elems["AddToMailingList"])
		a[a.length] = GetRadioButtonSetting(elems["InfoHow"])
		a[a.length] = GetRadioButtonSetting(elems["howSoon"])
		a[a.length] = GetRadioButtonSetting(elems["hasAgent"])
		a[a.length] = GetRadioButtonSetting(elems["whereheard"])
		SetCookie("grp1",a.join("{^}"))
		a = new Array()
		a[a.length] = sanestring(trim(elems["comments"].value))
		a[a.length] = sanestring(trim(elems["bestTimeToCall"].value))
		a[a.length] = sanestring(trim(elems["travelagent"].value))
		a[a.length] = sanestring(trim(elems["whereheardmoredetails"].value))
		SetCookie("grp2",a.join("{^}"))
		var aTags = new Array();
		for (var i=0;i<elems.length;i++)
		{
			if ((elems[i].name == "interest") && (elems[i].checked))
			{
				tag = elems[i].value;
				aTags[aTags.length] = (tag.split("~"))[0];
			}
		}
		//alert(aTags.toString());
		SetCookie("tags", aTags.toString());

		//alert("document.cookie: "  + document.cookie + "\n\nlist: " + GetCookieList())

	}
}



function MakeXHTMLCheckBox(nWidth,sNam,sVal,bChecked,sTxt,sHref)
{
// 	s += '<span style="position:relative;top:0px;left:20px;">';

	var s = '<td width="' + nWidth + '%" class="formcheckbox">';
	s += '<table width="100%" cellspacing="0" cellpadding="0" border="0" summary="checkbox layout"><tr valign="top"><td>';
	s += '<input type="checkbox" name="' + sNam + '" size="50"';
	//s += ' onclick="javascript:ClickTagCheckbox(this.value,this.checked,0)"';
	if (bChecked)
	{
		//alert("should be checked: " + sTxt)
		s += ' defaultChecked="true" checked="true"';
	}
	else
	{
		s += ' defaultChecked="false" ';
	}
	s += ' value="' + sVal + '" /> ';
	s += '</td><td width="100%" style="padding-top:2px;">'
	var bLink = ((sHref != null) && (sHref != ""));
	if (bLink)
	{
		s += '<a href="' + sHref + '">';
	}
	s += EscapeHTMLEntities(sTxt);
	if (bLink)
	{
		s += '</a>';
	}
	s += '</td></tr></table>';
	s += '</td>';
	return s;
}

function ArrayIndex(a,s){
	for (i=0;i<a.length;i++) if (a[i] == s) return i;
	return -1;
}

function MakeXHTMLInfoTagCheckboxes(){
	// If array properly initialized, build form checkboxes from that
	// otherwise build a generic form
  var aGenericFirst = new Array("Custom escorted tour","Custom independent tour");
  var aGenericLast = new Array("Other (please use the Comments box below)");
	var aDone = new Array();
	var bDone = false;
	var i = 0;
	var idPg = "", tag = "";;
	var nCol = 1;
	var nMaxCols = 2;
	var w = Math.round(100/nMaxCols);
	var bRowClosed = true;

	var aCheckTags = new Array();
	if (gbCookiesOK)
	{
		aCheckTags = (GetCookie("tags")).split(",");
	}


	var s = '<table width="100% border="0" cellspacing="4" cellpadding="0" summary="table of checkboxes for different information topics">\n';
	s += '<tr><td colspan="' +  nMaxCols+ '">Please check the tour(s) or topic(s) you are most interested in (check all that apply)</td></tr>';
	for (i=0;i<aGenericFirst.length;i++){
		if (nCol == 1)
		{
			s += '<tr>\n';
			bRowClosed = false;
		}
		tag = '_CUST' + (i+1);
		s += MakeXHTMLCheckBox(w, "interest", tag + '~' + aGenericFirst[i],
		  (ArrayIndex(aCheckTags, tag) > -1), aGenericFirst[i]);
		nCol++;
		if (nCol > nMaxCols)
		{
			nCol = 1;
			s += '\n</tr>';
			bRowClosed = true;
		}
	}

	if (gaLinkTable) {
		for (i=0; i<gaLinkTable.length; i++){

			tag = trim(gaLinkTable[i].idPg);

			// Keep track of which tags done so they are not repeated
			bDone = false;
			for (j=0; j < aDone.length; j++){
				if (aDone[j] == tag){
					bDone = true;
				}
			}
			if ((!bDone) && (tag != null) && (tag != "") && (tag.indexOf('SNJ') != 0)) {
			  // skip if prefix is SNJ or if this tag already done

				aDone[aDone.length] = tag;
				if (nCol == 1) {
					s += '<tr>\n';
					bRowClosed = false;
				}
				s += '<a name="' + idPg + '" id="' + tag + '"></name>';
				s += MakeXHTMLCheckBox(w, "interest", tag + '~' + gaLinkTable[i].title,
		  		(ArrayIndex(aCheckTags, tag) > -1),
		  		EscapeHTMLEntities(gaLinkTable[i].title),
		  		gaLinkTable[i].href);
				nCol++;
				if (nCol > nMaxCols) {
					nCol = 1;
					s += '\n</tr>'
					bRowClosed = true;
				}
			}

		} //for
	} else {
		// TBD if fallback is needed
	}
	for (i=0;i<aGenericLast.length;i++){
		if (nCol == 1)
		{
			s += '<tr>\n';
			bRowClosed = false;
		}
		tag = '_CUST' + (i+1+aGenericFirst.length);
		s += MakeXHTMLCheckBox(w, "interest", tag + '~' + aGenericLast[i],
		  (ArrayIndex(aCheckTags, tag) > -1), aGenericLast[i]);
		nCol++;
		if (nCol > nMaxCols)
		{
			nCol = 1;
			s += '\n</tr>';
			bRowClosed = true;
		}
	}

	if (!bRowClosed){
		s += '\n</tr>'
	}
	s += '</table>\n';

	//alert(s);
	return s
}

function ResetAll()
{
	if (document.forms["contactform"])
	{
		frm = document.forms["contactform"];
		// Reset all page info tags
		var s = GetCookie("tags");
		if ((s != null) && (s != "") && (GetCookie("PgTagged") == "1"))
		{
			if (!confirm("This remove all the marks on pages or topics."))
			{
				return
			}
			DeleteCookie("PgTagged");
		}
		DeleteCookie("tags");
		var elems = document.forms[0].elements
		for (i=0; i < elems.length; i++)
		{
			if (elems[i].checked)
			{
				elems[i].checked = false;
				elems[i].defaultChecked = false;
			}
		}
		frm.reset();
	}
	if (gContactHow) gContactHow = null;
	DeleteCookie("Optin")

}

function ClickedWhereHeard(which)
{
	var s = "More details about how you found us?";
	if (!gbIsIE5) return;
	switch(which)
	{
		case "Advertisement": s="Where did you see this ad?"; break;
		case "Web search": s="Which search engine? Whick keywords?"; break;
		case "ADL forum": break;
		case "ADL web site (not forum)": break;
		case "Other forum or mailing list": s="From which forum or mailing list?"; break;
		case "Other web site or blog": s="From which web site?"; break;
		case "Word of mouth": s="From whom?"; break;
		case "Other": break;
		default:
	}
	document.all.whereheardprompt.innerHTML = s;
}

var gContactHow = null;

function ClickedInfoHow(which)
{
	gContactHow = which;
}

/*

function ContactFormSanityCheck()
{
alert("sanity check");
	var s = "";
	var bOK = true;
	var bReqOK = true;
	var v = null;
	var aProblems = new Array();
	var p=0, i=0;

	var frm = document.forms[0];
	var frmMagic = document.forms[1];
	var elems = frm.elements;
	for (i=0; i<elems.length; i++)
	{
		if ((elems[i].type == "textarea")||(elems[i].type == "text"))
		{
			//alert("trimming " + elems[i].name);
			elems[i].value = trim(elems[i].value);
		}
	}

	// Paranoia
	for (i = 0; i < frm.elements.length; i++)
	{
		if ((elems[i].value) && (elems[i].value.indexOf('\<') > -1))
		{
			elems[i].value = "";
			alert("Sorry, disallowed or illegal input.")
			return
		}
	}

	v = elems["name"].value;
	if (v != null)
	{
		v = trim(v);
		elems["name"].value = v;
	}
	if ((v == null) || (trim(v) == ""))
	{
		aProblems[aProblems.length] = "Your name is required."
		bReqOK = false;
		bOK = false;
	}
	v = elems["email"].value;
	if (v != null)
	{
		v = trim(v);
		elems["email"].value = v;
	}
	if ((v == null) || (trim(v) == ""))
	{
		aProblems[aProblems.length] = "Your e-mail address is required."
		bReqOK = false;
		bOK = false;
	}
	else
	{
		//p = v.indexOf("@");
		//if ((p < 1) || (p == v.length-1) || (v.indexOf(" ") > -1))
		if (!TestEmailAddress(v))
		{
			aProblems[aProblems.length] = '"' + v + '" is not a usable e-mail address.';
			bReqOK = false;
			bOK = false;
		}
	}

	var sContactHow = GetRadioButtonValue(elems["InfoHow"])
	//alert("sContactHow = " + sContactHow)
	if ((sContactHow == null) || (sContactHow == "(nothing selected)"))
	{
		bOK = false;
		aProblems[aProblems.length] = "You did not specify the preferred way to contact you."
	}
	else if (sContactHow.indexOf("Mail") > -1)
	{
		v = elems["address1"].value;
		if ((v == null) || (trim(v) == ""))
		{
			aProblems[aProblems.length] = "You would like to be contacted by regular mail, but you did not provide an address. oops."
			bOK = false;
		}
		else
		{
			v = elems["zip"].value;
			if ((v == null) || (trim(v) == ""))
			{
				aProblems[aProblems.length] = "You would like to be contacted by regular mail, but you did not provide a zip or postal code."
				bOK = false;
			}
		}
	}
	else if (sContactHow.indexOf("Fax") > -1)
	{
		v = elems["faxphone"].value;
		if ((v == null) || (trim(v) == ""))
		{
			aProblems[aProblems.length] = "You would like to be contacted by fax, but you did not provide a fax number."
			bOK = false;
		}
	}
	else if (sContactHow.indexOf("Call") > -1)
	{
		v = elems["voicephone"].value;
		if ((v == null) || (trim(v) == ""))
		{
			aProblems[aProblems.length] = "You would like to be contacted by phone, but you did not provide a phone number."
			bOK = false;
		}
	}

	if (bOK)
	{
		// Format some of the data for easier reading
		MakeFormattedSummary(frm);
		frm.submit();
	}
	else
	{
		s += "There seems to be a problem with the data in this form:\n\n";
		for (i = 0; i < aProblems.length; i++)
		{
			s += aProblems[i] + '\n\n';
		}
		if (bReqOK)
		{
			s += "Unless we already have this information on file, we won't be able to contact you." +
			 "\n\nChoose OK to proceed, or Cancel to return to the form.";
			if (confirm(s)){
				MakeFormattedSummary(frm);
				frm.submit()
			}
		}
		else
		{
			alert(s);
		}
	}
}
*/

function MakeFormattedSummary(frm)
{
	if (frm == null) frm = document.forms[0];

	var frmMagic = document.forms[1];

	var aErr = new Array();

	var elems = frm.elements;

	var bPrefEmail = elems["byEmail"].checked;
	var bPrefPhone = elems["byPhone"].checked;
	var bPrefMobile = elems["byMobile"].checked;
	var bPrefFax = elems["byFax"].checked;
	var bPrefMail = elems["byMail"].checked;

	var phoneBestTime = sanestring(trim(elems["phoneBestTime"].value))
	var mobileBestTime = sanestring(trim(elems["mobileBestTime"].value))

	var realname = sanestring(trim(elems["fldrealname"].value));
	var title = sanestring(trim(elems["fldtitle"].value));
	var org = sanestring(trim(elems["fldorg"].value));
	var subject = sanestring(trim(elems["fldsubject"].value));
	var eMail = sanestring(trim(elems["fldemail"].value));
	var phone = sanestring(trim(elems["fldphone"].value));
	var mobile = sanestring(trim(elems["fldmobile"].value));
	var fax = sanestring(trim(elems["fldfax"].value));
	var mail = sanestring(trim(elems["fldaddress"].value));
	var project = sanestring(trim(elems["fldproject"].value));
	var question = sanestring(trim(elems["fldquestion"].value));

	var sL = ""

	if (eMail != "")
	{
		sL += "\nEmail: " + eMail;
		if (!TestEmailAddress(eMail))
		{
			aErr[aErr.length] = '"' + eMail + '" is not a usable e-mail address.';
		}
	}

	if (realname != "") sL += "\nName: " + realname
	else aErr[aErr.length] = "Your name is required.";

	if (title != "") sL += "\nTitle: " + title;

	if (org != "") sL += "\nOrg: " + org;

	if (subject == "") subject = "(no subject)";
	else subject = subject;

	sL += "\nSubject: " + subject;

	if (phone != "")
	{
		sL += "\nPhone: " + phone
		if (!TestPhoneNumber(phone))
		{
			aErr[aErr.length] = '"' + phone + '" is not a usable phone number.';
		}
	}
	if (mobile != "")
	{
		sL += "\nMobile: " + mobile
		if (!TestPhoneNumber(mobile))
		{
			aErr[aErr.length] = '"' + mobile + '" is not a usable phone number.';
		}
	}
	if (fax != "")
	{
		sL += "\nFax: " + fax
		if (!TestPhoneNumber(fax))
		{
			aErr[aErr.length] = '"' + fax + '" is not a usable phone number.';
		}
	}

	if (mail != "") sL += "\nAddress:\n " + mail

	sL += "\nPref. contact method(s):"

	if (bPrefEmail)
	{
		if (eMail == "") aErr[aErr.length] = "You indicated that you would like to be contacted by email but you did not provide an email address.";
		sL += " Email"
	}

	if (bPrefPhone)
	{
		if (phone == "") aErr[aErr.length] = "You indicated that you would like to be contacted by phone but you did not provide a phone number.";
		sL += " Phone"
		if (phoneBestTime != "") sL += "(" + phoneBestTime + ")";
	}

	if (bPrefMobile)
	{
		if (mobile == "") aErr[aErr.length] = "You indicated that you would like to be contacted by mobile but you did not provide a mobile number.";
		sL += " Mobile ";
		if (mobileBestTime != "") sL += "(" + mobileBestTime + ")";
	}

	if (bPrefFax)
	{
		if (fax == "") aErr[aErr.length] = "You indicated that you would like to be contacted by fax but you did not provide a fax number.";
		sL += " Fax"
	}

	if (bPrefMail)
	{
		if (mail == "") aErr[aErr.length] = "You indicated that you would like to be contacted by mail but you did not provide an address.";
		sL += " Mail"
	}

	if (aErr.length == 0)
	{
		if ((eMail == "") && (phone == "") && (mobile=="") && (fax == "") && (mail == ""))
		{
			aErr[aErr.length] = "You did not specify any way to contact you.";
		}
	}

	if (project != "") sL += "\nProject: " + project;
	if (question != "") sL += "\nQuestion: " + question;


	if (aErr.length > 0)
	{
		sL = "Sorry, there is a problem.";
		for (i = 0; i < aErr.length; i++)
		{
			sL += "\n\n" + aErr[i];
		}
		alert(sL);
	}
	else
	{
		frmMagic = document.getElementById("formMagic");
		elems = frmMagic.elements;
		elems["message"].value = sL;
		elems["subject"].value = "[OstynWebForm] " + subject;
		elems["realname"].value = realname;
		elems["email"].value = eMail;

		if (confirm("Thank you. This is the information that will be submitted. "+
		   "Please click OK to continue, or Cancel if you don't want to submit this." +
       "\n\n" +
       ReplaceLineBreaksForDisplay(elems["message"].value)))
		{
			frmMagic.submit();
		}
		else alert("Submit cancelled.");
	}

	return (aErr.length == 0);
}








