/**
* Copyright (c) 2006 Mashup Technologies LLC. All Rights Reserved.
* P.O. Box 397, Bellwood, PA 16617 
* http://www.mapbuilder.biz
*
* This code (including but not limited to the actual source code, documentation) 
* is not freeware and is intended for the use under special agreement between parties 
* according to the license agreement which is available at 
* http://mapbuilder.net/Integrator/License/
* 
* THIS SOFTWARE PRODUCT IS PROVIDED "AS IS" AND LICENSOR MAKE NO WARRANTY AS TO ITS USE, PERFORMANCE, 
* OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* ************************** UTIL ************************** */
Util = Class.create();
Util.Timer =  Class.create();

Util.Timer.prototype = {
	initialize: function() {
		this.date = new Date();
		this.milliseconds1 = this.date.getTime(); 
	},

	Tick: function(Iterations, Message) {
		if (typeof Iterations == "undefined") Iterations = 1;
		if (typeof Message == "undefined") Message = '';

		var date2 = new Date();
		var milliseconds2 = date2.getTime();

		var difference = (milliseconds2 - this.milliseconds1) / Iterations; 
		return Message + difference + " ms";
	}
}

// Format number to be as 1,000.00
Util.formatNumber = function(number)
{
  var dotIdx = -1;

  if(number!='')
  {					
    number=number.replace(/\$|\,/g,"");
    if(isNaN(number))
    {						
      number='';
    }
    else 
    {
      if(number.length>9)
        number=number.substring(0,9);	
           			
      // Remove cents
      dotIdx = number.indexOf('.');			
      if(dotIdx != -1)
        number = number.substring(0,dotIdx);	

      for (var i = 0; i < Math.floor((number.length-(1+i))/3); i++)
        number = number.substring(0,number.length-(4*i+3))+','+number.substring(number.length-(4*i+3));	
    }					
  }

  return number;					
}
	
Util.trunkStr = function(value, length)
{
   return (value.length <= length) ? value : (value.substr(0, length-3) + "...");
}
// Convert to int
Util.toInt = function(value)
{
  if(isNaN(value))
  {						
    return 0;
  }
  else
  {
    return parseInt(value);
  }
}
// Convert to float
Util.toFloat = function(value)
{
  if(isNaN(value))
  {						
    return 0;
  }
  else
  {
    return parseFloat(value);
  }
}

// Get radio button value
Util.getRadioValue = function(radiobtn)
{
  for (var i=0; i < radiobtn.length; i++)
  {
    if (radiobtn[i].checked)
    {
      return radiobtn[i].value;
    }
  }
  return 0;
}

/////////////////////////////////////////////////////////////////////////
// JSLoader class
/////////////////////////////////////////////////////////////////////////

/**
* @class
* Javascript loader responsible for external JavaScript file loading
* @constructor
* @param	{sting}	xmlDoc	fullUrl
*/
Util.JSLoader = function (fullUrl) {
  // request path
  this.fullUrl = fullUrl; 
  // Keep IE from caching requests

  // Do we have ? in the URL
  if (this.fullUrl.search(/\?/) == -1) 
    this.noCacheIE = '?noCacheIE=' + (new Date()).getTime();
  else
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();

  // Get the DOM location to put the script tag
  this.headLoc = document.getElementsByTagName("head").item(0);
  // Generate a unique script tag id
  this.scriptId = 'JSLoaderId' + Util.JSLoader.scriptCounter++;

  // buildScriptTag method
  this.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
  }

  // removeScriptTag method
  this.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
  }

  // addScriptTag method
  this.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
  }
}

Util.staticLoadScript = function(url)
{
  document.write('<script src="', url, '" type="text/JavaScript"><\/script>');
}
// Static script ID counter
Util.JSLoader.scriptCounter = 1;
