// JavaScript Document

function ValidProfForm(p_reg){
	var username_obj = document.frm_profile.txt_username;
	var pass1_obj = document.frm_profile.txt_password_1;
	var pass2_obj = document.frm_profile.txt_password_2;
	var email_obj = document.frm_profile.txt_email;
	var fname_obj = document.frm_profile.txt_firstname;
	var mname_obj = document.frm_profile.txt_middlename;
	var lname_obj = document.frm_profile.txt_lastname;
	var dob_day_obj = document.frm_profile.cb_day;
	var dob_month_obj = document.frm_profile.cb_month;
	var dob_year_obj = document.frm_profile.cb_year;
	var gender_obj = document.frm_profile.opt_gender;
	var country_obj = document.frm_profile.cb_country;
	var city_obj = document.frm_profile.cb_city;
	var district_obj = document.frm_profile.cb_district;
	var number_obj = document.frm_profile.txt_number;
	var street_obj = document.frm_profile.txt_street;
	var ward_obj = document.frm_profile.txt_ward;
	var homephone_1_obj = document.frm_profile.txt_homephone_1;
	var homephone_2_obj = document.frm_profile.txt_homephone_2;
	var cellphone_obj = document.frm_profile.txt_mobile;
	var workphone_1_obj = document.frm_profile.txt_workphone_1;
	var workphone_2_obj = document.frm_profile.txt_workphone_2;
	
	var my_error_obj = document.getElementById("feedback");
	my_error_obj.innerHTML = "";
	
	if (!doAvoidEmptyValue(trim(username_obj.value))){
		alert("Vui lòng cho biết tên truy cập");
		username_obj.focus();
		username_obj.select();
		return false;
	}
	if (member_id==-1)
	{
		if (!doAvoidEmptyValue(trim(pass1_obj.value))){
			alert("Vui lòng cho biết mật khẩu");
			pass1_obj.focus();
			pass1_obj.select();
			return false;
		}else if (!doAvoidEmptyValue(trim(pass2_obj.value))){
			alert("Vui lòng nhập lại mật khẩu");
			pass2_obj.focus();
			pass2_obj.select();
			return false;
		}
	}
	if(pass1_obj.value!=pass2_obj.value){
		alert("Mật khẩu không giống nhau");
		pass1_obj.value = "";
		pass2_obj.value = "";
		pass1_obj.focus();
		return false;
	}
	if (member_id==-1)
	{
		var confirm_obj = document.frm_profile.txt_confirm;
		if (!doAvoidEmptyValue(trim(confirm_obj.value))){
			alert("Vui lòng nhập mã xác nhận");
			confirm_obj.focus();
			confirm_obj.select();
			return false;
		}
	}
	if (!doAvoidEmptyValue(trim(email_obj.value))){
		alert("Vui lòng cho biết email");
		email_obj.focus();
		return false;
	}else if(!isEmail(email_obj.value)){
		alert("Địa chỉ email không hợp lệ");
		email_obj.focus();
		return false;
	}
	if (!doAvoidEmptyValue(trim(lname_obj.value))){
		alert("Vui lòng cho biết tên họ của bạn");
		lname_obj.focus();
		lname_obj.select();
		return false;
	}
	if (!doAvoidEmptyValue(trim(fname_obj.value))){
		alert("Vui lòng cho biết tên của bạn");
		fname_obj.focus();
		fname_obj.select();
		return false;
	}	
	if (!doAvoidEmptyOptions(dob_day_obj)){
		alert("Vui lòng cho biết ngày sinh của bạn");
		dob_day_obj.focus();
		return false;		
	}
	if (!doAvoidEmptyOptions(dob_month_obj)){
		alert("Vui lòng cho biết tháng sinh của bạn");
		dob_month_obj.focus();
		return false;		
	}
	if (!doAvoidEmptyOptions(dob_year_obj)){
		alert("Vui lòng cho biết năm sinh của bạn");
		dob_year_obj.focus();
		return false;		
	}
	if (!doAvoidNonSelect(gender_obj)){
		alert("Vui lòng cho biết giới tính");
		gender_obj.focus();
		return false;
	}
	if (!doAvoidEmptyOptions(country_obj)){
		alert("Vui lòng cho biết quốc gia của bạn");
		country_obj.focus();
		return false;
	}
	if (!doAvoidEmptyOptions(city_obj)){
		alert("Vui lòng cho biết tỉnh/thành phố của bạn");
		city_obj.focus();
		return false;
	}
	if (!doAvoidEmptyOptions(district_obj)){
		alert("Vui lòng cho biết quận/huyện của bạn");
		district_obj.focus();
		return false;
	}
	if (!doAvoidEmptyValue(trim(number_obj.value))){
		alert("Vui lòng cho biết số nhà của bạn");
		number_obj.focus()
		number_obj.select();
		return false;
	}
	if (!doAvoidEmptyValue(trim(street_obj.value))){
		alert("Vui lòng cho biết tên đường/ấp nhà bạn");
		street_obj.focus()
		street_obj.select();
		return false;
	}
	if (!doAvoidEmptyValue(trim(ward_obj.value))){
		alert("Vui lòng cho biết phường/xã của bạn");
		ward_obj.focus()
		ward_obj.select();
		return false;
	}
	if (!doAvoidEmptyValue(trim(homephone_1_obj.value))){
		alert("Vui lòng cho biết mã vùng số điện thoại nhà của bạn");
		homephone_1_obj.focus()
		homephone_1_obj.select();
		return false;
	}
	if (!doAvoidEmptyValue(trim(homephone_2_obj.value))){
		alert("Vui lòng cho biết số điện thoại nhà của bạn");
		homephone_2_obj.focus()
		homephone_2_obj.select();
		return false;
	}
	if (!doAvoidEmptyValue(trim(cellphone_obj.value))){
		alert("Vui lòng cho biết số điện thoại di động của bạn");
		cellphone_obj.focus()
		cellphone_obj.select();
		return false;
	}
	if (doAvoidEmptyValue(trim(workphone_1_obj.value))){
		if (!doAvoidEmptyValue(trim(workphone_2_obj.value))){
			alert("Vui lòng cho biết số điện thoại cơ quan của bạn");
			workphone_2_obj.focus()
			workphone_2_obj.select();
			return false;
		}
	}
	if (doAvoidEmptyValue(trim(workphone_2_obj.value))){
		if (!doAvoidEmptyValue(trim(workphone_1_obj.value))){
			alert("Vui lòng cho biết mã vùng số điện thoại cơ quan của bạn");
			workphone_1_obj.focus()
			workphone_1_obj.select();
			return false;
		}
	}
	
	var agree_2_receive_str = "";
	var confirm_captcha_str = "";
	var in_pregnancy_str = "";
	var dob_str = "";
	var fullname_str = "";
	var gender_str = "";
	if (p_reg)
	{
		var i_agree_obj = document.frm_profile.chk_i_agree;
		var agree_receive_obj = document.frm_profile.chk_agree_receive;
		var child_list_obj = document.getElementById("my_child_list");
		var month_obj = document.frm_profile.cb_cmonth;
		var day_obj = document.frm_profile.cb_cday;
		var year_obj = document.frm_profile.cb_cyear;
		
		var found_child = false;
		var now = new Date();
		var date_str = month_obj.options[month_obj.selectedIndex].value + "/" + day_obj.options[day_obj.selectedIndex].value + "/" + year_obj.options[year_obj.selectedIndex].value;
		var s_date = new Date(date_str);
/*
alert("doAvoidEmptyOptions(month_obj) = "+doAvoidEmptyOptions(month_obj))
alert("doAvoidEmptyOptions(day_obj) = "+doAvoidEmptyOptions(day_obj))
alert("doAvoidEmptyOptions(year_obj) = "+doAvoidEmptyOptions(year_obj))
*/
		if (doAvoidEmptyOptions(month_obj) || doAvoidEmptyOptions(day_obj) || doAvoidEmptyOptions(year_obj))
		{
//alert("HEY")
			if (!doAvoidEmptyOptions(month_obj) || !doAvoidEmptyOptions(day_obj) || !doAvoidEmptyOptions(year_obj))
			{
				var msg = "";
				msg = "Vui lòng cho biết ngày tháng năm dự sinh của bé bạn đang mang thai";
				alert(msg);
				year_obj.focus();
				return false;		
			}
			if (s_date<=now)
			{
				alert("Ngày dự sinh bé của bạn phải là một ngày trong tương lai!");
				year_obj.focus();
				return false;
			}else{
				found_child	= true;
			}
			var my_dob = year_obj.options[year_obj.selectedIndex].value + "-" + FormatLeadingZero(month_obj.options[month_obj.selectedIndex].value) + "-" + FormatLeadingZero(day_obj.options[day_obj.selectedIndex].value);
			fullname_str += (trim(fullname_str)=="")?"-1":",-1";
			gender_str += (trim(gender_str)=="")?"-1":",-1";
			in_pregnancy_str += (trim(in_pregnancy_str)=="")?"1":",1";
			dob_str += (trim(dob_str)=="")?my_dob:","+my_dob;
		}
		var child_amount = child_list_obj.rows.length;

		var my_month_obj = document.frm_profile.cb_cmonth_0;
		var my_day_obj = document.frm_profile.cb_cday_0;
		var my_year_obj = document.frm_profile.cb_cyear_0;
		var childname_obj = document.frm_profile.txt_childname_0;
		var gender_obj = document.frm_profile.opt_cgender_0;
		if (doAvoidEmptyOptions(my_month_obj) || doAvoidEmptyOptions(my_day_obj) || doAvoidEmptyOptions(my_year_obj) || doAvoidEmptyValue(childname_obj.value) || doAvoidNonSelect(gender_obj))
		{
			found_child = false;
		}else{
			if (found_child==false)
			{
				alert("Vui lòng cho biết thông tin về bé yêu của bạn");
				return false;			
			}			
		}

		if ((found_child==false) || (child_amount>1))
		{
			for (var i=0; i<child_amount; i++)
			{
				var my_in_pregnancy_obj = eval("document.frm_profile.hidd_in_pregnant_"+i);
				var my_month_obj = eval("document.frm_profile.cb_cmonth_"+i);
				var my_day_obj = eval("document.frm_profile.cb_cday_"+i);
				var my_year_obj = eval("document.frm_profile.cb_cyear_"+i);
				var childname_obj = eval("document.frm_profile.txt_childname_"+i);
				var gender_obj = eval("document.frm_profile.opt_cgender_"+i);
				if (!doAvoidEmptyValue(childname_obj.value))
				{
					alert("Vui lòng cho biết tên bé thứ "+(i+1)+" của bạn");
					childname_obj.focus();
					childname_obj.select();
					return false;
				}
				if (!doAvoidEmptyOptions(my_month_obj) || !doAvoidEmptyOptions(my_day_obj) || !doAvoidEmptyOptions(my_year_obj))
				{
					var msg = "";
					msg = "Vui lòng cho biết ngày tháng năm sinh của bé thứ "+(i+1);
					alert(msg);
					my_year_obj.focus();
					return false;		
				}
	
				var date_str = my_month_obj.options[my_month_obj.selectedIndex].value + "/" + my_day_obj.options[my_day_obj.selectedIndex].value + "/" + my_year_obj.options[my_year_obj.selectedIndex].value;
				var my_dob = my_year_obj.options[my_year_obj.selectedIndex].value + "-" + FormatLeadingZero(my_month_obj.options[my_month_obj.selectedIndex].value) + "-" + FormatLeadingZero(my_day_obj.options[my_day_obj.selectedIndex].value);
				var s_date = new Date(date_str);	
				if (s_date>now)
				{
					alert("Ngày sinh bé thứ "+(i+1)+" của bạn phải là một ngày trong quá khứ hoặc ngày hôm nay!");
					return false;
				}
				if (!doAvoidNonSelect(gender_obj))
				{
					alert("Vui lòng cho biết giới tính của bé thứ "+(i+1));
					return false;
				}
				if (gender_obj[0].checked)
				{
					my_gender = "1";
				}else if (gender_obj[1].checked){
					my_gender = "0";
				}
				fullname_str += (trim(fullname_str)=="")?childname_obj.value:","+childname_obj.value;
				gender_str += (trim(gender_str)=="")?my_gender:","+my_gender;

				in_pregnancy_str += (trim(in_pregnancy_str)=="")?my_in_pregnancy_obj.value:","+my_in_pregnancy_obj.value;
				dob_str += (trim(dob_str)=="")?my_dob:","+my_dob;
			}
		}
		
		/*
		var lst_in_pregnant_obj = document.frm_profile.hidd_in_pregnant;
		var lst_dob_obj = document.frm_profile.hidd_dob;
		var lst_fullname_obj = document.frm_profile.hidd_fullname;
		var lst_gender_obj = document.frm_profile.hidd_gender;
		var in_pregnant_obj = document.frm_profile.hidd_in_pregnant;
		var parent_type_obj = document.frm_profile.opt_parent_type[1];
		if (!doAvoidEmptyValue(trim(in_pregnant_obj.value))){
			alert("Vui lòng cho biết số thông tin về bé yêu của bạn");
			parent_type_obj.focus();
			return false;
		}
		*/
		agree_2_receive_str = (agree_receive_obj.checked)?"1":"0";
		confirm_captcha_str = document.frm_profile.txt_confirm.value;
		//in_pregnancy_str = lst_in_pregnant_obj.value;
		//dob_str = lst_dob_obj.value;
		//fullname_str = lst_fullname_obj.value;
		//gender_str = lst_gender_obj.value;
		
		//if (!doAvoidNonSelect(i_agree_obj)){
		if (!doAvoidNonChecked(i_agree_obj))
		{		
			alert("Bạn cần phải chấp nhận Điều lệ sử dụng và Chính sách bảo mật của chúng tôi");
			i_agree_obj.focus();
			return false;
		}
	}
	
	DisableEnableFormElements(document.frm_profile, "disable");
	my_error_obj.innerHTML = "<span class='feedback_bold'><img src='theme/images/icon_loading.gif' width='24' height='24' />&nbsp;Đang trong quá trình đăng ký. Vui lòng đợi</span>";
	
	var my_country = country_obj.options[country_obj.selectedIndex].value;
	var my_city = city_obj.options[city_obj.selectedIndex].value;
	var my_district = district_obj.options[district_obj.selectedIndex].value;
	var gender;
	if (gender_obj[0].checked)
	{
		gender = 1;
	}else if(gender_obj[1].checked){
		gender = 0;
	}
	if (member_id==-1)
	{
		my_password = pass1_obj.value;	
	}else{
		my_password = pass1_obj.value;
	}
	var dob = dob_year_obj.options[dob_year_obj.selectedIndex].value+"-"+dob_month_obj.options[dob_month_obj.selectedIndex].value+"-"+dob_day_obj.options[dob_day_obj.selectedIndex].value;
	
//alert("memberid="+member_id+"&username="+username_obj.value+"&password="+my_password+"&confirm_captcha="+confirm_captcha_str+"&email="+email_obj.value+"&firstname="+fname_obj.value+"&middlename="+mname_obj.value+"&lastname="+lname_obj.value+"&DOB="+dob+"&gender="+gender+"&country="+my_country+"&city="+my_city+"&district="+my_district+"&number="+number_obj.value+"&street="+street_obj.value+"&ward="+ward_obj.value+"&homephone_1="+homephone_1_obj.value+"&homephone_2="+homephone_2_obj.value+"&mobile="+cellphone_obj.value+"&workphone_1="+workphone_1_obj.value+"&workphone_2="+workphone_2_obj.value+"&child_in_pregnancy="+in_pregnancy_str+"&child_dob="+dob_str+"&child_fullname="+fullname_str+"&child_gender="+gender_str)
	
	$.ajax({
		type: "POST",
		url: "ajax/ajax.edit_profile.php",
		dataType: "json",
		data: { memberid: member_id, username: username_obj.value, password: my_password, confirm_captcha: confirm_captcha_str, email: email_obj.value, firstname: fname_obj.value, middlename: mname_obj.value, lastname: lname_obj.value, DOB: dob, gender: gender, country: my_country, city: my_city, district: my_district, number: number_obj.value, street: street_obj.value, ward: ward_obj.value, homephone_1: homephone_1_obj.value, homephone_2: homephone_2_obj.value, mobile: cellphone_obj.value, workphone_1: workphone_1_obj.value, workphone_2: workphone_2_obj.value, child_in_pregnancy: in_pregnancy_str, child_dob: dob_str, child_fullname: fullname_str, child_gender: gender_str, receive_offer: agree_2_receive_str },
		success: function(json){
			DisableEnableFormElements(document.frm_profile, "enable");
			document.location.href="#ac";
			var res = json.reply;
			if (res=="OK")
			{
				var msg = "";
				if (member_id==-1)
				{
					//ADMAX tracker
					var admax_tracker_obj = document.getElementById("admax_tracker");
					admax_tracker_obj.innerHTML = '<img width="1" height="1" border="0" vspace="0" hspace="0" src="http://ads.admaxasia.com/servlet/ajrotator/track?page=415643&zone=admaxasia2&type=sale&amount=0&customer=RegisterOK&order=">';
					
					EmptyFormElements(document.frm_profile);
					msg = "Đăng ký thành công!<br>Bạn vui lòng kiểm tra email của bạn để kích hoạt tài khoản<br><br>";
					msg += "LƯU Ý: Trong một số trường hợp email kích hoạt có thể bị trình duyệt email của bạn<br>chuyển nhầm vào trong thư mục \"Spam\"";
				}else{
					msg = "Cập nhật thông tin thành công!<br>Để thông tin mới có hiệu lực, vui lòng đăng nhập lại";
				}
				my_error_obj.innerHTML = "<span class='feedback_bold'>"+msg+"</span>";
				msg = msg.replace("<br>", "\n");
				msg = msg.replace("<br>", "\n");
				msg = msg.replace("<br>", "\n");
				msg = msg.replace("<br>", "\n");
				alert(msg);
				if (member_id==-1)
				{
					document.location.href="index.php";
				}else{
					document.location.href="index.php?p=login";
				}
			}else if(res=="INVALID_CAPTCHA"){
				document.img_captcha.src = "services/captcha/captcha.php"+ '?' + (new Date()).getTime();
				var confirm_obj = document.frm_profile.txt_confirm;
				my_error_obj.innerHTML = "<span class='feedback_bold'>Mã xác nhận không chính xác. <br>Vui lòng nhập lại!</span>";
				confirm_obj.focus();
				confirm_obj.select();				
			}else if(res=="SENDMAIL_FAIL"){
				my_error_obj.innerHTML = "<span class='feedback_bold'>Lỗi gửi email kích hoạt!<br>Quá trình đăng ký đã thành công, tuy nhiên việc gửi email kích hoạt đã không thực hiện được. Bạn vui lòng dùng chức năng quên mật khẩu để kích hoạt tài khoản</span>";
				EmptyFormElements(document.frm_profile);
			}else if(res=="FAILED"){
				my_error_obj.innerHTML = "<span class='feedback_bold'>Lỗi phát sinh trong quá trình đăng ký</span>";
			}else if(res=="USERNAME_EXIST"){
				my_error_obj.innerHTML = "<span class='feedback_bold'>Tên đăng nhập bạn chọn đã có người đăng ký sử dụng. <br>Vui lòng chọn tên khác!</span>";
				username_obj.focus();
				username_obj.select();
			}else if(res=="EMAIL_EXIST"){
				my_error_obj.innerHTML = "<span class='feedback_bold'>Địa chỉ email này đã có người đăng ký sử dụng. <br>Vui lòng cung cấp địa chỉ email khác!</span>";
				email_obj.focus();
				email_obj.select();
			}else{
				my_error_obj.innerHTML = "<span class='feedback_bold'>Lỗi phát sinh trong quá trình đăng ký</span>";
			}			
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			document.location.href="#inf";
			my_error_obj.innerHTML = "<span class='feedback'>Lỗi phát sinh trong quá trình đăng ký</span>";
			DisableEnableFormElements(document.frm_profile, "enable");
		}
	});
	return false;
}

function ChangeCity(p_city_obj)
{
	var district_obj = document.frm_profile.cb_district;
	var city = p_city_obj.options[p_city_obj.selectedIndex].value;
	var my_error_obj = document.getElementById("get_districts_feedback");

	DisableEnableFormElements(document.frm_profile, "disable");
	my_error_obj.innerHTML = "<span class='feedback_bold'><img src='theme/images/icon_loading_small.gif' width='16' height='16' /></span>";
	
	$.ajax({
		type: "POST",
		url: "ajax/ajax.get_districts.php",
		dataType: "xml",		
		data: { cityid: city },
		success: function(xmlDoc){

			var rootNode = xmlDoc.documentElement;
			if (rootNode.childNodes.length)
			{
				var count = district_obj.options.length;
				for (var i=count-1; i>=0; i--)
				{
					district_obj.remove(i);
				}
				
				var opt = new Option("-- Chọn quận/huyện --", -1);
				district_obj.options[district_obj.options.length] = opt;
				for(var i=0; i<rootNode.childNodes.length; i++)
				{
					var distID = rootNode.childNodes[i].attributes.getNamedItem("id").nodeValue;
					var distName = rootNode.childNodes[i].firstChild.nodeValue;
					opt = new Option(distName, distID);
					district_obj.options[district_obj.options.length] = opt;
				}
			}
			my_error_obj.innerHTML = "";
			DisableEnableFormElements(document.frm_profile, "enable");
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			document.location.href="#inf";
			my_error_obj.innerHTML = "<span class='feedback'>Lỗi phát sinh</span>";
			DisableEnableFormElements(document.frm_profile, "enable");
		}
	});
}

