/*	
    Esp_Javascript_Lib_ver1k_20_Jan_2011.js
        
    By Jack Penman
    
    NOTE this lib requires the Esp_StyleSheets_ver1a_4_March_2007.css
	  lib to be in the same dir. And it MUST be included in the html or tmpl file.
    
    11 Feb 2008 > Added the pop up calendar functions at the bottom.
	  
*/

/*
    Setup the Page Links for preview etc.
    Run the initAll() func when the window is displayed.
    
    NOTE Dont run this if you dont want ALL of the links to be previewed.
*/

// 10 June 2008 > I enabled this for the drop down menus.
window.onload = initAll;
// window.onload = alert("welcome");

var xhr = false;
var xPos, yPos;


/*
    Pop Up Text Array >>>>>>
	This array holds the text for the displayPopUp() function.
	The values can be set in the prog or web page.
*/
var popUpText = new Array();

// Set a default value.
popUpText[1] = "This is popuptext 1";

var popUpNo = 0;

/*
    Setup the getMousePos() func event handler.
    
    This will cause the getMousePos() func to be called every time the mouse is moved.
    It will update the global mouseX and mouseY vars so other funcs always know where the mouse is.
*/
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMousePos() function onMouseMove
document.onmousemove = getMousePos;

// Holds the current mouse pointer position on the screen.
//	Updated by the getMousePos() func.
var mouseX, mouseY;

/*
    22 Jan 2010 Jquery select all check boxes function.
	This is the function used on pg 531 etc to allow all the 
	row check boxes to be selected in one go.
*/
function jqCheckAll3( id, pID )
{
   $( "#" + pID + " :checkbox").attr('checked', $('#' + id).is(':checked'));
}

/* 31 Dec 2009 Disable Enter key

    Added this func to have a form field ignore the enter key if required. 
    ie if the user presses the enter key in a fomr field that has the 
	onkeydown="return DisableEnter();"  action in it the Enter key will NOT submit the form.
*/
function DisableEnter(e)
{
    /* If the use pressed the enter key stop the form submitting. */
    var key;
    if(window.event)
	key = window.event.keyCode;     //IE
    else
	key = e.which;     //firefox
	
    if(key == 13)
	return false;
    else
	return true;
}


/* 31 Dec 2009 Disable Enter key

    Added this func to have a form field ignore the enter key if required. 
    ie if the user presses the enter key in a fomr field that has the 
	onkeydown="return DisableEnter();"  action in it the Enter key will NOT submit the form.
	
    NOTE this is a short ver of the DisableENter() func above.
*/
function de(e)
{
    /* If the use pressed the enter key stop the form submitting. */
    var key;
    if(window.event)
	key = window.event.keyCode;     //IE
    else
	key = e.which;     //firefox
	
    if(key == 13)
	return false;
    else
	return true;
}


/* NOTE this one does NOT work in Firefox */
function DisableEnter_prev_good()
{
    /* If the use pressed the enter key stop the form submitting. */
    if( event.keyCode == 13 ) 
	/* alert('Pressed Enter key'); */
	return false;
   
    return true;
}



/*
    User Confirmation 
    Allow the user to confirm an action after clicking a button.
    A yes and no action is provided so we have something to do.
*/
function confirm_button(yes_action,no_action)
{
    var ok=confirm('Click "OK" to go to proceed, or "CANCEL" to stop.')
    if (ok)
	/* Yes Do it */
	location=yes_action
    else
	/* NO Dont Close the button */
	location=no_action    
}

/*
    15 Nov 2007 > Start Ajax Functions >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
*/
function getXMLHTTPRequest() 
{
var req = false;
try 
  {
   req = new XMLHttpRequest(); /* e.g. Firefox */
  } 
    catch(err1) 
    {
    try 
    {
        req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
    } 
    catch(err2) 
    {
    try 
      {
       req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
      } 
      catch(err3) 
        {
         req = false;
        } 
    } 
  }
return req;
}

var myRequest = getXMLHTTPRequest();

/*
    Call Ajax Function 
    
    This func is called when ever Ajax is used.
    Get the input mode so we know what to do.
    
    NOTE at the end of this func we will call the do_ajax_Final.cgi prog
         and provide it with the ajax_input_text and the mode we are working in.
*/
function callAjax(ajax_server,input_mode) 
{
    // declare a variable to hold some information to pass to the server
    var lastname = 'Smith';

    /*
	Get the inpuit ajax text.
	    This is the value from the ajax_input form field.
    */
    // var ajax_input_text_handle = document.getElementById("ajax_input");
    var ajax_input_text = document.esp_cpf.ajax_input.value;
    
    // var ajax_pg524_add_quantity = document.esp_cpf.pg524_add_quantity.value;
    // alert('Mode [' + input_mode + ']' + 'Qty [' + ajax_pg524_add_quantity + ']');
    // alert('Mode [' + input_mode + ']');

    /*
	Ajax Generic Customer Search Mode >>>>>>>>>>>
	    5 March 2008 > This is a generic mode for looking for 
		customers.
    */
    if( input_mode == "mode=ajax_find_cust" )
    {
	// Get the search text for the customer.
	var ajax_cust_search_text = document.esp_cpf.ajax_cust_search_text.value;

    
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg221_find_custm mode so it will return a select menu of customers.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg221_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=ajax_find_cust' + '&ajax_cust_search_text=' + ajax_cust_search_text + '&crc=0';
	
	// alert('Ajax Generic Find Cust Mode ');
    }




    /*
	Pg221 Find Customer Mode >>>>>>>>>>>
	    In this mode the user is looking for the customer to use with the order.
    */
    if( input_mode == "mode=pg221_find_cust" )
    {
    
	// Get the search text for the customer.
	var ajax_pg221_cust_search_text = document.esp_cpf.pg221_find_cust_text.value;

    
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg221_find_custm mode so it will return a select menu of customers.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg221_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=pg221_find_cust' + '&pg221_find_cust_text=' + ajax_pg221_cust_search_text + '&crc=0';
	
	// alert('Pg221 Find Cust Mode ');
    }



    /*
	Ajax Find Supplier Search Mode >>>>>>>>>>>
	    23 Sep 2008 > This is used for looking for suppliers.
    */
    if( input_mode == "mode=ajax_find_supplier" )
    {
	// Get the search text for the supplier.
	var ajax_supplier_search_text = document.esp_cpf.ajax_find_supplier_text.value;
    	
    
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//    Set the mode to ajax_find_supplier and put the search text provided into the
	//	ajax_supplier_search_text= header field so the do_ajax_Final prog can use it.
	ajax_input_text = ajax_input_text + '&ajax_mode=ajax_find_supplier' + '&ajax_supplier_search_text=' + ajax_supplier_search_text + '&crc=0';
	
	// alert('Ajax Generic Find Supplier Mode Text [' + ajax_supplier_search_text + ']');
    }



    /*
	POS Terminal Select Stock Item Mode >>>>>>>>>>>
	    20 Jan 2011 > The user wishes to select a stock item to add to a POS sale.
	    In this mode we are simply letting the user seaerch for stock items and
	    returning a select menu back for each search they do.
	    They can then select the desired item from the list.
    */
    if( input_mode == "mode=pos_select_item" )
    {
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg221_select_item mode so it will return a select menu of stock items.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg221_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=pos_select_item' +'&crc=0';
	
	// alert('Pg221 Select Item Mode ');

    } // End if( input_mode == "mode=pos_select_item" )


    /*
	POS Terminal Add Stock Item Mode >>>>>>>>>>>
	    20 Jan 2011 > Add the selected stock item to current POS terminal order.
	    Get the item barcode and quantity so we can call the do_ajax_Final prog and add it.
    */
    if( input_mode == "mode=pos_add_item" )
    {
	// Get the Barcode of the currently selected stock item we need to add >>>>>>
	var ajax_pg221_item_bcode = document.esp_cpf.pg221_select_item.value;
	
	// Get the quantity of the item to add from the pg524_add_quantity field.
	var ajax_pg221_add_quantity = document.esp_cpf.pg221_add_quantity.value;

	// Get the Order no
	var ajax_pg221_order_no = document.esp_cpf.pg221_order_no.value;

		
	// alert('Pg221 Add item Mode [' + input_mode + ']' + 'Bcode [' + ajax_pg221_item_bcode + ']' + 'Qty [' + ajax_pg221_add_quantity + ']');
	// alert('Mode [' + input_mode + ']' + 'Qty [' + ajax_pg524_add_quantity + ']');
		
	// Add the Ajax Mode, Item Bcode and quantity to the ajax_input_text >>>>>>>>>>>>>
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg221_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&add_item_bcode=' + ajax_pg221_item_bcode + '&add_item_qty=' + ajax_pg221_add_quantity + '&ajax_mode=pos_add_item' +'&order_no=' + ajax_pg221_order_no + '&crc=0';

    } // End if( input_mode == "mode=pos_add_item" )




    /*
	Pg221 Select Stock Item Mode >>>>>>>>>>>
	    In this mode we are simply letting the user seaerch for stock items and
	    returning a select menu back for each search they do.
	    They can then select the desired item from the list.
    */
    if( input_mode == "mode=pg221_select_item" )
    {
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg221_select_item mode so it will return a select menu of stock items.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg221_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=pg221_select_item' +'&crc=0';
	
	// alert('Pg221 Select Item Mode ');

    } // End if( input_mode == "mode=pg221_select_item" )


    /*
	Pg221 Add Stock Item Mode >>>>>>>>>>>
	    In this mode we want to ADD the selected stock item to the order on pg221.
	    Get the item barcode and quantity so we can call the do_ajax_Final prog and add it.
    */
    if( input_mode == "mode=pg221_add_item" )
    {
	// Get the Barcode of the currently selected stock item we need to add >>>>>>
	var ajax_pg221_item_bcode = document.esp_cpf.pg221_select_item.value;
	
	// Get the quantity of the item to add from the pg524_add_quantity field.
	var ajax_pg221_add_quantity = document.esp_cpf.pg221_add_quantity.value;

	// Get the Order no
	var ajax_pg221_order_no = document.esp_cpf.pg221_order_no.value;

		
	// alert('Pg221 Add item Mode [' + input_mode + ']' + 'Bcode [' + ajax_pg221_item_bcode + ']' + 'Qty [' + ajax_pg221_add_quantity + ']');
	// alert('Mode [' + input_mode + ']' + 'Qty [' + ajax_pg524_add_quantity + ']');
		
	// Add the Ajax Mode, Item Bcode and quantity to the ajax_input_text >>>>>>>>>>>>>
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg221_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&add_item_bcode=' + ajax_pg221_item_bcode + '&add_item_qty=' + ajax_pg221_add_quantity + '&ajax_mode=pg221_add_item' +'&order_no=' + ajax_pg221_order_no + '&crc=0';

    } // End if( input_mode == "mode=pg221_add_item" )



    /*
	Pg751 Find Customer Mode >>>>>>>>>>>
	    In this mode the user is looking for the customer to use with the order.
    */
    if( input_mode == "mode=pg751_find_cust" )
    {
    
	// Get the search text for the customer.
	var ajax_pg751_cust_search_text = document.esp_cpf.pg751_find_cust_text.value;

    
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg751_find_custm mode so it will return a select menu of customers.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg751_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=pg751_find_cust' + '&pg751_find_cust_text=' + ajax_pg751_cust_search_text + '&crc=0';
	
	// alert('Pg751 Find Cust Mode ');
    }



    /*
	Pg751 Purchase order Select Stock Item Mode >>>>>>>>>>>
	    In this mode we are simply letting the user seaerch for stock items and
	    returning a select menu back for each search they do.
	    They can then select the desired item from the list.
    */
    if( input_mode == "mode=pg751_select_item" )
    {
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg751_select_item mode so it will return a select menu of stock items.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg751_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=pg751_select_item' +'&crc=0';
	
	// alert('Pg751 Select Item Mode ');

    } // End if( input_mode == "mode=pg751_select_item" )


    /*
	Pg751 Add Stock Item to Purchase Order  Mode >>>>>>>>>>>
	    In this mode we want to ADD the selected stock item to the order on pg751.
	    Get the item barcode and quantity so we can call the do_ajax_Final prog and add it.
    */
    if( input_mode == "mode=pg751_add_item" )
    {
	// Get the Barcode of the currently selected stock item we need to add >>>>>>
	var ajax_pg751_item_bcode = document.esp_cpf.pg751_select_item.value;
	
	// Get the quantity of the item to add from the pg524_add_quantity field.
	var ajax_pg751_add_quantity = document.esp_cpf.pg751_add_quantity.value;

	// Get the Order no
	var ajax_pg751_order_no = document.esp_cpf.pg751_order_no.value;

		
	// alert('Pg751 Add item Mode [' + input_mode + ']' + 'Bcode [' + ajax_pg751_item_bcode + ']' + 'Qty [' + ajax_pg751_add_quantity + ']');
	// alert('Mode [' + input_mode + ']' + 'Qty [' + ajax_pg524_add_quantity + ']');
		
	// Add the Ajax Mode, Item Bcode and quantity to the ajax_input_text >>>>>>>>>>>>>
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg751_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&add_item_bcode=' + ajax_pg751_item_bcode + '&add_item_qty=' + ajax_pg751_add_quantity + '&ajax_mode=pg751_add_item' +'&order_no=' + ajax_pg751_order_no + '&crc=0';

    } // End if( input_mode == "mode=pg751_add_item" )



    /*
	Pg765 Purchase order Select Stock Item Mode >>>>>>>>>>>
	    In this mode we are simply letting the user seaerch for stock items and
	    returning a select menu back for each search they do.
	    They can then select the desired item from the list.
    */
    if( input_mode == "mode=pg765_select_item" )
    {
	// Setup the call to the do_ajax_Final.cgi Prog >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	//	This will use the pg765_select_item mode so it will return a select menu of stock items.	
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg765_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&ajax_mode=pg765_select_item' +'&crc=0';
	
	// alert('Pg765 Select Item Mode ');

    } // End if( input_mode == "mode=pg765_select_item" )


    /*
	Pg765 Add Stock Item to Purchase Order  Mode >>>>>>>>>>>
	    In this mode we want to ADD the selected stock item to the order on pg765.
	    Get the item barcode and quantity so we can call the do_ajax_Final prog and add it.
    */
    if( input_mode == "mode=pg765_add_item" )
    {
	// Get the Barcode of the currently selected stock item we need to add >>>>>>
	var ajax_pg765_item_bcode = document.esp_cpf.pg765_select_item.value;
	
	// Get the quantity of the item to add from the pg524_add_quantity field.
	var ajax_pg765_add_quantity = document.esp_cpf.pg765_add_quantity.value;

	// Get the Order no
	var ajax_pg765_order_no = document.esp_cpf.pg765_order_no.value;

		
	// alert('Pg765 Add item Mode [' + input_mode + ']' + 'Bcode [' + ajax_pg765_item_bcode + ']' + 'Qty [' + ajax_pg765_add_quantity + ']');
	// alert('Mode [' + input_mode + ']' + 'Qty [' + ajax_pg524_add_quantity + ']');
		
	// Add the Ajax Mode, Item Bcode and quantity to the ajax_input_text >>>>>>>>>>>>>
	//	This is so the do_ajax() func will know what mode we are in and thus knows what to do.
	// NOTE set the ajax_mode to pg765_select_item to that the do_ajax_Final prog knows what to do.
	//	ie we are selectig the item at this stage.
	ajax_input_text = ajax_input_text + '&add_item_bcode=' + ajax_pg765_item_bcode + '&add_item_qty=' + ajax_pg765_add_quantity + '&ajax_mode=pg765_add_item' +'&order_no=' + ajax_pg765_order_no + '&crc=0';

    } // End if( input_mode == "mode=pg765_add_item" )




    // build the URL of the server script we wish to call >>>>>>>>>>>>>
    //	ie this is the call to the do_ajax_Final.cgi prog that will handle the AJAX request processing.
    // var url = "myserverscript.php?surname=" + lastname;
    var url = ajax_server + '&ajax_input=' + ajax_input_text;

    // Tell the user we are searcing.
    // alert('Searching...');

    // ask our XMLHTTPRequest object to open a server connection
    myRequest.open("GET", url, true);

    // prepare a function responseAjax() to run when the response has arrived 
    //	NOTE this func will be called when the do_Ajax_Final.cgi prog has finished and provided its
    //		reply ie the html data to be displayed.
    myRequest.onreadystatechange = responseAjax;
    // myRequest.onreadystatechange = responseAjax(input_mode);

    // and finally send the request
    myRequest.send(null);

}  // End function callAjax(ajax_server,input_mode) 


/*
    This func is called when there is a reply from the ajax server.
    Get the response text from the server.
*/
function responseAjax() 
{

    // we are only interested in readyState of 4, i.e. "loaded"
    if(myRequest.readyState == 4) 
    {
	// if server HTTP response is "OK"
	if(myRequest.status == 200) 
	{
	    // The server reply text is in the myRequest.responseText var.
            // alert("The server said: " + myRequest.responseText);
	    
	    // Set the pg524_name form field value with the Ajax reply from the server.
	    // document.esp_cpf.pg524_name.value = myRequest.responseText

	    /*
		 Update the inner html of a <div> with the id ajax_result_div
		 First we  get a handle for the div element on pg 524 then we will set its
		 innerHTML value to that returned by the ajax server.
	    */
	    if ( myRequest.responseText.indexOf("pg221_select_item") > 0 )
	    {
		// get the result handle.
		var ajax_result_div_handle = document.getElementById("ajax_result_div");
	    
	        // Set the div's inner HTML to the value returned by the ajax server.
		ajax_result_div_handle.innerHTML = myRequest.responseText;
	//	alert('Pg221 Select Item');
	    }


	    /*
		 Update the inner html of a <div> with the id ajax_result_div
		 First we  get a handle for the div element on pg 524 then we will set its
		 innerHTML value to that returned by the ajax server.
	    */
	    if ( myRequest.responseText.indexOf("pg221_add_item") > 0 )
	    {
		// get the result handle.
		var ajax_result_div_handle = document.getElementById("ajax_result_div");
	    
	        // Set the div's inner HTML to the value returned by the ajax server.
		ajax_result_div_handle.innerHTML = myRequest.responseText;
		
		// alert('Pg221 Add Item');
	    }

	    /*
		 Update the inner html of a <div> with the id ajax_result_div
		 First we  get a handle for the div element on pg 524 then we will set its
		 innerHTML value to that returned by the ajax server.
	    */
	    if ( myRequest.responseText.indexOf("pg751_select_item") > 0 )
	    {
		// get the result handle.
		var ajax_result_div_handle = document.getElementById("ajax_result_div");
	    
	        // Set the div's inner HTML to the value returned by the ajax server.
		ajax_result_div_handle.innerHTML = myRequest.responseText;
		// alert('Pg751 Select Item');
	    }


	    /*
		 Update the inner html of a <div> with the id ajax_result_div
		 First we  get a handle for the div element on pg 524 then we will set its
		 innerHTML value to that returned by the ajax server.
	    */
	    if ( myRequest.responseText.indexOf("pg751_add_item") > 0 )
	    {
		// get the result handle.
		var ajax_result_div_handle = document.getElementById("ajax_result_div");
	    
	        // Set the div's inner HTML to the value returned by the ajax server.
		ajax_result_div_handle.innerHTML = myRequest.responseText;
		
		// alert('Pg751 Add Item');
	    }


	    /*
		 Update the inner html of a <div> with the id ajax_result_div
		 First we  get a handle for the div element on pg 524 then we will set its
		 innerHTML value to that returned by the ajax server.
	    */
	    if ( myRequest.responseText.indexOf("pg765_select_item") > 0 )
	    {
		// get the result handle.
		var ajax_result_div_handle = document.getElementById("ajax_result_div");
	    
	        // Set the div's inner HTML to the value returned by the ajax server.
		ajax_result_div_handle.innerHTML = myRequest.responseText;
		// alert('Pg765 Select Item');
	    }


	    /*
		 Update the inner html of a <div> with the id ajax_result_div
		 First we  get a handle for the div element on pg 524 then we will set its
		 innerHTML value to that returned by the ajax server.
	    */
	    if ( myRequest.responseText.indexOf("pg765_add_item") > 0 )
	    {
		// get the result handle.
		var ajax_result_div_handle = document.getElementById("ajax_result_div");
	    
	        // Set the div's inner HTML to the value returned by the ajax server.
		ajax_result_div_handle.innerHTML = myRequest.responseText;
		
		// alert('Pg765 Add Item');
	    }



	    /*
		5 March 2008 Generic Customer Select :
		
		    If we are on page 221 and the user has just done a search for a customer 
		    we need to display the cust select box on the page so they can choose the customer.
		    Thus check the server response and if it contains the pg221_seleect_cust
		    string this means we are on pg 221 and we need to display the 
		    cust select box.
	    */
	    if ( myRequest.responseText.indexOf("ajax_find_cust") > 0 )
	    {
		// Yes this is a cust search result from pg221.
		
		// Get the search text for the customer.
		var ajax_cust_search_text = document.esp_cpf.ajax_cust_search_text.value;

		// Get the handle for the div that will hold the search result.
		var ajax_find_cust_result_div_handle = document.getElementById("ajax_find_cust_result_div");

		// Set the div's inner HTML to the value returned by the ajax server.
	        ajax_find_cust_result_div_handle.innerHTML = myRequest.responseText;
	    
		// alert('Generic Ajax cust search [' + ajax_cust_search_text + ']');
	    }


	    
	    /*
		27 nov 2007 page 221 Customer Select :
		
		    If we are on page 221 and the user has just done a search for a customer 
		    we need to display the cust select box on the page so they can choose the customer.
		    Thus check the server response and if it contains the pg221_seleect_cust
		    string this means we are on pg 221 and we need to display the 
		    cust select box.
	    */
	    if ( myRequest.responseText.indexOf("pg221_select_cust") > 0 )
	    {
		// Yes this is a cust search result from pg221.
		
		// Get the search text for the customer.
		var ajax_pg221_cust_search_text = document.esp_cpf.pg221_find_cust_text.value;
		
		// Get the handle for the div that will hold the search result.
		var ajax_pg221_find_cust_result_div_handle = document.getElementById("ajax_pg221_find_cust_result_div");
	    
		// Set the div's inner HTML to the value returned by the ajax server.
	        ajax_pg221_find_cust_result_div_handle.innerHTML = myRequest.responseText;
	    
		// alert('Pg221 cust search [' + ajax_pg221_cust_search_text + ']');
	    }


	    /*
		24 Sep 2008 Generic Supplier Select :
		    The use has enetered some search text to look for a supplier 
		    so we need to get the reply from the do_Ajax_Final.cgi prog and 
		    send it back to the browser.
	    */
	    if ( myRequest.responseText.indexOf("ajax_find_supplier") > 0 )
	    {
		// Get the search text for the supplier.
		var ajax_supplier_search_text = document.esp_cpf.ajax_find_supplier_text.value;
	
		// Yes this is a cust search result from pg221.
		// Get the handle for the div that will hold the search result.
		var ajax_find_supplier_result_div_handle = document.getElementById("ajax_find_supplier_result_div");

		// Set the div's inner HTML to the value returned by the ajax server.
	        ajax_find_supplier_result_div_handle.innerHTML = myRequest.responseText;
		
		// alert('Generic Ajax Supplier search [' + ajax_supplier_search_text + ']');
	    }

	    // var pg524_item_select_handle = document.getElementById("pg524_item");
	    
	    // Set the div's inner HTML to the value returned by the ajax server.
	    // pg524_item_select_handle.options = myRequest.responseText;
	
	    	    
	    // document.esp_cpf.pg524_item.options = '<option selected >Jack</option>' + myRequest.responseText
	    // alert('Mode [' + input_mode + ']');
	    
        } else 
	{
            // issue an error message for any other HTTP response
            alert("An error has occurred: " + myRequest.statusText);
        }
    }
    
} // End function responseAjax() 


/*
    15 Nov 2007 > END Ajax Functions <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
*/



function getMousePos(evt)
{ // @gm
/*
    This func is called when ever the mouse is moved. Each time we come here we get the current
*/
// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

    /*
	Check the browser type and get the mouse pos.
    */
    if (IE) 
    { 
	// grab the x-y pos.s if browser is IE
	mouseX = event.clientX + document.body.scrollLeft
        mouseY = event.clientY + document.body.scrollTop
    } 
    else 
    {  
	// grab the x-y pos.s if browser is NS
	mouseX = evt.pageX;
	mouseY = evt.pageY;

    }  

    // catch possible negative values in NS4
    if ( mouseX < 0){ mouseX = 0}
    if ( mouseY < 0){ mouseY = 0}  

    // show the position values in the form named Show
    // in the text fields named MouseX and MouseY
    /*
	document.Show.MouseX.value = tempX
	document.Show.MouseY.value = tempY
    */
    return true;

}

    
/*
    10 June 2008 > Setup the drop down menus.
*/
function initAll() 
{
    var allLinks = document.getElementsByTagName("a");
	
    for (var i=0; i<allLinks.length; i++) 
    {
	if (allLinks[i].className.indexOf("menuLink") > -1) 
	{
	    /* allLinks[i].onclick = toggleMenu;  */
	    
	    /* Dont goto the main enu link if they click on it.  */
	    /* allLinks[i].onclick = function() {return false;} */
	    
	     allLinks[i].onmouseover = toggleMenu; 
	}
    }

} // End inintAll()
					
function toggleMenu() 
{
    /*
	Get the menu id from the link url ie we want the catno=6xxxx tag.
	ie the html link from esp contains the cat no we need. The menu id is catno=6xxx
    */
    var startMenu = this.href.lastIndexOf("catno=");
    var stopMenu = this.href.lastIndexOf("&");
    var thisMenuName = this.href.substring(startMenu,stopMenu);
    

    /*        alert("This menu  name [" + thisMenuName + "]" );

        thisMenuName = "catno=60000";		
    */
    /*
	Un hide the menu options so they can be seen.
    */
    document.getElementById(thisMenuName).style.display = "block";

    this.parentNode.className = thisMenuName;
    this.parentNode.onmouseout = toggleDivOff;
    this.parentNode.onmouseover = toggleDivOn;
    
} // End toggleMenu()
			
function toggleDivOn() {
    document.getElementById(this.className).style.display = "block";
}
    
function toggleDivOff() {
    document.getElementById(this.className).style.display = "none";
}											



/*
    When called this function will display the specified web page in a popup window.
*/
function DisplayPage(webaddr)
{
    /*  
	Display the specified web page in a new window.
    */
    newwin = window.open(webaddr,'popup','width=500,height=450,toolbar=false,scrollbars=yes');

}


function showPreview(evt) 
{
/*
    When called this function will show a preview of the specified links html.
    When the users mouse goes over a link this func is called.
*/
    
    getPreview(evt);

    // alert("Hi from Jack");
    return false;
}


// Hides the popup window.
function hidePreview() 
{
    document.getElementById("EspMainWin").style.visibility = "hidden";
}


function getPreview(evt) 
{
    if (evt) 
    {
        var url = evt.target;
    }
    else 
    {
        evt = window.event;
        var url = evt.srcElement;
    }
	
    xPos = evt.clientX;
    yPos = evt.clientY;
	
    if (window.XMLHttpRequest) 
    {
        xhr = new XMLHttpRequest();
    }
    else 
    {
        if (window.ActiveXObject) 
        {
	    try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); }
    	    catch (e) { }
	}
    }

    if (xhr) 
    {
        xhr.onreadystatechange = showContents;
	// xhr.onreadystatechange = displayPopUp("Hi from pop tart");
	xhr.open("GET", url, true);
        xhr.send(null);
    }
    else 
    {
        alert("Sorry, but I couldn't create an XMLHttpRequest");
    }
}



function showContents() 
{
    var prevWin = document.getElementById("EspMainWin");
	
    if (xhr.readyState == 4) 
    {
	// Get the html from the link to display in the window.
	//   NOTE this sets the text that will be displayed in the popup window.
	prevWin.innerHTML = (xhr.status == 200) ? xhr.responseText : "There was a problem with the request " + xhr.status;
	
	// 
	// prevWin.innerHTML = "Hi from Jack ";
	
	// Set the pos of the preview popup.
    	prevWin.style.top = parseInt(yPos)+2 + "px";
    	prevWin.style.left = parseInt(xPos)+2 + "px";
    	prevWin.style.visibility = "visible";
	
	// Close the pop up when the mouse leaves it.
    	prevWin.onmouseout = hidePreview;
    }
}




function displayPopUp(poptext)
{ // @dp
/*
    When called this function will display a popup window with the specified text.
    The position of the pop up will follow the mouse pointer.
*/
    var prevWin = document.getElementById("EspMainWin");

    // Get the current mouse position.
    /* Use + 10 so that the popup does NOT cover the link. */
    yPos = mouseY + 10;

//    yPos = mouseY;
    
    /* Display the pop up 20 pixels to the right of the mouse so it does not cover it up. */
    xPos = mouseX + 20;
        
    // Set the pop up text to display.	
    prevWin.innerHTML = poptext;

    // Set the popup windows position.
    prevWin.style.top = parseInt(yPos)+2 + "px";
    prevWin.style.left = parseInt(xPos)+2 + "px";

    // Display the popup.
    prevWin.style.visibility = "visible";
    prevWin.onmouseout = hidePreview;
    
} // End displayPopUp(poptext)


function hidePopUp()
{
    document.getElementById("EspMainWin").style.visibility = "hidden";
}




function displayTableRowPopUp(poptext)
{ // @dtrp
/*
    2 may 2007 > When called this function will display a popup window with the specified text
	on a tbale row. This is only for use with tables in esp.
    The position of the pop up will follow the mouse pointer.
*/
    var prevWin = document.getElementById("EspMainWin");

    // Get the current mouse position.
    /* Use + 10 so that the popup does NOT cover the link. */
    yPos = mouseY + 10;

//    yPos = mouseY;
    
    /* Display the pop up 20 pixels to the right of the mouse so it does not cover it up. */
    xPos = mouseX + 20;
        
    /* Check the Xpos 
	If its to far right move it left.
     */
    if ( xPos > 700 )
    {
	xPos = xPos - 450;
    }

    /* Check the Ypos 
	If its to low move it up.
     */
    if ( yPos > 200 )
    {
//	yPos = yPos - 250;
    }

    // Set the pop up text to display.	
    prevWin.innerHTML = poptext;

    // Set the popup windows position.
    prevWin.style.top = parseInt(yPos)+2 + "px";
    prevWin.style.left = parseInt(xPos)+2 + "px";
    // prevWin.style.height = parseInt(275)+2 + "px";
    // 30 jan 2009 set height to auto so window fits contents.
    prevWin.style.height = "auto";
    
    // 30 Sep 2009 > Use smaller font.
    prevWin.style.fontFamily = "Verdana";
    prevWin.style.fontSize = "small";


    // Display the popup.
    prevWin.style.visibility = "visible";
    prevWin.onmouseout = hidePreview;
    
} // End displayTableRowPopUp(poptext)



function hideTableRowPopUp()
{
    document.getElementById("EspMainWin").style.visibility = "hidden";
}




function displayImagePopUp(poptext)
{ // @image
/*
    14 July 2009 > When called this function will display a popup window with the specified 
	image in it.
    The position of the pop up will follow the mouse pointer.
*/
    var prevWin = document.getElementById("EspMainWin");

    // Get the current mouse position.
    /* Use + 10 so that the popup does NOT cover the link. */
    yPos = mouseY + 10;

//    yPos = mouseY;
    
    /* Display the pop up 20 pixels to the right of the mouse so it does not cover it up. */
    xPos = mouseX + 20;
        
    /* Check the Xpos 
	If its to far right move it left.
     */
    if ( xPos > 500 )
    {
	xPos = xPos - 450;
    }

    /* Check the Ypos 
	If its to low move it up.
     */
    if ( yPos > 200 )
    {
//	yPos = yPos - 250;
    }

    // Set the pop up text to display.	
    // prevWin.innerHTML = poptext;
    // 14 July 2009 Set the inner html to show the image.
    prevWin.innerHTML = '<span style=\"font-size:x-small;\"><b>Click to close.</b> To insert image into a document drag and drop it.</span><br><img src=\"../images1/' + poptext + '\" align=\"left\" border=\"0\" hspace=\"5\">';

    // alert('Inner html : [ ' + prevWin.innerHTML + ']');

    // Set the popup windows position.
    prevWin.style.top = parseInt(yPos) + "px";
    prevWin.style.left = parseInt(xPos) + "px";

    // prevWin.style.height = parseInt(275)+2 + "px";
    // 30 jan 2009 set height to auto so window fits contents.
    prevWin.style.height = "auto";
    prevWin.style.width = "auto";


    // Display the popup.
    prevWin.style.visibility = "visible";
    // prevWin.onmouseout = hidePreview;
    prevWin.onclick = hidePreview;
    
} // End displayImagePopUp(poptext)



function hideImagePopUp()
{
    document.getElementById("EspMainWin").style.visibility = "hidden";
}



function addBookmark(title, url) {
    if (window.sidebar) { // firefox
	 window.sidebar.addPanel(title, url,"");
     } else if( document.all ) { //MSIE
  window.external.AddFavorite( url, title);
      } else {
         alert("Sorry, your browser doesn't support this");
 }
}
									     



function displayPlotGraphPopUp(poptext)
{ // @dtrp
/*
    24 Oct 2007 > When called this function will display a popup window with the specified text
	on a plot graph. 
    The position of the pop up will follow the mouse pointer.
*/

    var prevWin = document.getElementById("EspMainWin");
    // Get the current mouse position.
    /* Use + 10 so that the popup does NOT cover the link. */
    yPos = mouseY + 10;
    
    /* Display the pop up 20 pixels to the right of the mouse so it does not cover it up. */
    xPos = mouseX + 20;

    /* Check the Xpos 
	If its to far right move it left.
     */
    if ( xPos > 600 )
    {
	xPos = xPos - 250;
    }


    // Set the pop up text to display.	
    prevWin.innerHTML = poptext;

    // Set the popup windows position.
    prevWin.style.top = parseInt(yPos)+2 + "px";
    prevWin.style.left = parseInt(xPos)+2 + "px";
    prevWin.style.width = "250px";
    prevWin.style.height = parseInt(200)+2 + "px";

    // Display the popup.
    prevWin.style.visibility = "visible";
    prevWin.onmouseout = hidePreview;
    
} // End displayPlotGraphPopUp(poptext)


function hidePlotGraphPopUp()
{
    document.getElementById("EspMainWin").style.visibility = "hidden";
}



function displayHelp(poptext)
{ // @dh
/*
    When called this function will display a popup window with the specified text.
    The position of the pop up will follow the mouse pointer.
    
    NOTE this is only ment for VERY short types of help.
*/
    var prevWin = document.getElementById("EspMainWin");

    // Get the current mouse position.
    xPos = mouseX;
    
    /* Use + 20 so that the popup does NOT cover the link. */
    yPos = mouseY + 10;
        
    /*
	Check that we are not betting to close to the right edge.
    */
    if (xPos > 600 )
    {
	xPos = xPos - 200;
    }

    // Setup the text for display.
    poptext = poptext + "X " + xPos + "Y " + yPos;
    
    // Set the pop up text to display.	
    prevWin.innerHTML = poptext;

    // Set the popup windows position.
    prevWin.style.top = parseInt(yPos)+2 + "px";
    prevWin.style.left = parseInt(xPos)+2 + "px";

    // Display the popup.
    prevWin.style.visibility = "visible";
//    prevWin.onmouseout = hidePreview;
//    prevWin.onmouseout = hidePopUp;
    
} // End displayHelp(poptext)


function hideHelp()
{
    document.getElementById("EspHelpWin").style.visibility = "hidden";
}


function displayHelpPage(webaddr)
{ // @dhp
/*
    When called this function will open a new browser window and display the 
    specified help page in it.
    NOTE that if there is already a window open it will be used instead of opening a new one.
*/
    
    openerURL=webaddr;
    
  //  alert('Help Page ' + openerURL);

    self.name = "original";
    var newWindow = window.open(openerURL,"Esp Help","status=no,location=no,menubar=no,toolbar=no,scrollbars=yes,height=800,width=900");
    document.write( newWindow.openwer.name);
    
//    opener.document.location = openerURL;
//    opener.focus();
    
} // End displayHelp(poptext)



function displayHelpFile(helpfile)
{ // @dh
/*
    When called this function will display a popup window with the specified text.
    The position of the pop up will follow the mouse pointer.
*/
    var prevWin = document.getElementById("EspHelpWin");

    xhr = new XMLHttpRequest();

    if (xhr) 
    {
        xhr.onreadystatechange = showContents;
	// xhr.open("GET",'$hub_ip_addr/includes1/pg48-help.html', true);
	xhr.open("GET",'www.google.com.sg', true);
        xhr.send(null);
    }
    else 
    {
        alert("Sorry, but I couldn't create an XMLHttpRequest");
    }
    
    
    // Get the current mouse position.
    xPos = mouseX;
    
    /* Use + 20 so that the popup does NOT cover the link. */
    yPos = mouseY + 10;
        
    /*
	Check that we are not betting to close to the right edge.
    */
    if (xPos > 600 )
    {
	xPos = xPos - 200;
    }
	
    helpfile = helpfile + "X " + xPos + "Y " + yPos;
    
    // Set the pop up text to display.	
    // prevWin.innerHTML = helpfile;
    prevWin.innerHTML = (xhr.status == 200) ? xhr.responseText : "There was a problem with the request " + xhr.status;

    // Set the popup windows position.
    prevWin.style.top = parseInt(yPos)+2 + "px";
    prevWin.style.left = parseInt(xPos)+2 + "px";

    // Display the popup.
    prevWin.style.visibility = "visible";
//    prevWin.onmouseout = hidePreview;
//    prevWin.onmouseout = hidePopUp;
    
} // End displayHelpFile(poptext)




function display_webpage_window(Width,Height,window_id,webaddr)
{ // @dhp
/*
    When called this function will open a new browser window and display the 
    specified website in it.
    NOTE that if there is already a window open it will be used instead of opening a new one.
    
    1 March 2009 Added window_id input so we can refer back to the later on.
	Also allows more than one window to be opened at a time if they have different ids.
*/
    
    // Get the URL to display.
    openerURL=webaddr;
    
    /* openerURL=openerURL + '&jack786=34'; */
    
    /* alert('Display Webpage Window ' + openerURL + 'Width [' + Width + ']  Height ' + Height + ']'); */

/*
    Dont display the menu bars etc keep it a very simple page.
*/
myRef=window.open(openerURL,window_id,
    'width=' + Width + 
    ',height=' + Height + 
    ',menubar=0' +
    ',toolbar=0' +
    ',status=0' +
    ',scrollbars=1' +
    ',resizable=1');
    
//    opener.document.location = openerURL;
//    opener.focus();
    
} // End display_Webpage_Window(Xpos,Ypos,webaddr)








// This is the func that does the jump to the link when the user releases the mouse button.
function jumptolink(what)
{
var selectedopt=what.options[what.selectedIndex]
    window.location=selectedopt.value
}


  function popjack(who)
  {
    alert("hfhgkjfdhg" + who);
  }

  function popup( obj, L, T) 
  {
    with (obj.style) 
    {
        display = "block";
        position = "absolute";
        left = L;
        top = T;
    }
  }



/*
    Esp Pop Up Text >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
*/
    popUpText[1]="This is the newly set popup text";
	
    // Logoff
    popUpText['logoff']="<h3>Log Off</h3><br>Click here to end your Esp session and log off.";


    // Text Format Tags Help for pg 224 etc.
    popUpText['pg224textformat']="<b>Text Format Tags</b><br> \
        Note XX = font size 10 to 99. 55 is normal.<br> \
	<b>!sXX</b>  Set plain font size.<br> \
	<b>!bXX</b>  Bold font XX = size.<br> \
	<b>!iXX</b>  Italic font XX = size.<br> \
	<b>!biXX</b>  Bold italic font XX = size.<br> \
	<b>!pb</b>  Start new page<br> \
	<b>!total</b> Item list total.<br> \
	<b>edoc=XXXXXXX</b> Include the text from espdoc.<br> \
	These apply to whole lines.<br> \
	<b>Note</b> Leave a space after each tag ie !b22 Jack NOT !b22Jack.<br><br>";
	
    popUpText['ordertemplates'] = "Order templates help";




