//-- calendar

var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function checkDate(val, possibleValues) {
	var result = new Object();
	result.value = true;
	Error = "";

	date = new String(val);

	if(possibleValues) {
		for(var index = 0; index < possibleValues.length; index++) {
			if(date == possibleValues[index]) { return result; }
		}
	}
	date_d = date.substring(0,2); 
	date_m = date.substring(3,5);
	date_y = date.substring(6);
	if (date.substring(2,3) != "." || date.substring(5,6) != ".") { Error = "Неверный формат введенной даты"; } else
	if(isNaN(date_d) || date_d <= 0 || date_d > getDays(date_m - 1, date_y)) { Error = "Неверное значение дня даты"; } else
		if(isNaN(date_m) || date_m <= 0 || date_m > 12) { Error = "Неверное значение месяца даты"; }  else
			if(isNaN(date_y) || date_y < 1910 || date_y > 2020) { Error = "Неверное значение года даты"; } 

	if(Error > "") {
		result.value = false;
		result.desc = Error;
	}
	
	return result;
}

function getDays(month, year) {
	if(month == 1) {
		return ((0 == year % 4) && (0 != (year % 100))) ||
			(0 == year % 400) ? 29 : 28;
	}
	else { return daysInMonth[month]; }
}

function dateSet(name) {
	var obj = document.getElementById(name + "_day");
	var month = document.getElementById(name + "_month").
		options[document.getElementById(name + "_month").selectedIndex].value;
	var year = document.getElementById(name + "_year").
		options[document.getElementById(name + "_year").selectedIndex].value;
	daysSet(obj, month, year);
	return;
}

function daysSet(obj, month, year) {
	var days = getDays(month, year);
	var selInd = obj.selectedIndex;
	
		for(var i = obj.length - 1; i >= days; i--) { obj.options[i] = null; }
		for(i = obj.length - 1; i < days; i++) {
			obj.options[i] = new Option("" + (i + 1), "" + (i + 1), false, false);
		}
		
		if(days <= selInd) {
			selInd = days - 1;
		}
		obj.options[selInd].selected = true;
	return;
}

function showCalendar(field) {
//	window.open("/calendar.asp?field=" + field + "&start_date=" + document.getElementById(field).value, "calendar_win", "width=220px, height=125px, left=400, top=300;");
}

//-- menu



function initKLayers(){
  isDOM=(document.getElementById)?true:false
  isOpera=isOpera5=window.opera && isDOM
  isOpera6=isOpera && window.print
  isOpera7=isOpera && navigator.userAgent.indexOf("Opera 7") > 0 || navigator.userAgent.indexOf("Opera/7") >= 0
  isMSIE=isIE=document.all && document.all.item && !isOpera
  isNC=navigator.appName=="Netscape"
  isNC4=isNC && !isDOM
  isNC6=isMozilla=isNC && isDOM

  if(!isDOM && !isNC && !isMSIE && !isOpera){
    KLayers=false
    return false
  }

  pageLeft=0
  pageTop=0

  KL_imagePreloaderCount=0
  KL_imagePreloaderArray=new Array()

  KL_imageRef="document.images[\""
  KL_imagePostfix="\"]"
  KL_styleSwitch=".style"
  KL_layerPostfix="\"]"

  if(isNC4){
    KL_layerRef="document.layers[\""
    KL_styleSwitch=""
  }

  if(isMSIE){
    KL_layerRef="document.all[\""
  }

  if(isDOM){
    KL_layerRef="document.getElementById(\""
    KL_layerPostfix="\")"
  }

  KLayers=true
  return true
}

initKLayers()

// document and window functions:

function getWindowLeft(w){
  if(!w) w=self
  if(isMSIE || isOpera7) return w.screenLeft
  if(isNC || isOpera) return w.screenX
}

function getWindowTop(w){
  if(!w) w=self
  if(isMSIE || isOpera7) return w.screenTop
  if(isNC || isOpera) return w.screenY
}

function getWindowWidth(w){
  if(!w) w=self
  if(isMSIE) return w.document.body.clientWidth
  if(isNC || isOpera) return w.innerWidth
}

function getWindowHeight(w){
  if(!w) w=self
  if(isMSIE) return w.document.body.clientHeight
  if(isNC || isOpera) return w.innerHeight
}

function getDocumentWidth(w){
  if(!w) w=self
  var d=w.document
  if(isMSIE || isOpera7) return d.body.scrollWidth
  if(isNC) return d.width
  if(isOpera5) return d.body.style.pixelWidth
}

function getDocumentHeight(w){
  if(!w) w=self
  var d=w.document
  if(isMSIE || isOpera7) return d.body.scrollHeight
  if(isNC) return d.height
  if(isOpera5) return d.body.style.pixelHeight
}

function getScrollX(w){
  if(!w) w=self
  if(isMSIE || isOpera7) return w.document.body.scrollLeft
  if(isNC || isOpera5) return w.pageXOffset
}

function getScrollY(w){
  if(!w) w=self
  if(isMSIE || isOpera7) return w.document.body.scrollTop
  if(isNC || isOpera5) return w.pageYOffset
}

function preloadImage(imageFile){
  KL_imagePreloaderArray[KL_imagePreloaderCount]=new Image()
  KL_imagePreloaderArray[KL_imagePreloaderCount++].src=imageFile
}

var KL_LAYER=0
var KL_IMAGE=1

function KL_findObject(what,where,type){
  var i,j,l,s
  var len=eval(where+".length")
  for(j=0;j<len;j++){
    s=where+"["+j+"].document.layers"
    if(type==KL_LAYER){
      l=s+"[\""+what+"\"]"
    }
    if(type==KL_IMAGE){
      i=where+"["+j+"].document.images"
      l=i+"[\""+what+"\"]"
    }
    if(eval(l)) return l
    l=KL_findObject(what,s,type)
    if(l!="null") return l
  }
  return "null"
}

function KL_getObjectPath(name,parent,type){
  var l=((parent && isNC4)?(parent+"."):(""))+((type==KL_LAYER)?KL_layerRef:KL_imageRef)+name+((type==KL_LAYER)?KL_layerPostfix:KL_imagePostfix)
  if(eval(l))return l
  if(!isNC4){
    return l
  }else{
    return KL_findObject(name,"document.layers",type)
  }
}

function layer(name){
  return new KLayer(name,null)
}

function layerFrom(name,parent){
  if(parent.indexOf("document.")<0) parent=layer(parent).path
  return new KLayer(name,parent)
}

function image(name){
  return new KImage(name,null)
}

function imageFrom(name,parent){
  if(parent.indexOf("document.")<0) parent=layer(parent).path
  return new KImage(name,parent)
}

// class "KLayer":

function KLayer(name,parent){
  this.path=KL_getObjectPath(name,parent,KL_LAYER)
  this.object=eval(this.path)
  if(!this.object)return
  this.style=this.css=eval(this.path+KL_styleSwitch)
}

KLP=KLayer.prototype

KLP.isExist=KLP.exists=function(){
  return (this.object)?true:false
}

function KL_getPageOffset(o){ 
  var KL_left=0
  var KL_top=0
  do{
    KL_left+=o.offsetLeft
    KL_top+=o.offsetTop
  }while(o=o.offsetParent)
  return [KL_left, KL_top]
}

KLP.getLeft=function(){
  var o=this.object
  if(isMSIE || isNC6 || isOpera) return o.offsetLeft-pageLeft
  if(isNC4) return o.x-pageLeft
}

KLP.getTop=function(){
  var o=this.object
  if(isMSIE || isNC6 || isOpera) return o.offsetTop-pageTop
  if(isNC4) return o.y-pageTop
}

KLP.getAbsoluteLeft=function(){
  var o=this.object
  if(isMSIE || isNC6 || isOpera) return KL_getPageOffset(o)[0]-pageLeft
  if(isNC4) return o.pageX-pageLeft
}

KLP.getAbsoluteTop=function(){
  var o=this.object
  if(isMSIE || isNC6 || isOpera) return KL_getPageOffset(o)[1]-pageTop
  if(isNC4) return o.pageY-pageTop
}

KLP.getWidth=function(){
  var o=this.object
  if(isMSIE || isNC6 || isOpera7) return o.offsetWidth
  if(isOpera5) return this.css.pixelWidth
  if(isNC4) return o.document.width
}

KLP.getHeight=function(){
  var o=this.object
  if(isMSIE || isNC6 || isOpera7) return o.offsetHeight
  if(isOpera5) return this.css.pixelHeight
  if(isNC4) return o.document.height
}

KLP.getZIndex=function(){
  return this.css.zIndex
}

KLP.setLeft=KLP.moveX=function(x){
  x+=pageLeft
  if(isOpera){
    this.css.pixelLeft=x
  }else if(isNC4){
    this.object.x=x
  }else{
    this.css.left=x
  }
}

KLP.setTop=KLP.moveY=function(y){
  y+=pageTop
  if(isOpera){
    this.css.pixelTop=y
  }else if(isNC4){
    this.object.y=y
  }else{
    this.css.top=y
  }
}

KLP.moveTo=KLP.move=function(x,y){
  this.setLeft(x)
  this.setTop(y)
}

KLP.moveBy=function(x,y){
  this.moveTo(this.getLeft()+x,this.getTop()+y)
}

KLP.setZIndex=KLP.moveZ=function(z){
  this.css.zIndex=z
}

KLP.setVisibility=function(v){
  this.css.visibility=(v)?(isNC4?"show":"visible"):(isNC4?"hide":"hidden")
}

KLP.show=function(){
  this.setVisibility(true)
}

KLP.hide=function(){
  this.setVisibility(false)
}

KLP.isVisible=KLP.getVisibility=function(){
  return (this.css.visibility.toLowerCase().charAt(0)=='h')?false:true
}

KLP.setBgColor=function(c){
  if(isMSIE || isNC6 || isOpera7){
    this.css.backgroundColor=c
  }else if(isOpera5){
    this.css.background=c
  }else if(isNC4){
    this.css.bgColor=c
  }
}

KLP.setBgImage=function(url){
  if(isMSIE || isNC6 || isOpera6){
    this.css.backgroundImage="url("+url+")"
  }else if(isNC4){
    this.css.background.src=url
  }
}

KLP.setClip=KLP.clip=function(top,right,bottom,left){
  if(isMSIE || isNC6 || isOpera7){
    this.css.clip="rect("+top+"px "+right+"px "+bottom+"px "+left+"px)"
  }else if(isNC4){
    var c=this.css.clip
    c.top=top
    c.right=right
    c.bottom=bottom
    c.left=left
  }
}

KLP.scrollTo=KLP.scroll=function(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  if(scrollX>this.getWidth()-windowWidth) scrollX=this.getWidth()-windowWidth
  if(scrollY>this.getHeight()-windowHeight) scrollY=this.getHeight()-windowHeight
  if(scrollX<0)scrollX=0
  if(scrollY<0)scrollY=0
  var top=0
  var right=windowWidth
  var bottom=windowHeight
  var left=0
  left=left+scrollX
  right=right+scrollX
  top=top+scrollY
  bottom=bottom+scrollY
  this.moveTo(windowLeft-scrollX,windowTop-scrollY)
  this.setClip(top,right,bottom,left)
}

KLP.scrollBy=KLP.scrollByOffset=function(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  var X=-parseInt(this.css.left)+windowLeft+scrollX
  var Y=-parseInt(this.css.top)+windowTop+scrollY
  this.scroll(windowLeft,windowTop,windowWidth,windowHeight,X,Y)
}

KLP.scrollByPercentage=function(windowLeft,windowTop,windowWidth,windowHeight,scrollX,scrollY){
  var X=(this.getWidth()-windowWidth)*scrollX/100
  var Y=(this.getHeight()-windowHeight)*scrollY/100
  this.scroll(windowLeft,windowTop,windowWidth,windowHeight,X,Y)
}

KLP.write=function(str){
  var o=this.object
  if(isMSIE || isNC6 || isOpera7){
    o.innerHTML=str
  }else if(isNC4){
    var d=o.document
    d.open()
    d.write(str)
    d.close()
  }
}

KLP.add=function(str){
  var o=this.object
  if(isMSIE || isNC6 || isOpera7){
    o.innerHTML+=str
  }else if(isNC4){
    var d=o.document
    d.write(str)
  }
}

// class "KImage":

KIP=KImage.prototype

function KImage(name){
  this.path=KL_getObjectPath(name,false,KL_IMAGE)
  this.object=eval(this.path)
}

KIP.isExist=KIP.exists=function(){
  return (this.object)?true:false
}

KIP.getSrc=KIP.src=function(){
  return this.object.src
}

KIP.setSrc=KIP.load=function(url){
  this.object.src=url
}
last = "";
function cancelCloseMenu(){
	if(self.tm) clearTimeout(tm) // cancel delayed closing
}

function initiateHideMenu(){
	tm=setTimeout("hideMenu()",2000) // 1 second delay to close a menu
}

function hideMenu(){
	if (last != ""){
		m2=layer(last)
		m2.style.display = "none";
		m2.hide()
	}
}

function change_this(obj){
	obj.src = obj.src.substr(0, obj.src.length - 4)+'_o.gif';
}
function dechange_this(obj){
	obj.src = obj.src.substr(0, obj.src.length - 6)+'.gif';
}
document.onclick = hideMenu;

function showMenu2(src, target){
     hideMenu();
     m1=layer(src);
     m2=layer(target);

     cancelCloseMenu();
     last = target;
     m2.style.top = (m1.getAbsoluteTop()+ 0 )+"px";
     m2.style.left = m1.getAbsoluteLeft()-m1.getWidth()-6 +"px";
     m2.moveTo(m1.getAbsoluteLeft()-m1.getWidth()-6, m1.getAbsoluteTop()); // move second menu relatively
     m2.show();
}

function showMenu(src, target){
	hideMenu();
	m1=layer(src);
	m2=layer(target);
	m2.style.display = "block";
	cancelCloseMenu();
	last = target;
	m2.style.top = (m1.getAbsoluteTop()- 10 + m1.getHeight())+"px";
	m2.style.left = m1.getAbsoluteLeft()+"px";
	m2.moveTo(m1.getAbsoluteLeft()+m1.getWidth(), m1.getAbsoluteTop());
	m2.show();
}
