﻿//
////location.href='/adm/reg/^if(def $form:letter){?letter=${form:letter}&}{?}todo=email&id=$u.id&action='+this.value
//
function subscribe_email ( user_id, action ) {
	$.ajax({
		url: "/adm/reg/", 
		type: "GET", 
		data: "todo=email" + "&id=" + user_id + "&action=" + action + "",
		cache: false, 
		success: function(html){
			if( action > 0 ){ 
				alert('Пользователь подписан на прайс');
			} else {
				alert('Пользователь отписан от рассылки');
			}
		}
	});
}

//
//
//
function add2search ( s_id ) {
	if( $("#model_image_"+s_id).attr("className") == "model_image_selected" ){

		$("#selected_models").html( $("#selected_models").html().replace( " | <b>" + $("#model_image_"+s_id).attr("title") + "</b>" ,"") )
		$("#selected_models").html( $("#selected_models").html().replace( "<b>" + $("#model_image_"+s_id).attr("title") + "</b>" ,"") )

		$("#model_image_"+s_id).attr("className", "model_image");
		$("#model_image_"+s_id).mouseover(function(){ $("#model_image_"+s_id).attr("className", "model_image_hover"); });
		$("#model_image_"+s_id).mouseout(function(){ $("#model_image_"+s_id).attr("className", "model_image"); });
	} else {

		if( $("#selected_models").html() == "") {
			$("#selected_models").html( "<b>" + $("#model_image_"+s_id).attr("title") + "</b>");
		} else {
			$("#selected_models").append( " | " + "<b>" + $("#model_image_"+s_id).attr("title") + "</b>" );
		}
		$("#model_image_"+s_id).attr("className", "model_image_selected");
		$("#model_image_"+s_id).mouseover(function(){ $("#model_image_"+s_id).attr("className", "model_image_selected"); });
		$("#model_image_"+s_id).mouseout(function(){ $("#model_image_"+s_id).attr("className", "model_image_selected"); });
	}
	goAjax();
}

//
//
//
function add2cart_ ( id ) {
	$("#cartmessage").hide("fast");
	$("#cartmessage").html("Для работы с системой он-лайн заказов необходима <a href=\"javascript:showloginform();$('#cartmessage').hide('slow');\">авторизация</a>.<div style='height: 5px'></div>Также Вы можете оформить заказ по телефону: <b>+7 (495) 22-11-272</b>");
	$("#cartmessage_close").click( function () { $("#cartmessage").hide("slow"); } );
	$("#cartmessage_cart").click( function () { location.href='/cart/' } );
	$("#cartmessage").show("slow");
}

//
// Красивые формочки и таблички
//
$(document).ready(function() {

//
//$("#user_cart").mouseover(function(){
//		$("#user_cart_details").load("/user_cart_detail.html");
//		$("#user_cart_details").fadeIn();
//});

//
$(".model_image").mouseover(function(){ $(this).attr("className", "model_image_hover"); });
$(".model_image").mouseout(function(){ $(this).attr("className", "model_image"); });
//$(".model_image").mousedown(function(){
//	if( $(this).attr("className") == "model_image_selected" ){
//
//		$("#selected_models").html( $("#selected_models").html().replace( " | <b>" + $(this).attr("title") + "</b>" ,"") )
//		$("#selected_models").html( $("#selected_models").html().replace( "<b>" + $(this).attr("title") + "</b>" ,"") )
//
//		$(this).attr("className", "model_image");
//		$(this).mouseover(function(){ $(this).attr("className", "model_image_hover"); });
//		$(this).mouseout(function(){ $(this).attr("className", "model_image"); });
//	} else {
//
//		if( $("#selected_models").html() == "") {
//			$("#selected_models").html( "<b>" + $(this).attr("title") + "</b>");
//		} else {
//			$("#selected_models").append( " | " + "<b>" + $(this).attr("title") + "</b>" );
//		}
//		$(this).attr("className", "model_image_selected");
//		$(this).mouseover(function(){ $(this).attr("className", "model_image_selected"); });
//		$(this).mouseout(function(){ $(this).attr("className", "model_image_selected"); });
//	}
//	goAjax();
//});
//$(".model_image a").click(function(){ return false; });
//

//
$(".logo,.logo_hover").mousedown(function(){
	$(".logo_hover").attr("className", "logo");

	if( $("#logo_hyundai_").attr("src").indexOf("_") < 0 ) { $("#logo_hyundai_").attr("src", $("#logo_hyundai_").attr("src").replace(".jpg","_.jpg") ); }
	if( $("#logo_kia_").attr("src").indexOf("_") < 0 ) { $("#logo_kia_").attr("src", $("#logo_kia_").attr("src").replace(".jpg","_.jpg") ); }
	if( $("#logo_daewoo_").attr("src").indexOf("_") < 0 ) { $("#logo_daewoo_").attr("src", $("#logo_daewoo_").attr("src").replace(".jpg","_.jpg") ); }
	if( $("#logo_ssangyong_").attr("src").indexOf("_") < 0 ) { $("#logo_ssangyong_").attr("src", $("#logo_ssangyong_").attr("src").replace(".jpg","_.jpg") ); }

	$("#model_hyundai").css("display","none");
	$("#model_kia").css("display","none");
	$("#model_daewoo").css("display","none");
	$("#model_ssangyong").css("display","none");

	$(this).attr("className", "logo_hover");
	$("#" + $(this).attr("id") + "_").attr("src", $("#" + $(this).attr("id") + "_").attr("src").replace("_","") );
	$("#model_" + $(this).attr("id").replace("logo_","") + "").css("display","block");
});
//

//
//$("#search_string").keyup( function(e){
//	if ( e.keyCode != '37' && e.keyCode != '38' && e.keyCode != '39' && e.keyCode != '40' && e.keyCode != '16' && e.keyCode != '17' && e.keyCode != '13' ) {
//		goAjax();
//	} else {
//		return false;
//	}
//});
//

//goAjax();

});


//
//
//
function goPage ( page_number ) {
	if( document.search_form ) {
	if( document.search_form.page ) {

		document.search_form.page.value = page_number;
		document.search_form.submit();

	}
	}
}

//
//
//
function goAjax( page_number, selected_ ) {

if( document.search_form ) {
if( document.search_form.search_string ) {

if( document.search_form.model_selected__ ){
	var m_string = ""+document.search_form.model_selected__.value+"";
	if ( $("#search_string").val() == "" ){
		var page_number = ""+document.search_form.page_number__.value+"";
	}
} else {
	var m_string = $("#selected_models").html().replaceAll(" | ",",");
	var m_string = m_string.replaceAll("<b>","");
	var m_string = m_string.replaceAll("</b>","");
	var m_string = m_string.replaceAll("hyundai ","");
	var m_string = m_string.replaceAll("kia ","");
	var m_string = m_string.replaceAll("daewoo ","");
} 
	$("#search_results").html("<img src='/i/ajax-loader.gif' />");
	$.ajax({
		url: "/search.html", 
		type: "GET", 
		data: "search_string=" + $("#search_string").val() + "&m=" + m_string + "&z=" + $("#zakaz").val() + "&p=" + page_number,
		cache: false, 
		success: function(html){
			$("#search_results").html(html);
			$(".search_tr").mouseover(function(){ $(this).attr("className", "search_tr_hover"); });
			$(".search_tr").mouseout(function(){ $(this).attr("className", "search_tr"); });
		}
	});

}
}

}

//
//
//
String.prototype.replaceAll = function(search, replace){
  return this.split(search).join(replace);
}

//
//
//
var ie = (document.all)? true : false
var nn6 = (!ie && document.getElementById)? true : false
var nn4 = (document.layers)? true : false

function popupWin(name, uri, wdt, hgt) {
	var posCode = ''
	if (nn4 || nn6 || ie) {
		if ( (screen.height < 481) && (hgt > 400) ) { hgt = 400 }
		posX = Math.round((screen.width - wdt) / 2)
		posY = Math.round((screen.height - hgt) / 2) - 35
		posCode = ",left="+posX+",top="+posY
	}
	popupedWin = window.open(uri, name, "status=yes,menubar=no,toolbar=no,resizable=yes,scrollbars=yes,location=no,width="+wdt+",height="+hgt+posCode)
	popupedWin.focus()
}

//
//
//
function dummy () {
}

//
//
//
function open_or_close_div ( div_id, dont_close_again ) {
	if ( document.getElementById(div_id).style.display == '' || document.getElementById(div_id).style.display == 'none' ) {
		document.getElementById(div_id).style.display = 'block';
	} else {
		if ( dont_close_again != '1' ) {
			document.getElementById(div_id).style.display = 'none';
		}
	}
}

//
//
//
function open_div ( div_id ) {
	document.getElementById(div_id).style.display = 'block';
}


//
//
//
function isBlank(sName, sText) {
	if (sName.value == "") {
		alert(sText);
		sName.className = 'input_required';
		sName.focus();
		return false;
	} else {
		sName.className = 'input_field';
		return true;
	}
}

//
// генерируем случайное число
//
function get_random () {
	var min_random = 0;
	var max_random = 10000;
	max_random++;
	var range = max_random - min_random;
	return Math.floor(Math.random()*range) + min_random;
}

//
//
//
function add2cart ( id, how ) {
	var i = new Image();
	var random_value = get_random();
	i.src = '/httprequest/add2cart/?id='+id+'&how='+how+'&random='+random_value;

	$("#cart_").val( parseInt($("#cart_").val()) + parseInt(how) );
//	alert($("#cart_").val());

	$("#cart").html("добавлено: " + $("#cart_").val() + "</a>");
	$("#cart").fadeIn("slow");

	$("#cartmessage").hide("fast");
	$("#cartmessage").html("Товар '"+ id + "' был успешно добавлен в заявку в количестве: "+how+"<br /><br /><a href='javascript:dummy()' id='cartmessage_cart'>перейти в корзину</a> | <a href='javascript:dummy()' id='cartmessage_close'>ОК</a>");
	$("#cartmessage_close").click( function () { $("#cartmessage").hide("slow"); } );
	$("#cartmessage_cart").click( function () { location.href='/cart/' } );
	$("#cartmessage").show("slow");

}

//
//
//
function remove_from_cart ( id, how ) {
	if( confirm ('Нажмите "ОК" для того, чтобы удалить товар из корзины') ) {
	var i = new Image();
	var random_value = get_random();
	i.src = '/httprequest/delete_from_cart/?id='+id+'&how='+how+'&random='+random_value;

//	$("part_"+id).hide("slow");

	document.getElementById('part_'+id).style.display = 'none';

	$("#cart_").val( parseInt($("#cart_").val()) - parseInt(how) );

	$("#cart").html("<a href='/cart/'>Заказано позиций: " + $("#cart_").val() + "</a>");
	$("#cart").fadeIn("slow");

	$("#cartmessage").hide("fast");
	$("#cartmessage").html("Товар '"+ id + "' был успешно удален из заявки в количестве: "+how+"<br /><br /><a href='javascript:dummy()' id='cartmessage_cart'>обновить корзину</a> | <a href='javascript:dummy()' id='cartmessage_close'>ОК</a>");
	$("#cartmessage_close").click( function () { $("#cartmessage").hide("slow"); } );
	$("#cartmessage_cart").click( function () { location.href='/cart/' } );
	$("#cartmessage").show("slow");

}
}

//
//
//
function show_managers ( id ) {
	document.getElementById('managers_header_1').className = 'managers_tr';
	document.getElementById('managers_header_2').className = 'managers_tr';
	document.getElementById('managers_header_3').className = 'managers_tr';
	document.getElementById('managers_header_4').className = 'managers_tr';

	document.getElementById('managers_header_' + id).className = 'managers_tr_selected';

	document.getElementById('managers_td_1').style.display = 'none';
	document.getElementById('managers_td_2').style.display = 'none';
	document.getElementById('managers_td_3').style.display = 'none';
	document.getElementById('managers_td_4').style.display = 'none';

	$("#managers_td_" + id).slideDown("slow");
}

//
//
//
function clear_value ( obj, default_text ) {
	if ( document.getElementById(obj).value == default_text ){
		document.getElementById(obj).value = '';
	}
}

function set_value ( obj, default_text ) {
	if ( document.getElementById(obj).value == '' ){
		document.getElementById(obj).value = default_text;
	}
}

