var div;
var no_loading = 1;

function setData(data, action, to_div)
{ 
    xmlHttp=GetXmlHttpObject();
	
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
    }
	
    div = to_div;
    var query = data + unescape('%26action=') + action;

    switch(action)
    {
        case 0:
            no_loading = 1;
            xmlHttp.onreadystatechange = handleGeneral;
            break;
        case 1:
            xmlHttp.onreadystatechange = handleSelect;
    }
	

    xmlHttp.open("POST", '/ajax/ajax.php', true);
    //Send the proper header information along with the request
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", query.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(query);
}

function handleGeneral()
{
    if (xmlHttp.readyState == 1)
    {
        if(document.getElementById(div) && no_loading == 1)
	{
            $('#'+div).animate({opacity:0, display:'none'},500,function(){$(this).animate({opacity:1}).html('<strong class="loading">Loading...</strong>').slideDown(500)});
	}
    }
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        var data        = xmlHttp.responseText;
        var data_split  = data.split('^^');

        //Logout
        if(data_split[0] == 0)
        {
            window.location = "index.php?action=100";
        }
        else
        {
            //General load to div
            if(data_split[1] == 1)
            {
                if(document.getElementById(div))
                {
                    $('#'+div).animate({opacity:0, display:'none'},500,function(){$(this).animate({opacity:1}).html(data_split[2]).slideDown(2000)});
                }
                else{alert('No specific div');}
            }
            //do nothing
            else if(data_split[1] == 2){}
            //save new
            else if(data_split[1] == 3)
            {
                //error
                if(data_split[2] == 0)
                {
                    if(document.getElementById("error"))
                    {
                        document.getElementById("error").innerHTML = data_split[3];
                    }
                }
                //after saving object if first object
                else if(data_split[2] == 13)
                {
                    if(document.getElementById(div))
                    {
                        document.getElementById(div).innerHTML = data_split[3];
                    }

                    if(document.getElementById('objects_div'))
                    {
                        document.getElementById('objects_div').innerHTML = data_split[4];
                    }
                }
                //OK load all
                else
                {
                    if(document.getElementById(div))
                    {
                        document.getElementById(div).innerHTML = data_split[3];
                    }
                }
            }//end save new
        }//else
    }//if
}

function handleSelect()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        var data = xmlHttp.responseText;

        if(document.getElementById(div))
        {
           $('#'+div).loadSelect($.parseJSON(data));
        }
        else{alert('No select found!');}
    }
}
function handleDelete()
{
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        var data = xmlHttp.responseText;
        
        if($('#'+data).length != 0)
        {
            $('#'+data).animate({opacity:0, display:'none'},1000,function(){$(this).remove()});
        }
        else{alert('No row found');}
    }
}

function GetXmlHttpObject()
{
    var xmlHttp=null;

    try
    {
        // Firefox, Opera 8.0+, Safari
	xmlHttp=new XMLHttpRequest();
    }catch (e){
        // Internet Explorer
	try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    return xmlHttp;
}

/*empty select*/
$.fn.emptySelect = function() {
    return this.each(function(){
      if (this.tagName=='SELECT') this.options.length = 0;
    });
}
/*load select options*/
$.fn.loadSelect = function(optionsDataArray) {
    return this.emptySelect().each(function(){
      if (this.tagName=='SELECT') {
        var selectElement = this;
        $.each(optionsDataArray,function(index,optionData){
          var option = new Option(optionData.caption,
                                  optionData.value);
          if ($.browser.msie) {
            selectElement.add(option);
          }
          else {
            selectElement.add(option,null);
          }
        });
      }
    });
 }

/*disable enable input*/
function enableDisableInput(check,input_field)
{
    if($('#'+check).is(':checked'))
    {
        $('#'+input_field).removeAttr('disabled');
    }else
    {
        $('#'+input_field).attr('disabled',true);
    }
}

function popitup(url)
{
    var w   =   700;
    var h   =   192;
    var l=(screen.width - w) / 2;
    var t=(screen.height - h) / 2-100;

    var data_o  =   'height='+ h + ',width=' + w + ',left=' + l + ',top=' + t;

    newwindow=window.open(url,'name',data_o);
    if (window.focus) {newwindow.focus()}
    return false;
}

/*show one price list hide other*/
function showPriceList(to_hide, to_show, toresetcolor, toaddcolor){
    $("#"+to_hide).each(function() {
        $(this).hide();
    });
    
    $("."+toresetcolor).each(function() {
        $(this).css("background-color",'#FFFFFF');
    });

    $("#"+to_show).show();
    $("."+toaddcolor).css("background-color",'#FFC624');
    
}
