// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();

// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)

    alert("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp;
}

function get_price(opt)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }


    var url="./components/com_virtuemart/ajax_vm.php?opt="+opt
    xmlHttp.onreadystatechange=stateChanged2
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)

}

function create_room(user_id, room_type, smeta_id)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var url="./components/com_smeta/ajax_smeta.php?task=create_room&room_type="+room_type+"&user_id="+user_id+"&smeta_id="+smeta_id
    xmlHttp.onreadystatechange=stateChanged2
    document.getElementById("create_room_text").style.display="none";
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}


function edit_room_info(room_id)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var url="./components/com_smeta/ajax_smeta.php?task=edit_room_info&room_id="+room_id
    xmlHttp.onreadystatechange=stateChanged2
    document.getElementById("room_info").style.display="none";
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function del_room(room_id, i)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var url="./components/com_smeta/ajax_smeta.php?task=del_room&room_id="+room_id
    if(confirm("Удалить помещение?")){
    document.getElementById("my_room_"+i+"").style.display="none";
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
}


function del_smeta(smeta_id, i)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var url="./components/com_smeta/ajax_smeta.php?task=del_smeta&smeta_id="+smeta_id
    if(confirm("Удалить смету?")){
    document.getElementById("my_smeta_"+i+"").style.display="none";
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
    }
}


function check_smeta_for_room(room_id, i)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var smeta_id_arr= new Array();
    for (var x = 0; x < i; x++) {

        if(document.getElementById("smeta_check_"+x+"").checked  ){
            smeta_id_arr[x]=document.getElementById("smeta_check_"+x+"").value

        } else {
          smeta_id_arr[x]='null';
        }
    }


    var url="./components/com_smeta/ajax_smeta.php?task=check_smeta_for_room&room_id="+room_id+"&i="+i+"&smeta_arr="+smeta_id_arr
    xmlHttp.onreadystatechange=stateChanged4

    xmlHttp.open("GET",url,true)

    xmlHttp.send()


}

function check_room_for_smeta(smeta_id, i)
{
    xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var room_id_arr= new Array();
    for (var x = 0; x < i; x++) {

        if(document.getElementById("room_check_"+x+"").checked  ){
            room_id_arr[x]=document.getElementById("room_check_"+x+"").value

        } else {
          room_id_arr[x]='null';
        }
    }


    var url="./components/com_smeta/ajax_smeta.php?task=check_room_for_smeta&smeta_id="+smeta_id+"&i="+i+"&room_arr="+room_id_arr
    xmlHttp.onreadystatechange=stateChanged2

    xmlHttp.open("GET",url,true)

    xmlHttp.send()


}



function room_smeta_list_ajax(room_id){
  xmlHttp=GetXmlHttpObject()

    if (xmlHttp==null) {
        alert ("Browser is not alowed HTTP")
        return
    }

    var url="./components/com_smeta/ajax_smeta.php?task=room_smeta_list_ajax&room_id="+room_id

     xmlHttp.onreadystatechange=stateChanged2  
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function stateChanged2()
{
    var ajaxloader = document.getElementById('ajaxloader');

    if (xmlHttp.readyState == 1) {
        ajaxloader.style.display = 'block';
    }

    else if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        ajaxloader.style.display = 'none';
        document.getElementById("response_module").innerHTML=xmlHttp.responseText
    }
}

function stateChanged4()
{
    var ajaxloader = document.getElementById('ajaxloader');

    if (xmlHttp.readyState == 1) {
        ajaxloader.style.display = 'block';
    }

    else if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        ajaxloader.style.display = 'none';
        document.getElementById("response_title8").innerHTML=xmlHttp.responseText;

    }
}

function stateChanged3()
{
    var ajaxloader = document.getElementById('ajaxloader3');

    if (xmlHttp.readyState == 1) {
        ajaxloader.style.display = 'block';
         document.getElementById("my_room_smeta_list").innerHTML="";
    }

    else if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
        ajaxloader.style.display = 'none';

        document.getElementById("my_room_smeta_list").innerHTML=xmlHttp.responseText;
    }
}


function GetXmlHttpObject()
{
var objXMLHttp=null
if (window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest()
}
else if (window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
}
return objXMLHttp
}

