function navigateURL(where) {
	if (where == 'ps') {
		// window.open("/clients/toolsofsassoonhk/tc/minisite");
		window.open("http://www.toolsofsassoon.com.hk/pink_series.html");
	} else if (where == 'mem_reg') {
		location.href = "/clients/toolsofsassoonhk/tc/promotions";
	} else if (where == 'terms') {
		window.open("/clients/toolsofsassoonhk/tc/terms/minisite");
	} else if (where == 'privacy') {
		window.open("/clients/toolsofsassoonhk/tc/privacy/reg");
	} else if (where == 'main') {
		location.href = "/clients/toolsofsassoonhk/tc/home";
	} else if (where == 'main_2') {
		location.href = "http://www.toolsofsassoon.com.hk/";
	} else if (where == 'pink') {
		location.href = "/clients/toolsofsassoonhk/tc/home";
		// location.href = "http://www.toolsofsassoon.com.hk/pink_series.html";
	} else if (where == 'reg') {
		location.href = "/clients/toolsofsassoonhk/tc/members/registration";
	} else if (where == 'lucky_draw') {
		location.href = "/clients/toolsofsassoonhk/tc/members/lucky_draw";
	} else if (where == 'mini_magz') {
		location.href = "/clients/toolsofsassoonhk/pink_magz.html";
	} else if (where == 'mini_game') {
		location.href = "/clients/toolsofsassoonhk/tc/vote";
	} else if (where == 'vs') {
		location.href = "/clients/toolsofsassoonhk/tc/home";
	} else if (where == 'promotion_vid') {
		location.href = "/clients/toolsofsassoonhk/tc/promotions/detail/7";
	}
}

function verifyPassword(field_name) {
	
	var pass = document.getElementById(field_name).value;
	var area_pass = document.getElementById('verify_area_' + field_name);
	var pass_con = document.getElementById(field_name + '_con').value;
	var area_pass_con = document.getElementById('verify_area_' + field_name + '_con');
	
	if ((pass == pass_con) && (trim(pass) != "") && (trim(pass_con) != "")) {
		//area_pass.innerHTML = '<span class="notice">OK</span>';
		//area_pass_con.innerHTML = '<span class="notice">OK</span>';
		
		formErrorHandle(field_name, "1");
		formErrorHandle(field_name + '_con', "1");
		
	} else {
		if (pass == "") {
			//area_pass.innerHTML = '<span class="error">Not Vaild</span>';
			formErrorHandle(field_name, "0");
		} else {
			//area_pass.innerHTML = '<span class="notice">OK</span>';
			formErrorHandle(field_name, "1");
		}
		//area_pass_con.innerHTML = '<span class="error">Not Vaild</span>';
		formErrorHandle(field_name + '_con', "0");
	}
	
}

function verifyMemRegForm(who) {
	var have_error = false;
	for (var i = 0; i < form_check_array.length; i++) {
		//document.getElementById('verify_area_' + who[form_check_array[i]].id).innerHTML = '';
		if (trim(who[form_check_array[i]].value) == "") {
			//document.getElementById('verify_area_' + who[form_check_array[i]].id).innerHTML = '<span class="error">Not Vaild</span>';
			formErrorHandle(who[form_check_array[i]].id, "0");
			have_error = true;
		}
	}

	if ((trim(who['member_birth_year'].value) == "") || (trim(who['member_birth_month'].value) == "") || (trim(who['member_birth_day'].value) == "")) {
		have_error = true;
	}

	if (!have_error) {
		who.submit();
	}
	
	return false;
}

function formErrorHandle(_id, _status) {
	if (document.getElementById(_id).type == "text" || document.getElementById(_id).type == "password") {
		if (_status == "1") {
			document.getElementById(_id).style.backgroundColor = "#FFFFFF";
		} else {
			document.getElementById(_id).style.backgroundColor = "#FF6666";
		}
	}
}

function showHideDiv(_id, _status) {
	if (_status) {
		document.getElementById(_id).style.display = "block";
	} else {
		document.getElementById(_id).style.display = "none";
	}
}

function voteShowDiv(who) {
	if (who == "photo") {
		document.getElementById("photo_panel").style.display = "block";
		document.getElementById("video_panel").style.display = "none";
	} else {
		document.getElementById("photo_panel").style.display = "none";
		document.getElementById("video_panel").style.display = "block";
	}
}

function createEl(type, attr, style, text) {
	element = document.createElement(type);
	// Attributes
	if (attr) {
		for (var key in attr) {
			if (key == 'class') {
				element.className = attr[key];
			} else if (key == 'id') {
				element.id = attr[key];
			} else {
				element.setAttribute(key, attr[key]);
			}
		}
	}
	// Styles
	if (style) {
		for (var key in style) {
			element.style[key] = style[key];
		}
	}
	// Text
	if (text) {
		element.appendChild(document.createTextNode(text));
	}
	return element;
}

function addWarRegOtherSelect(area_id, value) {
	// if (browser == "msie") {
	// 	image_area = document.getElementById(area_id).firstChild;
	// } else {
	// 	image_area = document.getElementById(area_id);
	// }
	_area = document.getElementById(area_id);
	id = _area.childNodes.length;
	if (!value) {
		value = "";
	}
	var new_tag = createWarRegOtherSelectTag(_area, id, value, area_id);
	_area.appendChild(new_tag);	
}

function createWarRegOtherSelectTag(_area, id, value, area_id) {
	var _div = createEl('div');
	var _select = createEl('select', {'id':'item_bought_other_'+id, 'name':'member_item_bought_other[]'});
	
	sel = "selected";
	var opt = new Option("- 請選擇 -", "", (value == ""), sel);
	if (browser == "msie") {
		_select.options.add(opt);
	} else {
		_select.appendChild(opt);
	}
	
	for (var j = 0; j < item_list_label_ary.length; j++) {
		var _optgrp = createEl('optgroup', {'label':item_list_label_ary[j][0]});
		_select.appendChild(_optgrp);
		for (var i = 0; i < item_list_ary[j].length; i++) {
			if ((value == item_list_ary[j][i][0])) {
				sel = "selected";
			} else {
				sel = "";
			}
			var opt = new Option(item_list_ary[j][i][1], item_list_ary[j][i][0], (value == item_list_ary[j][i][0]), sel);
			if (browser == "msie") {
				_select.options.add(opt);
			} else {
				_optgrp.appendChild(opt);
			}
		}
	}
	
	
	_select.onchange = function () {
		if (_select.value == "") {
			if (_area.childNodes.length > 1) {
				_area.removeChild(_div);
			}
		} else {
			if (_area.childNodes[_area.childNodes.length-1].childNodes[0].value != "") {
				addWarRegOtherSelect(area_id);
			}
		}
		calculateTotalPrice();
	};
	_div.appendChild(_select);
	
	var _add = createEl('a', {'href':'javascript:void(0);'});
	_add.onclick = function () {
		if (_area.childNodes[_area.childNodes.length-1].childNodes[0].value != "") {
			addWarRegOtherSelect(area_id);
		}
	};
	_add.appendChild(document.createTextNode(' 增加'));
	_div.appendChild(_add);
	
	if (_area.childNodes.length > 0) {
		var _remove = createEl('a', {'href':'javascript:void(0);'});
		_remove.onclick = function () {
			if (_area.childNodes.length > 1) {
				_area.removeChild(_div);
			}
			calculateTotalPrice();
		};
		_remove.appendChild(document.createTextNode(' 移除'));
		_div.appendChild(_remove);
	}
	
	return _div;
}

// String Functions
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// POP DIV Function
function getPageSize() {
	var d = document;
	var w = window;
	var iebody = (d.compatMode && d.compatMode != 'BackCompat') ? d.documentElement : d.body;	
	var b = d.body;

	var xScroll = (w.innerWidth && w.scrollMaxX) ? w.innerWidth + w.scrollMaxX : Math.max(b.scrollWidth, b.offsetWidth),
		yScroll = (w.innerHeight && w.scrollMaxY) ? w.innerHeight + w.scrollMaxY : Math.max(b.scrollHeight, b.offsetHeight),
		pageWidth = (document.all && !window.opera) ? iebody.scrollWidth : (d.documentElement.clientWidth || self.innerWidth),
      	pageHeight = (document.all && !window.opera) ? Math.max(iebody.scrollHeight, iebody.clientHeight) : (d.documentElement.clientHeight || self.innerHeight);

	var width = (document.all && !window.opera) ? iebody.clientWidth : (d.documentElement.clientWidth || self.innerWidth),
		height = (document.all && !window.opera) ? iebody.clientHeight : self.innerHeight;
			
	return {
		pageWidth: Math.min(pageWidth, xScroll),
		pageHeight: Math.max(pageHeight, yScroll),
		width: width,
		height: height,		
		scrollLeft: (document.all && !window.opera) ? iebody.scrollLeft : pageXOffset,
		scrollTop: (document.all && !window.opera) ? iebody.scrollTop : pageYOffset
	}
}

function addEventListener(el, event, func) {
	try {
		el.addEventListener(event, func, false);
	} catch (e) {
		try {
			el.detachEvent('on'+ event, func);
			el.attachEvent('on'+ event, func);
		} catch (e) {
			el['on'+ event] = func;
		}
	} 
}

function hideDiv() {
	if (document.getElementById('fb_fan_box')) {
		document.getElementById('fb_fan_box').style.display = 'none';
		closeOverlay();
		setDivPos('fb_fan_box');
	}
	if (document.getElementById('item_bought_shop_box')) {
		document.getElementById('item_bought_shop_box').style.display = 'none';
		closeOverlay();
		setDivPos('item_bought_shop_box');
	}
	
}

function setOverlaySize() {
	var page = getPageSize();
	document.getElementById('overlay').style.width = page.pageWidth + 'px';
	document.getElementById('overlay').style.height = page.pageHeight + 'px';
}

function openOverlay() {
	// alert("l:"+document.getElementById('overlay'));
	document.getElementById('overlay').style.display = 'block';
}

function closeOverlay() {
	document.getElementById('overlay').style.display = 'none';
}

function getFacebookDiv() {
	openOverlay();
	setDivPos('fb_fan_box');
	document.getElementById('fb_fan_box').style.display = 'block';
	setDivPos('fb_fan_box');
	window.onresize = setDivPos;
}

function setDivPos(who) {
	document.getElementById('overlay').style.width = 1;
	document.getElementById('overlay').style.height = 1;

	var page = getPageSize();
	if (document.getElementById(who)) {
		document.getElementById(who).style.left = (page.pageWidth / 2) - (parseInt(document.getElementById(who).style.width) / 2) + 'px';
		document.getElementById(who).style.top = page.scrollTop + 20 + 'px';
	}
	setOverlaySize();	
}

function getShopDiv() {
	openOverlay();
	setDivPos('item_bought_shop_box');
	document.getElementById('item_bought_shop_box').style.display = 'block';
	setDivPos('item_bought_shop_box');
	window.onresize = setDivPos;
}

function hideShopDiv(which, who) { 
	document.getElementById('item_bought_shop_box').style.display = 'none';
	closeOverlay();
	setDivPos('item_bought_shop_box');
	document.getElementById('member_item_bought_shop').value = which;
	showShopReceipt(who)
}

function showShopReceipt(who) {
	for (var i = 0; i < 13; i++) {
		document.getElementById('receipt_'+i).style.display = 'none';
	}
	document.getElementById('receipt_'+who).style.display = 'block';
}

//  Friends List Table functions
function addFriendsTableRow() {
	var frd_table = document.getElementById('friends_list_table');
	var tr_tag = createEl("tr");
	var td_tag_1 = createEl("td");
	var td_tag_2 = createEl("td");
	var td_tag_3 = createEl("td");
	
	var _id = document.createTextNode(frd_table.getElementsByTagName("tr").length + ". ");
	var _name = createEl('input', {'type':'text', 'name':'friends_name[]', 'size':'30'});
	var _email = createEl('input', {'type':'text', 'name':'friends_email[]', 'size':'30'});
	
	td_tag_1.appendChild(_id);
	td_tag_2.appendChild(_name);
	td_tag_3.appendChild(_email);
	
	tr_tag.appendChild(td_tag_1);
	tr_tag.appendChild(td_tag_2);
	tr_tag.appendChild(td_tag_3);
	
	frd_table.tBodies[0].appendChild(tr_tag);
}

function removeFriendsTableRow() {
	var frd_table = document.getElementById('friends_list_table');
	var total_row = frd_table.tBodies[0].getElementsByTagName('tr').length;
	if (total_row > 4) {
		frd_table.tBodies[0].removeChild(frd_table.tBodies[0].rows[total_row-1]);
	}
}

function openWindow(url) {
	window.open(url,"","menubar=0,width=650,height=500");
}

function sendToFrdEmail(param1,param2) {
	$.ajax({
		url: "/clients/toolsofsassoonhk/tc/ajax_handler/",
		type: "POST",
		data: ({ajax_type:"sendToFrdEmail",param1:param1,param2:param2}),
		success: function(data) {
			$("#dialog_txt").html(data);
			$("#dialog").dialog("open");
		}
	});
}