//send ajax request
function sendAjaxRequest(urlquery, elementname, handlername) {
    var ajaxHttp; //ajax http object
    var bMozilla = false; //is mozzila

    //supports activex
    if (window.ActiveXObject) {
        ajaxHttp = new ActiveXObject("Microsoft.XMLHTTP");
        if (ajaxHttp == null) { ajaxHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
    }
    else if (window.XMLHttpRequest) {
        ajaxHttp = new XMLHttpRequest(); bMozilla = true;
    }
    else {
        //http request not supported
        alert("The XMLHttpRequest nor ActiveXObject object could not be created.");
    }

    //prevent caching
    if (urlquery.indexOf("?") > -1)
        urlquery += "&";
    else urlquery += "?";
    urlquery += "timestamp=" + new Date().getTime();

    //handle server response
    ajaxHttp.onreadystatechange = function() {
        if (ajaxHttp.readyState == 4) {
            if (ajaxHttp.status == 200) //response OK
            {
                //set response text by elementname
                if (elementname != "") document.getElementById(elementname).innerHTML = ajaxHttp.responseText;

                //set response text by handlername
                if (handlername != "") eval(handlername + "(ajaxHttp.responseText)");
            }
        }
    }

    // open
    ajaxHttp.open("GET", urlquery, true); //asynchronous
    ajaxHttp.send(null); // send
}

//send/get survey
function sendSurvey(nDocId, nValidate) {
    var nNodePos = 0;
    var sTarget = "surveycontent";

    //show survey
    if (nValidate == 0) {
        sTarget = "survey";
    }
    //vote
    else if (nValidate == 1) {
        //get radio buttons
        var inputs = $(sTarget).getElementsByTagName('input');

        //loop radio buttons
        for (i = 0; i < inputs.length; i++) {
            //set checked value
            if (inputs[i].checked == true) nNodePos = inputs[i].value;
        }

        if (nNodePos <= 0) {
            return;
        }
    }
    else if (nValidate == 2) {
        $('surveyresultbutton').style.display = 'inline';
        $('surveysubmitbutton').style.display = 'inline';
        $('surveybackbutton').style.display = 'none';
    }

    //show hide buttons
    if ($('surveybackbutton').style.display == 'none') {
        //hide survey button
        $('surveysubmitbutton').style.display = 'none';
        $('surveyresultbutton').style.display = 'none';
        $('surveybackbutton').style.display = 'inline';
    }
    else {
        $('surveysubmitbutton').style.display = 'inline';
        $('surveyresultbutton').style.display = 'inline';
        $('surveybackbutton').style.display = 'none';
    }
    
    //send ajax
    sendAjaxRequest(rooturl + "ajaxsurvey.aspx?docid=" + nDocId + "&nodepos=" + nNodePos + "&validate=" + nValidate + "&localeid= " + localeid, sTarget, "");
}

//returns branch office list
function getBranchOffices(nodeid, organizationid) {
    //send ajax request for branch office
    sendAjaxRequest(rooturl + "ajaxbranchoffice.aspx?nodeid=" + nodeid + "&organizationid=" + organizationid + "&localeid= " + localeid, "branchofficesdata", "");

    if (organizationid > 0) {
        //window.scrollBy(0, document.body.scrollHeight);
        setTimeout("window.scrollBy(0, $('branchofficesdata').scrollHeight)", 500);
    }
}

//send to friend ajax    
function sendAjaxSendToFriend() {
    //message to send
    var message = escape(document.getElementById("message").value).replace(/%0A/g, "?newline?");
    sendAjaxRequest(rooturl + "ajaxsendtofriend.aspx?localeid= " + localeid, "sendtofrienddiv", "");
}

//open send to friend modal dialog
function openSendToFriendModalDialog(url, title, nodeid) {
    //set width
    $('modalWindow').style.width = '650px'

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxsendtofriend.aspx?url=" + url + "&title=" + escape(title) + "&nodeid=" + nodeid + "&localeid= " + localeid, "modalWindow", "");

    //open modal dialog
    openModalDialog();
}

//open popup info modal dialog
function InfoPopUp(nodeid, catalognodeid) {
    //send request
    sendAjaxRequest(rooturl + "ajaxinfo.aspx?nodeid=" + nodeid + "&localeid= " + localeid + "&catalognodeid=" + catalognodeid, "modalWindow", "CheckInfoPopUpContent");
}

function sendExchangeStock(exchangeid, exchangestockid) {
    //send exchange stock request
    sendAjaxRequest(rooturl + "ajaxexchangestock.aspx?exchangeid=" + exchangeid + "&exchangestockid=" + exchangestockid, "exchangestockdiv", "");
}

//on change: sends exchange stock id to ajax
function sendExchangeStockId(exchangeid) {
    //send exchange stock request
    sendAjaxRequest(rooturl + "ajaxexchangestock.aspx?exchangeid=" + exchangeid, "exchangestockdiv", "");
}

//show calendar from
function setCalendarFrom(day, month, year, date) {
    //send calendar request
    sendAjaxRequest(rooturl + "ajaxcalendar.aspx?date=" + date + "&day=" + day + "&month=" + month + "&year=" + year + "&setcalendar=setCalendarFrom&setdate=setDateFrom", "datefromcalendarajax", "");
    
    if (date != "")
        $('datefrom').value = date;
    else
        $('datefrom').value = day + "." + month + "." + year;
}

//show calendar to
function setCalendarTo(day, month, year, date) {
    //send calendar request
    sendAjaxRequest(rooturl + "ajaxcalendar.aspx?date=" + date + "&day=" + day + "&month=" + month + "&year=" + year +"&setcalendar=setCalendarTo&setdate=setDateTo", "datetocalendarajax", "");
    
    if (date != "")
        $('dateto').value=date;
    else
        $('dateto').value = day + "." + month + "." + year;
}

//set calendar currency exchange
function setCalendarCurrencyExchange(day, month, year, date, targetid) {
    //send calendar request
    sendAjaxRequest(rooturl + "ajaxcalendar.aspx?date=" + date + "&day=" + day + "&month=" + month + "&year=" + year + "&targetid=" + escape(targetid) + "&setcalendar=setCalendarCurrencyExchange&setdate=setDateCurrencyExchange", "calendarajax", "");

    if (date != "")
        $(targetid).value = date;
        
    else
        $(targetid).value = day + "." + month + "." + year;
}

//set calendar
function setCalendar(day, month, year, date, targetid) {
    //send calendar request
    sendAjaxRequest(rooturl + "ajaxcalendar.aspx?date=" + date + "&day=" + day + "&month=" + month + "&year=" + year + "&targetid=" + escape(targetid) + "&setcalendar=setCalendar", "calendarajax", "");
    
    if (date != "")
        $(targetid).value = date;
    else
        $(targetid).value = day + "." + month + "." + year;
}

//send product calculator deposit
function sendProductCalculatorDeposit(productcalculatorhashcode, step, deposittype, currencyid, methodofremuneration, interestpayout, dateto, periodinmonths, numofbindings, amount) {
    //send ajax request
    sendAjaxRequest(rooturl + "ajaxproductcalculatordeposit.aspx?productcalculatorhashcode=" + productcalculatorhashcode + "&step=" + step + "&deposittype=" + deposittype + "&currencyid=" + currencyid + "&methodofremuneration=" + methodofremuneration + "&interestpayout=" + interestpayout + "&dateto=" + dateto + "&periodinmonths=" + periodinmonths + "&numofbindings=" + numofbindings + "&amount=" + amount.replace('.', ''), "productcalculatordepositdiv", "handleProductCalculatorDeposit");
}

//send organization content request
function setOrganizationContent(url) {
    //send ajax request
    sendAjaxRequest(rooturl + "ajaxorganization.aspx?organizationid=" + url, "ajaxorganizationdiv", "");
}

//send ajax currency exchange office currency
function sendAjaxCurrencyExchangeOffice(currencyexchangehashcode, amount, amountcurrencyid, converttocurrencyid, currencyexchangetypeid, currencyexchangedate, validate) 
{
    sendAjaxRequest(rooturl + "ajaxcurrencyexchangeoffice.aspx?currencyexchangehashcode=" + currencyexchangehashcode + "&amount=" + amount + "&amountcurrencyid=" + amountcurrencyid + "&converttocurrencyid= " + converttocurrencyid + "&currencyexchangetypeid=" + currencyexchangetypeid + "&currencyexchangedate=" + currencyexchangedate + "&validate=" + validate, "ajaxgetcurreny", "handlerCurrencyexchangeoffice");
}

//handler for currency exchange office ajax
function handlerCurrencyexchangeoffice(responseText) {
    if (document.getElementById("amount")) {
        //format currency
        formatCurrency($("amount"), 2, "true");
    }
}
