//
// Colour selector functions.
//
// Author: PTH, QWD
// Date: 220609
// Version: alpha 0.1
//
// ----------------------------------------------
 var red0;
 var green0;
 var blue0;
 
 red0   = "ff";
 green0 = "ff";
 blue0  = "ff";
 
//------------------------------------------------
function handleCol (event, inner, curs, bgdef, form_id, fval, x0, xwid, pre_col, post_col)
{
 var w  = document.getElementById(inner);
 var w1 = document.getElementById(curs);
 var f1 = document.getElementById(form_id);
 var s;
 
 if (window.event) {
    x = window.event.clientX + document.documentElement.scrollLeft
                             + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop +
                             + document.body.scrollTop;
  }
  else {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 fr = (256.0*(x-x0)/xwid);		  
 col0 = parseInt(fr, 10).toString(16);
		  
 if (fr < 16)
   col0 = '0'+col0;
   
 col = pre_col + col0 + post_col;
 
 if (pre_col == "#")
    {
	 red0 = col0;
	}
 else if (pre_col == "#00")
    {
	 green0 = col0;
	}
 else
    {
	 blue0 = col0;
	}
 w.style.backgroundColor = bgdef; 
 w1.style.left           = (x-x0); 
 w.style.backgroundColor = col; 
 rgb = red0+green0+blue0;
 palcol = '#'+rgb;
 f1.elements[fval].value = palcol;
 f1.elements[fval].style.backgroundColor = palcol;
 f1.elements[fval].style.color = '#'+(0xffffff-parseInt(rgb, 16)).toString(16);
 
 return false;
}
//------------------------------------------------
function handleColourClick (form_id, fval, col)
{
 var f1 = document.getElementById(form_id);
		  
//		  document.write (form_id + ", " + fval + ", " + col);
 f1.elements[fval].value = '#' + col; 
 f1.elements[fval].style.backgroundColor = '#' + col; 
 
 return false;
}
//-----------------------------------------------------------------------------
var globalCursorPos; // global variabe to keep track of where the cursor was

//sets the global variable to keep track of the cursor position
function setCursorPos(form, taid) {
 var myForm = document.getElementById(form);
 var myTextArea = myForm.elements[taid]; // getElementById(taid);
// document.write ("<html><head></head><body><br> form = " + form + " tid = " + taid + " ta = " + myTextArea.value.length +"</body></html>");
 globalCursorPos = getCursorPos(myTextArea); // myForm.myTextArea);
// document.write ("<br>curs = " + globalCursorPos );
}

//This function returns the index of the cursor location in
//the value of the input text element
//It is important to make sure that the sWeirdString variable contains
//a set of characters that will not be encountered normally in your
//text
function getCursorPos(textElement) {
 //save off the current value to restore it later,
 var sOldText = textElement.value;
 //document.write ("<html><head></head><body><br>  t = " + sOldText +"</body></html>");

//create a range object and save off its text
 var objRange = document.selection.createRange();
 var sOldRange = objRange.text;
// document.write ("<html><head></head><body><br>  t = wqexwexwex</body></html>");

//set this string to a small string that will not normally be encountered
 var sWeirdString = '#%~';
// document.write ("<html><head></head><body><br>  t = " + sWeirdString +"</body></html>");

//insert the weirdstring where the cursor is at
 objRange.text = sOldRange + sWeirdString;
 objRange.moveStart('character', (0 - sOldRange.length - sWeirdString.length));

//save off the new string with the weirdstring in it
 var sNewText = textElement.value;

//set the actual text value back to how it was
 objRange.text = sOldRange;

//look through the new string we saved off and find the location of
//the weirdstring that was inserted and return that value
  // document.write ("<html><head></head><body><br>  o = " + sNewText +"</body></html>");
   
 for (i=0; i <= sNewText.length; i++) {
   var sTemp = sNewText.substring(i, i + sWeirdString.length);
   if (sTemp == sWeirdString) {
      var cursorPos = (i - sOldRange.length);
  // document.write ("<html><head></head><body><br>  cp = " + cursorPos +"</body></html>");
      return cursorPos;
     }
   }
   return 4;
}

//this function inserts the input string into the textarea
//where the cursor was at
function insertString(form, taid, stringToInsert) {
 var myForm = document.getElementById(form);
// var myTextArea = document.getElementById(taid);
 var myTextArea = myForm.elements[taid];
// document.write ("<p> text area: "+myTextArea.value.length+" gcp = " + globalCursorPos+ "</p>");
 var firstPart = myTextArea.value.substring(0, globalCursorPos);
 var secondPart = myTextArea.value.substring(globalCursorPos, myTextArea.value.length);
 myTextArea.value = firstPart + stringToInsert + secondPart;
}
// End