// returns part of request URI which starts after '?' and ends with the end of URI
function getQueryString() {
  var s = document.location.toString().split("?");
  if(s.length > 1)
    return "?" + s[s.length-1];
  else
    return "";
}
// removes all whitespaces from the beginning of the string
function trimLeft(str)
{
  var i = 0;
  str = str.toString();
  while(str.charAt(i) == " " || 
        str.charAt(i) == "\n" ||
        str.charAt(i) == "\r" ||
        str.charAt(i) == "\t")
      i++;
  if(i > 0)
    return str.substring(i,str.length);
  else
    return str;
}
// removes all whitespaces in the end of string
function trimRight(str)
{
  str = str.toString();
  var i = str.length - 1;
  while(str.charAt(i) == " " || 
        str.charAt(i) == "\n" ||
        str.charAt(i) == "\r" ||
        str.charAt(i) == "\t")
      i--;
  return str.substring(0,i+1);
}
// removes all whitespaces in the beginning and in the end of string
function trim(str)
{
  return trimLeft(trimRight(str));
}
// shows the specified html object if it has been hidden and hides it otherwise
// object is specified by id attribute which constructed by concatenation 
// 'divId' parameter and 'recid' attribute of object specified by 'obj' parameter 


// change content of specified node
function setText(obj,text)
{
  if(obj != null && obj.nodeType == 1) //element node
  {
    var c = obj.childNodes;
    var i;
    var empty = true;
    var replaced = false;
    if(text == null) // for
        text = "null";    // debug
    text = trim(text.toString());
    if(text.length == 0)
      text = "\xA0";

    while(obj.childNodes.length > 0)
      obj.removeChild(obj.childNodes.item(0));
    if(text != "" && ! replaced)
    {
      obj.appendChild(document.createTextNode(text));
    }
    return true;
  }
  else
    out("setText","obj is null","text:",text);
  return false;
}

// add's hidden field to form
function addParameter(name,value,formName,noCheck) {
  var form;
  if(formName == undefined)
    form = getField("paramsZone");
  else if(typeof(formName) == "string")
    form = getField(formName);
  else
    form = formName;
  if(form != null) {
    var el;
    if(!noCheck)
      el = getField(name,"");
    if(el == null) 
      el = document.createElement("INPUT");
    el.type = "hidden";
    el.name = name;
    el.value = value;
    el.id = name;
    form.appendChild(el);
  }
  else
    out("addParameter: "+formName+" is null");
}

// allows to sequentally create string of fields separated by 'delim'
function addToStr(str,value,delim) {
  if(str === "" || str === null)
    str = value;
  else
    str += delim + value;
  return str;
}

// returns width of the window contents
function getInnerWidth() {
  return document.body.offsetWidth;
}

// returns height of the window contents
function getInnerHeight() {
  return document.body.offsetHeight;
}

// get scroll X offset of the document
function getScrollX() {
  return document.body.scrollLeft;
}
// get scroll Y offset of the document
function getScrollY() {
  return document.body.scrollTop;
}
// this function removes all children in specified DOM element
function removeAllChildren(obj) {
  var children = obj.childNodes;
  for(;0 < children.length;) {
    obj.removeChild(children.item(0));
  }
}

var test = false;
function addSelectOption(selObj,optText,optValue,bSelected) {
  var i = document.createElement("OPTION");
  i.value = optValue;
  i.appendChild(document.createTextNode(optText));
  if(bSelected)
  	i.selected = true;
  selObj.appendChild(i);
  	
}
function removeSelectOption(selObj,optValue) {
  for(var i = 0;i< selObj.options.length;i++)
    if(selObj.options.length.item(i).value == optValue)
      selObj.removeChild(selObj.options.length.item(i));
}

// returns left position of specified element related to left side of window content
function getLeftPosition(obj) {
  var left = 0;
  while(obj != null) {
    left += obj.offsetLeft;
    obj = obj.offsetParent;
  }
  return left;
}
// returns top position of specified element related to top of window content
function getTopPosition(obj) {
  var top = 0;
  while(obj != null) {
    top += obj.offsetTop;
    obj = obj.offsetParent;
  }
  return top;
}

function getWindowName() {
  var name = window.name;
  if(name === undefined)
    name = window.Name;
  return name;
}
function setWindowName(name) {
  window.name = window.Name = name;
}


function getElem(name) {
	var el = document.getElementsByName(name);
	if(el == null || el.length == 0) {
		alert('Нет такого элемента ' + name);
		return '';
	}
	else
		return el.item(0);
}
function getElemValue(name) {
	var el = getElem(name);
	if(el == '' ) {
		return '' 
	}
	else
		return el.value;
}

function isUndefined(v) {
	return typeof(v) == "undefined";
}
// dumb function to avoid errors in pages where load isn't defined
function load() {
}


function parseDate(strDate) {
  var parts = strDate.split(".");
  var date = null;
  if(parts.length != 3) 
    return null;
  
  for(var i = 0; i < parts.length;i++) {
  	parts[i] = trim(parts[i]);
  	for(var j = 0; j < parts[i].length && parts[i].charAt(j) == '0' ;j++);
  	parts[i] = parts[i].substring(j,parts[i].length);
  }
	
  if(parseInt(parts[1]) == parts[1]) {
    if(parts[2] >= 0 && parts[2] < 100) {
      parts[2] = parseInt(parts[2]) + 2000;
    }
    date = new Date(parts[2],parts[1]-1,parts[0]);
  }
  return date;
}

function checkDate(fldName,caption) {
	var date = parseDate(document.getElementsByName(fldName).item(0).value);
	if(date == null) {
		alert("Неверная дата  в поле <<" + caption + ">>");
		return false;
	}
	return true;
}

// encodes string so it can be used as parameter in url (this is redundant when using form to send values)

function encode(str) {
  var i = 0;
  str = str.toString();
  while(i < str.length) {
  	if (str.charAt(i) == " ") {
       str = str.substring(0,i) + "%20" + str.substring(i+1);
       i += 2;
    }
  	if (str.charAt(i) == "&") {
       str = str.substring(0,i) + "%26" + str.substring(i+1);
       i += 2;
    }
  	if (str.charAt(i) == "=") {
       str = str.substring(0,i) + "%3d" + str.substring(i+1);
       i += 2;
    }
     i++;
  }
  return str;
}
