
/*
function GetElement(id) {
    if(document.all) {
        return document.all[id];
    } else {
        return document.getElementById(id);
    }
}
*/

function GetElement(id) {
  return document.getElementById(id);
}

function getLeft(obj)
{
	var curleft = 0;
    while(obj.offsetParent)
    {
        curleft += obj.offsetLeft;
        obj = obj.offsetParent;
    }
	return curleft;
}

function getTop(obj)
{
	var curtop = 0;
    while (obj.offsetParent)
    {
        //alert("*"+obj.offsetTop+"*");
        curtop += obj.offsetTop;
        obj = obj.offsetParent;
    }
	return curtop;
}

function PageXOffset() {
  if( window.pageXOffset != undefined ) {
    return window.pageXOffset;
  } else {
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    return iebody.scrollLeft;
  }
}

function PageYOffset() {
  if( window.pageYOffset != undefined ) {
    return window.pageYOffset;
  } else {
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    return iebody.scrollTop;
  }
}

function CreateDiv(id_name, class_name)
{
  var divTag = document.createElement("div");
  divTag.id = id_name;
  divTag.style.borderStyle = "solid";
  divTag.style.borderWidth = "1px";
  divTag.style.textAlign = "center";
  divTag.className = class_name;
  document.body.appendChild(divTag);
  return divTag;
}

function DisableSelection(target){
  if (typeof target.onselectstart!="undefined") //IE route
  	target.onselectstart=function(){return false}
  else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
  	target.style.MozUserSelect="none"
  else //All other route (ie: Opera)
  	target.onmousedown=function(){return false}
  target.style.cursor = "default"
}

var GrayUI_div = null;
function GrayUI(gray) {
  if(!GrayUI_div) {
    GrayUI_div = document.createElement("div");
    document.body.appendChild(GrayUI_div);
    GrayUI_div.style.position = "absolute";
    GrayUI_div.style.top = "0px";
    GrayUI_div.style.left = "0px";
    GrayUI_div.style.width = "100%";
    GrayUI_div.style.height = "100%";
    GrayUI_div.style.backgroundColor = "#FFFFFF";
    //GrayUI_div.style.backgroundColor = "#A06080";
    //GrayUI_div.style.backgroundColor = "#80A060";
    GrayUI_div.style.zIndex = "10000";
    GrayUI_div.style.filter = "alpha(opacity=30)";
    GrayUI_div.style.opacity = "0.3";
    GrayUI_div.style.display = "none";
  }
  if(gray) {
    GrayUI_div.style.display = "block";
  } else {
    GrayUI_div.style.display = "none";
  }
}


// calling the function
// insertAtCursor(document.formName.fieldName, ‘this value’);
function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE 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);
	} else {
  	myField.value += myValue;
	}
}


function bbcode_insert_link(id) {
  var field = GetElement(id);
  var url = prompt('Enter URL', '');
  if(url) {
    var title = prompt('Enter link title (this is optional)', '');
	  if(!title) {
	    title = url;
	  }
    var val = '[URL=' + url + ']' + title + '[/URL]';
    insertAtCursor(field, val);
  }
}

function bbcode_insert_image(id) {
  var field = GetElement(id);
  var url = prompt('Enter image URL', '');
  if(url) {
    var thumb;
    var regex_geturls = /^both\s(\S+)\s(\S+)/;
    var urls = regex_geturls.exec(url);
    if(urls && urls[1] && urls[2]) {
      url = urls[1];
      thumb = urls[2];
    } else {
      thumb = prompt('Enter thumbnail URL (this is optional)', '');
    }
    var val;
    if(thumb) {
      val = '[URL=' + url + '][IMG]' + thumb + '[/IMG][/URL]';
    } else {
      val = '[IMG]' + url + '[/IMG]';
    }
    insertAtCursor(field, val);
  }
}








