/* Remove last element */
function removeDiv(id) {
    var cnt = $("#"+id).children().length;
    if(cnt > 1) {
        $("#"+id).children('div').last().remove();
    }
}

/* Add US Education deatils */
function addUsEducation() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_education',
        data: 'ncc='+ncc+'&type='+1,
        success: function (result) {
            $('#usaEducation').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#us_university"+ncc).selectpicker('refresh');
            $("#country_"+ncc).selectpicker('refresh');
            $("#state_"+ncc).selectpicker('refresh');
            $("#city_"+ncc).selectpicker('refresh');
        
            $('#us_graduation_date'+ncc).datepicker({
                format: 'yyyy-mm-dd',
                todayHighlight: true,
                clearBtn: true,
                endDate: today
            });
        }
    })
}

/* Add IND Education */
function addIndEducation() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_education',
        data: 'ncc='+ncc+'&type='+2,
        success: function (result) {
            $('#indEducation').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#ind_university_id"+ncc).selectpicker('refresh');
            
            $('#ind_graduation_date'+ncc).datepicker({
                format: 'yyyy-mm-dd',
                todayHighlight: true,
                clearBtn: true,
                endDate: today
            });
        }
    })
}

/* Add Certification */
function addCertification() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_certification',
        data: 'ncc='+ncc,
        success: function (result) {
            $('#certificationDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#certificate_status"+ncc).selectpicker('refresh');

            $('#certification_date'+ncc).datepicker({
                format: 'yyyy-mm-dd',
                todayHighlight: true,
                clearBtn: true,
                endDate: today
            });
            
            $('#certification_validity'+ncc).datepicker({
                format: 'yyyy-mm-dd',
                todayHighlight: true,
                clearBtn: true,
                endDate: today
            });
        }
    })
}

/* Add Resume Experience */
function addResumeExperience() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_experience',
        data: 'ncc='+ncc+'&type='+1,
        success: function (result) {
            $('#resumeExperienceDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#country_"+ncc).selectpicker('refresh');
            $("#state_"+ncc).selectpicker('refresh');
            $("#city_"+ncc).selectpicker('refresh');
        }
    })
}

/* Add IT Experience */
function addItExperience() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_experience',
        data: 'ncc='+ncc+'&type='+2,
        success: function (result) {
            $('#itExperienceDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#country_"+ncc).selectpicker('refresh');
            $("#state_"+ncc).selectpicker('refresh');
            $("#city_"+ncc).selectpicker('refresh');
        }
    })
}

/* Add Reference */
function addReference() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_reference',
        data: 'ncc='+ncc+'&type='+2,
        success: function (result) {
            $('#referenceDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#country_"+ncc).selectpicker('refresh');
            $("#state_"+ncc).selectpicker('refresh');
            $("#city_"+ncc).selectpicker('refresh');
        }
    })
}

/* Add Attachment */
function addAttachment() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_attachment',
        data: 'ncc='+ncc,
        success: function (result) {
            $('#attachmentDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);
        }
    })
}

/* Add Us Contact */
function addUsContact() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_contact',
        data: 'ncc='+ncc+'&type=1',
        success: function (result) {
            $('#usContactDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);
        }
    })
}

/* Add Overseas Contact */
function addOverseasContact() {
    var present_country_count = $("#country_count").val();
    var ncc = +present_country_count + +1;

    $.ajax({
        url: '/add_contact',
        data: 'ncc='+ncc+'&type=2',
        success: function (result) {
            $('#overseasContactDiv').children('div').last().after(result);

            // Update present country count
            $("#country_count").val(ncc);

            $("#country_"+ncc).selectpicker('refresh');
        }
    })
}