﻿//===============================================================================
//								Action with Cookie Start
//===============================================================================
function readCookie(idContainer, w)
{
	var str = utilObj.getCookie(idContainer);
	if (str == null)
	{
		if (idContainer.indexOf("left") != -1) str = posLeftDefault;
		else if (idContainer.indexOf("center") != -1) str = posCenterDefault;
		else str = posRightDefault;
	}
	var arr = str.split(",");
	for (var i = 0; i < arr.length; i++)
	{
		if ((typeof(nameAdvertImg) != "undefined")
			&& (arr[i] == nameAdvertImg) 
			&& BKC.listObjPage[arr[i]] != null)
		{
			document.getElementById(idContainer).appendChild(BKC.listObjPage[arr[i]].createMain());
			//BKC.listObjPage[arr[i]].autoChangeImg();
		}
		else if (BKC.listObjPage[arr[i]] != null)
		{
			if (w)
				document.getElementById(idContainer).appendChild(BKC.listObjPage[arr[i]].createMain(w));
			else
				document.getElementById(idContainer).appendChild(BKC.listObjPage[arr[i]].createMain());
		}
	}
};

function getRegionFromCookie()
{
	var c = utilObj.getCookie(BKC.__CR);
	try
	{
		var a = c.split(BKC.__colon);
		return { id: a[0], name: a[1], priceList: a[2] }; 
	}
	catch (e)
	{
		return null;
	}
};

function downloadPriceList()
{
	var re = getRegionFromCookie();
	if (re != null)
	{
		utilObj.wd.open(re.priceList);
	}
};

//===============================================================================
//								Action with Cookie End
//===============================================================================



//===============================================================================
//								Create MapLink
//===============================================================================

var MapLink =
{
	listNodes : [],
	
	getContainer : function()
	{
		return utilObj.getElById("mapLink");
	},
	
	resetList : function()
	{
		this.listNodes = [];
		this.getContainer().innerHTML = "";
	},
	
	addNode : function(text, link)
	{
		this.listNodes.push({ text : text, link : link });
	},
	
	createLink : function(text, link)
	{
		var o = utilObj.createEl("A");
		this.setLinkURL(o, link);
		o.innerHTML = text;
		this.getContainer().appendChild(o);
	},
	
	setLinkURL : function(o, link)
	{
		utilObj.addEvent(o, "click", function(e) { if (link != null) eval(link); return false; } );
		o.href = "javascript:;";
		/*
		if (link != null)
		{
			var start = link.indexOf("#");
			var end = link.indexOf("'", start);
			if (end == -1) end = link.indexOf('"', start);
			o.href = link.substring(start, end);
		}
		else
		{
			o.href = "#";
		}
		*/
	},
	
	createSep : function()
	{
		var o = utilObj.createElText(" <-- ");
		this.getContainer().appendChild(o);
	},
	
	buildMapLink : function()
	{
		this.getContainer().style.display = "block";
		for (var i = 0; i < this.listNodes.length; i++)
		{
			if (i != 0) this.createSep();
			this.createLink(this.listNodes[i].text, this.listNodes[i].link);
		}
	},
	
	hide : function()
	{
		this.getContainer().style.display = "none";
	}
};

function goToTop()
{
	utilObj.getElById("anchorTargetTop").focus();
};

function catProduct_Click(url, isReload, e)
{
	if (typeof(isReload) == "undefined") isReload = true;
	styleBtnMM.resetAllStyle();
	if (typeof(tblNews) != "undefined" && tblNews != null) tblNews.resetAllStyle();
	if (typeof(tblSupport) != "undefined" && tblSupport != null) tblSupport.resetAllStyle();
	showDataWhenClick(url, isReload, e);
	return false;
};

function catSupport_Click(url, isReload)
{
	if (typeof(isReload) == "undefined") isReload = false;
	styleBtnMM.resetAllStyle();
	if (typeof(tblNews) != "undefined" && tblNews != null) tblNews.resetAllStyle();
	if (typeof(tblCat) != "undefined" && tblCat != null) tblCat.resetAllStyle();
	showDataWhenClick(url, isReload);
	return false;
};

function catNews_Click(path)
{
	styleBtnMM.resetAllStyle();
	if (typeof(tblSupport) != "undefined" && tblSupport != null) tblSupport.resetAllStyle();
	if (typeof(tblCat) != "undefined" && tblCat != null) tblCat.resetAllStyle();
	utilObj.wd.open(path);
	return false;
};

function menuTop_Click(url, isReload)
{
	if (typeof(isReload) == "undefined") isReload = false;
	if (typeof(tblSupport) != "undefined" && tblSupport != null) tblSupport.resetAllStyle();
	if (typeof(tblCat) != "undefined" && tblCat != null) tblCat.resetAllStyle();
	showDataWhenClick(url, isReload);
	return false;
};

function callDefaultProductsPage_Click()
{
	styleBtnMM.resetAllStyle();
	if (typeof(tblSupport) != "undefined" && tblSupport != null) tblSupport.resetAllStyle();
	showDataWhenClick("#DefaultProducts+AllCatID=" + DefaultProductsPage.getAllCatID(), true);
	return false;
};

function downloadPage_Click()
{
	otherLink_Click("#" + BKC.downloadPage.page, true);
};

function otherLink_Click(url, isReload)
{
	if (typeof(isReload) == "undefined") isReload = false;
	
	if (typeof(tblNews) != "undefined" && tblNews != null) tblNews.resetAllStyle();
	styleBtnMM.resetAllStyle();
	if (typeof(tblSupport) != "undefined" && tblSupport != null) tblSupport.resetAllStyle();
	if (typeof(tblCat) != "undefined" && tblCat != null) tblCat.resetAllStyle();
	showDataWhenClick(url, isReload);
	return false;
};

function gotoPage(url) {
	var query = "&SortName=" + document.getElementById("selSortName").value + "&SortType=" + document.getElementById("selSortType").value;
	showDataWhenClick(url + query, true);
};

function back_Click()
{
	if (BKC.oldURL == "" || BKC.oldURL == utilObj.getQueryStringByCharSharp()) return;
	var isReload = false;
	var page = BKC.oldURL.split("+")[0];
	switch (page)
	{
		case "Products": isReload = true; break;
		case "DetailProduct": isReload = true; break;
		case "Compare": isReload = true; break;
		case "Driver": isReload = true; MapLink.hide(); break;
		case "Promo": isReload = true; MapLink.hide(); break;
		default: MapLink.hide(); break;
	}
	showDataWhenClick("#" + BKC.oldURL, isReload);
};

utilObj.addEvent(window, "load", function(e)
{
	var regionFormServer = getRegionFromCookie();
	if (regionFormServer == null)
	{
		oMPopup = new ModalPopup("formCAFirst", "oMPopup");
		oMPopup.display();
	}
	// Advert Object
	BKC.objLoadIA = new LoadImageAdvert("BKC.objLoadIA", BKC.pathAdverts);

	var query = utilObj.getQueryStringByCharSharp();
	showDataWhenClick("#" + query, false, e);
	styleBtnMM.changeStyleWhenReload(query, e);
	
	// Load List Category Search
	SearchProduct.loadListCatParent();
	
	// Google Tracker
	GoogleTracker.tracker();
});

function showDataWhenClick(url, isReload, e)
{
	BKC.oldURL = utilObj.getQueryStringByCharSharp();
	location.href = url;
	var isOpen = true, isStatic = true;
	
	if (BKC.oOldContentDetail != null)
	{
		utilObj.getElById("body_right").removeChild(BKC.oOldContentDetail);
		BKC.oOldContentDetail = null;
	};
	
	var query = utilObj.getQueryStringByCharSharp();
	var objUrl = utilObj.analyseURL(query);
	var page = objUrl.page;
	switch (page)
	{
		case "Promo": utilObj.setTitleDocument(BKC.titlePage + " - Khuyen Mai"); break;
		case "ContactUs": utilObj.setTitleDocument(BKC.titlePage + " - Lien He"); break;
		case "DefaultProducts": utilObj.setTitleDocument(BKC.titlePage + " - Tat Ca San Pham"); break;
		case "Products": 
			utilObj.setTitleDocument(BKC.titlePage + " - San Pham");
			query = query.replace("Products", "Products1024_768");
			break;
		case "DetailProduct": utilObj.setTitleDocument(BKC.titlePage + " - Chi Tiet San Pham"); isOpen = false; isStatic = false; break;
		case "Compare":
			utilObj.setTitleDocument(BKC.titlePage + " - So Sanh San Pham");
			isOpen = false;
			isStatic = false;
			break;
		case "ViewCart": utilObj.setTitleDocument(BKC.titlePage + " - Gio Hang Cua Ban"); break;
		case "Register": utilObj.setTitleDocument(BKC.titlePage + " - Dang Ky Thanh Vien"); break;
		case "BuildingCom": utilObj.setTitleDocument(BKC.titlePage + " - Xay Dung May Tinh"); break;
		case "Recruit": utilObj.setTitleDocument(BKC.titlePage + " - Tuyen Dung"); break;
		case BKC.downloadPage.page: 
			utilObj.setTitleDocument(BKC.titlePage + " - " + BKC.downloadPage.title); 
			break;
		case "ConsultativeConsumption":
			utilObj.setTitleDocument(BKC.titlePage + " - Tu Van Tieu Dung");
			if (BKC.listContentPage[page] != null) BKC.listContentPage[page].url = query; // new Url
			query = page;
			break;
		case "DetailConsultativeConsumption": utilObj.setTitleDocument(BKC.titlePage + " - Chi Tiet Cau Hoi Thuong Gap"); break;
		case "ConsultativeTech": utilObj.setTitleDocument(BKC.titlePage + " - Tu Van Ky Thuat"); break;
		case "FAQs": utilObj.setTitleDocument(BKC.titlePage + " - Cau Hoi Thuong Gap"); break;
		case "DetailNews": utilObj.setTitleDocument(BKC.titlePage + " - Noi Dung Tin Tuc"); break;
		case "DetailFAQs": utilObj.setTitleDocument(BKC.titlePage + " - Chi Tiet Cau Hoi Thuong Gap"); break;
		case "PriceList": utilObj.setTitleDocument(BKC.titlePage + " - Download Bao Gia"); break;
		case "Content": utilObj.setTitleDocument(BKC.titlePage + " - Gioi Thieu"); break;
		case "ShowRoom": utilObj.setTitleDocument(BKC.titlePage + " - Show Room"); break;
		default: query = page = "Home1024_768"; utilObj.setTitleDocument(BKC.titlePage + " - Trang Chu"); break;
	};
	
	// Advert
	if (BKC.objLoadIA != null && page != "DetailProduct") BKC.objLoadIA.showAdvert(objUrl);
	
	if (BKC.listContentPage[query] == null)
	{
		BKC.listContentPage[query] = new CreateContentWebPage$BTD(query, "body_right", query, ".aspx");
		BKC.listContentPage[query].executeCMD = function() {
			switch (page) {
				case "Promo":
					BKC.oOldContentDetail = this.oContent;
					break;
				case "Register":
					BKC.oOldContentDetail = this.oContent;
					break;
				case "ViewCart":
					BKC.oOldContentDetail = this.oContent;
					ViewCartPage.init();
					break;
				case "Products":
					BKC.oOldContentDetail = this.oContent;
					ListProductsPage.reset();
					// Set title
					var oTitle = utilObj.getElById("TitleContent");
					if (oTitle != null) {
						utilObj.setTitleDocument(BKC.titlePage + " - " + utilObj.stripHtmlTags(oTitle.innerHTML));
					}
					break;
				case "DetailProduct":
					BKC.oOldContentDetail = this.oContent;
					ProductPage.run(e);
					// Set title
					var oTitle = utilObj.getElById("ProductTitle");
					if (oTitle != null) {
						utilObj.setTitleDocument(BKC.titlePage + " - " + utilObj.stripHtmlTags(oTitle.innerHTML));
					}
					// Advert
					objUrl["catID"] = utilObj.getElById("proCatID").innerHTML.trim();
					BKC.objLoadIA.showAdvert(objUrl);
					break;
				case "Compare":
					BKC.oOldContentDetail = this.oContent;
					ComparePage.init();
					break;
				case "DetailNews":
					BKC.oOldContentDetail = this.oContent;
					break;
				case "DefaultProducts":
					DefaultProductsPage.writeCatTitle();
					break;
				case "ConsultativeConsumption":
					ConsultativeConsumptionPage.initWhenSearch();
					break;
				case "Home1024_768":
					createHomePageCenter();
					createHomePageRight();
					// Advert
					BKC.objLoadIA.showAdvert(objUrl);
					LoginCustomer.showForm(BKC.customerStatus);
					break;
			}
		};
	};
	
	if (BKC.oResizeLeftBody != null) BKC.oResizeLeftBody.run(isOpen, isStatic);
	
	BKC.listContentPage[query].showMyContent(isReload);
	goToTop();
};
