$(function() {
    $('input[name="selectedgateway"]').click(changeGateway);
    $('form').validate({ ignore: '.ignore' });
    $('#password2').rules('add', { equalTo: '#password' });
});

function changeGateway() {
    if ($(this).val() == "0") {
        $('#fields').hide();
        $('#fields input[type="text"]').each(function(i, e) { $(e).val('').addClass('ignore'); });
        $('#pwdplaceholder').show(); $('.newpwd').hide();
        $('#password').add('#password2').attr('disabled', true).val('');
        $('#id').val(0);
        $('#verifywrapper').hide();
    }
    else {
        $.getJSON(base + 'smsgateway.ivp/GetGateway', { id: $(this).val() }, function(result) {
            if (result != null && result != '') {
                $('#fields').show();
                $('#fields input[type="text"]').each(function(i, e) { $(e).removeClass('ignore'); });
                $('#id').val(result.Id);
                $('#fromphone').val(result.FromPhone);
                $('#username').val(result.UserName);
                $('#gatewayurl').val(result.GatewayURL);
                $('a.link').attr('href', result.SiteURL).html(result.SiteURL);
                $('#verifywrapper').show();
            }
        });
    }
}

function verifySmsServer() {
    if ($('#phone').val() == '' || $('#phone').hasClass('error')) {
        alert(SmsResources.WrongPhone);
        return;
    }
    $('.send').attr('disabled', true);
    $.getJSON(base + 'smsgateway.ivp/VerifySmsServer', { sender: $('#fromphone').val(), login: $('#username').val(), password: $('#password').val(), gateway: $('#gatewayurl').val(), phone: $('#phone').val(), id: $('#id').val() },
     function(result) {
         if (result.hasOwnProperty('IsOK') && !result.IsOK) {
             $('#verifysms').hide();
             $('.send').attr("disabled", false);
             $('#errorholder').empty().css("background-color", "#ffd8d8").css("border-color", "#fca6a6").html("<div id='smserror'>" + result.Description + "</div>").show();
             $('#errorholder').css("height", $('#smserror').height() + 20);
             return;
         }
         $('#verifysms').hide();
         $('.send').attr("disabled", false);
         $('#errorholder').empty().css("background-color", "#e3ffd8").css("border-color", "#96e577").html("<div id='smserror'>" + result.Description + "</div>").show();
         $('#errorholder').css("height", $('#smserror').height() + 20);
     });

    return false;
}