//
//  sheef javascript
//

// Dialling functions
function DialAfterConfirmationID( url, txt )
{
	//var descr_id = 'd'+id;
	//var txt = 'Dial ' + num + '?' ;
  DeleteAfterConfirmation( url, txt );
}
function DialAfterConfirmation( url, txt )
{
  if ( DialConfirmation(txt) )
  {
    var Adresse = encodeURI( url ) ;
    this.location = Adresse ;
    }
}
function DialConfirmation( txt)
{
	return confirm(txt) ;
}


// Display name data in window
var winNameDisplay, winNumb ;

function submit_printsettingstbook(goback, tab)
{
  if (goback)
  {
    document.editform.goback.value = 'T' ;
    document.editform.tab.value = tab ;
  }
	else
	{
		showhelp = true;
		if ( document.editform.output != null )
	  	showhelp = document.editform.output[0].checked;

		if (showhelp)
		{
			if (document.editform.simplex.checked)
				help_show('printpdf');
			if (document.editform.duplex.checked)
				help_show('printpdf_d');
		}
	}

  document.editform.submit();
}
function submit_mainedit()
{
  document.editform.submit();
}
//function force2top()
//{
 //if(top!=self)
 // top.location=self.location;
//}
function mainClick( id )
{
  top.location.href = 'tb_display.php?id=' + id;
}
function clipboard_copy()
{
     var copyText = document.editform.cliptext.value;
     if (window.clipboardData)
     {  // IE send-to-clipboard method.
        window.clipboardData.setData('Text', copyText);
     }
     else if (window.netscape)
     {
          // You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
          netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

          // Store support string in an object.
          var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
          if (!str) return false;
          str.data=copyText;

          // Make transferable.
          var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
          if (!trans) return false;

          // Specify what datatypes we want to obtain, which is text in this case.
          trans.addDataFlavor("text/unicode");
          trans.setTransferData("text/unicode",str,copyText.length*2);

          var clipid=Components.interfaces.nsIClipboard;
          var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
          if (!clip) return false;

          clip.setData(trans,null,clipid.kGlobalClipboard);
     }
}
function display_email( s, t )
{
  top.std.location.href = 'mailto?amurray@murrayconsult.com&subject=' + s + '&body=' + t ;
}
function mainDblClick( id )
{                      1
  alert('mainDblClick') ;
}
function namedisplay(id)
{
  alert('namedisplay');
}
function mainkeyhandler(e)
{
  var nKey = e.keyCode - 0 ;
  var sKey = String.fromCharCode(e.keyCode) ;
  if ( ((nKey >= 65) && (nKey <= 122)) ) // A - z
  {
    top.location.href = "tb_main.php?mode=show&search=" + sKey ;
  }
  else
  {
  switch(nKey)
    {
      case 45: // Insert key
        top.location.href = 'tb_main.php?mode=edit&add=T';
        break;
      case 38: // Cursor UP
        top.location.href = 'tb_main.php?mode=browseup';
        break;
      case 40: // Cursor DN
        top.location.href = 'tb_main.php?mode=browsedn';
        break;
      case 39: // Cursor Right
        top.location.href = 'tb_main.php?mode=show&search=searchinc';
        break;
      case 37: // Cursor Left
        top.location.href = 'tb_main.php?mode=show&search=searchdec';
        break;
      case 49: // 1
        top.location.href = 'tb_main.php?mode=show&cols=' + sKey ;
        break;
      case 50: // 2
        top.location.href = 'tb_main.php?mode=show&cols=' + sKey ;
        break;
      case 51: // 3
        top.location.href = 'tb_main.php?mode=show&cols=' + sKey ;
        break;
      case 52: // 4
        top.location.href = 'tb_main.php?mode=show&cols=' + sKey ;
        break;

      default: // all other keys
        break;
    }
   }
}
function editkeyhandler(e)
{
  var nKey = e.keyCode - 0 ;
  switch(nKey)
    {
      case 27: // Escape key
        goto_main('M');
        break;
      case 13: // Enter key
        break;
      default: // all other keys
        break;
  }
}
function fld_keyhandler(e)
{
  var nKey = e.keyCode - 0 ;
  switch(nKey)
    {
      case 27: // Escape key
        goto_main();
        break;
      case 13: // Enter key
        document.editform.submit();
        break;
      default: // all other keys
        fieldchanged();
        break;
  }
}
function txt_keyhandler(e)
{
  var nKey = e.keyCode - 0 ;
  switch(nKey)
  {
      case 27: // Escape key
        goto_main();
        break;
      default: // all other keys
        fieldchanged();
        break;
  }
}
function fieldchanged()
{
  if ( document.getElementById('btn_submit') )
    document.getElementById('btn_submit').className = 'edit_changed'   ;
  return true;
}
function fieldchanged_msg(nam)
{
  document.getElementById(nam).value = 'T' ;
	fieldchanged();
	//alert('nam=' + nam);
  return true;
}
/*
function numb_display(txt, title)
{
  var h         = 50; // screen.height - 50;
  var w         = 250; // Math.round(screen.width / 3) ;
  var l         = 100; // screen.width - w;
  var t         = 100; // 0 ;
  var page      = '' ;
  var features  = '' ;

  features = 'width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',status=no,resizable=no,scrollbars=no,toolbar=no' ;
  page = 'tb_main_numb_display.php?text=' + txt + '&title=' + title ;

	winNumb = window.open(page, 'sheef', features) ;
	winNumb.setTimeout("self.close()", 5000);

}
function numb_clear()
{
		winNumb.close();
}
*/
function goto_main( sFirstLetter )
{
  top.location.href = "tb_main.php?mode=show&search=" + sFirstLetter ;
}
function goto_id( id )
{
  this.location = 'tb_main.php?mode=edit&add=F&id=' + id  ;
}
function help_show(code)
{
  var h         = screen.height - 50;
  var w         = Math.round(screen.width / 3) ;
  var l         = screen.width - w;
  var t         = 0 ;
  var page      = '' ;
  var features  = '' ;

  features = 'width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',status=no,resizable=yes,scrollbars=yes,toolbar=yes' ;
  //page = 'http://' + window.location.hostname + '/tb_help.php?code=' + code ;
  page = 'tb_help.php?code=' + code ;

	winHelp = window.open(page, 'sheef', features) ;
  winHelp.focus();
}

function help_onload()
{
  var h         = screen.height - 50;
  var w         = Math.round(screen.width / 3) ;
  var l         = screen.width - w;
  var t         = 0 ;

	window.moveTo(l, t);
	window.resizeTo(w, h)
	window.focus();
}

function output_onload()
{
	var fudge_h = 10;
	var fudge_v = 20;
	var h = screen.height - fudge_v;

  window.moveTo(0, 0);
  window.resizeTo(Math.round(screen.width/3*2) - fudge_h, h );
  window.focus();
}

function help_showimg(imgname, w, h)
{
	w += 10;
	h += 10;
  var l         = (screen.width/2)-(w/2);
  var t         = (screen.height/2)-(h/2) ;
  features = 'width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',status=no,resizable=no,menubar=no,scrollbars=no,toolbar=no' ;
  page = 'tb_help_img.php?imgname=' + imgname ;
	winHelpImg = window.open('', '', features) ;
	winHelpImg.location = page;
  winHelpImg.focus();
}

function DeleteAfterConfirmationID( url, id, deltxt )
{
	var descr_id = 'd'+id;
	var txt = deltxt + "\n" + document.getElementById(descr_id).value ;
  DeleteAfterConfirmation( url, txt );
}
function DeleteAfterConfirmation( url, txt )
{
  if ( DeleteConfirmation(txt) )
  {
    var Adresse = encodeURI( url ) ;
    this.location = Adresse ;
    }
}
function DeleteConfirmation( txt)
{
  return confirm(txt) ;
}

function setfocustofield( form_name, element_name )
{

  //alert('form_name=' + form_name + ' element_name=' + element_name);
  document.forms[form_name].elements[element_name].focus();
}

// Used in tb_main editform
function SetLastDayOfMonth()
{
  var nMonth = document.editform.date_m.value ;
  var nYear  = document.editform.date_y.value ;
  var nLen, nDay  = 0 ;
  //alert('nMonth/nYear=' + nMonth + '/' + nYear);
  nLen = GetLastDayOfMonth( nMonth, nYear) ;
  document.editform.date_d.options.length = nLen ;
  for ( nDay = 28 ; nDay < nLen ; nDay++ )
    {
      document.editform.date_d.options[nDay].text = (nDay + 1);
      document.editform.date_d.options[nDay].value = (nDay + 1);
    }
}

function GetLastDayOfMonth( nMonth, nYear )
{
  var lastDOM = new Date();
  lastDOM.setMonth( nMonth - 1) ;
  lastDOM.setFullYear( nYear)
  lastDOM.setTime(lastDOM.getTime() + ((32 - lastDOM.getDate()) * 86400000) );
  lastDOM.setTime(lastDOM.getTime() - (lastDOM.getDate() * 86400000) );
  return lastDOM.getDate();
}

function pref_colour_select_changed(fnt)
{
	with ( document.editform.elements['fontcolour'+fnt] )
	{
		bgCol = options[selectedIndex].text ;
		if ( selectedIndex > 8 )
			fgCol = 'black';
		else
			fgCol = 'white';

		// Now set the select itself (for Firefox mainly)
		style.backgroundColor = bgCol;
		style.color = fgCol;
	}
	// Mark form as changed
	fieldchanged();
}

//
// Number display in header
//
function numbDisplay_write(id1, id2)
{
	if (id1 == '0')
		numbDisplay_clear();
	else
	{
		document.getElementById('numbDisplay').innerHTML =
		document.getElementById(id1).value +
		'&nbsp;&nbsp;&nbsp;<span style="font-weight:bold;">' +
		document.getElementById(id2).value +
		'</span>';
	}
	//document.getElementById('numbDisplay').innerHTML = document.getElementById(id).innerHTML ;
}
function numbDisplay_clear()
{
	document.getElementById('numbDisplay').innerHTML = '';
}


//
// Main standard body load function
//
function main_onload()
{
	//document.getElementById('main_screenres').innerHTML = screen.width + ' x ' + screen.height ;
}


//
// Calendar selection options
//
/*
function calEnforce()
{
	minWeeks    = new Array(   4,    4,    2,    4,    8,     4);    // Minimum weeks for specific calendar types
	manBooksize = new Array(null, null, null, null, null,     5 );   // Mandatory booksize for certain calendar types
	with (document.editform)
	{
		ind = caltype.selectedIndex ;
		weeks.value = Math.max( minWeeks[ind], weeks.value ) ;
		bs = manBooksize[ind];
		if(bs) booksize.value = bs ;
	}
}
*/


