function trim(s){ return 	s.replace(/^(\s*)/,"$`").replace(/(\s*)$/,"$'"); }
function hover(obj) {
 obj.className="h";
}
function unhover(obj) {
 obj.className="";
}
function ajaxInit(url,oncomplete){
	url += '?uniqueID=' + (new Date()).getTime();
	var req = (window.XMLHttpRequest) ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
	req.onreadystatechange = processRequest;
	function processRequest(){
		if (req.readyState == 4)
			if (req.status == 200){
				var response = req.responseText;
				if (oncomplete!='') eval(oncomplete);
			}
	}
	this.sendPost = function(postStr) {
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(postStr);
	}
}
function preparePostVars(postVars){
	var first = new Boolean(true);
	var postStr = '';
	for (var pkey in postVars) {
		if (!first) postStr += "&";
		postStr += pkey + "=" + encodeURI(postVars[pkey]);
		first = false;
	}
	return postStr;
}
function testdriveRequest(obj){
	if (trim(obj.fio.value)=='') { alert("Представьтесь, пожалуйста"); return false; }
	if (trim(obj.amark.value)=='') { alert("Укажите модель автомобиля"); return false; }
	if (trim(obj.contact.value)=='') { alert("Укажите, как с вами связаться"); return false; }
	if (trim(obj.time.value)=='') { alert("Укажите удобное время"); return false; }
	var buttobj = document.getElementById('sendbutt');
	var timeobj = document.getElementById('sendwait');
	var sendobj = document.getElementById('sendsent');
	buttobj.style.display = "none";
	sendobj.style.display = "none";
	timeobj.style.display = "";
	var ai = new ajaxInit('/inc/testdrive.asp','sendOnComplete(response);');
	var postVars = new Array();
		postVars['fio'] = trim(obj.fio.value);
		postVars['amark'] = trim(obj.amark.value);
		postVars['contact'] = trim(obj.contact.value);
		postVars['time'] = trim(obj.time.value);
	ai.sendPost(preparePostVars(postVars));
	return false;
}
function serviceRequest(obj){
	if (trim(obj.fio.value)=='') { alert("Представьтесь, пожалуйста"); return false; }
	if (trim(obj.amark.value)=='') { alert("Укажите модель Вашего автомобиля"); return false; }
	if (trim(obj.contact.value)=='') { alert("Укажите, как с вами связаться"); return false; }
	if (trim(obj.goal.value)=='') { alert("Опишите неисправность"); return false; }
	var buttobj = document.getElementById('sendbutt');
	var timeobj = document.getElementById('sendwait');
	var sendobj = document.getElementById('sendsent');
	buttobj.style.display = "none";
	sendobj.style.display = "none";
	timeobj.style.display = "";
	var ai = new ajaxInit('/inc/servrequest.asp','sendOnComplete(response);');
	var postVars = new Array();
		postVars['fio'] = trim(obj.fio.value);
		postVars['amark'] = trim(obj.amark.value)+((trim(obj.ayear.value)!='')?', '+trim(obj.ayear.value)+' г. выпуска':'');
		postVars['contact'] = trim(obj.contact.value);
		postVars['goal'] = trim(obj.goal.value);
	ai.sendPost(preparePostVars(postVars));
	return false;
}
function consultQuest(obj){
	if (trim(obj.fio.value)=='') { alert("Представьтесь, пожалуйста"); return false; }
	if (!emailCheck(obj.email.value)) { alert("Укажите корректный адрес электронной почты"); return false; }
	if (trim(obj.question.value)=='') { alert("Укажите Ваш вопрос"); return false; }
	var buttobj = document.getElementById('sendbutt');
	var timeobj = document.getElementById('sendwait');
	var sendobj = document.getElementById('sendsent');
	buttobj.style.display = "none";
	sendobj.style.display = "none";
	timeobj.style.display = "";
	var ai = new ajaxInit('/inc/question.asp','sendOnComplete(response);');
	var postVars = new Array();
		postVars['fio'] = trim(obj.fio.value);
		postVars['email'] = trim(obj.email.value);
		postVars['phone'] = trim(obj.phone.value);
		postVars['rubric'] = 1;
		postVars['question'] = trim(obj.question.value);
	ai.sendPost(preparePostVars(postVars));
	return false;
}
function sendOnComplete(response){
	var buttobj = document.getElementById('sendbutt');
	var timeobj = document.getElementById('sendwait');
	var sendobj = document.getElementById('sendsent');
	if (response!=""){
		alert(response);
		buttobj.style.display = "";
		sendobj.style.display = "none";
		timeobj.style.display = "none";
	}
	else{
		buttobj.style.display = "none";
		sendobj.style.display = "";
		timeobj.style.display = "none";
		setTimeout(readyToSend,5000);
	}
}
function readyToSend(){
	var formobj = document.getElementById('mail');
	formobj.reset();
	var buttobj = document.getElementById('sendbutt');
	var timeobj = document.getElementById('sendwait');
	var sendobj = document.getElementById('sendsent');
	buttobj.style.display = "";
	sendobj.style.display = "none";
	timeobj.style.display = "none";
}
function changeModelColor(obj){
	var src = false;
	var path = document.getElementById('colorspath').value;
	var objs = document.getElementsByName('colorhref');
	if (obj){
		var code = obj.id.substr(3,2);
		src = path+code+'.jpg';
	}
	else{
		for (var i=0; i<objs.length; i++){
			if (objs[i].tagName=='A'){
				if (!src){
					var code = objs[i].id.substr(3,2);
					src = path+code+'.jpg';
					obj = objs[i];
				}
			}
		}
	}
	if (obj){
		for (var i=0; i<objs.length; i++){
			if (objs[i].tagName=='A'){
				document.getElementById('ach'+objs[i].id.substr(3,2)).style.display = 'none';
				objs[i].style.display = '';
			}
		}
		obj.style.display = 'none';
		document.getElementById('ach'+code).style.display = '';
	}
	if (src) document.getElementById('colors_main_img').src = src;
	else document.getElementById('colors_main_img').src = "/pics/blank.gif";
}
function changeModelPhoto(mode,obj){
	var src = false;
	var path = document.getElementById(mode+'path').value;
	var objs = document.getElementsByName(mode+'href');
	if (obj){
		var code = obj.id.substr(3);
		src = path+'b'+code+'.jpg';
	}
	else{
		for (var i=0; i<objs.length; i++){
			if (objs[i].tagName=='A'){
				if (!src){
					var code = objs[i].id.substr(3);
					src = path+'b'+code+'.jpg';
					obj = objs[i];
				}
			}
		}
	}
	if (obj){
		for (var i=0; i<objs.length; i++) if (objs[i].tagName=='A') objs[i].className = '';
		obj.className = 'active';
	}
	if (src) document.getElementById(mode+'_main_img').src = src;
	else document.getElementById(mode+'_main_img').src = "/pics/blank.gif";
}
function changeModelVideo(num){
	var actspan = document.getElementById('videomenuact'+num);
	if (document.getElementById('videomenuact'+num)) if (actspan.style.display=='') return false;
	var spanobjs = document.getElementsByTagName('SPAN');
	for (var i=0; i<spanobjs.length; i++){
		if (spanobjs[i].id.substr(0,9)=='videomenu') spanobjs[i].style.display = '';
		if (spanobjs[i].id.substr(0,12)=='videomenuact') spanobjs[i].style.display = 'none';
	}
	var divobjs = document.getElementsByTagName('DIV');
	for (var i=0; i<divobjs.length; i++) if (divobjs[i].id.substr(0,9)=='videocont') divobjs[i].style.display = 'none';
	if (num<1) for (var i=0; i<divobjs.length; i++) if (divobjs[i].id.substr(0,9)=='videocont') if (num<1) num = parseInt(divobjs[i].id.substr(9));
	if (num>0){
		if (document.getElementById('videocont'+num)) document.getElementById('videocont'+num).style.display = '';
		if (document.getElementById('videomenu'+num)) document.getElementById('videomenu'+num).style.display = 'none';
		if (document.getElementById('videomenuact'+num)) document.getElementById('videomenuact'+num).style.display = '';
	}
}
function	emailCheck (emailStr) 
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) return false;
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++)	if (user.charCodeAt(i)>127) 	return false;
	for (i=0; i<domain.length; i++)	if (domain.charCodeAt(i)>127)	return false;
	if (user.match(userPat)==null) 	return false;
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {	for (var i=1;i<=4;i++) if (IPArray[i]>255) return false;
		return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) if (domArr[i].search(atomPat)==-1)	return false;
	if (checkTLD && domArr[domArr.length-1].length!=2 &&	domArr[domArr.length-1].search(knownDomsPat)==-1)			return false;
	if (len<2)		return false;
	return true;
}