


function setSearchMode(mode) {
    var url = '/setSearchSession.aspx?mode=' + mode + '&refresh=' + escape(Date());
    SetInnerHTMLFromAjaxResponse(url, 'HIDDENDIV');
}

function searchPersAjax(txtBoxId, replaceDivId) {
    var searchString = document.getElementById(txtBoxId).value;
    var url = 'orgsearch_pers.ashx?search=' + searchString + '&refresh=' + escape(Date());
    SetInnerHTMLFromAjaxResponse(url, replaceDivId);
}

function selectPersonAjax(inId, replaceId, artist) {
    try {
        var src = document.getElementById(inId);
        var value = src.options[src.selectedIndex].value;
        var url = 'org_add_pers.ashx?perm_id=' + value + '&refresh=' + escape(Date()) + '&artist=' + artist;
        SetInnerHTMLFromAjaxResponse(url, replaceId);
    }
    catch (err) {
    }
}

function addPersonToOrgConnection(replaceId, counter) {
    var perm_id = document.getElementById("CUSTOM_PERS_ADD_ID").value + '';
    var username = document.getElementById("CUSTOM_PERS_ADD_NAME").value + '';
    var orgId = document.getElementById("CUSTOM_ORG_ID").value + '';
    var positionselect = document.getElementById("CUSTOM_PERS_ADD_POSITION");
    var position = positionselect.options[positionselect.selectedIndex].value + '';
    var direct = document.getElementById("CUSTOM_PERS_ADD_DIRECT").value + '';
    var mobile = document.getElementById("CUSTOM_PERS_ADD_MOBILE").value + '';
    var email = document.getElementById("CUSTOM_PERS_ADD_EMAIL").value + '';
    var positionText = document.getElementById("CUSTOM_PERS_ADD_POSITIONTEXT").value + '';
    var typeSelect = document.getElementById("CUSTOM_PERS_ADD_TYPE");
    var type = typeSelect.options[typeSelect.selectedIndex].value + '';
    var errortext = '';
    if (username == '') {
        errortext += '- Navn må fylles ut.\n';
    }
    if (position == '' && type == 'Person') {
        errortext += '- Stilling må velges.\n';
    }
    if (email == '' && type == 'Person') {
        errortext += '- E-post må fylles ut.\n';
    }
    if (errortext != '') {
        alert('Feil!\n' + errortext);
    } else {
        var url = 'add_person_to_org.aspx?org_id=' + orgId + '&perm_id=';
        url = url + perm_id + '&position=' + position + '&direct=' + direct + '&mobile=' + mobile + '&email=' + email + '&type=' + type + '&positiontext=' + positionText + '&username=' + username + '&counter=' + counter;
        SetInnerHTMLFromAjaxResponse(encodeURI(url), replaceId);
    }
}

function addPersonToOrgConnectionList(replaceId) {
    var perm_id = '';
    jQuery("input[type='checkbox']").each(
		function(el) {
		    if (this.checked) {
		        perm_id = this.name;
		        return false;
		    }
		}
		);

    if (perm_id == '') {
        alert('Feil!\n- Du må velge en av brukerne.');
    } else {
        var username = document.getElementById("CUSTOM_PERS_ADD_NAME").value + '';
        var orgId = document.getElementById("CUSTOM_ORG_ID").value + '';
        var positionselect = document.getElementById("CUSTOM_PERS_ADD_POSITION");
        var position = positionselect.options[positionselect.selectedIndex].value + '';
        var direct = document.getElementById("CUSTOM_PERS_ADD_DIRECT").value + '';
        var mobile = document.getElementById("CUSTOM_PERS_ADD_MOBILE").value + '';
        var email = document.getElementById("CUSTOM_PERS_ADD_EMAIL").value + '';
        var positionText = document.getElementById("CUSTOM_PERS_ADD_POSITIONTEXT").value + '';
        var typeSelect = document.getElementById("CUSTOM_PERS_ADD_TYPE");
        var type = typeSelect.options[typeSelect.selectedIndex].value + '';
        var errortext = '';
        if (username == '') {
            errortext += '- Navn må fylles ut.\n';
        }
        if (position == '' && type == 'Person') {
            errortext += '- Stilling må velges.\n';
        }
        if (email == '' && type == 'Person') {
            errortext += '- E-post må fylles ut.\n';
        }
        if (errortext != '') {
            alert('Feil!\n' + errortext);
        } else {
            var url = 'add_person_to_org.aspx?org_id=' + orgId + '&perm_id=';
            url = url + perm_id + '&position=' + position + '&direct=' + direct + '&mobile=' + mobile + '&email=' + email + '&type=' + type + '&positiontext=' + positionText + '&username=' + username + '&counter=2';
            SetInnerHTMLFromAjaxResponse(encodeURI(url), replaceId);
        }
    }
}


function deletePerson(permOrgId, orgId, replaceId) {
    var url = 'add_person_to_org.aspx?delete=1&org_id=' + orgId + '&permorgid=' + permOrgId;
    SetInnerHTMLFromAjaxResponse(url, replaceId);
}

function editPerson(permOrgId, orgId, replaceId) {
    var url = 'add_person_to_org.aspx?edit=1&cmd=find&org_id=' + orgId + '&permorgid=' + permOrgId;
    SetInnerHTMLFromAjaxResponse(url, replaceId);
}

function updatePerson(permOrgId, orgId, replaceId) {
    var permorgid = document.getElementById("CUSTOM_PERS_ADD_ID").value + '';
    var orgId = document.getElementById("CUSTOM_ORG_ID").value + '';
    var positionselect = document.getElementById("CUSTOM_PERS_ADD_POSITION");
    var position = positionselect.options[positionselect.selectedIndex].value + '';
    var direct = document.getElementById("CUSTOM_PERS_ADD_DIRECT").value + '';
    var mobile = document.getElementById("CUSTOM_PERS_ADD_MOBILE").value + '';
    var email = document.getElementById("CUSTOM_PERS_ADD_EMAIL").value + '';
    var positionText = document.getElementById("CUSTOM_PERS_ADD_POSITIONTEXT").value + '';
    var typeSelect = document.getElementById("CUSTOM_PERS_ADD_TYPE");
    var type = typeSelect.options[typeSelect.selectedIndex].value + '';
    var errortext = '';
    if (email == '' && type == 'Person') {
        errortext += '- E-post må fylles ut.\n';
    }
    if (errortext != '') {
        alert('Feil!\n' + errortext);
    } else if (permorgid == '') {
        alert('Feil!\nEn feil har oppstått,\n velg rediger på nytt');
    }
    else {
        var url = 'add_person_to_org.aspx?org_id=' + orgId + '&edit=1&cmd=update&perm_id=';
        url = url + permorgid + '&position=' + position + '&direct=' + direct + '&mobile=' + mobile + '&email=' + email + '&type=' + type + '&positiontext=' + positionText;
        SetInnerHTMLFromAjaxResponse(url, replaceId);
    }
}

/* AJAX functionality ------------------------------------------------------- */

/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

var debug = false;
/**
Browser Compatability function.
Returns the correct XMLHttpRequest depending on the current browser.
*/
function GetXmlHttp() {
    var xmlhttp = false;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)// code for IE
    {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    }
    return xmlhttp;
}


/**
<summary>
Gets the response stream from the passed url, and then calls the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="callbackFunction">The function to call after the response has been recieved. the response <b>must</b> always be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters you want to pass to the functions. (Note: only constants/strings/globals can be passed as params, most variables will be out of scope.) </param>
</summary>
<example>
<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'div1\',\'div2\',\'div3\'');

function FunctionToHandleTheResponse(response, d1, d2, d3){
var data = response.split(';');
document.getElementById(d1).innerHTML = data[0];
document.getElementById(d2).innerHTML = data[1];
document.getElementById(d3).innerHTML = data[2];
}
</code>
</example>
*/
function PassAjaxResponseToFunction(url, callbackFunction, params) {
    var xmlhttp = new GetXmlHttp();
    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {
        xmlhttp.
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
            if (xmlhttp.status == 200) {
                var response = xmlhttp.responseText;
                var functionToCall = callbackFunction + '(response,' + params + ')';
                if (debug) {
                    alert(response);
                    alert(functionToCall);
                }
                eval(functionToCall);
            } else if (debug) {
                document.write(xmlhttp.responseText);
            }
        }
    }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}


/**
///<summary>
///Sets the innerHTML property of obj_id with the response from the passed url./
///</summary>
///<param name="url">The url to make the request to get the response data.</param>
///<param name="obj_id">The object or the id of the object to set the innerHTML for.</param>
*/
function SetInnerHTMLFromAjaxResponse(url, obj_id) {
    var xmlhttp = new GetXmlHttp();
    //now we got the XmlHttpRequest object, send the request.
    if (xmlhttp) {

        xmlhttp.onreadystatechange = function() {
            if (xmlhttp && xmlhttp.readyState == 4) {//we got something back..
                if (xmlhttp.status == 200) {
                    if (debug) {
                        alert(xmlhttp.responseText);

                    }
                    if (typeof obj_id == 'object') {
                        obj_id.innerHTML = xmlhttp.responseText;
                    } else {
                        document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
                    }
                } else if (debug) {
                    document.Write(xmlhttp.responseText);
                }
            }
        }
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
    }
}


/* End if AJAX functionality ----------------------------------------------- */
