// Include for Opportunities with AAA/CAA clubs page, assumes AAAJobs.js is already included
//page scripts
function showMe(val)
{
	if(val == "US")
	{
		document.getElementById("selUS").style.display = "inline"; 
		document.getElementById("selCanada").style.display = "none"; 
		
	}
	else
	{
		document.getElementById("selUS").style.display = "none"; 
		document.getElementById("selCanada").style.display = "inline"; 
	}
	//set hidden country field
	document.getElementById("fldCountry").value = val;
}

//new window, print view
var couphtm = ""; 
var msgWindow;
var cnt = 0; 
function showResultsPrintView(htm,doc)
{
	try
	{
		var id = "divResults"; 
		couphtm = document.getElementById(id).innerHTML; 
		var doc = msgWindow.document;
		doc.getElementsByTagName("DIV")[0].innerHTML = couphtm;
	}
	catch(e)
	{
		if(cnt < 15) window.setTimeout(showResultsPrintView, 250);
		cnt++;
	}
}

//print results
function printResults()
{
	msgWindow= window.open("ViewResults.htm","msgWindow","height=600,width=800,screenX=150,screenY=120,left=150,top=50,scrollbars=yes,menubar=yes,toolbar=yes");
	try
	{
		window.setTimeout(showResultsPrintView, 250);
	}	
	catch(e){window.status = e.toString();}
}
//XML
var xdoc;
var nodes;

function XMLDOC(file, loadfcn)
{
	var xmlDoc;
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load(file);
		if(loadfcn) loadfcn();
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.async=false;
		xmlDoc.load(file);
		if(loadfcn) xmlDoc.onload=loadfcn;
	}
	else
	{
		alert('Your browser cannot handle this script');
		return null;
	}
	
	return xmlDoc;
}

//array of results objects
var results = [];

//returns an object 
function getResultsObj(node)
{
	var cnode, txt;
	var result = {};
	for(var i=0;i<node.childNodes.length;i++)
	{
		cnode = node.childNodes[i];
		if(cnode.nodeType == 1) //element node
		{
			txt = cnode.childNodes[0] ? cnode.childNodes[0].nodeValue : ""; 
			result[cnode.nodeName] = txt;
		}
	}
	results[results.length] = result; 
}

var maxsel = 5;
//1 = element node, 3 = text node, nodeType 3 nodeValue returns the value of the node
function getResult()
{
	var boxUS = document.getElementById("selUS");
	var boxCan = document.getElementById("selCanada");
	var box = document.getElementById("fldCountry").value == "Canada" ? boxCan : boxUS;
	
	var optVals = getSelectedVals(box);
	if(optVals.length > maxsel) 
	{
		showResult("<font color='red'><b>Too many regions are selected. Please limit your search to "+maxsel+" regions or less and try again.</b></font>");
		return;
	}
	
	if(optVals.length==0) 
	{
		showResult("<font color='red'><b>No regions are selected.</b></font>");
		return;
	}
	
	if(nodes == undefined) nodes = xdoc.getElementsByTagName("club");
	
	for (var i=0;i<nodes.length;i++)
	{
		for(var n=0;n<optVals.length;n++)
		{
			if(getChildText(nodes[i], "StateProv") == optVals[n])
			{
				getResultsObj(nodes[i]);
			}
		}
	}
	writeResults();
}

function writeResults()
{
	var tblOpen = "<table width='100%' border='0'>";
	var tblClose = "</table>";
	var hrrow = "<tr><td height='1' colspan='2'><div class='hr' style='height:1px;border-left:none;'><img src='images/shim.gif'/></div></td></tr>";
	var htm = tblOpen; 
	for(var n=0;n<results.length;n++)
	{
		if(n==0) //1st one
		{
			htm += "<tr><td colspan='2'><table border='0' cellpadding='0' cellspacing='0' width='100%'><tr><td class='h2'>" + display(results[n]["ClubName"]) + "</td><td width='60' align='right'><a href='javascript:printResults()'><img border='0' src='images/PrintIcon2.gif' width='35' align='right' style='cursor:hand;top:-25px;' alt='Print Results' /></a></td></tr></table></td></tr>";
		}
		else
			htm += "<tr><td colspan='2' class='h2'>" + display(results[n]["ClubName"]) + "</td></tr>";
		htm += "<tr><td align='right' valign='top' width='150'><b>State / Province:</b></td><td>" + display(results[n]["StateProv"]) + "</td></tr>";
		htm += "<tr><td align='right' valign='top'><b>Main Office Address:</b></td><td>" + display(results[n]["LocationAddress"]) +"<br>"+ display(results[n]["LocationCity"])+ ", "+ display(results[n]["LocationStateProv"])+", "+ display(results[n]["LocationZIPCode"])+ "</td></tr>";
		htm += "<tr><td align='right' valign='top'><b>Phone:</b></td><td>" + display(results[n]["ClubPhone"]) + "</td></tr>";
		htm += "<tr><td align='right' valign='top'><b>Fax:</b></td><td>" + display(results[n]["ClubFax"]) + "</td></tr>";
		htm += "<tr><td align='right' valign='top'><b>Web Site:</b></td><td  class='textsmall'>" + displayWebsite(results[n]["website"], results[n]["websitezip"], results[n]["ClubName"]) + "</td></tr>";
		htm += "<tr><td align='right' valign='top'><b>Email:</b></td><td>" + displayEmail(results[n]["email"],results[n]["websitezip"],results[n]["ClubName"]) + "</td></tr>";
		htm += "<tr><td align='right' valign='top'><b>Jurisdiction:</b></td><td class='textsmall'>" + display(results[n]["Jurisdiction"]) + "</td></tr>";
		htm += results[n]["OtherInfo"] ? "<tr><td align='right' valign='top'><b>Info:</b></td><td class='textsmall'>" + display(results[n]["OtherInfo"]) + "</td></tr>" : "";
		htm += hrrow;
	}
	htm += tblClose;
	showResult(htm); 	
	resetResults();
}

function display(val)
{
	return val == undefined ? "" : val;	
}

function displayEmail(val, zip, clubname)
{
	if(val == undefined) return ""; 
		
	if(val.indexOf("@") > -1) //email address, mail to
		return "<a class='linksmall' href='mailto:" + val + "'>" + val + "</a>";
	
	else //website contact form link
		return "<a class='linksmall' href='"+val+"' onclick='goWebsite(this,\""+zip+"\",\""+clubname+"\");return false;'>"+"Web Site Contact Form" +"</a>";
	
}

function displayWebsite(Url, zip, clubname)
{
	return Url == undefined ? "" : "<a class='linksmall' href='"+Url+"' onclick='goWebsite(this,\""+zip+"\",\""+clubname+"\");return false;'>Club Web Site</a>";
	
	//Url.substr(0, Url.lastIndexOf(".")+5) //was using truncated website url, might search for .com...etc.
}

function getSelectedVals(box)
{
	var selVals = [];
	for(var i=0;i<box.options.length;i++)
	{
		if(box.options[i].selected)
			selVals[selVals.length] = box.options[i].value;
	}
	return selVals;
}

function loadClubs()
{
	var file = "ClubsListing.xml"
	xdoc = XMLDOC(file);
	nodes = xdoc.getElementsByTagName("club");
	
	setOptions(nodes);
	window.setTimeout(checkSels, 250);
	
	var htm = "Please select a AAA or CAA branch by region at left."; 
	showResult(htm);
}

function checkSels()
{
	//doesn't seem to be working, fix
	if(document.getElementsByName("country")[0].checked)
		showMe("US")
	else
		showMe("Canada");	
}

function showResult(htm)
{
	document.getElementById("divResults").innerHTML = htm; 	
}

function setOptions(nodes)
{
	var boxUS = document.getElementById("selUS");
	var boxCan = document.getElementById("selCanada");
	var box, bListed;
	for(var i=0;i<nodes.length;i++)
	{
		box = getChildText(nodes[i], "Country") == "Canada" ? boxCan : boxUS; 
		var stateprov = getChildText(nodes[i], "StateProv");
		//check to see if already listed in the select box
		if(!isListed(box, stateprov))
		{
			var ind = box.options.length == 0 ? 0 : box.options.length;
			box.options[ind] = new Option(stateprov,stateprov);
		}
	}
}

function isListed(box, val)
{
	for(var n=0;n<box.options.length;n++)
	{
		if(box.options[n].value == val)
		{
			return true;
		}
	}
	return false;
}

function resetResults()
{
	results = [];
}

function getChildText(node, childnodename)
{
	for (var i=0;i<node.childNodes.length;i++)
	{
		if(node.childNodes[i].nodeName == childnodename)
		{
			return node.childNodes[i].childNodes[0] && node.childNodes[i].childNodes[0].nodeValue ? node.childNodes[i].childNodes[0].nodeValue : "";
		}
	}
	return ""; 
}

//cookie setting vars
var userzip = ""; 
var websitezip = ""; 
var clubsiteurl = ""; 
var clubsitename = ""; 
//topFrame,mainFrame, zipFrame, NewWin - GoPage
function goWebsite(Url, sitezip, sitename)
{
	clubsiteurl = Url; 
	websitezip = sitezip;
	clubsitename = sitename;
	var userZip = ReadAAACookie("hruserzip");
	if(!userZip || userZip == "-1" || userZip == "")
	{
		GoPage("getzip.htm", "_New");
	}
	else
	{
		SetAAACookie("bIsReset", "false");//need to reset afterwards
		var uz = userzip ? userzip : ReadAAACookie("hruserzip");
		GoPage("zipframe.htm?cval="+websitezip+"&uz="+uz, "_New");
		zipMessage(uz);
	}
}
var oldmessage = "";
var newmessage = ""; 
var timer; 
function zipMessage(zip)
{
	oldmessage = oldmessage ? oldmessage : document.getElementById("divMessage").innerHTML; 
	window.clearTimeout(timer);
	document.getElementById("divMessage").innerHTML = "<span style='color:red;'>Your default AAA.com or CAA.ca web site needs to be reset. Click to <a href='#' style='color:red;' onclick='GoPage(\"zipframe.htm?cval="+zip+"&close=true\",\"_New\", 100, 100, true, 2000, 2000);newmessage=\"Your default web site has been reset.\";window.setTimeout(message,250);timer=window.setTimeout(message,7000);return false;'>Reset my Default AAA.com Web Site</a>.</span>"
}
function resetMessage()
{
	 message(oldmessage, false);
}

function message(msg)
{
	//if(!msg || String(msg).length < 5) msg = newmessage;
	oldmessage = oldmessage ? oldmessage : document.getElementById("divMessage").innerHTML; 
	msg = (msg && String(msg).length > 5) ? msg : newmessage ? newmessage : oldmessage; 
	try{document.getElementById("divMessage").innerHTML = msg;} catch(e){}
	newmessage = ""; //clear the message once it's shown
}

function doUnload()
{
	/*var bIsReset = ReadAAACookie("bIsReset");
	var userZip = ReadAAACookie("hruserzip");
	if(userZip && userZip != "-1" && userZip != "" && bIsReset != "true")
	{
		try
		{
			var iFrame = document.getElementById("zippyFrame");
			var doc = (iFrame.contentWindow) ? iFrame.contentWindow.document : iFrame.contentDocument.document;
			var frm = doc.forms["zip"];
			frm["ZipCodeEntry"].value = zipcode;
			frm.target = "_self"
			frm.submit();
			SetAAACookie("bIsReset", "true");
		}
		catch(e){window.setTimeout(doUnload1,250);}
		//return false;
	}
	//return true;*/
}
function doLoad()
{
	loadClubs();
}
