/*

	NOTES
		Country listing from spreadsheet drives the condition
*/
var testing = false;
var xsltFile = '/xslt/salesRepsUsImgMap.xslt';
var xmlFile = '/xml/content/locator_usDistributors.xml';
    xmlFile = '/xml/content/salesContacts.xml';
var globalStateSelector = '';			//This is to find
var globalStateListing = '';			//This is to display
var globalStateListingFull = ''; 		//Display full name
var globalStateChoice = '';
var globalRegionChoice = 'US';
var mapSwitchNavColor = '';				//Styles initiallized from the style sheet - see getMapSwitchNavStyles()
var mapSwitchNavColorYAH = '';			
var mapSwitchNavTextDecoration = '';
var mapSwitchNavTextDecorationYAH = '';

function getState(stateAbr,stateFullName){
	var HTMLDisp = '';
	var results = document.getElementById('clickMapResults');
	
	globalStateListingFull = stateFullName;	
	globalStateSelector = stateAbr;
	globalStateChoice = stateAbr;
	
	if(stateAbr != 'XX'){
		globalStateListing = stateAbr;
	}
	importXML(xmlFile,'parseXml');
	return false;
}
function loadMapLocator(){
	/* same as explodeMapLocator() function at homePage.js */
	document.getElementById('locatorExplodeMap').style.display = 'block';
	xmlhttpPost('/O2Net/Applications/TransformDoc/default.aspx?XmlDoc=' + xmlFile + '&XmlVars=mapUse|internal&XsltDoc=' + xsltFile,'locatorExplodeMap');
}
function getMapSwitchNavStyles(){
	mapSwitchNavColor = getStyle('mapSwitchNav_INT','color');
	mapSwitchNavColorYAH = getStyle('mapSwitchNav_US','color');
	mapSwitchNavTextDecoration = getStyle('mapSwitchNav_INT','text-decoration');
	mapSwitchNavTextDecorationYAH = getStyle('mapSwitchNav_US','text-decoration');
}
function parseXml(xmlDoc) {
	var HTMLDisp = '';
	var x = xmlDoc.getElementsByTagName('listing');
	var stateChosen = window.globalStateChoice;
	var results = document.getElementById('clickMapResults');
	var stateRepresented = '';
	var region = window.globalRegionChoice;
	// xml node vars
	var locateMatchState = '';
	var salesRepName = '';
	var salesRepPhone = '';
	var salesRepFax = '';
	var salesRepEmail = '';
	var countListings = 0;

	for (i=0;i<x.length;i++)
	{	
		if(i == 0){
			HTMLDisp = HTMLDisp + '<div id=\"returnMap\"><a href=\"#\" onClick=\"backToMap();return false\">Back to map</a></div>';
			if(window.globalStateListingFull.length > 1){
				HTMLDisp = HTMLDisp + '<div id=\"stateChoosen\">' + window.globalStateListingFull + '</div>';
			}else{
				HTMLDisp = HTMLDisp + '<div id=\"stateChoosen\">' + window.globalStateListing + '</div>';
			}	
		}	
		for (j=0;j<x[i].childNodes.length;j++)
		{
			if (x[i].childNodes[j].nodeType != 1) continue;
			stateRepresented = x[i].childNodes[0].firstChild.nodeValue;
			if(stateRepresented.indexOf(stateChosen) > -1){
				if(testing){
					HTMLDisp = HTMLDisp + '<li>' + j + '=========' + '</li>';
					HTMLDisp = HTMLDisp + '<li>' + j + '---' + x[i].childNodes[j].firstChild.nodeValue + '</li>';
				}
				// xml node vars
				locateMatchState = x[i].childNodes[0].firstChild.nodeValue;
				salesRepName = x[i].childNodes[1].firstChild.nodeValue;
				salesRepPhone = x[i].childNodes[2].firstChild.nodeValue;
				salesRepFax = x[i].childNodes[3].firstChild.nodeValue;
				salesRepEmail = x[i].childNodes[4].firstChild.nodeValue;
				countListings ++;
			}
		}
		if(stateRepresented.indexOf(stateChosen) > -1){
			/*
				Condition needed to not record the same last entry for remainder of loop
				Cannnot place this within the J loop
			*/
			HTMLDisp = HTMLDisp + '<div class=\"company\">';
			HTMLDisp = HTMLDisp + '	<div class=\"contactName\">';
			HTMLDisp = HTMLDisp + 		salesRepName;
			HTMLDisp = HTMLDisp + '	</div>';
			HTMLDisp = HTMLDisp + '	<div class=\"companyContactPhone\">';
			HTMLDisp = HTMLDisp + '		Phone: ';
			HTMLDisp = HTMLDisp + 		salesRepPhone;
			HTMLDisp = HTMLDisp + '	</div>';
			HTMLDisp = HTMLDisp + '	<div class=\"companyContactPhone\">';
			HTMLDisp = HTMLDisp + '		FAX: ';
			HTMLDisp = HTMLDisp + 		salesRepFax;
			HTMLDisp = HTMLDisp + '	</div>';			
			HTMLDisp = HTMLDisp + '	<div class=\"companyAddress1\">';
			HTMLDisp = HTMLDisp + '		<a href=\"MailTo:\"' + salesRepEmail + '\">' + salesRepEmail + '</a>';
			HTMLDisp = HTMLDisp + '	</div>';
			if(window.globalStateChoice == 'XX'){
				HTMLDisp = HTMLDisp + '	..';	//Display something that means nothing to visitor to indicate default
			}
			HTMLDisp = HTMLDisp + '</div>';
		}
	}
	if(testing){
		HTMLDisp = HTMLDisp + "countListings " + countListings;
	}
	/* ~~~~~~~~~~~~~~~~~~~~[ default for no state found ] ~~~~~~~~~~~~~~~~~~ */
	if(countListings == 0){
		if(window.globalStateChoice != 'XX'){
			globalStateChoice = 'XX';
			parseXml(xmlDoc);
		}else{	
			HTMLDisp = HTMLDisp + 'NO LISTING';
			HTMLDisp = HTMLDisp + '<div class=\"hide\">No rows found with a trigger of XX which signifies a default return</div>';
			results.innerHTML = HTMLDisp;
		}	
	}else{
		results.innerHTML = HTMLDisp;
	}
	// Turn off the maps, display the results
	document.getElementById('locatorExplodeMapShell').style.display = 'none';
	document.getElementById('locatorExplodeMapShellCan').style.display = 'none';
	document.getElementById('locatorExplodeMapShellInternational').style.display = 'none';
	document.getElementById('clickMapResults').style.display = 'block';
}
function changeMap(mapType,obj){
	var results = document.getElementById('clickMapResults');
	var navIdYAH = 'mapSwitchNav_' + mapType;
	
	document.getElementById('mapSwitchNav_US').style.color = window.mapSwitchNavColor;
	document.getElementById('mapSwitchNav_CAN').style.color = window.mapSwitchNavColor;
	document.getElementById('mapSwitchNav_INT').style.color = window.mapSwitchNavColor;	
	document.getElementById(navIdYAH).style.color = window.mapSwitchNavColorYAH;
	
	document.getElementById('mapSwitchNav_US').style.textDecoration = window.mapSwitchNavTextDecoration;
	document.getElementById('mapSwitchNav_CAN').style.textDecoration = window.mapSwitchNavTextDecoration;
	document.getElementById('mapSwitchNav_INT').style.textDecoration = window.mapSwitchNavTextDecoration;
	document.getElementById(navIdYAH).style.textDecoration = window.mapSwitchNavTextDecorationYAH;
	
	if(mapType == 'US'){
		document.getElementById('locatorExplodeMapShell').style.display = 'block';
		document.getElementById('locatorExplodeMapShellCan').style.display = 'none';
		document.getElementById('locatorExplodeMapShellInternational').style.display = 'none';
		globalRegionChoice = 'US';
	}else if(mapType == 'CAN'){
		document.getElementById('locatorExplodeMapShell').style.display = 'none';
		document.getElementById('locatorExplodeMapShellCan').style.display = 'block';
		document.getElementById('locatorExplodeMapShellInternational').style.display = 'none';
		document.getElementById('clickMapResults').style.display = 'none';
		globalRegionChoice = 'Canada';
	}else if(mapType == 'INT'){
		document.getElementById('locatorExplodeMapShell').style.display = 'none';
		document.getElementById('locatorExplodeMapShellCan').style.display = 'none';
		document.getElementById('locatorExplodeMapShellInternational').style.display = 'block';
		document.getElementById('clickMapResults').style.display = 'none';
		globalRegionChoice = 'International';		
	}else if(mapType.length > 2){
		/* default */
		document.getElementById('locatorExplodeMapShell').style.display = 'none';
		document.getElementById('locatorExplodeMapShellCan').style.display = 'none';
		document.getElementById('locatorExplodeMapShellInternational').style.display = 'none';
		document.getElementById('clickMapResults').style.display = 'block';
		globalRegionChoice = mapType;
	}else{
		alert('mapType not set');
		globalRegionChoice = 'US';
	}
	results.innerHTML = 'Click above to see reps for your area.';
}
function backToMap(){
	if(window.globalRegionChoice == 'US'){
		document.getElementById('locatorExplodeMapShell').style.display = 'block';
		document.getElementById('clickMapResults').style.display = 'none';
	}else if(window.globalRegionChoice == 'Canada'){
		document.getElementById('locatorExplodeMapShellCan').style.display = 'block';
		document.getElementById('clickMapResults').style.display = 'none';
	}else if(window.globalRegionChoice == 'International'){
		document.getElementById('locatorExplodeMapShellInternational').style.display = 'block';
		document.getElementById('clickMapResults').style.display = 'none';		
	}else{
		document.getElementById('locatorExplodeMapShell').style.display = 'block';
		document.getElementById('clickMapResults').style.display = 'none';
	}	
}
function getStyle(el,styleProp)
{
	/*
		http://www.quirksmode.org/dom/getstyles.html
		Pass styles in as style sheet declorations, IE wants javascript way
	*/	
	var x = document.getElementById(el);
	if (x.currentStyle){
		//ie
		switch(styleProp)
		{
		case 'text-decoration':
		  styleProp = 'textDecoration';
		  break;    
		default:
		}
		var y = x.currentStyle[styleProp];
	}else if (window.getComputedStyle){
		var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
	}
	return y;
}


