$(document).ready(function() {

    var options = {
        
        target:        '#load_new_paciente',
        beforeSubmit:  showRequest,
        success:      showResponse,
        clearForm: true        // clear all form fields after successful submit
        //resetForm: true        // reset the form after successful submit
    };
    
    $('#newPaciente').submit(function() {
        $(this).ajaxSubmit(options);
        return false;
        
    });
    
});

function showRequest(formData, jqForm, options) {
  
    var queryString = $.param(formData);
    $("#load_new_paciente").show();
    $("#load_new_paciente").html("Cadastrando Paciente ...");
    return true;
}

function showResponse(responseText, statusText, xhr, $form)  {
    
    if(responseText == 'erro'){
       alert("Preecha os campos obrigatórios");
       return false;
    }
         
    else{
         alert("Paciente cadastrado com sucesso, termine o cadastro do exame.");
         
    }
   
}

function getBaseURL() {

    var url = location.href;  // entire url including querystring - also: window.location.href;
    var baseURL = url.substring(0, url.indexOf('/', 14));

    if (baseURL.indexOf('http://localhost') != -1) {
        // Base Url for localhost
        var url = location.href;  // window.location.href;
        var pathname = location.pathname;  // window.location.pathname;
        var index1 = url.indexOf(pathname);
        var index2 = url.indexOf("/", index1 + 1);
        var baseLocalUrl = url.substr(0, index2);

        return baseLocalUrl + "/";
    }
    else {
        // Root Url for domain name
        return baseURL + "/";
    }

}


