// ####### AJAX OBJECT #########################################################
function Ajax(url, method, params, response)
{
    this.url    = url       || "";
    this.method = method    || "GET";
    this.params = params    || "";
    this.response = response    || "";

    var request = null;
}
 
Ajax.prototype.runAjax = function() {
    this.createRequest();
    this.sendRequest();
}

Ajax.prototype.createRequest = function() {
    try {
        request = new XMLHttpRequest();
    } catch (tryms) {
        try {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (otherms) {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (failed) {
                request = null;
            }
        }
    }

    if (request == null)
        alert("Error implementing Ajax functionality");
}


Ajax.prototype.sendRequest = function() {
    oAjax = this;
    if(this.method.toUpperCase() == "GET")
    {
        request.open("GET", this.url + "?" + this.params, true);
        request.onreadystatechange = function(){oAjax.handleResponse(oAjax);};
        request.send(null); 
    }else{
        request.open("POST", this.url, true);
        request.onreadystatechange = function(){oAjax.handleResponse(oAjax);};
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        request.setRequestHeader("Content-length", this.params.length);
        request.send(encodeURIComponent(this.params));
    } 
}

Ajax.prototype.handleResponse = function(oAjax) {
    switch (request.readyState)
    {
        case 0:
            // uninitialized
            break;
        case 1:
            // loading
            break;
        case 2:
            // loaded
            break;
        case 3:
            // interactive
            break;
        case 4:
            // complete
            if (request.status == 200) { 
               if(typeof(oAjax.response) == 'function')
               {
                  oAjax.response();
               }else{
                  alert(oAjax.response.toString() + ' is not a function');
               }
                
                
            } else {
                var stat = request.getResponseHeader("Status");
                alert(request.status + " | " + stat);
            }
            break;
     }
}

// #############################################################################
// GENERAL FUNCTIONS
function calculateTopLocation(arr) {
  return calculateLocation(arr, "offsetTop");
}
      
function calculateLocation(arr, atribute) {
  var location = 0;
  while(arr) {
    location += arr[atribute];
    arr = arr.offsetParent;
  }
  return location;
}
      
function deleteData(element) {
  var ind = element.childNodes.length;
  for (var i = ind - 1; i >= 0 ; i--) {
    element.removeChild(element.childNodes[i]);
  }
}

// #############################################################################       
 //  onmouseover="paymentDescription('ShowIcon-Cash' ,this);" onmouseout="deleteData(this);"       
var dataDiv;  
var element;
function paymentDescription(paymentName, obj)
{
  element = obj;
  var paymentId;
  if(paymentName == "ShowIcon-Cash")
  {
    paymentId = 45;
  }
  else if(paymentName == "ShowIcon-CashOnDelivery")
  {
    paymentId = 25;
  }
  else if(paymentName == "ShowIcon-Ebanka")
  {
    paymentId = 47;
  }
  else if(paymentName == "ShowIcon-Transaction")
  {
    paymentId = 43;
  }

 	var par = parseInt(paymentId);
  var url = "WebService.asp";
  var ajax = new Ajax(url,"GET", "paymentID="+par, GetPaymentDescriptionByService);
  ajax.runAjax();
}

function GetPaymentDescriptionByService()
{ 
//  var response = request.responseXML;
  var response = request.responseText;
  alert(response);
  return;
  
  
  
  var txtNode;
  deleteData(element);
 
  
  var end = element.offsetWidth;
  var top = calculateTopLocation(element);
  dataDiv = document.createElement("div");
 
  dataDiv.style.position = "absolute";
  dataDiv.style.border = "#93BA0C 2px solid";
  dataDiv.style.background = "#EEEEEE";
  dataDiv.style.left = end + 15 + "px";
  dataDiv.style.top = top  + "px";
  dataDiv.style.padding = "2px";
  dataDiv.style.width = "150px";
  //dataDiv.style.height = "50px";
  txtNode = document.createTextNode(response);

  dataDiv.appendChild(txtNode);
  element.appendChild(dataDiv);
}
