var nitemphotoa='';

function SfindObj(n, d) 
{	//v4.01
	var p, i, x;
	if (!d) 
		d = window.document; 
	if ((p = n.indexOf("?") ) > 0 && parent.frames.length) 
	{
		d = parent.frames [n.substring(p + 1)].document; 
		n = n.substring(0, p);
	}
	if (!(x = d [n]) && d.all) 
		x = d.all [n]; 
	for (i = 0; !x && i < d.forms.length; i ++) 
		x = d.forms [i] [n];
	for (i = 0; !x && d.layers && i < d.layers.length; i ++)
		x = SfindObj(n, d.layers [i].document);
	if(!x && d.getElementById) 
		x = d.getElementById(n);
	return x;
}

function showtab(page)
{
	var spage = 'indextab1bm'+page;
	for (var i = 0; i < zakl.length; i++)
	{
		obj=SfindObj(zakl[i]);
		if(spage == zakl[i])
		{
			obj.style.visibility = 'hidden';
			obj.style.position = 'absolute';
		}
		else
		{
			obj.style.visibility = 'visible';
			obj.style.position = 'static';
		}
	}
	obj=SfindObj('indextabcontent');
	obj1=SfindObj('indextab'+page+'content');
	obj.innerHTML = obj1.innerHTML;
	return false;
} 


function SwitchBm(nNum, nCount)
{
	for (var i = 1; i <= nCount; i++)
	{
		obj=SfindObj('bmc'+i);
		if(obj)
		{
			obj.style.visibility = 'hidden';
			obj.style.position = 'absolute';
		}
		obj=SfindObj('bm'+i);
		if(obj)
			obj.className = 'item';
	}
	obj=SfindObj('bmc'+nNum);
	if(obj) {
		obj.style.visibility = 'visible';
		obj.style.position = 'static';
	}
	obj=SfindObj('bm'+nNum);
	if(obj)
		obj.className = 'itema';
	return false;
} 

function ShowProd (wwidth,wheight,isrc,modelid) {
	if(isrc=='/') return;
	
	obj1=SfindObj('preview');
	obj2=SfindObj('previewimg');
	obj3=SfindObj('previewtbl');
	obj4=SfindObj('loading');
	
	if (modelid >=0 ) {
		objLoad=SfindObj("loaddata");
		objLoad.innerHTML='';
		objLoad.style.display='none';
	}
	
	obj2.src='/i/dot.gif';
	wheight = wheight*1+20;
	obj1.style.width=wwidth+'px';
	obj1.style.height=wheight+'px';
	
	obj3.style.width=wwidth+'px';
	obj3.style.height=wheight+'px';
	
	obj4.style.width=wwidth+'px';
	obj4.style.height=wheight+'px';

	CenterLayer('preview',wwidth,wheight,window.document);
	obj1.style.visibility='visible';

	obj2.src=isrc;

	CheckScrollers ();
}

function HideProd () {
	obj1=SfindObj('preview');
	obj2=SfindObj('previewimg');
	objLoad=SfindObj("loaddata");
	objLoad.innerHTML='';
	objLoad.style.display='none';
	obj2.src='/i/dot.gif';
	obj1.style.visibility='hidden';
	
	CheckScrollers ();
}


function CenterLayer (layer,width,height,target,xoffset,yoffset) {
	if (!target)
		target=window.document;
	
	if (!xoffset)
		xoffset = 0;
		
	if (!yoffset)
		yoffset = 0;

	leftLayer = ((target.body.clientWidth - width)/2);
	topLayerABS = (target.body.clientHeight - height)/2;

	topLayer=target.body.scrollTop;
	
	newTop = topLayer + topLayerABS;
	if (newTop < 0)
		newTop=0;
		
	nTop = topLayer + topLayerABS + yoffset;
	nLeft = leftLayer + xoffset;
	
	if (nTop < target.body.scrollTop)
		nTop = target.body.scrollTop + 5;
	
	target.getElementById(layer).style.left = nLeft + 'px';
	target.getElementById(layer).style.top= nTop + 'px';
}

function ShowOrHide (sObject) {
	obj = SfindObj(sObject);
	if (obj) {
		if (obj.style.display != 'block')
			obj.style.display = 'block';
		else
			obj.style.display = 'none';
	}
}

function ShowOrHideV (sObject) {
	obj = SfindObj(sObject);
	
	if (obj) {
		if (obj.style.visibility != 'hidden')
			obj.style.visibility = 'hidden';
		else
			obj.style.visibility = 'visible';
	}
}

function Hide (sObject) {
	obj = SfindObj(sObject);
	if (obj)
		obj.style.display = 'none';
}

function HideClear (sObject) {
	obj = SfindObj(sObject);
	if (obj) {
		obj.innerHTML = '';
		//obj.style.visibility = 'hidden';
		obj.style.left = '-5000px';
		obj.style.top = '-5000px';
	}
	CheckScrollers ();
}

function ShowTitleSelector (nRubricId) {
	aThisRubricTitles = aRubricsAndTitles[nRubricId];
	if (aThisRubricTitles.length)
		window.document.forms['formitem'].titleid.disabled = false;
	else
		window.document.forms['formitem'].titleid.disabled = true;

	window.document.forms['formitem'].titleid.options.length = 0;
	for (i = 1; i <= aThisRubricTitles.length; i++) {
		if (aThisRubricTitles[i])
			window.document.forms['formitem'].titleid.options[i] = new Option (aThisRubricTitles[i],aThisRubricTitles[i]);
	}
	
	aThisRubricBrands = aRubricsAndBrands[nRubricId];
	aThisRubricBrandsId = aRubricsAndBrandsId[nRubricId];
	if (aThisRubricBrands.length)
		window.document.forms['formitem'].manufacturerid.disabled = false;
	else
		window.document.forms['formitem'].manufacturerid.disabled = true;

	window.document.forms['formitem'].manufacturerid.options.length = 0;
	for (i = 1; i <= aThisRubricBrands.length; i++) {
		if (aThisRubricBrands[i])
			window.document.forms['formitem'].manufacturerid.options[i] = new Option (aThisRubricBrands[i],aThisRubricBrandsId[i]);
	}
	
	
	
}

function SwitchBookMark(sObjectName,nToSet,nLength) {
	for (i = 1; i <= nLength; i++) {
		obj = SfindObj(sObjectName + i);
		if (obj) {
			obj.style.visibility='hidden';
			obj.style.position='absolute';
		}
		
		obj = SfindObj(sObjectName + 'a' + i);
		if (obj)
			obj.className = 'bmna';
	}
	obj = SfindObj(sObjectName + nToSet);
	if (obj) {
		obj.style.visibility='visible';
		obj.style.position='static';
		obj.style.display = 'block';
	}
	
	obj = SfindObj(sObjectName + 'a' + nToSet);
	if (obj)
		obj.className = 'bma';
}

function ActivateBookMark(sObjectName,nLength) {
	for (i = 1; i <= nLength; i++) {
		obj = SfindObj(sObjectName + i);
		if (obj)
			obj.style.display='block';
	}
	obj = SfindObj('itemform8');
	obj.style.display = 'none';
}

var imgcount=0;
var reviewcount=0;

function CreateImageBrowse (imglstobj) {
	place = document.getElementById(imglstobj);
	
	div = document.createElement("div");
	div.setAttribute('id', 'imgbrowsediv_'+imgcount);
	place.appendChild(div);
	
	browse = document.createElement("input");
	browse.setAttribute("type", "file");
	browse.setAttribute("name", "img_"+imgcount);
	browse.className = "file";
	
	httpbrowse = document.createElement("input");
	httpbrowse.setAttribute("type", "text");
	httpbrowse.setAttribute("name", "httpimg_"+imgcount);
	
	comment = document.createElement("input");
	comment.setAttribute("type", "text");
	comment.setAttribute("name", "comment_"+imgcount);
	
	num = document.createElement("div");
	num.setAttribute("className", "imglistnum");
	num.innerHTML = (imgcount+1) + '.';

	div.appendChild(num);
	div.appendChild(browse);
	div.appendChild(httpbrowse);
	div.appendChild(comment);
	
	browse.focus();
	imgcount++;
}


function CreateReviewInput (imglstobj) {

	var a = document.getElementsByTagName("div");
	j=1;
	for(i=0; i < a.length; i++) {
		if(a[i].className && a[i].className.indexOf("reviewinput") != -1)
			j++;
   }
   if (j > 5)
   	return;
	
	place = document.getElementById(imglstobj);
	
	div = document.createElement("div");
	div.setAttribute('id', 'reviewdiv_'+reviewcount);
	place.appendChild(div);
	
	comment = document.createElement("input");
	comment.setAttribute("name", "review_"+reviewcount);
	comment.className = "vlinput";
	comment.setAttribute("class", "vlinput");

	div.appendChild(comment);
	
	comment.focus();
	reviewcount++;
}


function IsSpaces (sStr) {
	sStr = "" + sStr;
	for (i = 0; i < sStr.length; i ++)
		if (sStr.substring (i, i + 1) != " ")
			return false;
	return true;
}

function WithNumbers (sStr) {
	sStr = '' + sStr;
	for (i = 0; i < sStr.length; i ++)
	{
		ch = sStr.substring (i, i + 1); 
		if (ch >= '0' && ch <= '9')
			return true;
	}
	return false;
}

function ValidateSendForm (formname,aFields) {
	f = window.document.forms[formname];
	
	for (i = 0; i < aFields.length; i++) {
		
		if (((f.elements[aFields[i]].getAttribute('type') == 'select-one') && (f.elements[aFields[i]].selectedIndex == 0) && (f.elements[aFields[i]].style.display != 'none')) 
		|| 
		((f.elements[aFields[i]].getAttribute('type') == 'text') && (trim(f.elements[aFields[i]].value,' ') == '') && (f.elements[aFields[i]].style.display != 'none'))
		|| 
		((f.elements[aFields[i]].getAttribute('type') == 'password') && (trim(f.elements[aFields[i]].value,' ') == '') && (f.elements[aFields[i]].style.display != 'none'))
		|| 
		((f.elements[aFields[i]].getAttribute('type') == 'textarea') && (trim(f.elements[aFields[i]].value,' ') == '') && (f.elements[aFields[i]].style.display != 'none'))
		|| 
		((f.elements[aFields[i]].getAttribute('type') == null) && (trim(f.elements[aFields[i]].value,' ') == '') && (f.elements[aFields[i]].style.display != 'none'))
		) {
			alert('Не заполнено обязательное поле!');
			try {
				f.elements[aFields[i]].focus();
			}
			catch(e) {}
			return false;
		}
	}

	obj=SfindObj('blinks');
	
	if (obj && (obj.innerHTML == 'Заметок нет') && (formname != 'formzametka')) {
		alert('Не заполнена ни одна заметка!');
		return false;
	}

	if ((formname != 'formzametka') && document.forms['formzametka'] && (document.forms['formzametka'].title.value || document.forms['formzametka'].zametka.value || document.forms['formzametka'].keywords.value)) {
		alert('Не забудьте сохранить заметку!');
		return false;
	}

	return true;
}



function trim(s,what) {
	if (!s)
		return;
	while (s.substring(0,1) == what) {
	s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == what) {
	s = s.substring(0,s.length-1);
	}
	return s;
}

function ZeroBefore (nVal) {
	if (nVal < 10)
		nVal='0'+nVal;
	return nVal;
}


var nInterval = 0;
var bAcceptBg = false;

function BackGroundPost () {
	
}


function collapseall (objid) {
	if (!objid)
		objid = '';
	obj = document.getElementsByTagName('div');	
	for (var i = 0; i < obj.length; i++) {
		 if (obj[i].id.indexOf("post_"+objid) != -1)  {
			if ((obj[i].style.display == '') || (obj[i].style.display == 'block'))
				obj[i].style.display = 'none';
			else
				obj[i].style.display = 'block';
		 }
		
	}
}

var searchTimeOut = null;

sDisableSearch = '';

function autosearch(e, fieldname, fieldvalue, divname) {
	if (!e) e = window.event || null; if (e.altKey || e.ctrlKey) return;
	var n = e.keyCode?e.keyCode:e.charCode;	if (n == 13 || n == 11) return;

	//как стереть признак?
	if (n == 8 || n == 9 || n == 16 || n == 17 || n == 20 || n == 35 || n == 36 || n == 37 || n == 39 || n == 45 || n == 46)
		sDisableSearch = '';
	
	if (sDisableSearch == fieldname)
		return;
		
	clearTimeout(searchTimeOut);
	searchTimeOut = window.setTimeout("autosearchexecute('"+fieldname+"','"+fieldvalue+"','"+divname+"')", 1000);
}

function autosearchexecute (fieldname, formname, divname) {
	AjaxSendRequest('/ajax/' + fieldname + 
				'search/', 
				'field=' + encodeURI(fieldname) + 
				'&title=' + encodeURI(window.document.forms[formname].title.value) + 
				'&fulltitle=' + encodeURI(window.document.forms[formname].fulltitle.value) + 
				'&class=' + window.document.forms[formname].category.value
				, divname, 250, 200, 300);
	obj=SfindObj('autosearch_' + fieldname);
	obj.style.visibility = 'visible';
}

function autosearch1(e, fieldname, fieldvalue, divname, value1) {
	if (!e) e = window.event || null; if (e.altKey || e.ctrlKey) return;
	var n = e.keyCode?e.keyCode:e.charCode;	if (n == 13 || n == 11) return;
	clearTimeout(searchTimeOut);
	searchTimeOut = window.setTimeout("autosearchexecute1('"+fieldname+"','"+fieldvalue+"','"+divname+"','"+value1+"')", 1000);
}

function autosearchexecute1 (fieldname, formname, divname, value1) {
	AjaxSendRequest('/ajax/' + fieldname + 
				'search', 
				'field=' + encodeURI(fieldname) + 
				'&formname=' + encodeURI(formname) + 
				'&value=' + encodeURI(value1)
				, divname, 250, 200, 300);
}


function activate (lnk,nUserId) {

	obj=SfindObj('ln_'+nUserId);if (obj) {obj.style.color='#008cd2';obj.style.borderBottom='1px dashed #008cd2';}
	obj=SfindObj('lr_'+nUserId);if (obj) {obj.style.color='#008cd2';obj.style.borderBottom='1px dashed #008cd2';}
	obj=SfindObj('lg_'+nUserId);if (obj) {obj.style.color='#008cd2';obj.style.borderBottom='1px dashed #008cd2';}
	obj=SfindObj('lb_'+nUserId);if (obj) {obj.style.color='#008cd2';obj.style.borderBottom='1px dashed #008cd2';}
	obj=SfindObj('ll_'+nUserId);if (obj) {obj.style.color='#008cd2';obj.style.borderBottom='1px dashed #008cd2';}
	obj=SfindObj('lf_'+nUserId);if (obj) {obj.style.color='#008cd2';obj.style.borderBottom='1px dashed #008cd2';}
	obj=SfindObj(lnk+'_'+nUserId);if (obj) {obj.style.color='#e86e00';obj.style.borderBottom='1px dashed #e86e00';}
}

/*function deactivate (nUserId) {
	if (bef) {
			bef.style.color='#008cd2';
			bef.style.borderBottom='1px dashed #008cd2';
		}
}*/

function tsw(num) {
	for (var i = 1; i <= 2; i++)
	{
			obj=SfindObj('tsw'+i);
			obj.className = '';
			
			obj=SfindObj('tbm'+i);
			obj.style.display = 'none';
	}
	obj=SfindObj('tsw'+num);
	obj.className = 'a';
	obj.blur();
	
	obj=SfindObj('tbm'+num);
	obj.style.display = 'block';
}

function CheckIfImageFilled () {
	if (window.document.forms['formupload']) {
		if (((window.document.forms['formupload'].httpfile.value != 'http://') && (window.document.forms['formupload'].httpfile.value != '')) || (window.document.forms['formupload'].imgupl && window.document.forms['formupload'].imgupl.value)) {
			if (confirm('Вы не разместили файлы в  фотогалерее. Все равно продолжить?'))
				return true;
			else
				return false;
		}
	}
	return true;
}

function ChangeWindowTitle (sNewTitle) {
	window.document.title = sNewTitle;
	obj=SfindObj('ajax1_1_title');obj.innerHTML=sNewTitle;
}

function CheckScrollers () {
	if (browser.isIE)
		return;
	sOverflow = 'auto';
	obj=SfindObj('ajax1');x=parseInt(obj.style.left);if (isNaN(x)) x=-5000; if (x > -4000) sOverflow = 'hidden';

	if (sOverflow == 'auto') {obj=SfindObj('ajax1_1');x=parseInt(obj.style.left);if (isNaN(x)) x=-5000; if (x > -4000) sOverflow = 'hidden';}

	if (sOverflow == 'auto') {obj=SfindObj('ajax2');x=parseInt(obj.style.left);if (isNaN(x)) x=-5000; if (x > -4000) sOverflow = 'hidden';}

	if (sOverflow == 'auto') {obj=SfindObj('ajax2_1');x=parseInt(obj.style.left);if (isNaN(x)) x=-5000; if (x > -4000) sOverflow = 'hidden';}

	if (sOverflow == 'auto') {obj=SfindObj('preview');if (obj.style.visibility && (obj.style.visibility != 'hidden')) sOverflow = 'hidden';}
	
	var divs = window.document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++) {
		if ((divs[i].className == 'tags') || (divs[i].id == 'photogalleryw')) {
			divs[i].style.overflow = sOverflow;
		}
	}
}

function findPosY(sObj)
{
	obj=SfindObj(sObj);
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

var bCaretProcess = false;
function storeCaret (textEl) 
{ 
	if (bCaretProcess)
		return;
	bCaretProcess = true;
	if (textEl.createTextRange)  
		textEl.caretPos = document.selection.createRange().duplicate(); 
	bCaretProcess = false;
}

function AddSnippet(textEl, snippet1, snippet2) 
{
	//IE support
	if (document.selection) 
	{
		textEl.focus();
		sel = document.selection.createRange();
		sel.text =  snippet1 + sel.text + snippet2;
	}
	//GECKO support
	else if (textEl.selectionStart || textEl.selectionStart == '0') 
	{
		var startPos = textEl.selectionStart;
		var endPos = textEl.selectionEnd;
		textEl.value = textEl.value.substring(0, startPos) + snippet1 + textEl.value.substring(startPos, endPos) + snippet2 + textEl.value.substring(endPos, textEl.value.length);
	}
	else 
		textEl.value += snippet1+snippet2;
}


function AddTag (sForm, sValue) {
		document.forms[sForm].keywords.value = document.forms[sForm].keywords.value + ", " + sValue;
		document.forms[sForm].keywords.value=trim(document.forms[sForm].keywords.value," ");
		document.forms[sForm].keywords.value=trim(document.forms[sForm].keywords.value,",");
		document.forms[sForm].keywords.value=trim(document.forms[sForm].keywords.value," ");
}
