function GetMouseX(e)
{
  if(e.pageX)
    return e.pageX;
  else if(e.clientX)
    return e.clientX + (document.documentElement.scrollLef ? document.documentElement.scrollLeft : document.body.scrollLeft);
  else return 0;
}

function GetMouseY(e)
{
  if(e.pageY)
    return e.pageY;
  else if(e.clientY)
    return e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  else return 0;
}

function GetWindowWidth()
{
  var w = 0;
  if(typeof(window.innerWidth) == 'number')
    w = window.innerWidth;
  else if(document.documentElement && document.documentElement.clientWidth)
    w = document.documentElement.clientWidth;
  else if(document.body && document.body.clientWidth)
    w = document.body.clientWidth;
  return w;
}

function GetWindowHeight()
{
  var h = 0;
  if(typeof(window.innerHeight) == 'number')
    h = window.innerHeight;
  else if(document.documentElement && document.documentElement.clientHeight)
    h = document.documentElement.clientHeight;
  else if(document.body && document.body.clientHeight)
    h = document.body.clientHeight;
  return h;
}

function GetDocumentWidth()
{
  var w = document.body.scrollWidth;
  if(w == 0)
    w = document.body.clientWidth;
  return w;
}

function GetDocumentHeight()
{
  var h = document.body.scrollHeight;
  if(h == 0)
    h = document.body.clientHeight;
  return h;
}

function GetXOffset()
{
  if(window.pageXOffset)
    return window.pageXOffset;
  else
    return document.body.scrollLeft;
}

function GetYOffset()
{
  if(window.pageYOffset)
    return window.pageYOffset;
  else
    return document.body.scrollTop;
}

var globalCursorPos; // global variabe to keep track of where the cursor was

function GetTextareaSelection(oTextarea)
{
      var ret = [];
  if(window.getSelection)
  {
    // Opera, Firefox
    ret[0] = oTextarea.selectionStart;
    ret[1] = oTextarea.selectionEnd;
  }
  else if(document.selection)
  {
    // IE
    var selRange = document.selection.createRange();
    if(selRange.text.length>0)
    {
      var fullRange = selRange.duplicate();
      fullRange.moveToElementText(oTextarea);
      fullRange.setEndPoint('EndToEnd',selRange);
      ret[0] = fullRange.text.length - selRange.text.length;
      ret[1] = ret[0] + selRange.text.length;
    }
    else
    {
      var node = oTextarea;
       node.focus(); 
 /* without node.focus() IE will returns -1 when focus is not on node */
// if(node.selectionStart) return node.selectionStart;
// else if(!document.selection) return 0;
 var c		= "\001";
 var sel	= document.selection.createRange();
 var dul	= sel.duplicate();
 var len	= 0;
 dul.moveToElementText(node);
 sel.text	= c;
 len		= (dul.text.indexOf(c));
 sel.moveStart('character',-1);
 sel.text	= "";
 ret[0] = len; ret[1]=len;

    }
  }
  return ret;
}


// Move the caret to the specified position
function setCursorPosition(field, pos) {
   if (field.createTextRange) {
      var range = field.createTextRange();
      range.collapse(true);
      range.moveEnd('character', pos);
      range.moveStart('character', pos);
      range.select();
   } else if (field.selectionEnd) {
      field.selectionEnd = pos;
   }
}

//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;

//create a range object and save off it's text
 var objRange = document.selection.createRange();
 var sOldRange = objRange.text;

//set this string to a small string that will not normally be encountered
 var sWeirdString = '#%~';

//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
 for (i=0; i <= sNewText.length; i++) {
   var sTemp = sNewText.substring(i, i + sWeirdString.length);
   if (sTemp == sWeirdString) {
     var cursorPos = (i - sOldRange.length);
     return cursorPos;
   }
 }
}

//this function inserts the input string into the textarea
//where the cursor was at
function insertString(stringToInsert) {
 var firstPart = myForm.myTextArea.value.substring(0, globalCursorPos);
 var secondPart = myForm.myTextArea.value.substring(globalCursorPos, myForm.myTextArea.value.length);
 myForm.myTextArea.value = firstPart + stringToInsert + secondPart;
}

function submitData(arr)
{
  var div = document.createElement('div');
  div.style.visibility = 'hidden';
  
  var form = document.createElement('form');
  form.setAttribute('method','post');
  form.setAttribute('action','');

  for(var i=0,j=arr.length; i<j; i++)
  {
    var input = document.createElement('input');
    input.setAttribute('type','hidden');
    input.setAttribute('name',arr[i][0]);
    input.setAttribute('value',arr[i][1]);
    form.appendChild(input);
  }

  div.appendChild(form);
  document.body.appendChild(div);
  form.submit();
}

function Submit(action,data,target,enctype) {
  var div = document.createElement('div');
  div.style.visibility = 'hidden';

  var form = document.createElement('form');
  form.setAttribute('method','post');
  form.setAttribute('action',action);
/*  if(target != undefined) {
    form.target = target;
  }*/
/*  if(encoding != undefined) {
    form.enctype = enctype;
    form.setAttribute('encoding',enctype);
  }*/

  if(data != null) {
    for(var j=0; j<data.length; j++)
    {
      var input = document.createElement('input');
      input.setAttribute('type','hidden');
      input.setAttribute('name', data[j]['name']);
      input.setAttribute('value',data[j]['val']);
      form.appendChild(input);
    }
  }

  div.appendChild(form);
  document.body.appendChild(div);

  form.submit();
}
function SubmitTarget(action,data,target) {
  var div = document.createElement('div');
  div.style.visibility = 'hidden';

  var form = document.createElement('form');
  form.setAttribute('method','post');
  form.setAttribute('action',action);
  form.target = target;
/*  if(encoding != undefined) {
    form.enctype = enctype;
    form.setAttribute('encoding',enctype);
  }*/

  if(data != null) {
    for(var j=0; j<data.length; j++)
    {
      var input = document.createElement('input');
      input.setAttribute('type','hidden');
      input.setAttribute('name', data[j]['name']);
      input.setAttribute('value',data[j]['val']);
      form.appendChild(input);
    }
  }

  div.appendChild(form);
  document.body.appendChild(div);

  form.submit();
}

