﻿// ******************************************************
// Profile functions
// ******************************************************

function listFindByValue(list, value) {
	for (i=0; i < list.options.length; i++) {
		if (list.options[i].value == value)
			return list.options[i];
	}
	return list.options[0];
}

function checkPersonalize() {
	var c = getCookie("p_Name");
	return ((c != null) && (c != ""));
}

function loadProfile(pForm) {
	if (!checkPersonalize()) return;
	pForm.txtName.value = getCookie("p_Name");
	listFindByValue(pForm.dropCity, getCookie("p_City")).selected = true;
	listFindByValue(pForm.dropAge, getCookie("p_Age")).selected = true;
	pForm.cbMale.checked = getCookie("p_Gender") == 1;
	pForm.cbFemale.checked = getCookie("p_Gender") == 0;
}

function updateProfile(pForm) {
	setStaticCookie("p_Name", pForm.txtName.value);
	setStaticCookie("p_City", pForm.dropCity.value);
	setStaticCookie("p_Age", pForm.dropAge.value);
	setStaticCookie("p_Gender", pForm.cbMale.checked ? 1:0);
}

function validateProfile(pForm) {
	check = (pForm.txtName.value != null) && (pForm.txtName.value != "");
	check = check && (pForm.dropCity.value != null) && (pForm.dropCity.value != "");
	check = check && (pForm.dropAge.value != null) && (pForm.dropAge.value != "");
	if (!check)
		alert("Bạn chưa điền đầy đủ thông tin");
	return check;
}

// ******************************************************
// Favourite Zones functions
// ******************************************************

function loadFavouriteZones(pForm, zoneList) {
	if (getCookie("f_Zones") == null)
		return;
	zones = getCookie("f_Zones").split("!");
	if (zones == null) 
		return;
	// clear all
	for (i = 0; i < pForm.checkbox.length; i++)
		pForm.checkbox[i].checked = false ;	

	// recheck 
	for (i=0; i<zones.length; i++) {
		for (j=0;j<pForm.checkbox.length; j++)
		{
			if (pForm.checkbox[j].value == "ZONE_"+zones[i]) 
			{
				pForm.checkbox[j].checked = true;
				break;
			}
		}
	}
}

function updateFavouriteZones(pForm, zoneList) {
	if (zoneList == null)
		return;
	favZones = "";
	for (i=0; i<pForm.checkbox.length; i++) {
		for (j=0;j<zoneList.length; j++)
		{
			if ((pForm.checkbox[i].value == zoneList[j]) && (pForm.checkbox[i].checked))
			{
				favZones += "!"+zoneList[j].replace("ZONE_", "");
				break;
			}
		}
	}
	if (favZones.length > 0) {
		favZones = favZones.substring(1);
	}
	setStaticCookie("f_Zones", favZones);
}

// ******************************************************
// Favourite Stories functions
// ******************************************************

function addFavouriteStory(storyId, title, link) {
	deleteFavouriteStory(storyId);
	current = getCookie("f_Stories");
	if (current == null)
		current = "";
	if (current.length > 0)
		current = "!"+current;
	current = storyId+"|"+title+"|http://"+document.location.host+link + current;
	setStaticCookie("f_Stories", current);
}

function deleteFavouriteStory(storyId) {
	current = getCookie("f_Stories");
	if (current == null)
		return;

	stories = current.split("!");
	newStories = "";	
	for (i=0; i<stories.length; i++) {
		story = stories[i].split("|");
		if (story[0] != storyId)
			newStories += "!"+stories[i];
	}
	if (newStories.length > 0)
		newStories = newStories.substring(1);
	setStaticCookie("f_Stories", newStories);
}

function clearAllFavouriteStories() {
	setStaticCookie("f_Stories", "");
}

function displayFavouriteStories() {
	// o^ng hardcode da^'y :p so chua :| he he

	current = getCookie("f_Stories");
	if (current == null) {
		document.write("Chưa có tin nào trong danh sách");
		return;
	}
	document.write("<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" width=\"100%\">");
	stories = current.split("!");
	for (i=0; i<stories.length; i++) {
		story = stories[i].split("|");
		document.write("<tr>");
		document.write("<td>");
		document.write("<a href=\"#\" onclick=\"javascript:window.opener.location='"+story[2]+"';\">"+story[1]+"</a>");
		document.write("</td>");
		document.write("<td>");
		document.write("<input type=\"image\" src=\"/Images/del.gif\" onclick=\"javascript:deleteFavouriteStory('"+story[0]+"');window.location.reload();\">");
		document.write("</td>");
		document.write("</tr>");
	}
	document.write("</table>");
}

function clearPersonalInfo() {
	deleteStaticCookie("p_Name");
	deleteStaticCookie("p_City");
	deleteStaticCookie("p_Age");
	deleteStaticCookie("p_Gender");
	deleteStaticCookie("f_Zones");
	deleteStaticCookie("f_Stories");
}
