var xmlhttpDelete
var xmlhttpCountry
var xmlhttpAddLookUp
var xmlhttpRemoveLookUp
var xmlhttpGetAdvertText
var xmlhttpAddNewAdvert
var xmlhttpPublishAdvert
var xmlhttpUnPublishAdvert
var xmlhttpAddClick
var xmlhttpAddClickEducation
var newAdvertType
var newAdvertSelectedCompany
var AdvertID
var thisRow
var thisPublishImage = new Image();
var thisUnPublishImage = new Image();
var AdvertIDPublish
var AdvertIDUnPublish


//function to delete an advert
function submitDelete(_AdvertID, _thisRow) {
    xmlhttpDelete = GetXmlHttpObject();
    if (xmlhttpDelete == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    thisRow = _thisRow
    AdvertID = _AdvertID;
    var requestUrl = "deleteAdvert.aspx";
    requestUrl = requestUrl + "?AdvertID=" + _AdvertID;
    requestUrl = requestUrl + "&sid=" + Math.random();
    xmlhttpDelete.onreadystatechange = deleteStateChanged;
    xmlhttpDelete.open("GET", requestUrl, true);
    xmlhttpDelete.send(null);
}

function deleteStateChanged() {
    if (xmlhttpDelete.readyState == 4) {
        var i = thisRow.parentNode.parentNode.rowIndex;
        document.getElementById('ctl00_masterContent_AdvertTable').deleteRow(i);
    }
}

//On country change
function CountryListOnChange() {
    xmlhttpCountry = GetXmlHttpObject();
    if (xmlhttpCountry == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var countryList = document.getElementById("ctl00_masterContent_countryDropDownList");
    var selectedCountry = countryList.options[countryList.selectedIndex].value;
    var requestUrl = "getCity.aspx";
    requestUrl = requestUrl + "?selectedCountry=" + selectedCountry;
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpCountry.onreadystatechange = getCityStateChanged;
    xmlhttpCountry.open("GET", requestUrl, true);
    xmlhttpCountry.send(null);
}

function getCityStateChanged() {
    if (xmlhttpCountry.readyState == 4) {
        //Gather the results from the callback          
        var cityString = xmlhttpCountry.responseXML.documentElement;

        if (cityString != null) {
            ClearAndSetCities(cityString);
        }
        else {
            alert("Δεν υπάρχουν πόλεις διαθέσιμες για αυτή την χώρα.");
        }
    }
}
function ClearAndSetCities(cityString) {

    var cityDropDownList = document.getElementById("ctl00_masterContent_cityDropDownList");
    //Clears the city dropdown list                   
    for (var count = cityDropDownList.options.length - 1; count > -1; count--) {
        cityDropDownList.options[count] = null;
    }

    var cityDescription = cityString.getElementsByTagName('description');
    var cityID = cityString.getElementsByTagName('id');
    var descriptionValue;
    var idValue;
    var optionItem;
    //Add new states list to the state combo box.
    for (var count = 0; count < cityDescription.length; count++) {
        descriptionValue = GetInnerText(cityDescription[count]);
        idValue = GetInnerText(cityID[count]);
        optionItem = new Option(descriptionValue, idValue, false, false);
        cityDropDownList.options[cityDropDownList.length] = optionItem;
    }
    cityDropDownList.disabled = false;
}
function addLookUp(dbAdvertTable, advertID, lookUpID, parentID) {
    xmlhttpAddLookUp = GetXmlHttpObject();
    if (xmlhttpAddLookUp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "addLookUpToAdvert.aspx";
    requestUrl = requestUrl + "?dbAdvertTable=" + dbAdvertTable;
    requestUrl = requestUrl + "&advertID=" + advertID;
    requestUrl = requestUrl + "&lookUpID=" + lookUpID;
    requestUrl = requestUrl + "&parentID=" + parentID;
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpAddLookUp.onreadystatechange = addLookUpStateChanged;
    xmlhttpAddLookUp.open("GET", requestUrl, true);
    xmlhttpAddLookUp.send(null);
}
function addLookUpStateChanged() {
    if (xmlhttpAddLookUp.readyState == 4) {
        document.forms[0].submit();
    }
}
function removeLookUp(dbAdvertTable, lookUpIDCurrent) {
    xmlhttpRemoveLookUp = GetXmlHttpObject();
    if (xmlhttpRemoveLookUp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "removeLookUpFromAdvert.aspx";
    requestUrl = requestUrl + "?dbAdvertTable=" + dbAdvertTable;
    requestUrl = requestUrl + "&lookUpIDCurrent=" + lookUpIDCurrent;
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpRemoveLookUp.onreadystatechange = removeLookUpStateChanged;
    xmlhttpRemoveLookUp.open("GET", requestUrl, true);
    xmlhttpRemoveLookUp.send(null);
}
function removeLookUpStateChanged() {
    if (xmlhttpRemoveLookUp.readyState == 4) {
        document.forms[0].submit();
    }
}
//On department change
function DepartmentListOnChange() {
    xmlhttpDepartment = GetXmlHttpObject();
    if (xmlhttpDepartment == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var departmentList = document.getElementById("ctl00_masterContent_departmentDropDownList");
    var selectedDepartment = departmentList.options[departmentList.selectedIndex].value;
    var requestUrl = "getPosition.aspx";
    requestUrl = requestUrl + "?selectedDepartment=" + selectedDepartment;
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpDepartment.onreadystatechange = getPositionStateChanged;
    xmlhttpDepartment.open("GET", requestUrl, true);
    xmlhttpDepartment.send(null);
}

function getPositionStateChanged() {
    if (xmlhttpDepartment.readyState == 4) {
        //Gather the results from the callback
        var positionString = xmlhttpDepartment.responseXML.documentElement;

        if (positionString != null) {
            ClearAndSetPositions(positionString);
        }
        else {
            alert("Δεν υπάρχουν θέσεις διαθέσιμες για αυτό το τμήμα.");
        }
    }
}
function ClearAndSetPositions(positionString) {

    var positionDropDownList = document.getElementById("ctl00_masterContent_positionDropDownList");
    //Clears the city dropdown list
    for (var count = positionDropDownList.options.length - 1; count > -1; count--) {
        positionDropDownList.options[count] = null;
    }

    var positionDescription = positionString.getElementsByTagName('description');
    var positionID = positionString.getElementsByTagName('id');
    var descriptionValue;
    var idValue;
    var optionItem;
    //Add new states list to the state combo box.
    for (var count = 0; count < positionDescription.length; count++) {
        descriptionValue = GetInnerText(positionDescription[count]);
        idValue = GetInnerText(positionID[count]);
        optionItem = new Option(descriptionValue, idValue, false, false);
        positionDropDownList.options[positionDropDownList.length] = optionItem;
    }
    positionDropDownList.disabled = false;
}
//Returns the node text value 
function GetInnerText(node) { return (node.textContent || node.innerText || node.text); }

//Gets the advert text real time
function getAdvertText(advertID) {
    xmlhttpGetAdvertText = GetXmlHttpObject();
    if (xmlhttpGetAdvertText == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "getAdvertText.aspx";
    requestUrl = requestUrl + "?advertID=" + advertID;
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpGetAdvertText.onreadystatechange = getAdvertTextChanged;
    xmlhttpGetAdvertText.open("GET", requestUrl, true);
    xmlhttpGetAdvertText.send(null);
}
function getAdvertTextChanged() {
    if (xmlhttpGetAdvertText.readyState == 4) {
        //Gather the results from the callback
        var advertText = xmlhttpGetAdvertText.responseText;
        document.getElementById("advertLabel").innerHTML = advertText;
    }
}
function getAdvertTextMouseOut() {
    document.getElementById("advertLabel").innerHTML = "<img src='images/loading.gif' alt='loading'/>";
}

//Create a new advert for the selected company
function addNewAdvertConfirmed(selectedCompany, advertType) {
    xmlhttpAddNewAdvert = GetXmlHttpObject();
    newAdvertType = advertType;
    newAdvertSelectedCompany = selectedCompany;
    if (xmlhttpAddNewAdvert == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "addNewAdvert.aspx";
    requestUrl = requestUrl + "?companyID=" + selectedCompany;
    requestUrl = requestUrl + "&advertType=" + advertType;
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpAddNewAdvert.onreadystatechange = addNewAdvertChanged;
    xmlhttpAddNewAdvert.open("GET", requestUrl, true);
    xmlhttpAddNewAdvert.send(null);
}
function addNewAdvertChanged() {
    if (xmlhttpAddNewAdvert.readyState == 4) {
        //Gather the results from the callback
        var editAdvertType;
        if (newAdvertType == 1)
            editAdvertType = "editAdvertSmall.aspx";
        else if (newAdvertType == 2)
            editAdvertType = "editAdvertBig.aspx";
        else if (newAdvertType == 3)
            editAdvertType = "editAdvertNoname.aspx";

        window.open(editAdvertType + '?AdvertID=' + xmlhttpAddNewAdvert.responseText + '&companyID=' + newAdvertSelectedCompany, '_blank', 'width=800,height=740,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no');
    }
}
//For the button that publishes or unpublishes the advert
function publishAdvert(publishImage, _AdvertID) {
    xmlhttpPublishAdvert = GetXmlHttpObject();
    thisPublishImage = publishImage;
    AdvertIDPublish = _AdvertID;
    if (xmlhttpPublishAdvert == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "publishAdvert.aspx";
    requestUrl = requestUrl + "?advertID=" + _AdvertID;
    requestUrl = requestUrl + "&publish=1";
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpPublishAdvert.onreadystatechange = publishAdvertChanged;
    xmlhttpPublishAdvert.open("GET", requestUrl, true);
    xmlhttpPublishAdvert.send(null);
}
function publishAdvertChanged() {
    if (xmlhttpPublishAdvert.readyState == 4) {
        if (xmlhttpPublishAdvert.status == 200) {
            thisPublishImage.src = '../images/published.gif';
            //thisPublishImage.onclick = function() { unpublishAdvert(thisPublishImage, AdvertIDPublish); ModalPopupsIndicator2(); }
            thisPublishImage.onclick = new Function("unpublishAdvert(this," + AdvertIDPublish + ");ModalPopupsIndicator2();");
            thisPublishImage.title = 'Απόκρυψη Αγγελίας';
        }
    }
}
function unpublishAdvert(publishImage, _AdvertID) {
    xmlhttpUnpublishAdvert = GetXmlHttpObject();
    thisUnPublishImage = publishImage;
    AdvertIDUnPublish = _AdvertID;
    if (xmlhttpUnpublishAdvert == null) {
        alert("Your browser does not support AJAX!");
    }
    var requestUrl = "publishAdvert.aspx";
    requestUrl = requestUrl + "?advertID=" + _AdvertID;
    requestUrl = requestUrl + "&publish=0";
    requestUrl = requestUrl + "&sid=" + Math.random();

    xmlhttpUnpublishAdvert.onreadystatechange = unpublishAdvertChanged;
    xmlhttpUnpublishAdvert.open("GET", requestUrl, true);
    xmlhttpUnpublishAdvert.send(null);
}
function unpublishAdvertChanged() {
    if (xmlhttpUnpublishAdvert.readyState == 4) {
        if (xmlhttpUnpublishAdvert.status == 200) {
            thisUnPublishImage.src = '../images/unpublished.gif';
            //thisUnPublishImage.onclick = function() { publishAdvert(thisUnPublishImage, AdvertIDUnPublish); ModalPopupsIndicator2(); }
            thisUnPublishImage.onclick = new Function("publishAdvert(this," + AdvertIDUnPublish + ");ModalPopupsIndicator2();");
            thisUnPublishImage.title = 'Δημοσίευση Αγγελίας';
        }
    }
}


//add a click to file
function addClick(fileID) {
    xmlhttpAddClick = GetXmlHttpObject();
    if (xmlhttpAddClick == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "addClick.aspx";
    requestUrl = requestUrl + "?fileID=" + fileID;
    requestUrl = requestUrl + "&sid=" + Math.random();
    //xmlhttpAddClick.onreadystatechange = addClickChanged;
    xmlhttpAddClick.open("GET", requestUrl, true);
    xmlhttpAddClick.send(null);
}

function addClickChanged() {
    if (xmlhttpAddClick.readyState == 4) { }
}

//add a click to file, education
function addClickEducation(fileID) {
    xmlhttpAddClickEducation = GetXmlHttpObject();
    if (xmlhttpAddClickEducation == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    var requestUrl = "../addClickEducation.aspx";
    requestUrl = requestUrl + "?fileID=" + fileID;
    requestUrl = requestUrl + "&sid=" + Math.random();
    //xmlhttpAddClick.onreadystatechange = addClickChanged;
    xmlhttpAddClickEducation.open("GET", requestUrl, true);
    xmlhttpAddClickEducation.send(null);
}

//The ajax object
function GetXmlHttpObject() {
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}
