//editor

var rightCaret = String.fromCharCode (62);
function getSelectionRange () {
		var selectionRange;
		selectionRange = document.selection.createRange ();

		if (isEditable (selectionRange)) {
			var currentText = selectionRange.text;
			if (currentText != "")
				return (selectionRange);   
		}

		return (null);
	}
	
	function isEditable (selectionRange) {

		if (selectionRange.parentElement ().tagName == "TEXTAREA")
			return (true);

		return (false);
	} 
	
function simpleEnclose (tagName) {

 if (tagName == "")
  return (false);

 var selectionRange;
 selectionRange = getSelectionRange ();

 if (selectionRange != null) {

  var currentText;
  currentText = selectionRange.text;

  var preText, postText;
  preText = "<" + tagName + rightCaret;
  postText = "</" + tagName + rightCaret;

  replaceText (selectionRange, preText, postText, currentText);
  }
 } 


	
function replaceText (selectionRange, preText, postText, currentText) {

selectionRange.text = preText + currentText + postText;

selectionRange.parentElement ().focus ();
 } 
 
// slut editor
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}


function getQSItem(key) {
	if(queryString(key)=='false') 
	{
	    if(queryString(key.toLowerCase())=='false') 
		    return '';
		else
    		return queryString(key.toLowerCase());
	} else {
		return queryString(key);
	}
}


function openPrintableVersion(lWidth, lHeight) {
	var printWin = window.open(fixURL(document.location.href) + '&xsl=popupContent.xsl','print','scrollbars=yes,resizable=yes,width=' + lWidth +',height=' + lHeight +',menubar=no,toolbar=no');
	if(window.print)
		printWin.print();
}


function sendToFriend(lWidth, lHeight) {
	var SendWin = window.open('Content.asp?Ref=' + getQSItem('Ref') + '&NodeRef=SEND_TO_FRIEND&xsl=popupContent.xsl&URL='+ escape(document.location.href),'print','scrollbars=auto,resizable=yes,width=' + lWidth +',height=' + lHeight +',menubar=no,toolbar=no');
	SendWin.focus();
}


function fixURL(sURL) {
	return sURL;
}



function getExpiration(lMinutes) {

  var exp = new Date();
  exp.setTime(exp.getTime() + (lMinutes*60*1000));
  return  exp;
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
   }
  return null;
}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : (";expires=" + expires.toGMTString())) + ((path == null) ? "" : (";path=" + path)) +  ((domain == null) ? "" : (";domain=" + domain)) +    ((secure == true) ? ";secure" : "");
}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}

function getCookieVal(offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}


function checkOpenPopup(lMinutes, sCookieName) {
  if(lMinutes!=0 && lMinutes!=null) {
    var exp = getExpiration(lMinutes);
    var lCount = GetCookie(sCookieName);
    if (lCount == null) {
      lCount=1;
      SetCookie(sCookieName, lCount, exp);
      return true;
    }
    else {
      return false;
    }
  }
  else {
    return true;
  }
}


function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"));
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0));
}

function isInteger (s)
{   
    var i;
    for (i = 0; i < s.length; i++) 
    {   
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }
    return true;
}

function EditInt(FormItem) { 
	if( !isInteger(FormItem.value) ) {
		FormItem.focus();
		FormItem.select();
		alert("Only numbers are allowed in this field.");
		return false;
	}
	return true;
}
