/***************************************************************************
* Program Name       : Javascript program
* Copyright Notice   : COPYRIGHT (C) 2006 BY HSBC
* Creation Date      : 16/12/2008
* Programmer         : e-Crusade
* Abstract           : This file contains common functions required
*                      for home&Away project
***************************************************************************/
/***************************************************************************************************************
* File Name		: category_card_manager.js
* Usage			: 1. Define function to set Category-Card Relation.
*			: 2. Define function to validate is a Category-Card pair have relation.
*			: 3. Define function to grep the elements in a list which have relation
*			:    with input parameter
*			:
* Author		: e-Crusade
* Creation Date		: 2008-12-16
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/


var _category_card_relation_ = new Array();
var _category_have_init_ = new Array();


/***************************************************************************************************************
* Function		: pws_ha_addCategoryCardRelation(category,card,country)
* Description		: Create a Category and Card Relation on a specific Country
*			:
* Parameter Usage	: "category" 	- a category
* 			: "card"	- a card
*			: "country"	- the country that the relation apply on
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

	function pws_ha_addCategoryCardRelation(category,card,country){
		if(_category_card_relation_[category]==null)
			 _category_card_relation_[category] = new Array();

		if(_category_card_relation_[category][card]==null)
			_category_card_relation_[category][card] = new Array();


		_category_card_relation_[category][card][country] = true;
	}


/***************************************************************************************************************
* Function		: pws_ha_checkCategoryCardRelation(category,card,country)
* Description		: To check whether a Category and Card Relation apply on the specific country
*			:
* Parameter Usage	: "category" 	- a category
* 			: "card"	-  a card
*			: "country"	- the country that the relation apply on
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

	function pws_ha_checkCategoryCardRelation(category,card,country){
		var haveRelation = false;
		try{
			if(_category_card_relation_[category][card][country] == true)
				haveRelation = true;
		}
		catch(e){}
		return haveRelation;

	}

/***************************************************************************************************************
* Function		: pws_ha_getCategoryListByCard(category_search_list,card,country)
* Description		: To grep the category type elements in a list which have relation
*			: with specific card type on the specific country
*			:
* Parameter Usage	: "category_search_list" 	- a category's list to be search on
* 			: "card"			- a card
*			: "country"			- specific country to be search
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

	function pws_ha_getCategoryListByCard(category_search_list,card){
		if(card == g_option_all_default_value_ ){
			return category_search_list;
		}

		var country = g_current_selected_country_ ;
		var result_list = new Array();
		for(var i=0; i<category_search_list.length ;i++){
			if(pws_ha_checkCategoryCardRelation(category_search_list[i],card,country)){
				result_list.push(category_search_list[i]);
			}
		}
		return result_list;
	}


/***************************************************************************************************************
* Function		: pws_ha_getCardListByCategory(category,card_search_list,country)
* Description		: To grep the card type elements in a list which have relation
*			: with specific category type on the specific country
*			:
* Parameter Usage	: "card_search_list" 	- a card's list to be search on
* 			: "category"		- a category
*			: "country"		- specific country to be search
*			:
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

	function pws_ha_getCardListByCategory(category,card_search_list){
		if(category == g_option_all_default_value_ ){
			return card_search_list;
		}

		var country = g_current_selected_country_ ;
		var result_list = new Array();
		for(var i=0; i<card_search_list.length ;i++){
			if(pws_ha_checkCategoryCardRelation(category,card_search_list[i],country)){
				result_list.push(card_search_list[i]);
			}
		}
		return result_list;
	}

/***************************************************************************************************************
* Function		: pws_ha_validOfferSearchForm()
* Description		: To validate the selected country, card, category in offer search
* Parameter Usage	:
*
* Creation Date		: 2008-12-16
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

	function pws_ha_validOfferSearchForm(){
		if ((g_current_selected_country_ == "_default_") )
			return false;
		else
			return true;
	}

/***************************************************************************************************************
* Function		: pws_ha_submitOfferSearchForm()
* Description		: validate  and submit the offer search form
* Parameter Usage	: 1. "lang": language code of current page
* Creation Date		: 2005-05-30
* Side effect		: No
* Amendment History	:
* Date		By		Description
* ---------	------------	--------------------------------------------------------------------------------
***************************************************************************************************************/

	function pws_ha_submitOfferSearchForm(lang){
		if (pws_ha_validOfferSearchForm())
		{
			
			var lang = "";
			var country="other";
			var type= "offers";
			var cardType = "re";
			var catId = "000"
			
			if (g_current_selected_card_.indexOf('pr')>=0)
			{
				type = "premier-offers";
				cardType= "pr";
				document.getElementById("cardtype").value = "";
			}
			if (location.href.indexOf('/chinese/')>=0)
			{
				lang= "chinese/";
			}
			if (g_current_selected_category_.indexOf('sp') >=0)
				catId = "001";
			else if (g_current_selected_category_.indexOf('wd') >=0)
				catId = "002";
			else if (g_current_selected_category_.indexOf('hb') >=0)
				catId = "003";
			else if (g_current_selected_category_.indexOf('tl') >=0)
				catId = "004";
			for (x in g_all_country_home_list_)
			{
				if (g_all_country_home_list_[x] == g_current_selected_country_)
				{
					country = g_current_selected_country_;
					break;
				}
			}
			var path = "/1/2/hna2/"+lang+"local/"+country+"/"+type;
			var trackObj = document.getElementById("WT.ac");
			if (trackObj != null)
				trackObj.value = "ASPHA_"+ country+"_"+ cardType+"_"+ catId+"_s";
			document.offer_search_form.action = path;
			document.offer_search_form.submit();
		}
		else
		{
			if (lang == "zh")
			{
				alert("\u8acb\u9078\u64c7\u570b\u5bb6\u6216\u5730\u5340");
			}
			else
			{
				alert("Please select the country or territory.");
			}
		}

	}
