//------------------------------------------------------------------------------
// GENERAL PURPOSE FUNCTIONS


//---------------------------------



function _log(txt) {if (window.console) console.log(txt);}

function _StrVal(str)
{
  var v=parseInt(str);
  if (isNaN(v)) v=0;
  return v;
}

function _SoftBreaks(word,size)
{
  word.replace(".","&shy;.");  
  word.replace("_","&shy;_");
  
  word=word.split("&shy");
  newWord='';
  for (i in word)
  {
    oldPart=word[i];
    newPart='';
    while (oldPart.length>size) 
    {
      newPart+=oldPart.substr(0,size)+'&shy;'; 
      oldPart =oldPart.substr(size);
    } 
    newPart+=oldPart;
    
    newWord+=newPart+'&shy;';
  }
  return newWord;
}

////////////////////////////// Colour Functions /////////////////////////////////////
//Convert a hex value to its decimal value - the inputed hex must be in the
// format of a hex triplet - the kind we use for HTML colours. The function
// will return an array with three values.
function hex2num(hex) {
  if(hex.charAt(0) == "#") { 
    hex = hex.slice(1);
  }
  hex = hex.toUpperCase();
  var hex_alphabets = "0123456789ABCDEF";
  var value = new Array(3);
  var k = 0;
  var int1,int2;
  for(var i=0;i<6;i+=2) {
    int1 = hex_alphabets.indexOf(hex.charAt(i));
    int2 = hex_alphabets.indexOf(hex.charAt(i+1));
    value[k] = (int1 * 16) + int2;
    k++;
  }
  return(value);
}
//Give a array with three values as the argument and the function will return
// the corresponding hex triplet.
function num2hex(triplet) {
  var hex_alphabets = "0123456789ABCDEF";
  var hex = "#";
  var int1,int2;
  for(var i=0;i<3;i++) {
    int1 = triplet[i] / 16;
    int2 = triplet[i] % 16;

    hex += hex_alphabets.charAt(int1) + hex_alphabets.charAt(int2);
  }
  return(hex);
}

function _OuterW(Obj) {return Obj.offsetWidth+_BorderLW(Obj)+_BorderRW(Obj);}
function _OuterH(Obj) {return Obj.offsetHeight+_BorderTW(Obj)+_BorderBW(Obj);}


//------------------------------------------------------------------------------
// Stylesheets and inline styles
function getPropertyFromStyle(StyleObj,PropertyName)
{
  return StyleObj[PropertyName];  
}
function getStyleProperty(StyleName,PropertyName)
{
  StyleObj=_getStyle(StyleName);
  if (StyleObj)
    return getPropertyFromStyle(StyleObj,PropertyName); 
  else
    return 'Style Not Found'; 
}

//------------------------------------------------------------------------------
// Dialogs

var _dialog=
{

  currDialog: null,
  modalBox:null,
	client:null,      //Client field or object that opens the dialog
  
  open: function(Name,isModal,oClient)
  {
    this.currDialog=document.createElement('div');
    this.currDialog.setAttribute('id',Name);
    this.currDialog.style.position='absolute';
		this.client=oClient;
		
    var htm='<DIV ID="title_'+Name+'" STYLE="position:absolute;top:0;left:0;width:100%;" CLASS=_DialogTitle>Pick File</DIV>';
    htm   +='<DIV ID="dialog_'+Name+'" STYLE="position:absolute;top:25px;left:0;width:100%;" CLASS=_Control></DIV>';
    htm   +='<DIV ID="button_'+Name+'" STYLE="position:absolute;top:300px;left:0;width:100%;text-align:right;" CLASS=_DialogButtons>';
    htm   +='  <INPUT TYPE=Hidden ID="Response_'+Name+'">';
    htm   +='  <BUTTON onClick=_dialog.submit("'+Name+'")>Submit</BUTTON>';
    htm   +='</DIV>';
    
    
    this.currDialog.innerHTML=htm;
    _SetStyleClass(this.currDialog,'_DialogContainer');
    if (isModal)
    {
      this.modalBox=document.createElement('div'); 
			this.modalBox.style.top='0px';
			this.modalBox.style.left='0px';
      this.modalBox.style.width='100%'; 
      this.modalBox.style.height='100%';
      _SetStyleClass(this.modalBox,'_DialogModalBox');
      document.body.appendChild(this.modalBox);
      this.modalBox.appendChild(this.currDialog);
    }
    else
      document.body.appendChild(this.currDialog);
    
    var TitleD=document.getElementById('title_'+Name);
    this.currDialog.update=this.dragMove;
    _mouse.makeDraggable(this.currDialog,TitleD);  
  },
  
  dragMove: function()
  {
    //var PTop=this.parentNode.offsetTop-this.offsetTop;
    //this.parentNode.style.top=(this.offsetTop+mouseOffset.y)+'px';
    //this.parentNode.style.left=(this.offsetLeft+mouseOffset.x)+'px';
    //this.style.top=0;
    //this.style.left=0;
  },
  
  reSize: function(Name,w,h)
  {
    //this.currDialog=document.getElementById(Name);
    this.currDialog.style.width=w;
    this.currDialog.style.height=h;
    
    var DialogW=this.currDialog.clientWidth;
    var DialogH=this.currDialog.clientHeight;
    
    ButtonBar=document.getElementById('button_'+Name);
    barH=ButtonBar.offsetHeight;
    ButtonBar.style.top=(DialogH-barH)+'px';
    ButtonBar.style.width=(DialogW-_BorderLW(ButtonBar)-_BorderRW(ButtonBar))+'px';
    
    Title=document.getElementById('title_'+Name);
    titleH=Title.offsetHeight;
    Title.style.width=(DialogW-_BorderLW(Title)-_BorderRW(Title))+'px';
    
    ContentD=document.getElementById('dialog_'+Name);
    ContentD.style.top    =titleH+'px';
    ContentD.style.height =DialogH-titleH-barH+'px';
    ContentD.style.width  =(DialogW-_BorderLW(ContentD)-_BorderRW(ContentD))+'px';
  },
	
	setTitle: function(sName,sTitle)
	{
	  oTitle=document.getElementById('title_'+sName);
		oTitle.innerHTML=sTitle;	
	},
  
  position: function(Name,x,y)
  {
    //this.currDialog=document.getElementById(Name);
    this.currDialog.style.left=x;
    this.currDialog.style.top=y;  
    
  },
  
  submit: function()
  {
    
    _dialog.client.onReturn(_dialog.currDialog.value);
    
    _dialog.close();
      
  },
	
	close: function()
	{
    _dialog.currDialog.parentNode.removeChild(_dialog.currDialog);
    if (_dialog.modalBox) _dialog.modalBox.parentNode.removeChild(_dialog.modalBox);    	
	}

}

function _decodeHTMLEntities(sHtml)
{

	if (sHtml) {
		//sHtml=decodeURI(sHtml);
		sHtml = sHtml.replace(/&amp;/g, '&');
  	sHtml = sHtml.replace(/&lt;/g, '<');
  	sHtml = sHtml.replace(/&gt;/g, '>');
  	sHtml = sHtml.replace(/&quot;/g, '"');
  	sHtml = sHtml.replace(/&#039;/g, "'");
  	sHtml = sHtml.replace(/&amp;/g, '&');
  }
	return sHtml;
}
function _encodeHTMLEntities(sHtml)
{
 
    //sHtml=decodeURI(sHtml);
    //sHtml = sHtml.replace(/&/g, '&amp;');
    sHtml = sHtml.replace(/\</g, '&lt;');
    sHtml = sHtml.replace(/\>/g, '&gt;');
    sHtml = sHtml.replace(/"/g, '&quot;');
    sHtml = sHtml.replace(/'/g, "&#039;");
    //sHtml = sHtml.replace(/&amp;/g, '&');

  return sHtml;	
}


function _goURL(strURL)
{
	location.href=strURL;
}

function _parseString(sString,oData)
{

  var arrParts=sString.split('%%');
  var odd=0;
  var res='';
  
  for (var i in arrParts)
  {
    if (odd==0)
    {
      res=res+arrParts[i];
    }
    else
    {
      var sCmd=arrParts[i].substr(0,3);
			var aParams=arrParts[i].substr(4);
			aParams=aParams.split('::');
			switch(sCmd)
			{
				case 'ARR':
				  res+=oData[aParams[0]];
					break;
				case 'VAL':
				  res+=oData[aParams[0]];
					break;
				case 'DOM':
				  oDom=document.getElementById(aParams[0]); 
					res+=oDom(aParams[1]);
					break;
			}
    }
    odd=1-odd;
  }
  return res;
}

function _moveSelected(fromObject,toObject) {
    for (var i=0, l=fromObject.options.length;i<l;i++) {
        if (fromObject.options[i].selected)
            _addOption(toObject,fromObject.options[i].text,fromObject.options[i].value);
    }
    for (var i=fromObject.options.length-1;i>-1;i--) {
        if (fromObject.options[i].selected)
            _deleteOption(fromObject,i);
    }
}
function _deleteOption(object,index) {
    object.options[index] = null;
}

function _addOption(object,text,value) {
    var defaultSelected = true;
    var selected = true;
    var optionName = new Option(text, value, defaultSelected, selected)
    object.options[object.length] = optionName;
}





