function checkSiteLoginForm()
{
	p = document.getElementById('id_sitelogin');
	if(p && p.value=="")
	{
		alert('Введите логин для входа на сайт');
		p.className = "required";
		p.focus();
		return false;
	}
	return true;
}

function checkLoginForm()
{
	p = document.getElementById('id_auth_login');
	if(p && p.value=="")
	{
		alert('Введите логин для входа на сайт');
		p.className = "required";
		p.focus();
		return false;
	}
	return true;
}

function checkLoginForm2()
{
	p = document.getElementById('id_auth_login2');
	if(p && p.value=="")
	{
		alert('Введите логин для входа на сайт');
		p.className = "required";
		p.focus();
		return false;
	}
	return true;
}

function validEmail(email)
{
	return (/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/).test(email);
}

function delobj_ok(id)
{
	document.getElementById('obj_'+id).style.display='none';
}

function deladvert_ok(id, catid, typeid)
{
	window.location.href='realty/my/c'+catid+'/type_id'+typeid;
}

function showEl(el)
{
	p = document.getElementById(el);
	if(p) p.style.display = 'block';
}

function sh(el)
{
	p = document.getElementById(el);
	if(p) p.style.display = p.style.display == 'none' ? 'block' : 'none';
}

function hideEl(el)
{
	p = document.getElementById(el);
	if(p) p.style.display = 'none';
}

function hlBtn(btnID, btnState)
{	
	document.getElementById("btnl_"+btnID).className = "blbtn_left"+btnState;
	document.getElementById("btnr_"+btnID).className = "blbtn_right"+btnState;
}

function format_price(price)
{
	string = price+" ";
	len = string.length;
	str = "";
	len--;
	for(i=0;i<len;i+=3)
	{
		from = len-i-3;
		cc=3;
		if(from<0) {cc = 3+from;from=0;}
		s2 = string.substring(from, from+cc);
		str = str ? s2+" "+str : s2;
	}
	return str;
}

function setLocationOptions(type)
{
	// get oblast div
	poblast = document.getElementById('location_oblast');
	// get city div
	pcity = document.getElementById('location_city');
	// get location_region
	pregion = document.getElementById('location_region');
	// get location_pkievoblregion
	pkievoblregion = document.getElementById('location_kievoblregion');
	// get kievregion div
	pkievregion = document.getElementById('location_kievregion');
	// get kievmetro div
	pkievmetro = document.getElementById('location_metro');
	pautometro = document.getElementById('id_autometro');

	poblast.style.display = type!=2 ? "none" : "block";
	pcity.style.display = type==0 ? "none" : "block";
	pkievmetro.style.display = /*type!=0*/true ? "none" : "block";
	pautometro.style.display = type!=0 ? "none" : "block";
	pkievregion.style.display = type!=0 ? "none" : "block";
	pregion.style.display = type==2 ? "block" : "none";
	pkievoblregion.style.display = type==1 ? "block" : "none";
}

function checkCallbackForm()
{
	p = document.getElementById('id_callback_text');
	if(p && p.value=="")
	{
		alert('Введите текст сообщения');
		p.className = "required";
		p.focus();
		return false;
	}
	p = document.getElementById('id_callback_mail');	
	if(p && (p.value=="" || !validEmail(p.value)))
	{
		alert('Введите корректный e-mail');
		p.className = "required";
		p.focus();
		return false;
	}
	return true;
}

function quoteComment(id)
	{
		p = document.getElementById('id_comment_'+id);
		if(!p)
			return;
		p_usr = document.getElementById('id_comment_user_'+id);
		if(p_usr)
			usr = p_usr.innerHTML;
		p2 = document.getElementById('id_comment_text');
		if(!p2)
			return;
		if(p2.value) p2.value+="\n\n";
		p2.value += "[quote_user]"+usr+"[/quote_user][quote]"+p.innerHTML+"[/quote]\n";
	}


function focusById(id)
{
	p = document.getElementById(id);
	if(p) p.focus();
}

function openclose(img, el)
{
	p = document.getElementById(el);
	if(p) p.style.display = p.style.display == 'none' ? 'block' : 'none';

	img.src = p.style.display == 'none' ? "img/plus.gif" : "img/minus.gif";
}


	var ms = Array();  // metro ids array to send before showing popup
	var ds = Array();  // district ids, the same

	function loadMDPanel(mIDs, dIDs){	
		sendMVal = "";
		sendDVal = "";
		
		for(i=0; i<mIDs.length; i++) sendMVal += mIDs[i]+",";
		for(i=0; i<dIDs.length; i++) sendDVal += dIDs[i]+",";	
			
		document.getElementById("md_popup_bg").style.display = "block";
		document.getElementById("md_popup").style.display = "block";
		var so = new SWFObject("panel.swf", "mdpanel", "961", "640", "8", "white");
		so.addParam("wmode", "transparent");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("dInput", sendDVal);
		so.addVariable("mInput", sendMVal);
		so.write("mdmap");
	}

	function closeMDPanel(newMS, newDS, saveData){	
		if (saveData){
			ms = newMS;    // update metro ids on APPLY btn
			ds = newDS;		 // update districts on APPLY btn
			updateRegionsAndMetroIDS(newMS, newDS);
		}	
		
		// just close da fakin panel on CLOSE btn
		document.getElementById("mdmap").innerHTML = "";
		document.getElementById("md_popup").style.display = "none";
		document.getElementById("md_popup_bg").style.display = "none";
	}


function nl2br(s) {
    s = s.split("\u000A").join("<br />\u000A");
    return s;
}

function str_replace(str, strold, strnew)
{
	i=0;
	while(str.indexOf(strold)>=0 && i<100)		
	{
		str = str.replace(strold, strnew);
		i++;
	}
	return str;
}

function vote(id)
{
	var value = 0;
	var custom_value = "";
	for(i=0;i<poll_answers.length;i++)
	{
		a = $('poll_'+id+'_'+poll_answers[i]);
		if(a && a.checked)
		{
			value = a.value;
			if($('id_poll_'+id+'_'+poll_answers[i]+'_custom'))
			{
				custom_value = $('id_poll_'+id+'_'+poll_answers[i]+'_custom').value;
				if(custom_value=='')
				{
					alert('Укажите свой вариант ответа');
					return false;
				}
			}
		}
	}

	if(value)
	{
		$('poll_answers').innerHTML = "<img src='img/ajax-loader.gif' /><br/>";
		
		new Ajax.Request('action.php?vote='+id+'&answer='+value+'&custom='+custom_value,
		{
			method:'get',
			onSuccess: function(transport){
				$('poll_block').innerHTML = transport.responseText;
			}
		});
	}

	return false;
}
