﻿/**
 * Javascript
 * 
 * @package    BKC.Web
 * @author     Brian Lam <banhthidiem@gmail.com>
 * @copyright  2007 Bach Khoa Computer Inc.
 * @version    $Id: CreateContentWebPage.js, v1.0 2007/09/13
 * ex: var webpage = new CreateContentWebPage$BTD("idMyContent", "idContainer", "#ProductDetail?ProID=2", ".aspx");
 webpage.executeCMD = function()
 {
	// Action when load complete content webpage
 }
 */
//===============================================================================
//								Create content webpage
//===============================================================================

function CreateContentWebPage$BTD(idContent, idContainer, url, tail)
{
	var u = utilObj;
	this.oContent = null;
	this.isRun = false;
	this.url = url;
	
	this.run = function()
	{
		if (this.oContent != null)
		{
			this.oContent = null;
		}
		this.makeAjax(u.analyseURL(this.url, tail));
	};
	
	this.getChildCatID = function(parentCatID)
	{
		var strCatID = "";
		if (listItemsCatAll[parentCatID] != null)
		{
			var list = listItemsCatAll[parentCatID].listChild;
			for (var i = 0; i < list.length; i++)
			{
				var arrTemp =list[i].id.split("*");
				var id = arrTemp[arrTemp.length - 1];
				if (strCatID != "") strCatID += ",";
				strCatID += id;
				if (listItemsCatAll[id].listChild.length > 0)
					strCatID += "," + this.getChildCatID(id);
			}
		}
		return strCatID;
	};
	
	this.makeAjax = function(result)
	{
		try
		{
			// Category is Parent
			if (result.paramater["IsParent"] != null && result.paramater["CatID"] != null)
			{
				result.paramater["StrCatID"] = this.getChildCatID(result.paramater["CatID"]);
			}
			this.xmlObj = new XMLAjax(this, true);
			this.xmlObj.ajaxDo(result.page, result.paramater);
		}
		catch (e)
		{
			alert(GetTextLang(0));
		}
	};
	
	this.getContentObject = function()
	{
		var o = u.getElById("idContentWebPage_" + idContent);
		if (o == null)
		{
			o = u.createEl("DIV");
			o.id = "idContentWebPage_" + idContent;
			if (u.getElById(idContainer))
				u.getElById(idContainer).appendChild(o);
		}
		return o;
	};
	
	this.showData = function(data)
	{
		if (data.trim() == "Error")
		{
			top.location.href = "Default.aspx";
			return;
		}
		this.hideAllDataContainer();
		this.oContent = this.getContentObject();
		this.oContent.style.display = "";
		this.oContent.innerHTML = data;
		this.isRun = true;
		this.executeCMD();
	};
	
	this.hideAllDataContainer = function()
	{
		var listChilds = u.getElById(idContainer).childNodes;
		for (var i = 0; i < listChilds.length; i++)
		{
			if (listChilds[i].style)
				listChilds[i].style.display = "none";
		}
	};
	
	this.showMyContent = function(isReLoad)
	{
		if (this.isRun && !isReLoad)
		{
			this.hideAllDataContainer();
			this.oContent.style.display = "";
		}
		else
		{
			this.run();
		}
	};
	
	this.executeCMD = function() { };
};