
var popupUserSetting = function() {
	popupDialogByDivById('divUserSetting');
	$('#containerUserSetting').triggerTab(1);
};

var setMaskIndex = function(zindex) {
	$('#transDiv').css('width', "100%");
	$('#transDiv').css('height', getPageSize()[1]);
	$('#transDiv').css('z-index', zindex);
	if (zindex < 0) {
		$('#transDiv').css('display','none');
	} else {
		$('#transDiv').css('display','block');
	}
};

var adjustLength = function(which) {
	var max_length = 36;
	var min_length = 14;
	if (which.size <= max_length) {
		iCount = which.value.replace(/[^\u0000-\u00ff]/g, "aa");
		which.size = iCount.length < max_length ? (iCount.length > min_length ? iCount.length : min_length) : max_length;
	}
};

// change phone
var changePhone = function() {
	if (changePhone_validate()) {
		var divId = 'changePhoneMessage';
		var newPhone = $.trim($('#cpNewPhone').val());
		$.ajax({type: 'POST', 
			url: 'change-phone.rmt',
			data: {'newPhone':newPhone},
			dataType: "text",
			success: function(message) {
				if ($.trim(message) != "") {
					if (message == "Y") {
						showMessage(divId, '更换号码成功，请等待短信验证。', 'info');
						$('#cpOldPhone').text(newPhone);
						$('#bpPhone').text(newPhone);
						$('#cpNewPhone').val('');
						popupDialogByDivById('divUserSetting');
					} else {
						showMessage(divId, '您的原手机号有误，为了您的安全，更换失败！', 'error');
					}
				} else {
					alert("您的安全登录已超时，请重新登录！");
					window.location.href = 'login.html';
					showMessage(divId, '您的安全登录已超时，请重新登录！', 'error');
				}
			},
			error: function() {
				alert("您的安全登录已超时，请重新登录！");
				window.location.href = 'login.html';
				showMessage(divId, '您的安全登录已超时，请重新登录！', 'error');
			}});
	}
};

var changePhone_validate = function() {
	var re = false;
	var divId = 'changePhoneMessage';
	if (!validate_phonenumber($('#cpNewPhone').val())) {
		showMessage(divId, '请正确填写手机号!', 'error');
	} else {
		hideMessage(divId);
		re = true;
	}
	return re;
};

// bind phone
var bindPhone = function() {
	if (bindPhone_validate()) {
		var divId = 'bindPhoneMessage'; 
		var validationCode = $.trim($('#bpValidationCode').val());
		$.ajax({type: 'POST', 
			url: 'bind-phone.rmt',
			data: {validationCode:validationCode},
			dataType: "text",
			success: function(message) {
				if ($.trim(message) != "") {
					if (message == "Y" || message == "E") {
						window.location='user-guide-profile.html';

					} else {
						showMessage(divId, '您输入的验证码有误或失效，请重新验证！', 'error');
					}
				} else {
					alert("您的安全登录已超时，请重新登录！");
					window.location.href = 'login.html';
					showMessage(divId, '您的安全登录已超时，请重新登录！', 'error');
				}
			},
			error: function() {
				alert("您的安全登录已超时，请重新登录！");
				window.location.href = 'login.html';
				showMessage(divId, '您的安全登录已超时，请重新登录！', 'error');
			}});
	}
};

var bindPhone_enabled = function() {
	$('#divBindPhoneTitle h3').text('请输入注册后系统发送给您的验证码，以确认手机真实性！');
	$('#bpValidationCode').val('');
	$('#divBindPhoneForm').css('display', 'block');
};

var bindPhone_disabled = function() {
	$('#divBindPhoneTitle h3').text('您的手机号已经绑定，欢迎使用火种！');
	$('#bpValidationCode').val('');
	$('#divBindPhoneForm').css('display', 'none');
};

var bindPhone_validate = function() {
	var re = false;
	var divId = 'bindPhoneMessage';
	if (!validate_require($('#bpValidationCode').val())) {
		showMessage(divId, '请输入验证码!', 'error');
	} else {
		hideMessage(divId);
		re = true;
	}
	return re;
};

var sendValidationCodeTimes = 0;

// send validation code
var sendValidationCode = function() {
	var divId = 'bindPhoneMessage';
	$.ajax({type: 'POST', 
		url: 'send-validate-code.rmt',
		dataType: "text",
		success: function(message) {
			if ($.trim(message) != "") {
				if (message == "Y") {
					if (sendValidationCodeTimes > 0) {
						showMessage(divId, '重新发送验证成功。', 'info');
					} else {
						sendValidationCodeTimes = 1;
					}
				} else if (message == "E") {
					showMessage(divId, '该手机已绑定，无须发送验证码。', 'info');
				} else {
					showMessage(divId, '发送验证失败，请确定您的手机号是否正确！', 'error');
				}
			} else {
				alert("您的安全登录已超时，请重新登录！");
				window.location.href = 'login.html';
				showMessage(divId, '您的安全登录已超时，请重新登录！', 'error');
			}
		},
		error: function() {
			alert("您的安全登录已超时，请重新登录！");
			window.location.href = 'login.html';
			showMessage(divId, '您的安全登录已超时，请重新登录！', 'error');
		}});
};

// data cannot be null or empty
var validate_require = function(data) {
    return ($.trim(data).length > 0);
};

// validate length of text should be not less than "length"
var validate_length = function(data, length) {
    return ($.trim(data).length >= length);
};

// data1 and data2 should be equal
var validate_equal = function(data1, data2) {
	return (data1 == data2);
};

// mobile phone number should be like 13*********/15*********
var phone_pattern = /^1[358]\d{9}$/;
var validate_phonenumber = function(data) {
    var v = $.trim(data);
    return (v != '' && phone_pattern.test(v));
};

// birthday validate
var birthday_pattern = /\d{4}-\d{2}-\d{2}/;
var validate_birthday = function(data) {
	var v = $.trim(data);
    return (v != '' && birthday_pattern.test(v));
};

// QQ
var qq_pattern = /^([0-9])+$/;
var validate_qq = function(data) {
    var v = $.trim(data);
    return (v != '' && qq_pattern.test(v));
};

// normal phone, i.e. 0576-83333333
var normalPhone_pattern = /^[0-9-]+$/
var validate_normalPhone = function(data) {
    var v = $.trim(data);
    return (v != '' && normalPhone_pattern.test(v));
};

// email should be like a@b.c
var email_pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var validate_email = function(data) {
    var v = $.trim(data);
    return (v != '' && email_pattern.test(v));
};

// show message in div's span of user setting dialog
var showMessage = function(divId, message, type) {
	var div = $('#'+divId);
	var span = $('#'+divId+' span');
	span.text(message);
	div.attr('class', 'userSettingMessage ' + type);
	div.css('visibility', 'visible');
};

// hide message div of user setting dialog
var hideMessage = function(divId) {
	var div = $('#'+divId);
	var span = $('#'+divId+' span');
	span.text('&nbsp;');
	div.css('visibility', 'hidden');
};

// hide div by its id
var hideDivById = function(divId) {
    var arr = $('#' + divId);
    arr.css("display", 'none');
};

// show div by its id
var showDivById = function(divId) {
    var arr = $('#' + divId);
    arr.css("display", 'block');
};

var changeDisplayOfDivById = function(divId){ 
    var arr = $('#' + divId);
    arr.css("display", (arr.css("display") == 'block') ? 'none' : 'block');
};

// popup the dialog with the zindex 199
var popupDialogByDivById = function(divId){ 
	var div = $('#' + divId);
	div.bgiframe();
	
    if (div.css('display') != 'none') {
    	div.jqm().jqmHide();
    } else {
    	div.jqm({modal:true}).jqmShow();
    }
	
};

//TODO can be replace with jqm
// set the dialog's zindex value
var setDialogByDivById = function(divId, zindex){ 
	var div = $('#' + divId);
	if (div.css('display') != 'none') {
		//setMaskIndex(-1);
		div.css('display', 'none');
	} else {
		//setMaskIndex(zindex);
		div.css('display', 'block');
	}
};