/*gaLinkTable
			x.idPg = idPg;
			x.href = href;
			x.title = title;
			x.idParent = idParent;
*/

function testemail(s)
{
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return (regex.test(s))
}

var gaContactHow = new Array("Via Email","Via Mail","Via Phone","Via Fax");
var gContactHow = null;

function ClickedInfoHow(which)
{
	gContactHow = which;
}


function initPgData()
{
	var elems=document.forms["contactform"].elements;

	SetRadioButtonValue(elems["InfoHow"], "Via Email")
	SetRadioButtonValue(elems["AddToMailingList"], "No")

	if (gbCookiesOK)
	{
		//alert("safe cookies read")
		window.status="Safe cookies OK"
		var i=0, j=0, a = new Array();
		var s = GetCookie("grp1");
		if (s != "")
		{
			a = s.split("{^}");
			i = 0;
			if (a[i]) elems["name"].value = a[i++];
			if (a[i]) elems["email"].value = a[i++];
			if (a[i]) elems["address1"].value = a[i++];
			if (a[i]) elems["address2"].value = a[i++];
			if (a[i]) elems["city"].value = a[i++];
			if (a[i]) elems["state"].value = a[i++];
			if (a[i]) elems["zip"].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]) SetRadioButtonSetting(elems["AddToMailingList"], a[i++]);
			if (a[i]) SetRadioButtonSetting(elems["InfoHow"], a[i++]);
			if (a[i]) SetRadioButtonSetting(elems["howSoon"], a[i++]);
			if (a[i]) SetRadioButtonSetting(elems["hasAgent"], a[i++]);
			if (a[i]) SetRadioButtonSetting(elems["whereheard"], a[i++]);
		}
		s = GetCookie("grp2");
		if (s != "")
		{
			var a = s.split("{^}");
			i = 0;
			elems["comments"].value = a[i++];
			elems["bestTimeToCall"].value = a[i++];
			elems["travelagent"].value = a[i++];
			elems["whereheardmoredetails"].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()
{
	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%" class="formCell" 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 = '<p>Please check the tour(s) or topic(s) you '
  // + 'are most interested in (check all that apply</p>';
	var s = '<table border="0" cellspacing="4" cellpadding="0" summary="table of checkboxes for different information topics">\n';
	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="' + tag + '" 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("Are you sure you want to clear the form? This will also remove all the bookmarks on pages or topics.\nOK to clear, Cancel otherwise."))
			{
				return;
			}
			DeleteCookie("PgTagged");
		}
		else
		{
			if (!confirm("Are you sure you want to clear the form?\nOK to clear, Cancel otherwise."))
			{
				return;
			}
		}
		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 "Travel Agent": s="Travel agent name:"; break;
		case "Article": s="In which publication or web site?"; break;
		case "Link": s="From which web site?"; break;
		case "Word of mouth": s="From whom?"; break;
		case "Tourist Office of Spain": s="Web site, publication, or a particular office?"; break;
		case "Tourist Office of Portugal": s="Web site, publication, or a particular office?";break;
		case "Other": break;
		default:
	}
	document.all.whereheardprompt.innerHTML = s;
}


var gbContactFormDebug = false;

function ContactFormSanityCheck()
{
	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 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;
		gbContactFormDebug = (v.toLowerCase() == "claudedebug");
	}
	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 (!testemail(v))
		{
			aProblems[aProblems.length] = '"' + v + '" is not a valid e-mail address.';
			bReqOK = false;
			bOK = false;
		}
	}

	var sContactHow = GetRadioButtonValue(frm,"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 specified that you prefer to be contacted by regular mail, but you did not specify an address."
			bOK = false;
		}
		else
		{
			v = elems["zip"].value;
			if ((v == null) || (trim(v) == ""))
			{
				aProblems[aProblems.length] = "You specified that you prefer to be contacted by regular mail, but you did not specify 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 specified that you prefer to be contacted by fax, but you did not specify a fax number."
			bOK = false;
		}
	}
	else if (sContactHow.indexOf("Call") > -1)
	{
		v = elems["voicephone"].value;
		if ((v == null) || (trim(v) == ""))
		{
			aProblems[aProblems.length] = "You specified that you prefer to be contacted by phone, but you did not specify a phone number."
			bOK = false;
		}
	}

	if (bOK)
	{
		// Format some of the data for easier reading
		MakeFormattedSummary(frm);
		if (!gbContactFormDebug) 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)
{
		var elems = frm.elements;

		var sL = "\n-------------------------------"
		var sF = "\nName: " + sanestring(trim(elems["name"].value));
		sF += "\nEmail: " + sanestring(trim(elems["email"].value));
		sF += "\nTA: " + (elems["IsTravelAgent"].checked?"Yes":"No");
		sF += "\nAdd to mailing list: " + GetRadioButtonValue(frm,"AddToMailingList");
		sF += "\nHow soon: " + GetRadioButtonValue(frm,"howSoon");
		sF += sL;
		var aS = null;
		var s = ""
		obj = frm.elements["interest"]
		for (i=0;i<obj.length;i++)
		{
			if (obj[i].checked)
			{
				aS = obj[i].value.split("~");
				s += '\n* ' + aS[1] + ' ('+ aS[0] + ')';
			}
		}
		if (s != "")
		{
			s = "\nInterests:" + s;
		}
		else
		{
			s = "\nInterests: (Nothing selected)"
		}
		sF += s;
		sF += sL;
		s = sanestring(trim(elems["comments"].value));
		if (s != "")
		{
			sF += "\nComments: " + s;
		}
		else
		{
			sF += "\n(no comments)";
		}
		sF += sL;
		sF += "\nHow to send info: " + GetRadioButtonValue(frm,"InfoHow");
		sF += sL;
		sF += "\nFavorite travel agent: " + GetRadioButtonValue(frm,"hasAgent");
		s = sanestring(trim(elems["travelagent"].value));
		if (s != "")
		{
			sF += "\nTravel agent info: " + s;
		}
		else
		{
			if (GetRadioButtonValue(frm,"hasAgent") == "Yes")
			{
				sF += " (no details)"
			}
		}
		sF += sL;
		sF += "\nContact Info:\n" + elems["name"].value;
		s = sanestring(trim(elems["address1"].value));
		if (s != "")
		{
			sF += "\nAddress1: " + s;
		}
		s = sanestring(trim(elems["address2"].value));
		if (s != "")
		{
			sF += "\nAddress2: " + s;
		}
		s = sanestring(trim(elems["city"].value));
		if (s != "")
		{
			sF += "\nCity: " + s;
		}
		s = sanestring(trim(elems["state"].value));
		if (s != "")
		{
			sF += "\nState: " + s;
		}
		s = sanestring(trim(elems["zip"].value));
		if (s != "")
		{
			sF += "\nZip: " + s;
		}
		s = sanestring(trim(elems["country"].value));
		if (s != "")
		{
			sF += "\nCountry: " + s;
		}
		s = sanestring(trim(elems["voicephone"].value));
		if (s != "")
		{
			sF += "\nPhone: " + s;
		}
		s = sanestring(trim(elems["faxphone"].value));
		if (s != "")
		{
			sF += "\nFax: " + s;
		}
		s = sanestring(trim(elems["bestTimeToCall"].value));
		if (s != "")
		{
			sF += "\nBest time to call: " + s;
		}
		sF += sL;
		sF += "\nWhere heard: " + GetRadioButtonValue(frm,"whereheard");
		s = sanestring(trim(elems["whereheardmoredetails"].value));
		if (s != "")
		{
			sF += "\nDetails: " + s;
		}
		sF += sL;

		elems["formattedData"].value = sF;

		if (gbContactFormDebug) alert(sF);

		if ((gbCookiesOK) && (gbCookiesOK == true))
		{
			s = GetCookie("refby")
			if (s != "")
			{
				// extract date
				var a = s.split("\n");
				var sNow = (new Date()).toUTCString();
				if ((!a[1]) || (a[1].substr(0,16) != sNow.substr(0,16)))
				{
					// this looks like another visit on another day
					s += "\n(now: " + sNow + ")"
				}
				elems["refBy"].value = s;
				s = s.toUpperCase();
				if ((s.indexOf("=CLAUDE") > -1) && (s.indexOf("=DEBUG") > -1))
				{
					alert("refBy: " + s)
				}
			}
		}
}




