var id = 0;
var ftype = 'mssg';
var fstat = 0;

function addHandler(object, event, handler, useCapture) {
	if (object.addEventListener) {
		object.addEventListener(event, handler, useCapture ? useCapture : false);
	} else if (object.attachEvent) {
		object.attachEvent('on' + event, handler);
	} else alert("Add handler is not supported");
}

function removeHandler(object, event, handler) {
	if (object.removeEventListener) {
		object.removeEventListener(event, handler, false);
	} else if (object.detachEvent) {
		object.detachEvent('on' + event, handler);
	} else alert("Remove handler is not supported");
}

// Обработчик события
function wheel(event) {
	var delta; // Направление скролла
	// -1 - скролл вниз
	// 1 - скролл вверх
	event = event || window.event;
	// Opera и IE работают со свойством wheelDelta
	if (event.wheelDelta) {
		delta = event.wheelDelta / 120;
		// В Опере значение wheelDelta такое же, но с противоположным знаком
		if (window.opera) delta = -delta;
		// В реализации Gecko получим свойство detail
	} else if (event.detail) {
		delta = -event.detail / 3;
	}
	// Запрещаем обработку события браузером по умолчанию
	if (event.preventDefault) event.preventDefault();
	event.returnValue = false;
	return delta;
}




function ShowForm (id, chnum, event) {
	if (chnum == 1) {
		var wi = document.body.clientWidth;
		var he = document.documentElement.clientHeight;		
		with (document) {
			getElementById('fon').style.width = wi+'px';
			getElementById('fon').style.height = he+'px';
			getElementById('fonf').style.width = wi+'px';
			getElementById('fonf').style.height = he+'px';
			getElementById('fonf').style.visibility='visible';
			getElementById('fon').style.visibility='visible';
			getElementById(id).style.left = (body.clientWidth - getElementById(id).clientWidth)/2+'px'
			getElementById(id).style.top = (documentElement.clientHeight - getElementById(id).clientHeight)/2+'px'
			getElementById(id).style.visibility='visible';
			ftype = id;
			fstat = 1;
			/* Gecko */
			addHandler(window, 'DOMMouseScroll', wheel);
			/* Opera */
			addHandler(window, 'mousewheel', wheel);
			/* IE */
			addHandler(document, 'mousewheel', wheel);
		}
	}
}

function CloseForm (id) {
	with(document) {
		getElementById(id).style.visibility='hidden';
		getElementById('fonf').style.visibility='hidden';
		getElementById('fon').style.visibility='hidden';
		contact.subj.value='';
		contact.mail.value='';
		contact.name.value='';
		contact.messg.value='';
		contact.ch_num.value='';
		ftype = id;
		fstat = 0;
		removeHandler(window, 'DOMMouseScroll', wheel);
		removeHandler(window, 'mousewheel', wheel);
		removeHandler(document, 'mousewheel', wheel);
	}
}

function trim(x) {
	var ch,c;
	ch=x.toString();
	c=ch.charAt(0);
	while(c==" ") { 
		ch=ch.slice(1);
		c=ch.charAt(0);
	}
	c=ch.charAt(ch.length-1);
	while(c==" ") { 
		ch=ch.slice(0,-1);
		c=ch.charAt(ch.length-1);
	}
	return ch;
}

function check_mail(email) { 
	var re = new RegExp("^[\\w\\.-]+@[\\w\\.-]+\\.[A-Za-z]{2,}");
  	return (re.test(email));
} 

function pr_Inp(obj,cl,fl) {
	if((fl)?check_mail(obj.value):trim(obj.value)) {
		obj.className=cl;
		return true;
	}
	else {
		obj.className=cl+'_err';
		obj.focus();
		return false;
	}
}

function cu_Form(action) {
	with(document) {
		if (action == 'contact') {
			var ok=0;
			if(pr_Inp(contact.subj,'inp_text_2')) ++ok;
			if(pr_Inp(contact.mail,'inp_text_2',1)) ++ok;
			if(pr_Inp(contact.name,'inp_text_2')) ++ok;
			if(pr_Inp(contact.messg,'inp_text_2')) ++ok;
			if(pr_Inp(contact.ch_num,'inp_text_2')) ++ok;
			if(ok==5) {
				contact.action.value='2';
				contact.submit();
			}
		} else if (action == 'report') {
			var ok=0;
			if (car_report.gos_num.value !== '') {
				if (pr_Inp(car_report.gos_num,'gos_num')) ++ok;
				var ch_num = 1;
			} else {
				if(pr_Inp(car_report.vin,'vin_code')) ++ok;			
				var ch_num = 1;				
			}
			if(ok==ch_num) {
				car_report.submit();
			}
		} else if (action == 'send_rep') {
			var ok=0;
			report.rep_id.value = refreshf.id_ref.value;
			if(pr_Inp(report.mail_r,'inp_text',1)) ++ok;
			//alert(report.rep_id.value);
			if(ok==1) {
				report.action.value='2';
				report.submit();
			}
		}
	}
}

function sendZ (id) {
	var SendArray = new Array();
	SendArray.id = id;
	var req = new JsHttpRequest();
	req.onreadystatechange = function() {		
		if (req.readyState == 4) {
			var stat = req.responseJS.stat;
			//alert(stat+'ёпрст');
			if (stat.length == 32) {
				document.refreshf.status.value = 1;
				document.refreshf.submit();
			} else RefreshPage(0);
		 }
	 }
	 req.open('POST','refresh.php', true);
	 req.send({SendArray:SendArray});
}

function RefreshPage (status) {
	if (status == 0) {
		setTimeout('sendZ('+id+')',10000);
	}
}

function OCost() {
	switch (document.getElementById('operators').value) {
		case "utel": 
			var c1 = "106.2 рублей";
			var c2 = "Стоимость услуги 106.2 р";
		break;
		case "beeline": 
			var c1 = "130 рублей";
			var c2 = "Стоимость услуги 130 р";
		break;
		case "megafon": 
			var c1 = "147.5 рублей";
			var c2 = "Стоимость услуги 147.5 р";
		break;
		case "mts": 
			var c1 = "111.76 рублей";
			var c2 = "Стоимость услуги 111.76 р";
		break;
	}
	document.getElementById('cost_1').innerHTML = c1;
	document.getElementById('cost_2').innerHTML = c2;
}
function showPhoto(src){
	$('#dtp_img_full').remove();
	$('#dtp_img_full3').remove();
	$('#dtp_img_full4').remove();
	$('#dtp_img_full_close').remove();
	$('body').append("<div id='dtp_img_full'><div id='dtp_img_full2'><img src='"+src+"' /></div></div><div id='dtp_img_full3'></div><div id='dtp_img_full4'style='color:#8FB34F;cursor:pointer;text-align:center;' onclick=\"$('#dtp_img_full').remove();$('#dtp_img_full3').remove();$('#dtp_img_full4').remove();$('#dtp_img_full_close').remove();\">Закрыть</div>");
	var wi = document.body.clientWidth;
	var he = getDocumentHeight();
	$('#dtp_img_full')
		.css('z-index','6')
		.css('position','absolute')
		.css('top','0')
		.css('left','0')
/*		.css('display','table')*/
		.css('width','100%')
		.css('height','100%')
		.css('vertical-align','middle')
		.css('text-align','center');
	$('#dtp_img_full2')
		.css('position','relative')
		.css('opacity','2')
/*		.css('display','table-cell')*/
		.css('width','100%')
		.css('vertical-align','middle');
	$('#dtp_img_full3')
		.css('z-index','5')
		.css('opacity','0.5')
		.css('background','#000')
		.css('position','absolute')
		.css('top','0')
		.css('left','0')
		.css('width','100%')
		.css('height',he+'px');
	$('#dtp_img_full4')
		.css('z-index','7')
		.css('position','absolute')
		.css('top','0')
		.css('right','0');
}

var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera')  > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);
 
function getDocumentHeight() {
  return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}
 
function getViewportHeight() {
  return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}
function asLoadModels(el){
	$("select[name=as-model]",$(el).parent().parent().parent()).html("").attr("disabled","disabled");	
	$("select[name=as-bodytype]",$(el).parent().parent().parent()).html("").attr("disabled","disabled");
	if (el.value!='0'){
		request="&step=asLoadModels&make_id="+el.value;
		$.ajax({
			type: 'POST',
			data: request,
			url: 'ajax_requests.php',
			dataType: "json",
			success: function(data){
				$("select[name=as-model]",$(el).parent().parent().parent()).html(data.html).attr("disabled","");
			}
		});
	}
}
function asLoadBodytypes(el){
	$("select[name=as-bodytype]",$(el).parent().parent().parent()).html("").attr("disabled","disabled");
	if (el.value!='0'){
		request="&step=asLoadBodytypes&model_id="+el.value;
		$.ajax({
			type: 'POST',
			data: request,
			url: 'ajax_requests.php',
			dataType: "json",
			success: function(data){
				$("select[name=as-bodytype]",$(el).parent().parent().parent()).html(data.html).attr("disabled","");
			}
		});
	}
}
function showCaptcha(el,site){
	if($('.info',$(el).parent()).html()==""){
		$('.info',$(el).parent()).toggle().html('Получение кода безопасности...');
		request="&step=showCaptcha&site="+site;
		$.ajax({
			type: 'POST',
			data: request,
			url: 'ajax_requests.php',
			dataType: "json",
			success: function(data){
				$('.info',$(el).parent()).html(data.html);
			}
		});
	}else{
		$('.info',$(el).parent()).toggle();
	}
}
function checkCaptcha(el,id){
	request="&step=checkCaptcha&id="+id+"&val="+$(el).prev().attr("value");
	$.ajax({
		type: 'POST',
		data: request,
		url: 'ajax_requests.php',
		dataType: "json",
		success: function(data){
			$(el).next().html(data.html);
		}
	});
}
