function fieldColorSet(id)
{
 document.getElementById(id).style.backgroundColor = "#ced7f9";
}

function fieldColorOut(id)
{
 document.getElementById(id).style.backgroundColor = "white";
}

function cFieldSet(fieldId,numSubField)
{
 var check = document.getElementById("CField[" + fieldId + "]").checked;
 if(check) for(i=1;i<=numSubField;i++) document.getElementById("CSubField[" + fieldId + "][" + i + "]").checked = true;
 else for(i=1;i<=numSubField;i++) document.getElementById("CSubField[" + fieldId + "][" + i + "]").checked = false;	
}

function cFieldSetMain(fieldId,subFieldId,fromSubField,numSubField)
{
 var check = document.getElementById("CSubField[" + fieldId + "][" + (subFieldId - fromSubField + 1) + "]").checked;
 if(check) document.getElementById("CField[" + fieldId + "]").checked = true;
 else 
	{
	 var checkField = false;
	 for(i=1;i<=numSubField;i++) if(document.getElementById("CSubField[" + fieldId + "][" + i + "]").checked) checkField = true;
	 if(!checkField) document.getElementById("CField[" + fieldId + "]").checked = false;
	}
}

function cOkrugSet(countryId,numOkrug,numRegion)
{
 var check = document.getElementById("CCountry[" + countryId + "]").checked;
 if(check) 
	{
	 for(i=0;i<numOkrug;i++) document.getElementById("COkrug[" + countryId + "][" + i + "]").checked = true;
	 for(i=0;i<numRegion;i++) document.getElementById("CRegion[" + countryId + "][" + i + "]").checked = true;
	}
 else 
 	{
	 for(i=0;i<numOkrug;i++) document.getElementById("COkrug[" + countryId + "][" + i + "]").checked = false;
	 for(i=0;i<numRegion;i++) document.getElementById("CRegion[" + countryId + "][" + i + "]").checked = false;
	}
}

function cRegionSet(countryId,okrugId,fromRegion,numRegion,numAllRegion)
{
 var check = document.getElementById("COkrug[" + countryId + "][" + okrugId + "]").checked;
 if(check) 
	{
	 document.getElementById("CCountry[" + countryId + "]").checked = true;
	 for(i=fromRegion;i<fromRegion+numRegion;i++) document.getElementById("CRegion[" + countryId + "][" + i + "]").checked = true;
	}
 else 
 	{	 
	 for(i=fromRegion;i<fromRegion+numRegion;i++) document.getElementById("CRegion[" + countryId + "][" + i + "]").checked = false;
	 var checkRegion = false;
	 for(i=0;i<numAllRegion;i++) if(document.getElementById("CRegion[" + countryId + "][" + i + "]").checked) checkRegion = true;
	 if(!checkRegion) document.getElementById("CCountry[" + countryId + "]").checked = false;
	} 
}

function cRegionSetMain(countryId,okrugId,regionId,fromRegion,numRegion,numAllRegion)
{
 var check = document.getElementById("CRegion[" + countryId + "][" + regionId + "]").checked;
 if(check) 
	{
	 document.getElementById("CCountry[" + countryId + "]").checked = true;
	 document.getElementById("COkrug[" + countryId + "][" + okrugId + "]").checked = true;
	}
 else 
 	{	 
	 var checkRegion = false;
	 var checkOkrug = false;
	 for(i=fromRegion;i<fromRegion+numRegion;i++) if(document.getElementById("CRegion[" + countryId + "][" + i + "]").checked) checkOkrug = true;
	 if(!checkOkrug) document.getElementById("COkrug[" + countryId + "]["+ okrugId +"]").checked = false;	 
	 for(i=0;i<numAllRegion;i++) if(document.getElementById("CRegion[" + countryId + "][" + i + "]").checked) checkRegion = true;
	 if(!checkRegion) document.getElementById("CCountry[" + countryId + "]").checked = false;
	} 
}


function openWinCentered(loc, winname, widthh, heightt, params)
{
    var tp = Math.ceil((screen.height - heightt) / 2);
    var lf = Math.ceil((screen.width - widthh) / 2);
    if (params.length > 0)
	    params = "," + params;

    var win = window.open(loc, "_blank", "width=" + widthh + ",height=" + heightt + ",top=" + tp + ",left=" + lf + params);
    win.focus();
    return win;
}

function addFile()
{
	var div = document.createElement("div");
	div.innerHTML = "<div><input type=\"file\" name=\"file0[]\" size=\"30\" />&nbsp;<input name=\"buttonDel\" value=\"Отменить\" type=\"button\" onclick=\"javascript:delFile(this);\" /></div>";
	document.getElementById("filesField").appendChild(div);
}

function delFile(a)
{
	var contDiv = a.parentNode;
	contDiv.parentNode.removeChild(contDiv);
}

var lotNum = 0;

function addLot()
{
	var div = document.createElement("div");
	lotNum++;
	div.innerHTML = "<div style='border-top: 4px solid #dddddd'><table><tr><td align='center' class='l'><em>Лот</em></td><td><input name='buttonDel' value='Удалить' type='button' onclick='javascript:delLot(this);' /></td></tr><tr><td style='border:0'>Предмет контракта&nbsp;<span style='color:red;'>*</span>&nbsp;</td><td style='border:0'><textarea name='lot_name[]' style='width:400px' rows='3'></textarea></td></tr><tr><td style='border:0'>Цена лота&nbsp;</td><td style='border:0'><input type='text' name='lot_cost[]' size='58' value=''>&nbsp;<em>руб.</em></td></tr><tr><td align='center' style='border:0'>&nbsp;</td><td style='border:0' class='l'><em>Позиции лота</em>&nbsp;<input name='buttonAdd' value='Добавить' type='button' onclick='javascript:addPos("+lotNum+");' /></td></tr><tr><td align='center' style='border:0'>&nbsp;</td><td style='border:0;padding-left:45px'><div id='possField"+lotNum+"'></div></td></tr></table></div>";
	document.getElementById("lotsField").appendChild(div);	
}

function delLot(a)
{
	var contDiv = a.parentNode.parentNode.parentNode.parentNode.parentNode;
	contDiv.parentNode.removeChild(contDiv);
}

function addPos(i)
{
	var div = document.createElement("div");
	div.innerHTML = "<div style='border-top: 1px solid #dddddd'>Позиция&nbsp;<span style='color:red;'>*</span>&nbsp;<input type='text' name='pos_name["+(i-1)+"][]' size='31' value=''>&nbsp;<input name='buttonDel' value='Удалить' type='button' onclick='javascript:delPos(this);' /><br />Количество&nbsp;<input type='text' name='pos_num["+(i-1)+"][]' size='10' value=''><br />Ед.&nbsp;измерения&nbsp;<input type='text' name='pos_num_type["+(i-1)+"][]' size='7' value=''></div>";
	document.getElementById("possField"+i).appendChild(div);
}

function delPos(a)
{
	var contDiv = a.parentNode;
	contDiv.parentNode.removeChild(contDiv);
}

function openField()
{

	var a = document.getElementById('fieldsField').style.display;	
	if('none' == a) 
		{
		 document.getElementById('fieldsField').style.display = '';
		 document.images["plusField"].src = '../img/minus.gif';
		}
	else 
		{
		 document.getElementById('fieldsField').style.display = 'none';
		 document.images["plusField"].src = '../img/plus.gif';
		}
}

function openFieldNew(field)
{
	
	var a = document.getElementById(field).style.display;	
	if('none' == a) 
		{
		 document.getElementById(field).style.display = '';
		 document.images[field].src = '../img/minus.gif';
		}
	else 
		{
		 document.getElementById(field).style.display = 'none';
		 document.images[field].src = '../img/plus.gif';
		}
}

function openField2()
{
	var a = document.getElementById('fieldsField2').style.display;	
	if('none' == a) 
		{
		 document.getElementById('fieldsField2').style.display = '';
		 document.images["plusField2"].src = '../img/minus.gif';
		}
	else 
		{
		 document.getElementById('fieldsField2').style.display = 'none';
		 document.images["plusField2"].src = '../img/plus.gif';
		}
}

var xmlHttp = createXmlHttpRequestObject();

// кэш с запросами к серверу на проверку
var cache = new Array();

// создание экземпляра объекта XMLHttpRequest
function createXmlHttpRequestObject()
	{
	 var xmlHttp;
	 
	 try // для всех нормальных браузеров
		{
		 xmlHttp = new XMLHttpRequest();
		}
	 catch(e) // если IE
		{
		 var XmlHttpVersions = new Array("Microsoft.XMLHTTP",
										 "MSXML2.XMLHTTP.6.0",
										 "MSXML2.XMLHTTP.5.0",
										 "MSXML2.XMLHTTP.4.0",
										 "MSXML2.XMLHTTP.3.0",
										 "MSXML2.XMLHTTP");
		 for(var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
			{
			 try
				{
				 xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
				}
			 catch(e) {}
			}
		}
	 
	 if(xmlHttp) return xmlHttp;
	 else alert("Ошибка");
	}
	
	function loadData(getPost,link)
	{
	 if(xmlHttp)
		{
		 if(link) cache.push(link);
		 if(!getPost) getPost = "GET";
		 
		 try // попытка установить соединение с сервером
			{
			 // продолжать только если объект XMLHttpRequers не занят и кэш не пуст
			 if((4 == xmlHttp.readyState || 0 == xmlHttp.readyState) && cache.length > 0)
				{
				 var cacheElement = cache.shift();
				 
				 xmlHttp.open(getPost, cacheElement, true);
				 xmlHttp.onreadystatechange = handleRequestStateChange;
				 xmlHttp.send(null);
				}
			}
		 catch(e) {}
		}
	}

// эта функция обслуживает ответы
function handleRequestStateChange()
	{
	 // когда readyState == 4, мы можем прочитать ответ сервера
	 if(4 == xmlHttp.readyState)
		{
		 if(200 == xmlHttp.status)
			{
			 var response = xmlHttp.responseText;
			 
			 response = response.slice(response.indexOf(">")+1);
			 response = response.slice(0,response.lastIndexOf("<"));
			 
			 setFieldId = response.slice(0,response.indexOf("</fieldTD"));
			 setFieldId = setFieldId.slice(setFieldId.indexOf(">")+1);
			 
			 response = response.slice(response.indexOf("</fieldTD>")+10);
			 
			 document.getElementById(setFieldId).innerHTML = response;
			 
			 // вызывать loadData() еще раз, на случай, если кэш не пуст 
			 setTimeout("loadData('GET')",500);
			}
		}
	}


var calendarShowStatus = false;
	
function calendarShow(inputField)
	{
	 documentRegisterEvents();
	 
	 var getPost = "GET";
	 loadData(getPost,'../js/calendar.php?inputField='+inputField);
	 
	 var object = document.getElementById(inputField);
	 var left = object.offsetLeft;
	 var top = object.offsetTop;
	 var parent = object.offsetParent;
	 //while(parent && parent.tagName != "BODY")
	 while(parent)
		{
		 left += parent.offsetLeft + parent.clientLeft;
		 top += parent.offsetTop + parent.clientTop;
		 parent = parent.offsetParent;
		}
	 
	 top += 19;
	 var a = top+"px";
	 var b = left+"px";
	 document.getElementById('Calendar').style.top = a;
	 document.getElementById('Calendar').style.left = b;
	 document.getElementById('Calendar').style.visibility = 'visible';
	 
	 calendarShowStatus = true;
	}
	
function calendarHide()
	{
	 document.getElementById('Calendar').style.visibility = 'hidden';
	 calendarShowStatus = false;
	}
	
function setDate(date,inputField)
	{	 
	 document.getElementById(inputField).value = date;
	 document.getElementById('Calendar').style.visibility = 'hidden';
	}

function calendarMonth(inputField,nextMonth)
	{
	 var getPost = "GET";
	 loadData(getPost,'../js/calendar.php?inputField='+inputField+'&nextMonth='+nextMonth);
	}
	
function documentRegisterEvents()
	{
	 document.onkeypress = function calendarHide_trap1() 
		{
		 if (event.keyCode == 27) calendarHide();
		}
	 
	 document.onclick = function calendarHide_trap2()
		{
		 if (!calendarShowStatus) calendarHide();
		 calendarShowStatus = false;
		}
	}