//////////////////////////////////////////////////////////////////////////////
//
// Project: AdEngine 2.0
// File: general.js
// Author: Frank van Wensveen (frank@silverwolfmedia.com). Based on earlier
// work by Tom Khoury (twaks@yahoo.com).
// Purpose: General Javascript functions.
//
// --------------------------------------------------------------------------
//
// Revision history
// ----------------
// 2007-02-10: 	Initial version
// 2007-02-21:	Collated several general scripts into this one
//
// --------------------------------------------------------------------------
//
// LEGAL NOTICE
// ------------
// THE CONTENTS OF THIS FILE ARE PROPRIETARY, AND THE EXCLUSIVE PROPERTY OF
// SILVER WOLF MEDIA. UNAUTHORIZED COPYING, DISTRIBUTION, DISLOSURE TO THIRD
// PARTIES, MODIFICATION OR OTHER USE ARE EXPRESSLY/ PROHIBITED.
// (c) 2005 Silver Wolf Media - www.silverwolfmedia.com - All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////


// ---------------------------------------------------------------------------
// Onload and onunload init/exit handlers:
// ---------------------------------------------------------------------------


// ---------------------------------------------------------------------------
// Init all functions below (by calling init() as an onLoad in the <body> tag)
function init () {
  placeFocus ();			// Place focus on 1st form field
  if (document.getElementById('readroot')) {	// Page has a DOM template
    document.getElementById('moreFields').onclick = moreFields;
  scroll (0,0);			// Position window at top of page
  }
}


// ---------------------------------------------------------------------------
// Exit code to be called upon body unload
function outit () {
  tidy ();
}


// ---------------------------------------------------------------------------
// Functions follow:
// ---------------------------------------------------------------------------


// ---------------------------------------------------------------------------
// Place the focus (i.e. cursor) on the first field of any form on any page:
function placeFocus () {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i = 0; i < field.length; i++) {
      if ((field.elements[i].type == "text") ||
         (field.elements[i].type == "textarea") ||
         (field.elements[i].type.toString().charAt(0) == "s")) {
        document.forms[0].elements[i].focus();
        break;
      }
    }
  }
}


// ---------------------------------------------------------------------------
// A simple DOM script to dynamically add fields to, and remove them from,
// HTML forms.

var DOMfieldcounter = 0;

function moreFields() {
  if (DOMfieldcounter < 50) {
    var newFields = document.getElementById('readroot').cloneNode(true);
    newFields.id = '';
    newFields.style.display = 'block';
    var newField = newFields.childNodes;
    for (var i=0; i<newField.length; i++) {
      var theName = newField[i].name
      if (theName)
        newField[i].name = theName.replace (/#/, DOMfieldcounter);
    }
    var insertHere = document.getElementById('writeroot');
    insertHere.parentNode.insertBefore(newFields,insertHere);
    DOMfieldcounter++;
  }
}


// ---------------------------------------------------------------------------
// A simple popup to display images in a separate, correctly sized window.

var newwindow = '';

function imgPopUp(url,iWidth,iHeight) {

  iW = iWidth + 30;	// Add margin
  iH = iHeight + 30;
  if (newwindow.location && !newwindow.closed) {
    newwindow.location.href = url;
    newwindow.focus(); 
  } else {
    newwindow=window.open(url,'htmlname','width='+iW+',height='+iH+',menubar=no,scrollbars=no,status=no,resizable=1,screenX=200,screenY=200,top=200,left=200');
  }
}

// Clean up on unload:
function tidy () {
  if (newwindow.location && !newwindow.closed) {
     newwindow.close(); 
  }
}


// ---------------------------------------------------------------------------
// Insert BBcode into the adminAd form


// Variables for controling the opening and closing tags:
var b = 2;
var t = 2;
var u = 2;
var q = 2;
var c = 2;

// Function for creating BBcode tags:
function tag (v, tagadd, newbut, tagclose, oldbut, name) {
  if (eval(v)%2 == 0) {
    eval ("window.document.entryform."+name+".value = newbut;");
    insertAtCursor(document.entryform.textbody, tagadd);
  } else {
    eval("window.document.entryform."+name+".value = oldbut;");
    insertAtCursor(document.entryform.textbody, tagclose);
  }
  eval (v+"++;");
}

// Helpbox messages:
bold_help = "Bold text: [b]text[/b]";
italic_help = "Italic text: [i]text[/i]";
underline_help = "Underlined text: [u]text[/u]";
quote_help = "Quoted text: [quote]text[/quote] or [quote=name]text[/quote]";
code_help = "Mono-spaced font: [code]code[/code]";
none_help = "";

// Function for displaying help information in the helpline texbox::
function helpline (help) {
  var helpbox = document.entryform.helpbox;
  helpbox.value = eval(help + "_help");
}

// Insert text at cursor:
function insertAtCursor(myField, myValue) {
  // IE support:
  if (document.selection) {
    myField.focus();
    sel = document.selection.createRange();
    sel.text = myValue;
  }
  // Moz/NS support:
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
  // Other browsers:
  } else {
    myField.value += myValue;
  }
}


// EOF



// -->
// EOF

