var xmlHttp;
var ns=(document.layers);
var ie=(document.all);
var ns6=(document.getElementById&&!document.all);

function xstooltip_findPosX(obj) {
   var curleft = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curleft += obj.offsetLeft
         obj = obj.offsetParent;
      }
   }
   else if (obj.x) {
       curleft += obj.x;
   }
   return curleft;
}
function xstooltip_findPosY(obj) {
   var curtop = 0;
   if (obj.offsetParent) {
      while (obj.offsetParent) {
         curtop += (obj.offsetTop - 5)
         obj = obj.offsetParent;
      }
   }
   else if (obj.y) {
      curtop += obj.y;
   }
   return curtop;
}

function xstooltip_show(tooltipId, parentId, posX, posY) {
   it = document.getElementById(tooltipId);

   if ((it.style.top == '' || it.style.top == 0) && (it.style.left == '' || it.style.left == 0)) {
      // need to fixate default size (MSIE problem)
      it.style.width = it.offsetWidth + 'px';
      it.style.height = it.offsetHeight + 'px';

      img = document.getElementById(parentId);

      // if tooltip is too wide, shift left to be within parent
      if (posX + it.offsetWidth > img.offsetWidth) posX = img.offsetWidth - it.offsetWidth;
      if (posX < 0 ) posX = 0;

      x = xstooltip_findPosX(img) + posX;
      y = xstooltip_findPosY(img) + posY;

      it.style.top = y + 'px';
      it.style.left = x + 'px';
   }

   it.style.visibility = 'visible';
}

function xstooltip_hide(id) {
   it = document.getElementById(id);
   it.style.visibility = 'hidden';
}

function sendQuery(url,id) {
   xmlHttp = GetXmlHttpObject();
   if (xmlHttp == null) {
      alert ("Browser does not support HTTP Request");
      return;
   }

   xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
         document.getElementById(id).innerHTML=xmlHttp.responseText;
      }
   }
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}

function getDate()
{
   var now = new Date();
   var month = now.getMonth() + 1;
   var day = now.getDate();
   if(day < 10) { day = '0' + day; }
   var year = now.getYear();
   if(year < 2000) { year = year + 1900; }
   var dateString = year + '-' + month + '-' + day;
   return dateString;
}

function editPayment(order_id) {
   var amount = document.getElementById("textPaid"+order_id).innerHTML;
   if (document.getElementById('checkDonate'+order_id).checked == true) {
      var chk = ' checked="checked"';
   } else { 
      var chk = '';
   }
   var html = '<button style="float:right" onclick="javascript:enterPayment(\'' + order_id + '\')">Save</button>';
   html = html + '$<input type="text" id="textPaid' + order_id + '" value="' + amount + '" size="5" />';
   html = html + '<input' + chk + ' onclick="javascript:donationWarning(\'' + order_id + '\')" title="Donate Credit (yes/no)" type="checkbox" id="checkDonate' + order_id + '" />'
   document.getElementById('tdPaid'+order_id).innerHTML = html;
   document.getElementById('checkDonate'+order_id).disabled = false;
}

function enterPayment(order_id) {
   var amount = document.getElementById("textPaid"+order_id).value;
   if (document.getElementById("checkDonate"+order_id).checked == true) {
      var donate = '&donate';
   } else {
      var donate = '';
   }
   var url="enter_payment.php";
   url=url + "?order_id=" + order_id;
   url=url + "&amount=" + amount;
   url=url + donate;

   sendQuery(url,'tdPaid' + order_id);

   if (amount > 0) {
      document.getElementById('tdDate' + order_id).innerHTML = getDate();
      document.getElementById('tdDelete' + order_id).innerHTML = "&nbsp;";
   } else if (amount == 0) {
      document.getElementById('tdDate' + order_id).innerHTML = "";
      document.getElementById('tdDelete' + order_id).innerHTML = '<img title="Delete Order" alt="Delete Order" style="cursor:pointer" src="images/cross.png" height="16" width="16" onclick="javascript:confirmDelete(\'' + order_id + '\')" />';
   }
   document.getElementById('checkDonate'+order_id).disabled = true;
}

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;
}

function JumpToIt(list)
{
   var selection = list.options[list.selectedIndex].value
   if (selection != "None") 
      location.href = selection
}

function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num)) num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10) cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
   num = num.substring(0,num.length-(4*i+3))+','+
   num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function openWin(url, name, w, h) {
   popupWin = window.open(url, name, 'width=' + w + ',height=' + h + ',buttons=no,scrollbars=no,location=no,menubar=no,resizable=yes,status=no,directories=no,toolbar=no');
}

function openCalendar(url, name, w, h) {
   popupWin = window.open(url, name, 'width=' + w + ',height=' + h + ',buttons=no,scrollbars=no,location=no,menubar=no,resizable=yes,status=no,directories=no,toolbar=no');
   popupWin.focus();
}

function donationWarning(order_id) {
   if (document.getElementById('checkDonate'+order_id).checked == false) {
      alert('Please Note: By unchecking this box, you will be re-allocating the donation submitted as a credit.');
   }
}

function showAddFieldset () {
   document.getElementById('add').style.display = 'block';
   document.getElementById('addButton').style.display = 'none';
   document.getElementById('cancelButton').style.display = 'inline';
}

function hideAddFieldset () {
   document.getElementById('add').style.display = 'none';
   document.getElementById('addButton').style.display = 'inline';
   document.getElementById('cancelButton').style.display = 'none';
}

function toggleField() {
   var el = document.getElementById('addField');
   var l1 = document.getElementById('showLink');
   if (el.style.display != "block") {
      el.style.display = "block";
      l1.style.display = "none";
   } else {
      el.style.display = "none";
      l1.style.display = "inline";
   }
}


