/* JavaScript file */

function dynamicTextChange(textValue,elementId) {
	document.getElementById(elementId).innerText = textValue;
}

modelMatrixVisibility = 1;

function onOffModelMatrix() {
	if ( modelMatrixVisibility == 1) {
		document.getElementById('modelMatrix').style.visibility = 'visible';
		document.getElementById('modelMatrixBackground').style.visibility = 'visible';
		modelMatrixVisibility = 0;
	}
	else {
		document.getElementById('modelMatrix').style.visibility = 'hidden';
		document.getElementById('modelMatrixBackground').style.visibility = 'hidden';
		modelMatrixVisibility = 1;
	}
}

function hideIt(objid) {
	var el = document.getElementById(objid);
	if (el) { // element found
		el.style.display = 'none';
	}
}

function showIt(objid) {
	var el = document.getElementById(objid);
	if (el) { // element found
		el.style.display = 'block';
	}
}

function additionalStyles() {
	if ( screen.width > 1024 ) {
		document.write('<LINK href="/styles/more1024.css" rel="stylesheet" type="text/css">');
	}
}

function changemodelcolor(imgpath) {
	document.images.coloredmodel.src = imgpath;
}	

function monthlypay(credit,credit_period,credit_percent,dist) {
	
	if ( credit_period > 0 ) {
			credit_period_value=credit_period;
	} else {
			credit_period_value=1;
	}	 

	if (credit_percent > 0 ) {
		creditsum = credit*((credit_percent/(100*12))/(1-Math.pow(1+(credit_percent/(100*12)),-(credit_period_value))));
	} else {
		creditsum = credit / credit_period_value;
	}
	dist.value = Math.round(creditsum*100)/100;
	return true;
}

function checkminmax(obj,min,max) {
	if ( obj.value > max) {
		obj.value = max;
	}
	if ( obj.value < min) {
		obj.value = min;
	}
}

var wnd = null;
/* pop-up онлайн-склад - заказ */  
function wfloat (url,wname,params) {
	url   = (url) ? url : '';
	wname =  (wname) ? wname : 'wfloat';
	params = (params) ? params : 'width=640,height=640,scrollbars=no';
	if(!wnd || wnd.closed)
		wnd = open (url,wname,params);
	else{
		if (url && wnd.location.pathname+wnd.location.search!=url ) {wnd.location = url;}
		wnd.focus()
	}
}


var wndsmall = null;
function wfloatsmall () {
	if(!wnd || wnd.closed)
		wndsmall = open ('','wfloatsmall','width=507,height=350,scrollbars=no');
	else{
//	if(wndsmall.location.pathname+wndsmall.location.search!=url) wndsmall.location=url
		wndsmall.focus()
	}
}

var wndmid = null;
function wfloatmid () {
	if(!wndmid || wndmid.closed)
		wndmid = open ('','wfloatmid','width=507,height=350,scrollbars=yes');
	else {
		wndmid.location=url
		wndmid.focus()
	}
}

function swDiv (objid) {
 	eval ("obj = document.all."+objid)
		if ( objid == 'mapmain' ) {
	    maindiv.innerHTML=smap;
		} else {
      maindiv.innerHTML=obj.innerHTML;
		}
 }

function OpenWindowImage (url,alt,width,height) {
	wnd = null;
	var url = url || '';
	var alt = alt || '';
	var width = width || 300;
	var height = height-19 || 300;
	var size = 'width='+width+',height='+height;
	wnd = window.openwnd (url,'wfloat',size+'toolbar=no,menubar=yes,scrollbars=no,status=no');
	wnd.document.open();
	wnd.document.open();
	wnd.document.write("<html><head><script>function resize(){");
	wnd.document.write("self.resizeTo (photo.width+12,photo.height+52); } onload=resize;");
	wnd.document.write("</script><title>Увеличенное изображение</title></head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0><img src='"+url+"' alt='"+alt+"' border='0' vspace='0' hspace='0' id='photo'/><body></html>");
  wnd.document.close();
}
function hide() {
   if (navigator.appName.index.Of('Mozilla')) {
    document.write ('<div style="display:none;">');
}}
function hide2() {
   if (navigator.appName.index.Of('Mozilla')) {
   document.write ('</div>');
}}


/* new calc */

function SwitchCircle(id,price,pos,uniquegroup) {
//alert (id);
price = price * 1;
if (uniquegroup != '')
{
	eval('var group = window.group'+uniquegroup+';');
	eval('var priceGroup = window.priceGroup'+uniquegroup+';');
	if (group != 0 && group != id)
	{
		//alert(group);
		var off = document.getElementById(group);
		off.src=circle.src;
		off.alt='Опция, не включенная в комплектацию';
		if (priceGroup == 0) { document.getElementById('price'+pos).innerHTML=parseInt(document.getElementById('price'+pos).innerHTML)-price; } else { document.getElementById('price'+pos).innerHTML=parseInt(document.getElementById('price'+pos).innerHTML)-priceGroup; }
		document.getElementById('pricecredit'+pos).value=document.getElementById('price'+pos).innerHTML;
	}
	if (group != 0 && document.getElementById(group).src==disc.src && group == id) { eval('window.group'+uniquegroup+' = 0;'); } else { eval('window.group'+uniquegroup+' = '+id+';'); }
	if (group != 0 && document.getElementById(group).src==disc.src && group == id) { eval('window.priceGroup'+uniquegroup+' = 0;'); } else { eval('window.priceGroup'+uniquegroup+' = '+price+';'); }
}

var img=document.getElementById(id);
if (img.src==circle.src) {
	img.src=disc.src;
	img.alt='Опция, включенная в комплектацию';
	document.getElementById('price'+pos).innerHTML=parseInt(document.getElementById('price'+pos).innerHTML)+price;
} else {
	img.src=circle.src;
	img.alt='Опция, не включенная в комплектацию';
	document.getElementById('price'+pos).innerHTML=parseInt(document.getElementById('price'+pos).innerHTML)-price;
}
	document.getElementById('pricecredit'+pos).value=document.getElementById('price'+pos).innerHTML;
return false;
}

function Setintegration(id) {
	if (document.getElementById('integration_'+id).checked==0) {
		//вырубили
		eval("document.getElementById('integration_"+countint+"id').value='0'");
		countint--;
	} else {
		//врубили
		if (countint<2) {
		countint++;
		eval("document.getElementById('integration_"+countint+"id').value="+id);
		} else {
			alert("Вы можете выбрать не больше 2 комплектации!");
			document.getElementById('integration_'+id).checked=0;
		}
	}
}

function Checkthisfunkyform(){
	if (countint<1) {
		alert("Необходимо выбрать вариант комплектации");
		return false;
	}
}
	
function SwitchComplect(i) {
	var id=document.getElementById('mod'+i).value;
	//alert(id);
	eval("document.getElementById('divmodel'+oldcomp"+i+"+'_'+i).style.display='none';");
	document.getElementById('divmodel'+id+'_'+i).style.display="block";
	eval("oldcomp"+i+"=id;");
}


function CountCalc1() {
	var inspercent1=parseFloat(document.getElementById('strpercent1').innerHTML);
	var insurance1=Math.round(autocost1*inspercent1/100);
	document.getElementById('insurance1').innerHTML=insurance1;

	var flag1=document.getElementById('incins1').value;
	if (flag1==0) {
		var creditprice1=autocost1-Math.round((autocost1*paymin1/100));
		document.getElementById('insurance1s').innerHTML=insurance1;
	} else {
		var creditprice1=autocost1-Math.round((autocost1*paymin1/100))+insurance1;
		document.getElementById('insurance1s').innerHTML="0";
	}
	document.getElementById('creditprice1').innerHTML=creditprice1;
	/* var monthpay1=Math.round(creditprice1/time1); - old version */
	if(percent1==0) {
		var monthpay1=Math.round(creditprice1/time1);
		document.getElementById('monthpay1').innerHTML=monthpay1;
	} else {
		var pn1=Math.pow((1+percent1/12/100),time1);
		var monthpay1=Math.round(creditprice1*(percent1/12/100/(1-1/pn1)));
		document.getElementById('monthpay1').innerHTML=monthpay1;
	}
	var familyvalue1=Math.round(monthpay1/0.4);
	document.getElementById('familyvalue1').innerHTML=familyvalue1;
	var totalpercent1=Math.round(creditprice1*(time1/12*percent1)/100);
	document.getElementById('totalpercent1').innerHTML=totalpercent1;
	var minvalue1=Math.round(autocost1*paymin1/100);
	document.getElementById('minvalue1').innerHTML=minvalue1;
	var comisstotal1=komiss1;
	document.getElementById('comisstotal1').innerHTML=comisstotal1;
	var total1=minvalue1+parseInt(document.getElementById('osago1').innerHTML);
	if (flag1==0) {
		total1=total1+insurance1;
	}
	document.getElementById('total1').innerHTML=total1;
}

function CountCalc2() {
	var inspercent2=parseFloat(document.getElementById('strpercent2').innerHTML);
	var insurance2=Math.round(autocost2*inspercent2/100);
	document.getElementById('insurance2').innerHTML=insurance2;
	var flag2=document.getElementById('incins2').value;
	if (flag2==0) {
		var creditprice2=autocost2-Math.round((autocost2*paymin2/100));
		document.getElementById('insurance2s').innerHTML=insurance2;
	} else {
		var creditprice2=autocost2-Math.round((autocost2*paymin2/100))+insurance2;
		document.getElementById('insurance2s').innerHTML="0";
	}
	document.getElementById('creditprice2').innerHTML=creditprice2;
	if(percent2==0){
		var monthpay2=Math.round(creditprice2/time2);
		document.getElementById('monthpay2').innerHTML=monthpay2;
	} else {
		var pn2=Math.pow((1+percent2/12/100),time2);
		var monthpay2=Math.round(creditprice2*(percent2/12/100/(1-1/pn2)));
		document.getElementById('monthpay2').innerHTML=monthpay2;
	}
	var familyvalue2=Math.round(monthpay2/0.4);
	document.getElementById('familyvalue2').innerHTML=familyvalue2;
	var totalpercent2=Math.round(creditprice2*(time2/12*percent2)/100);
	document.getElementById('totalpercent2').innerHTML=totalpercent2;
	var minvalue2=Math.round(autocost2*paymin2/100);
	document.getElementById('minvalue2').innerHTML=minvalue2;
	var comisstotal2=komiss2;
	document.getElementById('comisstotal2').innerHTML=comisstotal2;
	var total2=minvalue2+parseInt(document.getElementById('osago2').innerHTML);
	if (flag2==0) {
		total2=total2+insurance2;
	}
	document.getElementById('total2').innerHTML=total2;
}

var elementId = 'no';
	
		function activeMenu (id)
		{
			if (id != 'no' || window.elementId != 'no')
			{
				if (id == 'no' && window.elementId != 'no') { document.getElementById(window.elementId).style.display = 'none'; }
				else { document.getElementById(id).style.display = 'block'; }
				window.elementId = id;
			}
		}

function loadPage(pageURL){
	location.href = pageURL.options[pageURL.selectedIndex].value
}

// проверка корректного e-mail

function check_email(s)

{ 
flag=true;
	if (window.RegExp) {
		st="a";ex=new RegExp(st);
		if (st.match(ex)) {
			r1=new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
			r2=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
			b=(!r1.test(s)&&r2.test(s));
		} else { flag=false; }
	} else { flag=false; }
	if(!flag) b=(s.indexOf("@")>0&&s.indexOf(".")>0&&s!=""&&s!="Введите e-mail");
	return (b);
}
function f(param) { param.focus();param.select(); }
function check(email)
{	if(val(email)) return true;
		else return false;
}
function val(param)
{	s=param.value;
	if(check_email(s)) { return true;
	} else {
		alert("Пожалуйста, введите корректный e-mail адрес.");
		f(param);return false; }
}