/**
 * extract from jVal, because it make this method private.
 * @param {Object} selectElement
 * @param {Object} message
 * @param {Object} autoHide
 * @param {Object} styleType
 */
function showWarning(selectElement, message, autoHide, styleType){
    $(selectElement).each(function(ind){
        var par = $(this).parent().get(0);
        $(this).css({
            'margin-top': '',
            'position': '',
            'borderColor': 'red'
        }).parent().find('.jfVal').stop().remove();
        $(par).find('.jValRelWrap').remove().end().append('<div class="jValRelWrap" style="display:none;"></div>').find('.jValRelWrap').append($(this).clone());
        var fieldWidth = $(par).find('.jValRelWrap').width(), fieldHeight = $(par).find('.jValRelWrap').height();
        $(par).find('.jValRelWrap').css({
            'width': fieldWidth,
            'height': fieldHeight
        }).empty();
        var paddedHeight = (fieldHeight + ($.fn.jVal.defaultPadding * 2));
        $(this).before('<div class="jfVal' + (styleType ? ' jfVal' + styleType : '') + '" style="left:' + ($(this).position().left - $.fn.jVal.defaultPadding - $.fn.jVal.defaultBorderWidth) + 'px; ' +
        'top:' +
        ($(this).position().top - $.fn.jVal.defaultPadding - $.fn.jVal.defaultBorderWidth + $.fn.jVal.IETopNudge) +
        'px;">' +
        ((styleType == 'pod') ? '<div class="spacerBorder" style="height:' + paddedHeight + 'px;">' : '') +
        '<div class="spacer' +
        (styleType ? ' spacer' + styleType : '') +
        '" style="height:' +
        paddedHeight +
        'px;"></div>' +
        ((styleType == 'pod') ? '</div>' : '') +
        '<div class="icon' +
        (styleType ? ' icon' + styleType : '') +
        '" style="height:' +
        paddedHeight +
        'px;"><div class="iconbg"></div></div>' +
        '<div class="content' +
        (styleType ? ' content' + styleType : '') +
        '" style="height:' +
        paddedHeight +
        'px; line-height:' +
        paddedHeight +
        'px;">' +
        message +
        '</div>' +
        '</div>');
        var spacerWidth = fieldWidth + ($.fn.jVal.defaultPadding * 2) + 8;
        $(par).find(styleType == 'pod' ? '.spacerBorder' : '.jfVal').css('padding', parseInt($.fn.jVal.defaultBorderWidth) + 'px').corner("round tr br 3px");
        $(par).find('.jfVal').width(spacerWidth + $(par).find('.icon').width() + $(par).find('.content').width() + $.fn.jVal.defaultPadding + $.fn.jVal.defaultBorderWidth);
        if (autoHide) {
            $(par).find('.spacer').width(spacerWidth).animate({
                'opacity': 0.95
            }, 2000).animate({
                'width': 0
            }, 200);
            $(par).find('.jfVal').css({
                'opacity': 0.93,
                'borderWidth': 0
            }).animate({
                'borderWidth': 0
            }, 2000).animate({
                'opacity': 0
            }, 200, function(){
                $(this).remove();
            });
            $(this).stop().animate({
                'opacity': 0.95
            }, 2000, function(){
                $(this).css('borderColor', '');
            });
        }
        else {
            $(par).find('.spacer').width(0).animate({
                'width': spacerWidth
            }, 200);
            $(par).find('.jfVal').css('opacity', 0).animate({
                'opacity': 0.95
            }, 400);
        }
        $(this).css(($.browser.msie) ? {
            'margin-top': 1,
            'position': 'absolute'
        } : {
            'position': 'absolute'
        }).parent().find('.jValRelWrap').css('display', 'block');
    });
};
	
function refreshCaptcha(){
	$.ajax({
	   type: "GET",
	   url: "/member/auth/servicerefreshcaptcha", 
	   cache: false,
	   dataType: "json", 
	   success: function(data){
	   	  $('#captcha').attr('src', "/captcha/" + data['id'] + ".png");
	   	  $('#verifyId').attr('value', data['id']);
	    },
	   error: function() {
        showModalMessageBox("Failed to connect server", "Ajax Failed");
      }
	   }
	);
	return false;
}

function checkBoxChanged() {
	if ( $("#tos").attr("checked") ) {
		$("#submit01").removeAttr("disabled");
	} else {
		$("#submit01").attr("disabled", "disabled");
	}
}

function doRegister() {
	var form = $("#register_form");
	if ( form.jVal() ) {
		var data = collectData();
		$.ajax({
			type: "POST",
			url: "/member/auth/serviceregister",
			cache: false,
			dataType: "json",
			data: data,
			success: function(reply, status){
				if (reply["success"]) {
					window.location = reply["redirect"];
				}
				else {
					var fieldElement = $("#"+reply["field"]);
					showWarning(fieldElement, reply["message"], false, "cover");
					fieldElement.focus();
				}
			},
			error: function(){
				showModalMessageBox("Failed to connect server", "Ajax Failed");
			}
		});
	}
	return false;
}

function collectData() {
	var data = {};
	data["email"] = $("#email").val();
	data["passwd"] = $("#passwd").val();
	data["fname"] = $("#fname").val();
	data["lname"] = $("#lname").val();
	data["verify"] = $("#verify").val();
	data["verifyId"] = $("#verifyId").val();
	return data;
}

function remove(id, hash, redirect) {
	$.ajax({
		type: "POST",
		url: "/member/auth/serviceremove",
		cache: false,
		dataType: "json",
		data: {
			id: id,
			hash: hash
		},
		success: function(reply, status) {
			if (reply["success"]) {
				showModalMessageBox("Remove Done", "Login account removed.", function(){
					window.location = redirect;
				});
			} else {
				showModalMessageBox("Remove Fail", reply["message"]);
			}
		},
		error: function() {
			showModalMessageBox("Failed to connect server", "Ajax Failed");
		}
	});
	return false;
}
