<!--
/* 
-------------------------------------------------------------------------------------------------
DLVRCntryUtils.js

Author: mdeangelis

Description
-----------
Scripts for the DYN_CNTRY_UTILS, DYN_CNTRY_SELECT portlets. These support the "Countries" feature.

Modification History: (LIFO)
---------------------------
Oct 10, 2007 - mdeangelis - dlvr_handle_select_country() changed to receive id of select element,
                            if not provided, default is assumed.  If provided, that's the element
                            id that is looked up.  This change required in order to allow for
                            case where multiple country selection portlets appear on same page.
Sep 17, 2007 - mdeangelis - Changed dlvr_handle_select_country to no longer require form/post
Sep 11, 2007 - mdeangelis - Corrected end comment (for old browsers)
Sep 09, 2007 - mdeangelis - Added dlvr_countries_cancel_form
Sep 06, 2007 - mdeangelis - Added strApply to dlvr_countries_post_form
Aug 30, 2007 - mdeangelis - Created
------------------------------------------------------------------------------------------------ 
*/
var bDlvrCountriesFormChanged = false;

function dlvr_countries_set_changed_flag(bFlag)
{
	bDlvrCountriesFormChanged = bFlag;
}

function dlvr_countries_cancel_form(strFormId, strOKURL, strCancelURL)
{
	var strFunc = 'DYN Page for Countries Feature';

	if (document.getElementById)
	{
		var theForm = document.getElementById(strFormId);
		if (theForm)
		{
			var strURL = strOKURL;
			
			if (bDlvrCountriesFormChanged)
			{
				//Set the apply action parameter
				var bConfirm = confirm('Changes will be lost ... \nPress OK to leave without saving changes? Otherwise press Cancel.');
				
				if (false == bConfirm)
				{
					strURL = strCancelURL;
				}
			}
			else
			
			// To the form (if we cancel, allow option of not providing 
			// strCancelURL - i.e. remain on page without recur)  
			if (strURL)
			{
				window.location.href = strURL;
			}			
		}
		else
		{
			alert('Error in '+strFunc+' : form element not detected - unable to process request');
		}
	}
	else
	{
		alert('Error in '+strFunc+' : required browser capability not supported');
	}
}

function dlvr_countries_post_form(strFormId, strMethod, strAction, strIdApply, strApply)
{
	var strFunc = 'DYN Page for Countries Feature';

	if (document.getElementById)
	{
		var theForm = document.getElementById(strFormId);
		if (theForm)
		{
			if (strApply)
			{
				//Set the apply action parameter
				var eltApply = document.getElementById(strIdApply);
				if (eltApply)
				{
					eltApply.value = strApply;
				}
			}
			// Submit the form
			theForm.action = strAction;
			theForm.method = strMethod;
			theForm.submit();			
		}
		else
		{
			alert('Error in '+strFunc+' : form element not detected - unable to process request');
		}
	}
	else
	{
		alert('Error in '+strFunc+' : required browser capability not supported');
	}
}

var strCountriesBase = '/dhome/countries'; // Holds where Countries navigation should go
var strDfltSelectId  = 'dlvr_select_country';

// Select a country or region from drop-down
function dlvr_handle_select_country( strInSelectEltId, strNavToURL )
{
	var strFunc        = 'DYN_CNTRY_SELECT javascript handler : dlvr_handle_select_country()';
    var strSelectEltId = strInSelectEltId;
	
	if (!strSelectEltId)
	{
		strSelectEltId = strDfltSelectId;
	}
	
	if (document.getElementById && document.getElementsByTagName)
	{
		var eltSelect    = document.getElementById(strSelectEltId);
		var strPerspName = eltSelect[eltSelect.selectedIndex].value;

        if (!strPerspName)
        {
		   alert('First you need to select a country or region from the list');
		   return;
        }

		var strSearch	= window.location.search;
		var strURL		= '';
		var strPrefix   = '';
		var strArgs     = '';
		var arrParams   = new Array();
		var bFoundArg   = false;
		
		if ( strNavToURL )
		{
			strURL = strNavToURL; // We have override of target URL (i.e. instead of recurring, navigate away)
		}
		else
		{
			strURL = window.location.href; // recurse
		}

		if ( strSearch )
		{
			// separate base URL from query string
			strArgs = strURL.split('?')[1];
			strURL  = strURL.split('?')[0];
		}

		// If we have a query string and its the news detail page, go to the news page (brute force override)
		if ( strURL.indexOf('/countrynewsdetail') > 0 )
		{
			strURL = '/dhome/countries/countrynews';
		}
				
		//Special case: we're on a news detail item and user switches countries - reset URL to news
		if ( strURL.indexOf('/countrynewsdetail') > 0 )
		{
			strURL = '/dhome/countries/countrynews';
		}
		
		strPrefix = '?'; // to start
		
		if ( strArgs )
		{	
			arrParams = strArgs.split('&'); // Split query string into individual N=V pars
			
			// We now reassemble, replacing p_persp if we find it reassemble
			
			strArgs = '';
			
			for ( var i = 0; i < arrParams.length; i++ ) 
			{
			    var strParamValue = arrParams[i].split('=') [1];
				var strParamName  = arrParams[i].split('=') [0];

				if ( strParamName && ( strParamName == 'p_persp' ) )
				{
					bFoundArg = true;
					strParamValue = strPerspName; // replace p_persp in current search string
				}
				strArgs += strPrefix + strParamName + '=' + strParamValue;

				strPrefix = '&' // args 2+
			}
			if ( ! bFoundArg )
			{
				strArgs += strPrefix + 'p_persp=' + strPerspName; // append p_persp to current search string  
			}
		}
		else
		{
			strArgs = '?' + 'p_persp=' + strPerspName; // append p_persp to create search string
		}
				 
		strURL += strArgs;
		
		window.location.href = strURL;

		return true; // doesn't matter, but just in case
	}
	else
	{
		alert('Error in '+strFunc+' : required browser capability not supported');
	}
}

//-->