var CPV_clientPC = navigator.userAgent.toLowerCase();
var CPV_clientVer = parseInt(navigator.appVersion);

var CPV_is_ie = ((CPV_clientPC.indexOf("msie") != -1) && (CPV_clientPC.indexOf("opera") == -1));
var CPV_is_nav = ((CPV_clientPC.indexOf('mozilla')!=-1) && (CPV_clientPC.indexOf('spoofer')==-1)
                && (CPV_clientPC.indexOf('compatible') == -1) && (CPV_clientPC.indexOf('opera')==-1)
                && (CPV_clientPC.indexOf('webtv')==-1) && (CPV_clientPC.indexOf('hotjava')==-1));
var CPV_is_moz = 0;

var CPV_is_win = ((CPV_clientPC.indexOf("win")!=-1) || (CPV_clientPC.indexOf("16bit") != -1));
var CPV_is_mac = (CPV_clientPC.indexOf("mac")!=-1);

var CPV_scrollTopPos = 0;
var CPV_scrollLeftPos = 0;

/* var txtarea = document.vCardPreview.PostMessage; */


function CPV_emoticon(icon)
{
/*  var txtarea = document.vCardPreview.PostMessage; */
  var txtarea = window.document.forms['vCardPreview'].PostMessage;
  CPV_scrollToCursor(txtarea, 0);
  txtarea.focus();
  CPV_pasteAtCursor(txtarea, ' ' + icon + ' ');
  CPV_scrollToCursor(txtarea, 1);
}
/*
 * END of template function
 */
 
/*
 * ALL OTHERS UTILS FUNCTION
 */
function CPV_insertTags(bbStart, bbEnd) {
/*  var txtarea = document.vCardPreview.PostMessage; */
  var txtarea = window.document.forms['vCardPreview'].PostMessage;
  CPV_scrollToCursor(txtarea, 0);
  txtarea.focus();

  if ((CPV_clientVer >= 4) && CPV_is_ie && CPV_is_win) {
    theSelection = document.selection.createRange().text;
    if (theSelection) {
      document.selection.createRange().text = bbStart + theSelection + bbEnd;
      theSelection = '';
      return;
    } else {
      CPV_pasteAtCursor(txtarea, bbStart + bbEnd);
	}
  } else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    var s1 = (txtarea.value).substring(0,selStart);
    var s2 = (txtarea.value).substring(selStart, selEnd)
    var s3 = (txtarea.value).substring(selEnd, selLength);
    txtarea.value = s1 + bbStart + s2 + bbEnd + s3;
    txtarea.selectionStart = selStart + (bbStart.length + s2.length + bbEnd.length);
    txtarea.selectionEnd = txtarea.selectionStart;
    CPV_scrollToCursor(txtarea, 1);
    return;
  } else {
    CPV_pasteAtCursor(txtarea, bbStart + bbEnd);
	CPV_scrollToCursor(txtarea, 1);
  }
}

function CPV_scrollToCursor(txtarea, action) {
  if (CPV_is_nav) {
    if (action == 0) {
      CPV_scrollTopPos = txtarea.scrollTop;
      CPV_scrollLeftPos = txtarea.scrollLeft;
    } else {
      txtarea.scrollTop = CPV_scrollTopPos;
      txtarea.scrollLeft = CPV_scrollLeftPos;
    }
  }
}

function CPV_pasteAtCursor(txtarea, txtvalue) {
  if (document.selection) {
    var sluss;
    txtarea.focus();
    sel = document.selection.createRange();
    sluss = sel.text.length;
    sel.text = txtvalue;
    if (txtvalue.length > 0) {
      sel.moveStart('character', -txtvalue.length + sluss);
    }
  } else if (txtarea.selectionStart || txtarea.selectionStart == '0') {
    var startPos = txtarea.selectionStart;
    var endPos = txtarea.selectionEnd;
    txtarea.value = txtarea.value.substring(0, startPos) + txtvalue + txtarea.value.substring(endPos, txtarea.value.length);
    txtarea.selectionStart = startPos + txtvalue.length;
    txtarea.selectionEnd = startPos + txtvalue.length;
  } else {
    txtarea.value += txtvalue;
  }
}
