/** 
Respite Splash How-To :
searchCommunityPostalCode() javascript function takes four parameters :
1- Postal Code Text Field id
2- Error message area div id for invalid postal code error message
3- English site <a> tag id
4- French site <a> tag id
5- Error message area div id for no site found error message
The function gets a postal code, parse it. 
- If it's invalid shows the div tag passed in parameter 2.
- If no site is associated with the postal code, the div passed in parameter 5 is shown.
- If an english and french site are associated with the postal code, the a tags passed in parameters 3 and 4 are shown.
- If only one language is associated with the postal code, a redirect to the appropriate site is done.
**/
function searchCommunityPostalCode(postalCodeField, invalidPostalCodeDivId, englishUrlId, frenchUrlId, noSiteDivId) 
{	
	var englishHref = document.getElementById(englishUrlId);
	var frenchHref = document.getElementById(frenchUrlId);
	if (document.getElementById(invalidPostalCodeDivId) != null)
		document.getElementById(invalidPostalCodeDivId).style.display='none';
	if (document.getElementById(invalidPostalCodeDivId) != null)
		document.getElementById(noSiteDivId).style.display='none';
	if (englishHref != null)
		englishHref.style.display='none';
	if (frenchHref != null)
		frenchHref.style.display='none';
	
	var pcode = document.getElementById(postalCodeField).value;	

	pcode = jsonClient.JSONClientSplash.isValidPostalCode(pcode);
	if (pcode.length == 0)
	{
		document.getElementById(invalidPostalCodeDivId).style.display='block';
	}
	else 
	{
		var urls = jsonClient.JSONClientSplash.getSiteUrlsForPostalCode(pcode);
		switch (urls.length) 
		{
			case 0 :
				document.getElementById(noSiteDivId).style.display='block';
				break;
			case 1 : 
				document.location.href = urls[0];
				break;
			default :
				englishHref.style.display='block';
				frenchHref.style.display='block';
				englishHref.href = urls[0];
				frenchHref.href = urls[1];
				break;
		}
	}
}

function selectRegion(communityId, invalidPostalCodeDivId, englishUrlId, frenchUrlId, noSiteDivId, lang) 
{	
	var englishHref = document.getElementById(englishUrlId);
	var frenchHref = document.getElementById(frenchUrlId);
	if (document.getElementById(invalidPostalCodeDivId) != null)
		document.getElementById(invalidPostalCodeDivId).style.display='none';
	if (document.getElementById(invalidPostalCodeDivId) != null)
		document.getElementById(noSiteDivId).style.display='none';
	if (englishHref != null)
		englishHref.style.display='none';
	if (frenchHref != null)
		frenchHref.style.display='none';
	if (communityId != "NULL") {
		var urls = jsonClient.JSONClientSplash.getSiteUrlsForCommunity(communityId,lang);
		switch (urls.length) 
		{
			case 0 :
				document.getElementById(noSiteDivId).style.display='block';
				break;
			case 1 : 
				document.location.href = urls[0];
				break;
			default :
				englishHref.style.display='block';
				frenchHref.style.display='block';
				englishHref.href = urls[0];
				frenchHref.href = urls[1];
				break;
		}
	}
}

function hitEnter(event, postalCodeField, invalidPostalCodeDivId, englishUrlId, frenchUrlId, noSiteDivId) 
{
  if (event && event.keyCode == 13)
    searchCommunityPostalCode(postalCodeField, invalidPostalCodeDivId, englishUrlId, frenchUrlId, noSiteDivId);
  else
    return true;
}
