﻿//Does a postback with custom action type
function doIndivPostback(name) {
    document.aspnetForm.action_type.value = name;
    document.aspnetForm.submit();
}

function CheckPdf(obj) {
    var mimeType = "pdf";
    var source = obj.value;
    var ext = source.substring(source.lastIndexOf(".") + 1, source.length).toLowerCase();

    if (ext == mimeType)
        return true;
    else {
        alert("Only PDF files are allowed!");
        obj.value = "";
        return false;
    }
}

function loadTab(tabObj) {
    var objId = "#" + tabObj.attr('link');
    var infoId = "#" + tabObj.attr('link') + "-info";

    if (!tabObj || !tabObj.length) {
        return;
    }

    /*$(objId).css("display", "block");
    $(infoId).css("display", "block");*/

    $(objId).fadeIn(1000);
    $(infoId).fadeIn(1000);
}

function hideTab(tabObj) {
    var objId = "#" + tabObj.attr('link');
    var infoId = "#" + tabObj.attr('link') + "-info";

    if (!tabObj || !tabObj.length) {
        return;
    }

    $(objId).css("display", "none");
    $(infoId).css("display", "none");
}

// JQuery DatePicker
//$(document).ready(function () {
//     $("#StudentDateOfBirth").datepicker({ dateFormat: 'dd.mm.yy' });
//     $("#DateFromText").datepicker({ dateFormat: 'dd.mm.yy' });
//     $("#DateToText").datepicker({ dateFormat: 'dd.mm.yy' });
//     $("#DateText").datepicker({ dateFormat: 'dd.mm.yy' });


// JSON call to the server to get states of a country
function doJSONGetStateListDyn(ctyIndex) {
    doUpdateStateRegion("/de/content/UpdateCountryRegion/", ctyIndex, "Reg" + ctyIndex, "Cnt" + ctyIndex);
    doJSONGetList("/de/content/GetStateList/", "Reg" + ctyIndex, "Cnt" + ctyIndex);
}



function doJSONUpdateRegion(ctyIndex) {
    doUpdateStateRegion("/de/content/UpdateCountryRegion/", ctyIndex, "Reg" + ctyIndex, "Cnt" + ctyIndex);
}
function doJSONGetPosStateList() {
    doJSONGetList("/de/content/GetStateList/", "PosRegion", "PosCountry");
}
function doJSONGetStateList() {
    doJSONGetList("/de/content/GetStateList/", "Region", "Country");
}

// JSON call to the server to get focus
function doJSONGetJobFocus0List() {
    doJSONGetList("/de/content/GetJobFocus0List/", "Focus0", "JobField");
}
function doJSONGetJobFocus1List() {
    doJSONGetList("/de/content/GetJobFocus1List/", "Focus1", "Focus0");
}
function doJSONGetJobFocus2List() {
    doJSONGetList("/de/content/GetJobFocus2List/", "Focus2", "Focus1");
}

function doJSONGetJobFocus0ListDyn() {
        doJSONGetList("/de/content/GetJobFocus0List/", "Focus0", "JobField");
        doJSONGetList("/de/content/GetJobFocus0List/", "FocusB0", "JobField");
        doJSONGetList("/de/content/GetJobFocus0List/", "FocusC0", "JobField");
}
function doJSONGetJobFocus1ListDyn(focusIndex) {
    if (focusIndex == 0) {
        doJSONGetList("/de/content/GetJobFocus1List/", "Focus1", "Focus0");
    }
    else if (focusIndex == 1) {
        doJSONGetList("/de/content/GetJobFocus1List/", "FocusB1", "FocusB0");
    }
    else if (focusIndex == 2) {
        doJSONGetList("/de/content/GetJobFocus1List/", "FocusC1", "FocusC0");
    }
}
function doJSONGetJobFocus2ListDyn(focusIndex) {
    if (focusIndex == 0) {
        doJSONGetList("/de/content/GetJobFocus2List/", "Focus2", "Focus1");
    }
    else if (focusIndex == 1) {
        doJSONGetList("/de/content/GetJobFocus2List/", "FocusB2", "FocusB1");
    }
    else if (focusIndex == 2) {
        doJSONGetList("/de/content/GetJobFocus2List/", "FocusC2", "FocusC1");
    }
}

function doJSONGetUniFocusList() {
    // doJSONGetList("/de/content/GetUniFocusList/", "Focus", "UnivField");
    doJSONGetList("/de/content/GetJobFocus0List/", "Focus", "UnivField");
}

function doJSONGetUniFocus123List() {
    doJSONGetList("/de/content/GetJobFocus0List/", "Focus", "UnivField");
    doJSONGetList("/de/content/GetJobFocus0List/", "Focus2", "UnivField");
    doJSONGetList("/de/content/GetJobFocus0List/", "Focus3", "UnivField");
}

//function doJSONGetList(url, elementId, parentElementId) {

//    document.getElementById(parentElementId).value;
//    doJSONGetList(url, "Focus", "UnivField");

//}

// JSON call to the server to get states of a country
function doJSONGetList(url, elementId, parentElementId) {

    // Build the operation URL
    url = url + document.getElementById(parentElementId).value;

    // Build the body of the JSON message
    var body = ''; // {}

    // Send the HTTP request
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/json");
    xmlHttp.send(body);

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4) {
            var jsondata = eval(xmlHttp.responseText);
            ClearAndSetStateListItems(elementId, jsondata);
        }
    }
}

// Call to the server to update country and region
function doUpdateStateRegion(url, index, regionElementId, ctyElementId) {

    // Build the operation URL
    url = url + index;
    url = url + "#" + document.getElementById(ctyElementId).value;
    url = url + "#" + document.getElementById(regionElementId).value;

    // Build the body of the JSON message
    var body = ''; // {}

    // Send the HTTP request
    var xmlHttp = new XMLHttpRequest();
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/json");
    xmlHttp.send(body);

}

//Clears the contents of a combo box and adds the jsondata
function ClearAndSetStateListItems(elementID, jsondata) {
    var stateList = document.getElementById(elementID); 

    //Clears the state combo box contents.
    for (var count2 = stateList.options.length - 1; count2 > -1; count2--) {
        stateList.options[count2] = null;
    }

    var optionItem;
    //Add new states list to the state combo box.
    if (jsondata != null) {
        for (var count = 0; count < jsondata.length; count++) {
            optionItem = new Option(jsondata[count].Name, jsondata[count].ID, false, false);
            stateList.options[stateList.length] = optionItem;
        }
    }
}
