var thisPage = new _BMTObject;

function _BMTObject() {	
//public members
	this.invokeMethod = _BMT_invokeMethod;
	this.advise = _BMT_advise;
	this.unadvise = _BMT_unadvise;
	this.CheckNumeric = _BMT_CheckNumeric;
	this.ChangeComma = _BMT_ChangeComma;
	this.ChangeDot = _BMT_ChangeDot;
	this.MoneyFormat = _BMT_MoneyFormat;
	this.ChangeDate = _BMT_ChangeDate;
	this.cancelEvent = false;
	this.delim = ".";
	this.ampers = "&";
	this.divname = "";
	this.bodydiv = "BODYDIV";
//events
	PAGE_ONBEFORESERVEREVENT = 'onbeforeserverevent';
//private members
	this._fireEvent = _BMT__fireEvent;
	this._switchFireEvent = _BMT__switchFireEvent;
	this._pulldown = _BMT__pulldown;
	this._dialwindow = _BMT__dialwindow;
	this._modalwindow = _BMT__modalwindow;
	this._disabled = _BMT__disabled;
	this._enabled = _BMT__enabled;
	this._movereturn = _BMT__movereturn;
	this._nullvalue = _BMT__nullvalue;
	this._emptyvalue = _BMT__emptyvalue;
	this._notnullvalue = _BMT__notnullvalue;
	this._reloadparentwindow = _BMT__reloadparentwindow;
	this._refreshparentwindow = _BMT__refreshparentwindow;
	this._setvalue = _BMT__setvalue;
	this._switchvalue = _BMT__switchvalue;
	this._swap = _BMT__swap;
	this._setstyle = _BMT__setstyle;
	this._fredirect = _BMT__fredirect;
	this._fsettimeout = _BMT__fsettimeout;
	this._opendiv = opendiv;
	this._switchdiv = switchdiv;
	this._setfilter = setfilter;
	this._resetfilter = resetfilter;
	this._adviseList = null;
	this._nLastAdviseID = 0;
	this._eventkey = "WCE=";
	this._userkey = "WCU=";
	this._scrollkey = "WCS="
	this._pagekey = "WCP="
}

function _BMT__setvalue(strObjName, strValue, strFormName) {	
	var elem = document.forms(strFormName).item(strObjName);
	if (elem != null) {
		elem.value = strValue;
	}
}

function _BMT__switchvalue(sObjName, sVal1, sVal2, strFormName) {
	var obj = document.forms(strFormName).item(sObjName);
	if (obj != null) {
		if (obj.value != sVal1) obj.value = sVal1;
		else {
			if (obj.value != sVal2) obj.value = sVal2;
		}
	}
}

function _BMT__dialwindow(url, name, features) {
    var nPos = name.indexOf('.');
    if (nPos > 0) name = name.substr(0, nPos);
	var description = window.open(url, name, features);
	description.focus();
}

function _BMT__refreshparentwindow() {
	var trew = window.opener;
	if(trew == null) trew = window.parent;
	if(trew != null) trew.location.reload();
}

function _BMT__reloadparentwindow(strObjName, strEventType, strEventValue, strFormName, strPage) {
	var trew = window.opener;
	if(trew == null) trew = window.parent;
	if(trew != null) {
	    var FormObject = trew.document.forms(strFormName);
	    var strAction = FormObject.action;
	    var strPattern = this._eventkey;
	    var strAmpers = this.ampers + this._userkey;
	    var nPos = strAction.indexOf(strPattern);
	    var nPosU = strAction.indexOf(strAmpers);
	    if (typeof(strEventValue) != "undefined" && strEventValue != "") {
		    strEventType += this.delim + strEventValue;
	    }
	    if (document.all('BODY') != null) var strScroll = this.ampers + this._scrollkey + document.all('BODY').scrollTop;
	    else var strScroll = this.ampers + this._scrollkey + '0';
	    if (strPage != null && strPage != '') var spage = this._pagekey + strPage;
	    else var spage = '';
	    strAction = strAction.substr(0, nPos) + this._eventkey + strObjName + this.delim + strEventType + strAction.substr(nPosU) + strScroll + spage;
	    FormObject.action = strAction;
	    FormObject.submit();
    }
}

function _BMT_invokeMethod(strObjName, strEventType, strEventValue, strFormName, strPage) {	
	var FormObject = window.document.forms(strFormName);
	var ObjName = window.document.all(strObjName);
	var strAction = FormObject.action;
	var strPattern = this._eventkey;
	var strAmpers = this.ampers + this._userkey;
	var nPos = strAction.indexOf(strPattern);
	var nPosU = strAction.indexOf(strAmpers);
	if (typeof(strEventValue) != "undefined" && strEventValue != "") {
		strEventType += this.delim + strEventValue;
	}
	if (document.all('BODY') != null) var strScroll = this.ampers + this._scrollkey + document.all('BODY').scrollTop;
	else var strScroll = this.ampers + this._scrollkey + '0';
	if (strPage != null && strPage != '') var spage = this._pagekey + strPage;
	else var spage = '';
	strAction = strAction.substr(0, nPos) + this._eventkey + strObjName + this.delim + strEventType + strAction.substr(nPosU) + strScroll + spage;
	FormObject.action = strAction;
	if (ObjName != null) ObjName.disabled = true;
	if (this.divname!='') opendiv(this.divname);
	setfilter(this.bodydiv);
	FormObject.submit();
}

function _BMT_advise(strEvent,funcToCall)
{
	if ((typeof(strEvent) == 'string') && 
		(strEvent == PAGE_ONBEFORESERVEREVENT) && 
		(funcToCall != null))
	{
		if (this._adviseList == null)
			this._adviseList = new Object;
		this._adviseList[++this._nLastAdviseID] = funcToCall;
		return this._nLastAdviseID;
	}
	return -1;
}

function _BMT_unadvise(strEvent,nAdviseID) {
	if (this._adviseList != null &&
		(typeof(strEvent) == 'string') && 
		(strEvent == PAGE_ONBEFORESERVEREVENT) && 
		(nAdviseID != null)) {
		if (this._adviseList[nAdviseID] != null) {
			this._adviseList[nAdviseID] = null;
			return true;
		}
	}
	return false;
}

function _BMT__pulldown(sObjName, sImg, sImgUp, sImgDn) {	
	var obj = document.all(sObjName);
	var img = document.all(sImg);
	var imgup = document.all(sImgUp);
	var imgdn = document.all(sImgDn);
	if (obj != null) {
	    var i = obj.length; 
	    if (i != null) {
		    for (var j = 0; j < i; j++) {
		        if (obj[j].style.display == 'none') {
			        obj[j].style.display = 'block';
			        if (img[j] != null && imgup != null && imgdn != null) img[j].src = imgdn.src;
		        }
		        else {
			        obj[j].style.display = 'none';
			        if (img[j] != null && imgup != null && imgdn != null) img[j].src = imgup.src;
		        }
		    }
		}
		else {
	        if (obj.style.display == 'none') {
		        obj.style.display = 'block';
		        if (img != null && imgup != null && imgdn != null) img.src = imgdn.src;
	        }
	        else {
		        obj.style.display = 'none';
		        if (img != null && imgup != null && imgdn != null) img.src = imgup.src;
	        }
		}
	}
}

function _BMT__disabled(sObjName, sCondObjName, sDisObjName, sValDisObjName) {	
	obj = document.all(sObjName);
	condobj = document.all(sCondObjName);
	disobj = document.all(sDisObjName);
	valdisobj = document.all(sValDisObjName);
	if (obj != null && condobj != null && disobj != null) {
		if (obj.value == condobj.value)	{
			disobj.disabled = false;
			disobj.value = valdisobj.value;
		}
		else {
			disobj.disabled = true;
			disobj.value = '';
		}
	}
}

function _BMT__enabled(sObjName, sCondObjName, sDisObjName, sValDisObjName) {	
	obj = document.all(sObjName);
	condobj = document.all(sCondObjName);
	disobj = document.all(sDisObjName);
	valdisobj = document.all(sValDisObjName);
	if (obj != null && condobj != null && disobj != null) {
		if (obj.value == condobj.value) {
			disobj.disabled = true;
			disobj.value = '';
		}
		else {
			disobj.disabled = false;
			disobj.value = valdisobj.value;
		}
	}
}

function _BMT__movereturn(sFormName, sAction) {
	var fform = document.forms(sFormName);
	fform.action = sAction;
	fform.submit();
}

function _BMT__nullvalue() {
	var argum = _BMT__nullvalue.arguments;
	var i = argum.length; 
	if (i != null) {
		for (var j = 0; j < i; j++)
			if (argum[j].charAt(0) != "#") {
				objname = argum[j];
				obj = document.all(objname);
				if (obj != null) {
					if (obj.length != null) {
						for (x = 0; x < obj.length; x++) 
							if (obj[x].value != "" || obj[x].value != "0") {
								obj[x].value = "0";
							}
					}
					else {
						if (obj.value != "" || obj.value != "0") {
							obj.value = "0";
						}
					}
				}
			}
	}
}

function _BMT__emptyvalue() {
	var argum = _BMT__emptyvalue.arguments;
	var i = argum.length; 
	if (i != null) {
		for (var j = 0; j < i; j++)
			if (argum[j].charAt(0) != "#") {
				objname = argum[j];
				obj = document.all(objname);
				if (obj != null) {
					if (obj.length != null) {
						for (x = 0; x < obj.length; x++) 
							if (obj[x].value != "" || obj[x].value != "0") obj[x].value = "";
					}
					else {
						if (obj.value != "" || obj.value != "0") obj.value = "";
					}
				}
			}
	}
}

function _BMT__notnullvalue() {
	var argum = _BMT__notnullvalue.arguments;
	var i = argum.length;  
	for (var j = 0; j < i; j++)
		if (argum[j].charAt(0) != "#") {
			objname = argum[j];
			obj = document.all(objname);
			if (obj != null) 
				if (obj.value == "" || obj.value == "0") obj.value = "1";
		} 
}

function _BMT__switchFireEvent(strCond, strConfirm, strObjName, strEventType, strEventValue, strFormName) {
	var obj = document.all(strCond);
	if (obj != null) thisPage._fireEvent(strConfirm, strObjName, strEventType, strEventValue, strFormName);
}

function _BMT__fireEvent(strConfirm, strObjName, strEventType, strEventValue, strFormName, strPage) {	
	if (strConfirm == '' || confirm(strConfirm))
	{
		// fire client-side onbeforeserverevent hook
		if (typeof(thisPage_onbeforeserverevent) == 'function')
		{	// fire default onbeforeserverevent handler
			thisPage_onbeforeserverevent(strObjName, strEventType, strEventValue);
		}
		if (this._adviseList != null)
		{	// fire handlers in advise list
			for (var i = 1; i <= this._nLastAdviseID; i++)
			{
				var adviseType = typeof(this._adviseList[i]);
				if (adviseType == 'function')
					this._adviseList[i](strObjName, strEventType, strEventValue);
				else if (adviseType == 'string')
					eval(this._adviseList[i]);
			}
		}
		// propogate event to server if not cancelled
		if (this.cancelEvent == '0' || this.cancelEvent == 'false')
		{
			thisPage.invokeMethod(strObjName, strEventType, strEventValue, strFormName, strPage);
		}
	}
	this.cancelEvent = false;
}

function _BMT__swap(sVal) {	
	obj = window.event.srcElement;
	if (obj != null) {
		switch (obj.tagName) {
		case "IMG":
			if (obj.src != sVal) obj.src = sVal;
			break;
		case "FONT":
			if (obj.color != sVal) obj.color = sVal;
			break;
		case "AREA":
			var s = "#" + obj.parentElement.name;
			for (var i = 0; i < document.images.length; i++) {
				var oImg = document.images(i);
				if (s == oImg.useMap){
					if (oImg.src != sVal) oImg.src = sVal;
				}
			}
			break;
		default:
			if (obj.style != sVal) obj.style = sVal;
		}
	}
}

function _BMT__setstyle(sVal) {	
	obj = window.event.srcElement;
	if (obj != null) {
		switch (obj.tagName) {
		case "FONT":
			switch (sVal) {
				case "none":
					obj.style.textDecorationNone = true;
					break;
				case "underline":
					obj.style.textDecorationUnderline = true;
					break;
			}
		}
	}
}

function  _BMT__fredirect(sURL) {
	alert('Redirect');
	window.location.href = sURL;
}

function _BMT__fsettimeout(sFunction, sTime) {
	alert('Set timeout');
	TimeoutID=window.setTimeout(sFunction, sTime); 
}

function MM_preloadImages() {
  if (document.images) {
    var imgFiles = MM_preloadImages.arguments;
    if (document.preloadArray == null) document.preloadArray = new Array();
    var i = document.preloadArray.length;
    with (document) {
		for (var j = 0; j < imgFiles.length; j++) {
			if (imgFiles[j].charAt(0) != "#") {
				preloadArray[i] = new Image;
				preloadArray[i++].src = imgFiles[j];
			}
		} 
	}
  }
}

function _BMT_CheckNumeric(TheString) {
	var ret;
	ret = new String(parseFloat(this.ChangeComma(TheString)));
	if (ret != "NaN") {
	    ret = this.ChangeDot(ret);
	    return ret;
	}
	else return "0";
}

function _BMT_ChangeComma(TheString) {
	var loc;
	var len;
	var ChComma='';
	if(TheString!='') {
	    len = TheString.length;
	    loc = TheString.indexOf(",");
	    if(loc > 0) ChComma = TheString.substring(0, loc) + "." + TheString.substring(loc + 1, len);
	    else ChComma = TheString;
	}
 	return ChComma;
}

function _BMT_ChangeDot(TheString) {
	var loc;
	var len;
	var ChDot;
	len = TheString.length;
	loc = TheString.indexOf(".");
	if(loc > 0) ChDot = TheString.substring(0, loc) + "," + TheString.substring(loc + 1, len);
	else ChDot = TheString;
 	return ChDot;
}

function _BMT_MoneyFormat(TheSumma) {
	var loc;
	var len;
	var sum;
	sum = new String(Math.round(TheSumma * 100) / 100);
	len = sum.length;
	loc = sum.indexOf(".");
	if (loc == -1 ) {
		sum = sum + ",00";
		return sum;
	}
	else {
		if (loc == len - 3) return sum = this.ChangeDot(sum);
		else sum = this.ChangeDot(sum) + "0";
		return sum;
	}
}

function formatprice(strValue) {
	var oObject = document.all(strValue);
	var sl;
	if (oObject != null) {
		  sl = thisPage.ChangeComma(thisPage.CheckNumeric(oObject.value));
		  if (sl == "") sl = "0";
		  sli = parseFloat(sl);
		  oObject.value = thisPage.MoneyFormat(sli);	
	}
}

function formatquant(strValue) {
	var oObject = document.all(strValue);
	var sl;
	if (oObject != null) {
		  sl = thisPage.ChangeComma(thisPage.CheckNumeric(oObject.value));
		  if (sl == "") sl = "0";
		  sli = parseInt(sl);
		  oObject.value = sli;	
	}
}

function _BMT_ChangeDate(NameFlDate) {
	var regFields = new Array();
	var sObj = document.all[NameFlDate];
	if (sObj != null) {
	    if (sObj.readOnly == false && sObj.disabled == false) {
	        var sReturnDate = sObj.value;
	        regFields = window.showModalDialog("AdmPages/ChdateModal.htm", "Календарь", "dialogWidth:16em; dialogHeight:17em; scroll:no; resizable:no; status:no;");
	        if(regFields!=null){
		        if(regFields["Return"] == "OK")	{
			        sReturnDate = regFields["d"]+"/"+regFields["m"]+"/"+regFields["y"];
			        sObj.value = sReturnDate;
        			
		        }
	        }
	    }
	    else alert('Поле закрыто для редактирования.');
	}
}

function _BMT__modalwindow(url, name, features) {	
	window.showModalDialog(url, name, features);
}

function NavigateToUrl(sUrl) {
    if (this.divname!='') opendiv('WAITMSG');
	setfilter('BODYDIV');
	location.assign(sUrl);
}

function opendiv(name,W,H) {	
	var objdiv = document.all(name);
	if(W==null) W=300;
	if(H==null) H=60;
	if (objdiv != null) {
		switch (objdiv.style.display) {
			case "none": {
				objdiv.style.display="block";
				var X=Math.round(document.body.clientWidth/2);
				var Y=Math.round(document.body.clientHeight/2+document.body.scrollTop);
				objdiv.style.height=H;
				objdiv.style.top=Y-(H/2);
				objdiv.style.width=W;
				objdiv.style.left=X-(W/2);
				objdiv.style.visibility="visible";
				objdiv.style.zIndex=1;
				break;
			}
			case "block": {
				objdiv.style.display="none";
				break;
			}
		}
	}
}
function setfilter(sBodyDiv) {
    var ofield = document.all(sBodyDiv);
    if (ofield != null) {
		ofield.style.filter+="progid:DXImageTransform.Microsoft.Alpha(Opacity=50,FinishOpacity=0,Style=0)";
	}
}
function resetfilter(sBodyDiv) {
    var ofield = document.all(sBodyDiv);
    if (ofield != null) {
		ofield.style.filter="";
	}
}
function switchdiv(sOpenField, sCloseField) {
    var ofield = document.all(sOpenField);
    var cfield = document.all(sCloseField);
    if (ofield != null && cfield != null) {
		ofield.style.display="block";
		cfield.style.display="none";
	}
}
function ParentDiv(el) {
	if(el.parentElement.tagName=="DIV") {
		return(el.parentElement)
	}
	else {
		return(ParentDiv(el.parentElement))
	}
}
function ParentBody(el) {
	if(el.parentElement.tagName=="BODY") {
		return(el.parentElement)
	}
	else {
		return(ParentBody(el.parentElement))
	}
}
function getSelectTitle() {
    var coll = window.event.srcElement;
    if(coll != null) {
        if(coll.tagName=="SELECT") {
            var opt = coll.options;
            if (opt.length>0) {
                for (i=0; i< opt.length; i++) {
                    if(opt[i].selected) {
                        coll.title=opt[i].text;
                        break;
                    }
                }
            }
        }
    }
}
