var searchRequest = null;
var searchMessage = null;
var searchShowClose = false;

function getStoreList ( region, country, state, brand, language )
{
	region = ( new Number ( region ) ).valueOf ( );
	brand = ( new Number ( brand ) ).valueOf ( );
	country = decodeURIComponent ( decodeURIComponent ( country ) );
	state = decodeURIComponent ( decodeURIComponent ( state ) );

	searchMessage = getMessage ( "searchingLabel" );
	searchShowClose = false;
	showPopup ( searchMessage, searchShowClose );
	
	var searchRequest = new xmlRPCRequest ( cgiURL + '/Go.cgi?a_application=SiteXMLRPCApp', 'lth.getStoreList', onGetStoreList );
	
	searchRequest.addParameter ( region );
	searchRequest.addParameter ( country );
	searchRequest.addParameter ( state );
	searchRequest.addParameter ( brand );
	searchRequest.addParameter ( language );
	
	searchRequest.send ( );
}

function onGetStoreList ( )
{
	if ( this.request.readyState != 4 )
	{
		return;
	}
	
	try
	{
		onRequestErrorCheck ( this );
		
		searchMessage = null;

		hidePopup ( );
		
		var html = '';
		
		if ( this.response != null && this.response.length > 0 )
		{
			html = formatStores ( this.response );
		}
		else
		{
			html = '<div style="padding-top: 20px;">' + getMessage ( "emptySearchResultLabel" ) + '</div>';
		}

		document.getElementById ( "storeList" ).innerHTML = html;
	}
	catch ( e )
	{
		searchMessage = e;
		searchShowClose = true;
		showPopup ( searchMessage, searchShowClose );
	}
	
	this.complete ( );
}

function formatStores ( stores )
{
	var html = '<table width="660" border="0" cellspacing="0" cellpadding="0" style="margin-top: 20px;">';
	
	var currentCountry = null;
	var currentType = null;
	
	for ( var index = 0; index < stores.length; index++ )
	{
		var id			= decodeURIComponent ( stores [ index ].id );
		var type		= decodeURIComponent ( stores [ index ].type );
		var name		= decodeURIComponent ( stores [ index ].name );
		var address		= decodeURIComponent ( stores [ index ].address );
		var city		= stores [ index ].city ==null ? null : decodeURIComponent ( stores [ index ].city );
		var state		= stores [ index ].state == null ? null : decodeURIComponent ( stores [ index ].state );
		var country		= decodeURIComponent ( stores [ index ].country );
		var postalCode	= stores [ index ].postalCode == null ? null : decodeURIComponent ( stores [ index ].postalCode );
		var mapRaw		= stores [ index ].mapRaw == null ? null : decodeURIComponent ( stores [ index ].mapRaw );
		var hours		= stores [ index ].hours == null ? null : decodeURIComponent ( stores [ index ].hours );
		var moreInfo	= stores [ index ].moreInfo == null ? null : decodeURIComponent ( stores [ index ].moreInfo );
		var brands		= stores [ index ].brands == null ? null : decodeURIComponent ( stores [ index ].brands );
	
		if ( country != currentCountry && index > 0 ) 
		{
			html += '<tr><td colspan="5" style="padding:0px 0px 20px 0px;border-top-width:1px;border-top-style:solid;border-top-color:#F1F2F2;"><img src="images/spacer.gif" alt="" width="1" height="1" border="0" /></td></tr>';
		}
		
		html += '<tr><td nowrap valign="top" align="left" style="padding:0px 4px 20px 4px;">';
		
		if ( country != currentCountry ) 
		{
			currentCountry = country;
			currentType = null;
			html += currentCountry;
		}

		html += '</td>';
		html += '<td nowrap valign="top" align="left" style="padding:0px 4px 20px 4px;">';
 
		if ( type != currentType ) 
		{
			currentType = type;
			
			if ( currentType == 1 )
				html += getMessage ( "retailStoresLabel" );
			
			if ( currentType == 2 )
				html += getMessage ( "outletStoresLabel" );
			
			if ( currentType == 3 )
				html += getMessage ( "departmentStoresLabel" );
			
			if ( currentType == 4 )
				html += getMessage ( "specialtyStoresLabel" );
		}

		html += '</td>';
		
		html += '<td valign="top" align="left" style="padding:0px 4px 20px 4px;">';
		html += '<b>' + name + '</b>';
		html += '</td>';
						
		html += '<td valign="top" align="left" style="padding:0px 4px 20px 4px;">';
		
		if ( currentLanguage != 'ja_JP' || country != getMessage ( 'japanCountryValue' ) ) 
		{
			html += address;
			
			if ( city != null && city.length ) 
			{
				html += ' ' + city;
			}
			
			if ( state != null && state.length ) 
			{
				html += ', ' + state;
			}
			
			if ( postalCode != null && postalCode.length ) 
			{
				html += ' ' + postalCode;
			}
		} 
		else 
		{
			if ( postalCode != null && postalCode.length ) 
			{
				html += postalCode;
			}
			
			if ( state != null && state.length ) 
			{
				html += state;
			}
			
			if ( city != null && city.length ) 
			{
				html += city;
			}
			
			html += address;
		}
		
		html += '<br />';
		
		if ( moreInfo != null && moreInfo.length )
		{
			html += moreInfo + '<br />';
		}
		
		if ( hours != null && hours.length )
		{
			html += hours + '<br />';
		}
		
		if ( brands != null && brands.length )
		{
			html += '<span style="color:#999999;">' + brands + '</span>';
		}
		
		html += '</td><td nowrap valign="top" align="left" style="padding:0px 4px 20px 4px;">';
		
		if ( mapRaw != null && mapRaw.length )
		{
			html += '<a href="javascript:popUp(\'map_' + id + '.htm\',\'map\',\'520\',\'570\', 0, 0, \'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=1,resizable=1\')">' + getMessage ( 'mapLabel' ) + '</a>';
		}
		
		html += '</td></tr>';
	}

	html += '</table>';
	
	return html;
}

function getChildElementById ( parent, id )
{
	if ( parent == null )
	{
		return document.getElementById ( id );
	}
	
	if ( parent.id == id )
	{
		return parent;
	}
	
	var element = null;
	
	for ( var index = 0; index < parent.childNodes.length; index++ )
	{
		element = getChildElementById ( parent.childNodes [ index ], id );
		
		if ( element != null )
		{
			break;
		}
	}
	
	return element;
}

function setElementVisibility ( parent, id, visible, height )
{
	var element = getChildElementById ( parent, id );
	
	setElementVisibility2 ( element, visible, height );
	
	return element;
}

function setElementVisibility2 ( element, visible, height )
{
	element.style.visibility = visible ? 'visible' : 'hidden';
	element.style.height = visible ? ( height == null ? 'auto' : height ) : 0;
}

function linkElement ( element )
{
	if ( element.formerParentNode != null && element.formerParentNode != element.parentNode )
	{
		element.formerParentNode.appendChild ( element );
	}
}

function unlinkElement ( element )
{
	if ( element.formerParentNode == null )
	{
		element.formerParentNode = element.parentNode;
	}
	
	if ( element.parentNode != null )
	{
		element.parentNode.removeChild ( element );
	}
}

function showPopup ( message, showClose, left, top )
{
	showPopupEx ( message, showClose ? [ { "link": 'javascript:hidePopup ( )', "text": getMessage ( "closeWindowLabel" ) } ] : null, top, left );
}

function showPopupEx ( message, buttons, left, top )
{
	var popupContainer	= setElementVisibility ( null, "popupContainer", true, 307 );
	var popup			= getChildElementById ( popupContainer, "popup" );
	var popupText		= getChildElementById ( popupContainer, "popupText" );
	var buttonContainer	= getChildElementById ( popupContainer, "closeButton" );
	
	popupText.innerHTML = message;
	
	if ( buttons != null )
	{
		var html = "";
		
		for ( var index = 0; index < buttons.length; index++ )
		{
			// We assume the link is okay to be embedded in double quotes.
			
			html += '<a href="' + buttons [ index ].link + '">' + buttons [ index ].text + '</a>';
			
			if ( index < buttons.length - 1 )
			{
				html += ' | ';
			}
		}
		
		buttonContainer.innerHTML = html;
		buttonContainer.style.top = popup.offsetHeight - 40;
		buttonContainer.style.visibility = "visible";
	}
	else
	{
		buttonContainer.style.visibility = "hidden";
	}
	
	if ( top != null && left != null )
	{
		popupContainer.style.left = left + popupContainer.parentNode.scrollLeft;
		popupContainer.style.top = top + popupContainer.parentNode.scrollTop;
	}
}

function hidePopup ( )
{
	var popupContainer	= setElementVisibility ( null, "popupContainer", false, null );
	var closeButton		= getChildElementById ( popupContainer, "closeButton" );
	closeButton.style.visibility = "hidden";
}
