/**
 * 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 doContactus() {
    var form = $("#contactus_form");
    
    form.ajaxForm({
      dataType: 'json',
      success: confirmContactSubmit
    });
    
    if (form.jVal()) {
       form.submit();
    }
    return false;
}

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;
}
/**
 * confirm the data submission
 * @param {} data
 */
function confirmContactSubmit(reply){
   if (reply["success"]) {
	  $(reply["message"]).appendTo("head");
      showModalMessageBox("Your request has been submitted", "error");  
   }
   else {
      var fieldElement = $("#"+reply["field"]);
      showModalMessageBox(reply["message"], "error");
      showWarning(fieldElement, reply["message"], false, "pod");
      fieldElement.focus();
   }
}