/***************************************************************************
* Program Name       : Javascript program
* Copyright Notice   : COPYRIGHT (C) 2006 BY HSBC
* Creation Date      : 25/04/2006
* Programmer         : e-Crusade
* Abstract           : This file contains common functions required
*                      for home&Away project
***************************************************************************/
/***************************************************************************************************************
* File Name		: ui_controller.js
* Usage			: 1. View controller to update the ui according to the most
*			:   updated 3 selection box option lists:
*			:	g_country_list_ , g_category_list_ , g_card_list_
*			:
* Author		: e-Crusade
* Creation Date		: 2008-12-16
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/


/***************************************************************************************************************
* Function		: pws_ha_updateCountrySelectionBox(default_value,boolean_enable_all)
* Description		: update "Country" selection scroll bar
*			:
* Parameter Usage	: "default_value" 	- the default selected value of scroll bar
*			: boolean_enable_all	- boolean to determine whether option "ALL"
*			:			  include in the scroll bar list
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

function pws_ha_updateRegionSelectionBox(default_value,boolean_enable_all){
	var region_selection_box = document["offer_search_form"].region;
	region_selection_box.length=0;

	var selectedIdx = -1;
	if(boolean_enable_all){
		region_selection_box.options[0] = new Option(g_option_all_display_value_, "", false, false);
		if(default_value=="ALL")
			selectedIdx = 0;
	}

	for(var i=0 ; i<g_region_list_.length ;i++){
		var idx = (boolean_enable_all? i+1 : i);
		region_selection_box.options[idx] = new Option(g_region_name_mapping_[g_region_list_[i]], g_region_list_[i], false, false);
		if(default_value==g_region_list_[i])
			selectedIdx = idx;
	}

	if(selectedIdx != -1)
		region_selection_box.selectedIndex = selectedIdx;

	if(!boolean_enable_all && g_region_list_.length <= 0){
		region_selection_box.options[0] = new Option("N/A", "N/A", false, true);
	}
}


/***************************************************************************************************************
* Function		: pws_ha_updateCountrySelectionBox(default_value,boolean_enable_all)
* Description		: update "Country" selection scroll bar
*			:
* Parameter Usage	: "default_value" 	- the default selected value of scroll bar
*			: boolean_enable_all	- boolean to determine whether option "ALL"
*			:			  include in the scroll bar list
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

function pws_ha_updateCountrySelectionBox(default_value,boolean_enable_all){
	var country_selection_box = document["offer_search_form"].country;
	country_selection_box.length=0;

	var selectedIdx = -1;
	if(boolean_enable_all){
		country_selection_box.options[0] = new Option(g_option_all_display_value_, "", false, false);
		if(default_value=="ALL")
			selectedIdx = 0;
	}
	for(var i=0 ; i<g_country_list_.length ;i++){
		var idx = (boolean_enable_all? i+1 : i);
		country_selection_box.options[idx] = new Option(g_country_name_mapping_[g_country_list_[i]], g_country_list_[i], false, false);
		if(default_value==g_country_list_[i])
			selectedIdx = idx;
	}

	if(selectedIdx != -1)
		country_selection_box.selectedIndex = selectedIdx;

	if(!boolean_enable_all && g_country_list_.length <= 0){
		country_selection_box.options[0] = new Option("N/A", "N/A", false, true);
	}
}



/***************************************************************************************************************
* Function		: pws_ha_updateCategorySelectionBox(default_value,boolean_enable_all)
* Description		: update "Category" selection scroll bar
*			:
* Parameter Usage	: "default_value" 	- the default selected value of scroll bar
*			: boolean_enable_all	- boolean to determine whether option "ALL"
*			:			  include in the scroll bar list
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

function pws_ha_updateCategorySelectionBox(default_value,boolean_enable_all){
	var category_selection_box = document["offer_search_form"].category;
	category_selection_box.length=0;

	var selectedIdx = -1;
	if(boolean_enable_all){
		category_selection_box.options[0] = new Option(g_option_all_display_value_, "", false, false);
		if(default_value=="ALL")
			selectedIdx = 0;
	}

	for(var i=0 ; i<g_category_list_.length ;i++){
		var idx = (boolean_enable_all? i+1 : i);
		category_selection_box.options[idx] = new Option(g_category_name_mapping_[g_category_list_[i]], g_category_list_[i],  false, false);
		if(default_value==g_category_list_[i])
			selectedIdx = idx;
	}

	if(selectedIdx != -1)
		category_selection_box.selectedIndex = selectedIdx;

	if(!boolean_enable_all && g_category_list_.length <= 0){
		category_selection_box.options[0] = new Option("N/A", "N/A", false, true);
	}
}

/***************************************************************************************************************
* Function		: pws_ha_updateCardSelectionBox(default_value,boolean_enable_all)
* Description		: update "Card" selection scroll bar
*			:
* Parameter Usage	: "default_value" 	- the default selected value of scroll bar
*			: boolean_enable_all	- boolean to determine whether option "ALL"
*			:			  include in the scroll bar list
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
*2009-03-18	Cathy	Update: "pr" card will not show if the country with country home is selected
***************************************************************************************************************/

function pws_ha_updateCardSelectionBox(default_value,boolean_enable_all){
	var card_selection_box = document["offer_search_form"].cardtype;
	card_selection_box.length=0;

	var selectedIdx = -1;
	if(boolean_enable_all){
		card_selection_box.options[0] = new Option(g_option_all_display_value_, "",  false, false);
		if(default_value=="ALL")
			selectedIdx = 0;
	}

	var isCountryHome = false;
	for (x in g_all_country_home_list_)
	{
		
		if (g_all_country_home_list_[x] == g_current_selected_country_)
		{
			isCountryHome = true;
			break;
		}
	}
		
	var optionsIdx =0;
	for(var i=0 ; i<g_card_list_.length ;i++){
		var idx = (boolean_enable_all? optionsIdx+1 : optionsIdx);
		
		if ((isCountryHome && "re" !=g_card_list_[i]) || (!isCountryHome && "pr" !=g_card_list_[i] && "re" !=g_card_list_[i]))
		//if ( "re" !=g_card_list_[i])
		{
			card_selection_box.options[idx] = new Option(g_card_name_mapping_[g_card_list_[i]], g_card_list_[i],   false, false);
			optionsIdx++;
		}
		if(default_value==g_card_list_[i])
			selectedIdx = idx;
	}

	if(selectedIdx != -1)
		card_selection_box.selectedIndex = selectedIdx;

	if(!boolean_enable_all && g_card_list_.length <= 0){
		card_selection_box.options[0] = new Option("N/A", "N/A", false, true);
	}
}


/***************************************************************************************************************
* Function		: pws_ha_refreshSelectionBox(	default_country,boolean_enable_all_country,
*			:			default_category, boolean_enable_all_category,
*			:			default_card, boolean_enable_all_card)
* Description		: call up the above 3 functions to refresh the 3 scroll bar
*			:
* Parameter Usage	: "default_country" 		- the default selected value of Country's scroll bar
*			: "boolean_enable_all_country"	- boolean to determine whether option "ALL" included in Country's scroll bar
*  			: "default_category" 		- the default selected value of Category's scroll bar
*			: "boolean_enable_all_category"	- boolean to determine whether option "ALL" included in Category's scroll bar
*  			: "default_card" 		- the default selected value of Card's scroll bar
*			: "boolean_enable_all_card"	- boolean to determine whether option "ALL" included in Card's scroll bar
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/
function pws_ha_refreshSelectionBox(default_region,boolean_enable_all_region,default_country,boolean_enable_all_country,	default_category, boolean_enable_all_category,default_card, boolean_enable_all_card)
{
		pws_ha_updateRegionSelectionBox(default_region,boolean_enable_all_region);
		pws_ha_updateCountrySelectionBox(default_country,boolean_enable_all_country);
		pws_ha_updateCategorySelectionBox(default_category, boolean_enable_all_category);
		pws_ha_updateCardSelectionBox(default_card, boolean_enable_all_card);
}

/***************************************************************************************************************
* Function		: pws_ha_clearTxtField()
* Description		: reset the text field value
* Parameter Usage	: N/A
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/
function pws_ha_clearTxtField(htmlTxtField)
{
	if (htmlTxtField.value == htmlTxtField.defaultValue)
	{
		htmlTxtField.value = "";
	}
}
/***************************************************************************************************************
* Function          : pws_ha_getUrlParameter(urlParameterName)
* Description       : Get URL parameter value from query string.
*                     If urlParameterName cannot be found in query string, then return null.
* Parameter Usage   : 1. "urlParameterName": parameter name
* Creation Date     : 2008-12-16
* Side effect       : N/A
* Amendment History :
* Date       By         Description
* ---------- ---------- ----------
***************************************************************************************************************/
function pws_ha_getUrlParameter(urlParameterName)
{
	var pStr = location.search;
	var urlParameterValue = null;
	if (pStr == null || pStr.length == 0)
	{
	}
	else
	{
		// Remove the ? at the begining of pStr
		pStr = pStr.substr(1);

		var pList = pStr.split("&");
		for (var i=0; i<pList.length; i++)
		{
			var nvPair = pList[i].split("=");
			if (nvPair[0] == urlParameterName)
			{
				urlParameterValue = nvPair[1];
				break;
			}
		}
	}
	return (urlParameterValue)
}

/***************************************************************************************************************
* Function		: pws_ha_submitOfferTxtSearchForm()
* Description		: validate and submit the offer text search form
* Parameter Usage	: 1. "lang": language code of current page
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/
function pws_ha_submitOfferTxtSearchForm(lang)
{
		var fromcountry = pws_ha_getUrlParameter('fromcountry')==null ? "" : pws_ha_getUrlParameter('fromcountry');
		var ad_id = pws_ha_getUrlParameter('ad_id')==null ? "" : pws_ha_getUrlParameter('ad_id');
		var track = fromcountry + "_" + document.offer_txt_search_form.lang.value + "_" + g_current_selected_country_ + "_" + "offersearch" + "_" + g_current_selected_category_ + "_" + g_current_selected_card_;

		document.offer_txt_search_form.merchantname.value = document.offer_txt_search_form.merchantname.value.replace(/^\s*|\s*$/g,"");

		if (document.offer_txt_search_form.merchantname.value.length > 50)
		{
			if (lang == "zh")
			{
				alert("\u8ACB\u8F38\u5165\u4E0D\u591A\u65BC\u4E94\u5341\u500B\u5B57\u7684\u5546\u6236\u540D\u7A31 ");
			}
			else
			{
				alert("Please enter the merchant name which is less than 50 characters.");
			}
		}else
		if ((document.offer_txt_search_form.merchantname.value != "") && (document.offer_txt_search_form.merchantname.value != document.offer_txt_search_form.searchdeftxt.value))
		{

			document.offer_txt_search_form.submit();
		}
		else
		{
			if (lang == "zh")
			{
				alert("\u8acb\u8f38\u5165\u5546\u6236\u540d\u7a31");
			}
			else
			{
				alert("Please enter the merchant name.");
			}
		}
}

/***************************************************************************************************************
* Function		: pws_ha_createPPSelectionBox()
* Description		: create selection box for premier and platinum privileges
* Parameter Usage	: 1. "lang": language code of current page
*                   2. "cardType": Premier (pr) or Platinum (pl)
*                   3. "countryCode": list of country codes separated by "-"
*                   4. "coutnryName": list of country names separated by "-"
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/
function pws_ha_createPPSelectionBox(lang, cardType, countryCode, countryName)
{
	var countrySelectionBox = document["country_selection_form"].country_selection_box;
	var optionCnt = 0;
	var selectCountryTxt  = "Pick Your Destination";
	if (lang == "zh")
	{
		selectCountryTxt = "\u8acb\u9078\u64c7\u65c5\u904a\u76ee\u7684\u5730";
	}
	
	// Get fromcountry value from URL
	var fromcountry = pws_ha_getUrlParameter("fromcountry");
	// Get ad_id value from URL
	var ad_id = pws_ha_getUrlParameter("ad_id");
	var cardtype = pws_ha_getUrlParameter("cardtype");
	countrySelectionBox.options[optionCnt++] = new Option(selectCountryTxt, "",  false, false);
	if (countryCode != "")
	{
		var countryCodeList = countryCode.split("-");
		var countryNameList = countryName.split("-");
		for (var i=0; i<countryCodeList.length; i++)
		{
			var link = "";
			if (cardType == "pr") {
				var tag = getExOfferSearchSpotlightClickTag(countryCodeList[i]);
				
				if (tag != null) {
					var tagName = tag.tagName;
					link = "javascript:LocalPredefinedFunction('sltClickTracking;"+tagName+";" + fromcountry + "_" + lang + "_" + countryCodeList[i] + "_exoffersearch__"+cardtype+";')||redirect";
				}
				else {
					link = "/homeaway/action/exoffersearch?fromcountry=" + fromcountry + "&country=" + countryCodeList[i] + "&lang=" + lang + "&cardtype=" + cardType + "&ad_id=" + ad_id + "&track=" + fromcountry + "_" + lang + "_" + countryCodeList[i] + "_exoffersearch__"+cardtype+"||redirect";
				}
			}
			else if (cardType == "pl") {
				link = "/homeaway/action/exoffersearch?fromcountry=" + fromcountry + "&country=" + countryCodeList[i] + "&lang=" + lang + "&cardtype=" + cardType + "&ad_id=" + ad_id + "&track=" + fromcountry + "_" + lang + "_" + countryCodeList[i] + "_exoffersearch__"+cardtype+"||redirect";
			}
			countrySelectionBox.options[optionCnt++] = new Option(countryNameList[i], link, false, false);
		}
	}
}

/***************************************************************************************************************
* Function		: pws_ha_genRandomBg()
* Description		: Generate random table cell background on Premier and Platinum gateway
* Parameter Usage	: 1. "lang": language code of current page
*                   2. "cardtype": pr (Premier) or pl (Platinum)
*                   3. "maxbg": max. number of background can be supported
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/
function pws_ha_genRandomBg(lang, cardtype, maxbg)
{
	var imgPrefix = "/cardha/common/images/";
	if (lang == "zh")
	{
		imgPrefix = "/cardha/chinese/common/images/";
	}
	
	var randomIdx = 1+Math.floor(Math.random()*maxbg);
	var bgImgPath = imgPrefix + cardtype + "_bg_" + randomIdx + ".jpg";
	var targetCell = document.getElementById("toptable").rows[0].cells[0];
	targetCell.style.backgroundImage = "url(" + bgImgPath + ")";
}

/***************************************************************************************************************
* Function		: pws_ha_validateOfferTxtSearch()
* Description		: Prevent user submit a search with empty (using Enter key)
* Parameter Usage	: 1. "lang": language code of current page, either "en" or "zh"
* Creation Date		: 2009-08-24
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/
function pws_ha_validateOfferTxtSearch(lang) 
{ 
	var fromcountry = pws_ha_getUrlParameter('fromcountry')==null ? "" : pws_ha_getUrlParameter('fromcountry'); 
	var ad_id = pws_ha_getUrlParameter('ad_id')==null ? "" : pws_ha_getUrlParameter('ad_id'); 
	var track = fromcountry + "_" + document.offer_txt_search_form.lang.value + "_" + g_current_selected_country_ + "_" + "offersearch" + "_" + g_current_selected_category_ + "_" + g_current_selected_card_; 

	document.offer_txt_search_form.merchantname.value = document.offer_txt_search_form.merchantname.value.replace(/^\s*|\s*$/g,""); 

	if (document.offer_txt_search_form.merchantname.value.length > 50) 
	{ 
			if (lang == "zh") 
			{ 
					alert("\u8ACB\u8F38\u5165\u4E0D\u591A\u65BC\u4E94\u5341\u500B\u5B57\u7684\u5546\u6236\u540D\u7A31 "); 
			return false; 
			} 
			else 
			{ 
					alert("Please enter the merchant name which is less than 50 characters."); 
			return false; 
			} 
	}else 
	if ((document.offer_txt_search_form.merchantname.value == "") || (document.offer_txt_search_form.merchantname.value == document.offer_txt_search_form.searchdeftxt.value)) 
	{ 
			if (lang == "zh") 
			{ 
					alert("\u8acb\u8f38\u5165\u5546\u6236\u540d\u7a31"); 
			return false; 
			} 
			else 
			{ 
					alert("Please enter the merchant name."); 
			return false; 
			} 
	}
}