//initialize the cpaint functions
var cp = new cpaint();
cp.set_response_type('text');
cp.set_debug(false);

//global variable to hold preload the updated barcode image
var imgBarcode;

//*********************************************************
//Function: BuildBarcode
//Author: Jamie Dougal
//Date: December 12, 2005
//Description:  calls server-side function to obtain updated
//              barcode image
//*********************************************************
function BuildBarcode()
{
  //show the wait icon
  document.getElementById("waitIcon").style.display = "inline";
  //clear the error message
  document.getElementById("errorMsg").innerHTML = "";  
  
  //get the barcode settings
  var barcode = document.getElementById("txtBarcode").value;
  var symbology = document.getElementById("selSymbology").value;
  var fileFormat = document.getElementById("selFileFormat").value;

  if (barcode == "")
  {
    document.getElementById("errorMsg").innerHTML = "Please specify a value to barcode";
    //hide the wait icon
    document.getElementById("waitIcon").style.display = "none";
    return false;
  }  
  
  //make the ajax call
  cp.call('buildbarcode.asp', 'BuildBarcode', BuildBarcodeCallback, barcode, symbology, fileFormat);  
  
  //return false to cancel the submit
  return false;
}
  
//*********************************************************
//Function: BuildBarcodeCallback
//Author: Jamie Dougal
//Date: December 12, 2005
//Description:  callback function containing new barcode image information
//*********************************************************
function BuildBarcodeCallback(result)
{
  //check for an error message
  if(Left(result, 6)=="Error:")
  {
    document.getElementById("errorMsg").innerHTML = result;
    //hide the wait icon
    document.getElementById("waitIcon").style.display = "none";
    return false;
  }    
  else if (result == "")
  {
    document.getElementById("errorMsg").innerHTML = "Error creating barcode.<br>A common problem is that the specified value can't be encoded with the selected format.";
    //document.getElementById("errorMsg").innerHTML = "1" + result;
    //hide the wait icon
    document.getElementById("waitIcon").style.display = "none";
    return false;
  }    
  else
  {
    //create the new image
	  imgBarcode = new Image();
	  //set the id
	  imgBarcode.id = "barcode";
	  //set the width property
	  imgBarcode.width = "200";
	  //set the onload function
	  imgBarcode.onload = ReplaceGraph;
	
	  //set the src to start the load process
	  imgBarcode.src = result;
	}
}

//*********************************************************
//Function: ReplaceGraph
//Author: Jamie Dougal
//Date: December 12, 2005
//Description:  once the image has been loaded, let the browser set the image
//*********************************************************
function ReplaceGraph()
{
  //get the barcode image holder
  var barcodeHolder = document.getElementById("barcodeHolder");
	//clear the inner text
	barcodeHolder.innerHTML = "";
	
	//cancel the onload function
	imgBarcode.onload=null;
	
	//add the new image
	barcodeHolder.appendChild(imgBarcode);
  //set the instruction
  document.getElementById("instructionHolder").innerHTML="<br>This is a preview image. Right click the image to save or copy a full size version.";
	//clear the image holder
	imgBarcode = null;
	//clear the status bar
	window.status = "";
	
	//clear the barcode window
	document.getElementById("txtBarcode").value = "";
	document.getElementById("txtBarcode").focus();
  //hide the wait icon
  document.getElementById("waitIcon").style.display = "none";
}
  
  
/*Left(string, length): Returns a specified number of characters from the
                      left side of a string
========================================================================*/
function Left(str, n)
/***
        IN: str - the string we are LEFTing
            n - the number of characters we want to return

        RETVAL: n characters from the left side of the string
***/
{
        if (n <= 0)     // Invalid bound, return blank string
                return "";
        else if (n > String(str).length)   // Invalid bound, return
                return str;                // entire string
        else // Valid bound, return appropriate substring
                return String(str).substring(0,n);
}