// JavaScript Document


//***********************************************************************
//Important note (STANDARD)
//***********************************************************************
//Note: When exchanging data between an ajax action page and a querying page, the following is the
//order for characters that I will use as data separators: | ’ „
// Example:
//If you need information avbout restaurants you will get the following:
//Origin page|Restaurant_ID(1)„Restaurant_Name(1)„Restaurant_Address(1)[1]„ResturantAddress(1)[2]’Restaurant_ID(2)...


//The following functions are used to print the DOM tree
//while writing AJAX interfaces. It helps during the debug.
function printDOMTree(domElement, destinationWindow)
{
	var outputWindow=destinationWindow;
	if (!outputWindow)
	{
		outputWindow=window.open();
	}
	
	outputWindow.document.open("text/html", "replace");
	outputWindow.document.write("<HTML><HEAD><TITLE>DOM</TITLE><//HEAD><BODY>\n");
	outputWindow.document.write("<CODE>\n");
	traverseDOMTree(outputWindow.document, domElement, 1);
	outputWindow.document.write("</CODE>\n");
	outputWindow.document.write("</BODY></HTML>\n");
	outputWindow.document.close();
}

function traverseDOMTree(targetDocument, currentElement, depth)
{
  if (currentElement)
  {
    var j;
    var tagName=currentElement.tagName;
    // Prints the node tagName, such as <A>, <IMG>, etc
    if (tagName)
      targetDocument.writeln("&lt;"+currentElement.tagName+"&gt;");
    else
      targetDocument.writeln("[unknown tag]");

    // Traverse the tree
    var i=0;
    var currentElementChild=currentElement.childNodes[i];
    while (currentElementChild)
    {
      // Formatting code (indent the tree so it looks nice on the screen)
      targetDocument.write("<BR>\n");
      for (j=0; j<depth; j++)
      {
        // &#166 is just a vertical line
        targetDocument.write("&nbsp;&nbsp;&#166");
      }								
      targetDocument.writeln("<BR>");
      for (j=0; j<depth; j++)
      {
        targetDocument.write("&nbsp;&nbsp;&#166");
      }					
      if (tagName)
        targetDocument.write("--");

      // Recursively traverse the tree structure of the child node
      traverseDOMTree(targetDocument, currentElementChild, depth+1);
      i++;
      currentElementChild=currentElement.childNodes[i];
    }
    // The remaining code is mostly for formatting the tree
    targetDocument.writeln("<BR>");
    for (j=0; j<depth-1; j++)
    {
      targetDocument.write("&nbsp;&nbsp;&#166");
    }			
    targetDocument.writeln("&nbsp;&nbsp;");
    if (tagName)
      targetDocument.writeln("&lt;/"+tagName+"&gt;");
  }
}

//Browser Independent
//Based on code from:
//http://www.webpasties.com/xmlHttpRequest/xmlHttpRequest_tutorial_1.html
function getHTTPObject()
{

  var xmlhttp;
  
  try 
	{ 
		xmlhttp = new XMLHttpRequest(); 
	} 
	catch (error) 
	{ 
 		try 
 		{ 
   			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
 		} 
 		catch (error) 
 		{ 
			return false; 
		} 
	}
		
	return xmlhttp; 
}

//This function will trim white spaces
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//Return number in currency format
//Based on code from http://javascript.internet.com
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);
}

