myWidth = 0, myHeight = 0;
myTop = 0, myLeft = 0;

function checkWideScreen(){
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
    myTop = window.pageYOffset;
    myLeft = window.pageXOffset;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
    myTop = document.documentElement.scrollTop;
    myLeft = document.documentElement.scrollLeft;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
    myTop = document.body.scrollTop;
    myLeft = document.body.scrollLeft;
  }
}
function do_used(id)
{
	document.getElementById("s_u").style.display = "";
	document.getElementById("s_n").style.display = "none";
	document.forms["sf"].elements["used"].value = "1";
	do_resel(id);
	return false;
}
function no_used(id)
{
	document.getElementById("s_n").style.display = "";
	document.getElementById("s_u").style.display = "none";
	document.forms["sf"].elements["used"].value = "";
	do_resel(id);
	return false;
}
function do_resel(id)
{
/*	tmp = document.forms["sf"].elements["prod"];
	id = tmp.value;
	tmp.selectedIndex = 0;
	for (x = tmp.options.length - 1; x > 0; x--) tmp.remove(x);

	if (document.forms["sf"].elements["on_way"][1].checked)
	{
		if (document.forms["sf"].elements["used"].value)
		{
			if (typeof(po_used) == "object")
			{
				for (x = 1; x < po_used.length; x++) tmp.options.add(new Option(po_used[x][1],po_used[x][0],po_used[x][0] == id,po_used[x][0] == id));
			}
		}
		else
		{
			if (typeof(po_new) == "object")
			{
				for (x = 1; x < po_new.length; x++) tmp.options.add(new Option(po_new[x][1],po_new[x][0],po_new[x][0] == id,po_new[x][0] == id));
			}
		}
	}
	else
	{
		if (document.forms["sf"].elements["used"].value)
		{
			if (typeof(p_used) == "object")
			{
				for (x = 1; x < p_used.length; x++)
				{
					tmp.options.add(new Option(p_used[x][1],p_used[x][0],false,false));
					if (p_used[x][0] == id) tmp.selectedIndex = x;
				}
			}
		}
		else
		{
			if (typeof(p_new) == "object")
			{
				for (x = 1; x < p_new.length; x++)
				{
					tmp.options.add(new Option(p_new[x][1],p_new[x][0],false,false));
					if (p_new[x][0] == id) tmp.selectedIndex = x;
				}
			}
		}
	}
*/
	if (document.forms["sf"].elements["used"].value)
	{
		var tmp = document.getElementById("prod_new");
		var z = tmp.value;
		if (tmp.disabled) return;
		tmp.disabled = true;
		tmp.style.display = "none";
		tmp = document.getElementById("prod_used");
		tmp.disabled = false;
		tmp.selectedIndex = 0;
		tmp.style.display = "";
		for (var x = 1; x < tmp.length; x++) if (tmp.options[x].value == z) tmp.selectedIndex = x;
	}
	else
	{
		var tmp = document.getElementById("prod_used");
		var z = tmp.value;
		if (tmp.disabled) return;
		tmp.disabled = true;
		tmp.style.display = "none";
		tmp = document.getElementById("prod_new");
		tmp.disabled = false;
		tmp.selectedIndex = 0;
		tmp.style.display = "";
		for (var x = 0; x < tmp.length; x++) if (tmp.options[x].value == z) tmp.selectedIndex = x;
	}
}
function op_cat(id)
{
	tmp = document.getElementById("c_n");
	if (tmp) tmp.style.display = (id == tmp.id && tmp.style.display == "none") ? "" : "none";
	tmp = document.getElementById("c_u");
	if (tmp) tmp.style.display = (id == tmp.id && tmp.style.display == "none") ? "" : "none";
	tmp = document.getElementById("m_n");
	if (tmp) tmp.style.display = (id == tmp.id && tmp.style.display == "none") ? "" : "none";
	tmp = document.getElementById("m_u");
	if (tmp) tmp.style.display = (id == tmp.id && tmp.style.display == "none") ? "" : "none";
	tmp = document.getElementById("o_a");
	if (tmp) tmp.style.display = (id == tmp.id && tmp.style.display == "none") ? "" : "none";
}
function pic_big(src)
{
	document.images["im_big"].src = src;
	return false;
}
function toFavorites(key,name,url)
{
	date = new Date();
	date = new Date(date.getFullYear(),date.getMonth(),date.getDate(),date.getHours() + 6);

	document.cookie = "fav_" + key + "=" + name + " :: " + url + "; path=/; expires=" + date.toGMTString();

	books = 0;
	cooks = document.cookie.split("; ");
	for (x = 0; x < cooks.length; x++) if (cooks[x].substr(0,4) == "fav_") books++;
	cooks = document.getElementById("fav_cnt");
	if (cooks) cooks.innerHTML = books ? "&nbsp;(" + books + ")" : "";

	alert('Модель успешно сохранена в избранном!');
	return false;
}
function unFavorites(id,reload)
{
	if (!id) id = "";
	books = 0;
	cooks = document.cookie.split("; ");
	for (x = 0; x < cooks.length; x++) if (cooks[x].substr(0,4) == "fav_")
	{
		if (cooks[x].substr(4,id.length + 1) == id + "=" || id == "")
		{
			document.cookie = cooks[x].substr(0,cooks[x].indexOf("=")) + "=; path=/; expires=0";      
		}
		else books++;
	}
	cooks = document.getElementById("fav_cnt");
	if (cooks) cooks.innerHTML = books;
	if (reload) location.reload();
	return false;
}
function sendFriend(site,name,url)
{
	location.href = ("mailto:?subject=Ссылка%20на%20авто%20%22" + name + "%22%20на%20сайте%20" + site + "&body=" + url);
	return false;
}
function goSearch()
{
	frm = document.forms["sf"];
	if (frm.elements["prod"].selectedIndex < 1 && !frm.elements["pri_lo"].value && !frm.elements["pri_hi"].value)
	{
		frm.elements["prod"].focus();
		alert("Выберите марку или введите цену");
		return false;
	}
	if (frm.elements["prod"].selectedIndex < 1 && !frm.elements["pri_lo"].value && !frm.elements["pri_hi"].value && (!frm.elements["used"].value || (!frm.elements["release"].value && !frm.elements["run_lo"].value && !frm.elements["run_hi"].value))) return false;
	return true;
}
function op_room()
{
	tmp = document.getElementById("room_ctrl_i");
	if (tmp) tmp.style.display = "";
	tmp = document.getElementById("room_ctrl");
	if (tmp) tmp.style.display = "";
	return false;
}
function cl_room()
{
	tmp = document.getElementById("room_ctrl_i");
	if (tmp) tmp.style.display = "none";
	tmp = document.getElementById("room_ctrl");
	if (tmp) tmp.style.display = "none";
	return false;
}
function goRoom(id)
{
	tmp = document.getElementById("room_cont");
	if (tmp) tmp.style.backgroundImage = "url('/i/floor" + id + ".gif')";
}
function op_mailFrm()
{
	window.open("/mailfrm.php","_mail","width=320,height=360,top=5,left=5,resizable=no,toolbar=0,location=0,status=no,menubar=0,directories=0,scrollbars=no").focus();
	return false;
}
function openForm()
{
//	window.open("/sendmail.phtml?ref=" + escape(location.href),"_form","toolbar=no,bar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=500,height=300").focus();
	document.images["_cap"].src = "/captcha.php?id=" + Math.random();
	var tmp = document.getElementById("send_form");
	checkWideScreen();
	tmp.style.left = (myLeft + (myWidth >> 1) - 265) + "px";
	tmp.style.top = (myTop + (myHeight >> 1) - 170) + "px";
	tmp.style.display = "block";
	return false;
}
	function CheckForm(){	
	  var error=0;
	  var mf=document.forms['zakaz'];		
	    if (mf.fio.value=='' && error==0) {
	      alert('Заполните, пожалуйста, поле \"ФИО\"');
		  mf.fio.focus();
		  error=1;
	    }
		if (mf.email.value=='' && error==0) {
	      alert('Укажите, пожалуйста, \"E-mail\"');
		  mf.email.focus();
		  error=1;
	    }
		if (mf.about.value=='' && error==0) {
	      alert('Введите, пожалуйста, текст сообщения');
		  mf.about.focus();
		  error=1;
	    }
		if (error==0 && mf.phone.value=='' && (isEmail() == false)){
			error=1;
			mf.email.focus();
		}
		if (!mf.code.value.match(/^\d{4}$/) && error==0) {
	    alert('Введите, пожалуйста, четыре цифры кода');
		  mf.code.focus();
		  error=1;
	    }
		if (!mf.code.value.match(/^\d{4}$/) && error==0) {
	    alert('Введите, пожалуйста, четыре цифры кода');
		  mf.code.focus();
		  error=1;
	    }
	    if (error==0) {
		document.getElementById('send_form').style.display = 'none'
	       mf.submit();
//				script = document.createElement("script");
//				script.src = "/sendmail.phtml?fio="+mf.fio.value+"&email="+mf.email.value+"&ref="+escape(location.href)+"&code="+mf.code.value+"&phone="+mf.phone.value+"&about="+mf.about.value;			
//				document.body.appendChild(script);				 
	    }			
	 }
	 function isEmail() {
	
		var strEmail = document.forms["zakaz"].elements["email"].value;
		//Данная функция проверяет строку strEmail на соответствие формату почтового адреса.
		//При этом строка разбивается на составные части:
		//имя_пользователя@сервер
	
		if (strEmail=="") {
			alert("Введите, пожалуйста, E-mail");
			document.forms["zakaz"].elements["email"].focus();
			return false;
		}
	
		var letters="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		var numbers="0123456789";
		
		var name="";
		var server="";
		var strings=new Array();
		
		//разделяем строку на имя пользователя и имя сервера
		strings=strEmail.split("@");
		if (strings.length==1) {
			alert("Ошибка: e-mail должен содержать символ @");
			document.forms["zakaz"].elements["email"].focus();
			return false;
		} else if (strings.length==2) {
			name=strings[0];
			server=strings[1];
		} else {
			alert("Ошибка: e-mail не может содержать более одного символа @");
			document.forms["zakaz"].elements["email"].focus();
			return false;
		}
		
		//проверка имени на допустимость символов
		if (!isValid(name,letters+numbers+"._-")) {
			alert("Недопустимые символы в имени пользовател\я");
			document.forms["zakaz"].elements["email"].focus();
			return false;
		}
		
		//разделяем имя сервера на составные части
		strings=server.split(".");
		if (strings.length<2) {
			alert("Нераспознанный e-mail");
			document.forms["zakaz"].elements["email"].focus();
			return false;
		}
		//проверяем каждую часть на допустимые символы
		for (i=0;i<strings.length;i++) {
			if (!isValid(strings[i],letters+numbers+"_-")) {
				alert("Нераспознанный e-mail");
				document.forms["zakaz"].elements["email"].focus();
				return false;
			}
		}
		
		return true;
		
	}
	
	function isValid(what,chars) {
		//проверяет, что все символы из what принадлежат множеству chars
		//если what пустая - ошибка
		while (what.charAt(0)==" ") {
			what=what.substring(1);
		}
		if (what=="") {
			return false;
		}
		bool1=false;
		for (count1=0; (count1<what.length)&&(!bool1);count1++) {
			bool2=false;
			for (count2=0; (count2<chars.length)&&(!bool2);count2++) {
				if (what.charCodeAt(count1)==chars.charCodeAt(count2)) {
					bool2=true;
				}
			}
			if (!bool2) {
				bool1=true;
			}
		}
		return !bool1;
	}

var MTt = false;
var MTto = 5000;
var MTrun = false;
function rollMT()
{
	$("#mt_next").click();
//	MTt = window.setTimeout(rollMT,MTto);
}

function bind_events()
{
	$("#mt_next").bind("click", function(){
		
//		unbind_events();

		if (MTrun) return false;
		MTrun = true;

		if (MTt)
			window.clearTimeout(MTt);
		MTt = window.setTimeout(rollMT,MTto);

		tmp = $("#mt_cont > table");
		if (tmp)
		{
			hide = $(tmp.get(0));
			hide.clone(true).appendTo("#mt_cont");
			hc = $(tmp.get(tmp.size() - 1));
					
			hide.animate({
				marginLeft: '-' + hide.width() + 'px'
			},	function(){
				hide.remove();
				MTrun = false;
//				bind_events()
			}
			);
		}
		else
		{
			MTrun = false;
//			bind_events();
		}

		return false;
	});
	$("#mt_prev").bind("click", function(){
		
//		unbind_events();

		if (MTrun) return false;
		MTrun = true;

		if (MTt)
			window.clearTimeout(MTt);
		MTt = window.setTimeout(rollMT,MTto);

		tmp = $("#mt_cont > table");
		if (tmp)
		{
			hide = $('#mt_cont > table:last');
			tmp = $('#mt_cont > table:first');
			hide.clone(true).css({marginLeft: '-' + hide.width() + 'px'}).prependTo("#mt_cont");
			show = $('#mt_cont > table:first');		
				
			show.animate({
				marginLeft: '0px'
			},function(){
				hide.remove();
				MTrun = false;
//				bind_events()
			}
			);
		}
		else
		{
			MTrun = false;
//			bind_events();
		}

		return false;
	});
}

function unbind_events()
{
	$("#mt_next").unbind('click');
	$("#mt_prev").unbind('click');
	
	$("#mt_next").bind('click',function(){
		return false;
	});
	
	$("#mt_prev").bind('click',function(){
		return false;
	});
}

