function selectModel(){
	var id = document.getElementById('modelSelect').value;
	document.getElementById('modelSelectLink').href = "/index.html?id="+id;
	var banner = document.getElementById('activeModelBanner');
	setTimeout( 'changeModel()', 400 );
	new Effect.SlideUp(banner, {duration: 0.4}); 
	new Effect.SlideDown(banner, {duration: 0.4, queue: 'end'}); 
}

function changeModel(){
	var id = document.getElementById('modelSelect').value;
	document.getElementById('activeModelBanner').innerHTML = document.getElementById('modelBanner'+id).innerHTML;
}

function openPrintWindow( pageToLoad, width, height) {
  xposition=0; yposition=0;
  if ((parseInt(navigator.appVersion) >= 4 ) ) {
   xposition = (screen.width - width) / 2;
   yposition = (screen.height - height) / 2;
  }
  args = "width=" + width + ","
  + "height=" + height + ","
  + "location=0,"
  + "menubar=1,"
  + "resizable=1,"
  + "scrollbars=1,"
  + "status=0,"
  + "titlebar=0,"
  + "toolbar=1,"
  + "hotkeys=0,"
  + "screenx=" + xposition + "," //NN Only
  + "screeny=" + yposition + "," //NN Only
  + "left=" + xposition + "," //IE Only
  + "top=" + yposition; //IE Only
  window.open( pageToLoad, 'print', args );
}

function sendContactForm( form ){
	if( trim( form['name'].value ) == '' ) 
		{ alert( 'Поле Имя должно быть заполнено' ); form['name'].focus(); return false; }
	if( trim( form['message'].value ) == '' ) 
		{ alert( 'Поле Сообщение должно быть заполнено' ); form['message'].focus(); return false; }
	form.action = "/index.html?id=28";
   	form.submit();	
}

function sendSubscribeForm( form ){
	if( trim( form['Email'].value ) == '' ) 
		{ alert( 'Поле Email должно быть заполнено' ); form['Email'].focus(); return false; }
	form.action = "/index.html?id=103";
   	form.submit();	
}

function selectPackage(){
	var form = document.forms['package'];
	var selection = radioValue( form, 'package' );
	if( selection == '' ){
		alert('Выберите комплектацию');
		return false;
	}
	return true;
}

function minusyear(){
	var form = document.forms['calendar'];
	var fYear = document.getElementById('CalendarYear').value;
	fYear = fYear-1;
	document.getElementById('CalendarYear').value = fYear;
	form.submit();

}

function plusyear(){
	var form = document.forms['calendar'];
	var fYear = document.getElementById('CalendarYear').value;
	fYear++;
	document.getElementById('CalendarYear').value = fYear;
	form.submit();

}

function zagruzka(n){
	document.getElementById('CalendarMonth').value=n;
	document.forms['calendar'].submit();
	}  

function searchClick(a){
	var f = document.forms[a]; 
	if( ( trim( f['Query'].value ) == '' ) || ( f['Query'].value == 'Слово для поиска' ) ){
		alert('Укажите ключевые слова для поиска');
		return false;
	}
	ignoreBodyClick = true;
	f.submit();
	return true;
}

function getRelLeft(el) {
	var pos = el.offsetLeft;
	while (el.offsetParent != null) { el = el.offsetParent; pos += el.offsetLeft; if (el.tagName == 'BODY') break; }
	return pos;
}

function getRelTop(el) {
	var pos = el.offsetTop;
	while (el.offsetParent != null) { el = el.offsetParent; pos += el.offsetTop; if (el.tagName == 'BODY') break; }
	return pos;
}

function getBodyHeight() {
	if (self.innerWidth) return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientHeight;
	else if (document.body) return document.body.clientHeight;
}

function getBodyWidth() {
	if (self.innerWidth) return self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientWidth;
	else if (document.body) return document.body.clientWidth;
}

function startAlignDivs() {
	window.onscroll = alignDivs;
	window.onresize = alignDivs;
	alignDivs();
}

function alignDivs(){
	var gazLogo = document.getElementById('gazLogo');
	var gazCellBg = document.getElementById('gazCellBg');
	gazLogo.style.left = getRelLeft( gazCellBg ) + 75;
	gazLogo.style.visibility = "visible";
}

function selectRegion( link ){
	var linkDiv = document.getElementById('regionLinkDiv');
	var content = document.getElementById('selectRegionDiv').innerHTML;
	ignoreBodyClick = true;
	showPopup( getRelLeft( link ), getRelTop( link ) + linkDiv.clientHeight + 7, content, true );
}

var autoHidePopup = false;
var hidePopupTimer = 0;
var inPopup = false;

function showPopup( left, top, content, autoHide ){
	hidePopup();
	var anounceDiv = document.getElementById('anounce');
	anounceDiv.innerHTML = content;
	anounceDiv.style.left = left + "px";
	var h = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	h += getBodyHeight();
	anounceDiv.style.top = top + "px";
	anounceDiv.style.visibility = 'visible';
	var shadowImg = document.getElementById('anounceBg');
	shadowImg.style.width = ( anounceDiv.clientWidth + 36 ) + "px";
	shadowImg.style.height = ( anounceDiv.clientHeight + 33 ) + "px";
	shadowImg.style.top = ( top - 12 ) + "px";
	shadowImg.style.left = ( left - 15 ) + "px";
	var midRow = document.getElementById('anounceMiddleRow');
	midRow.style.height = ( anounceDiv.clientHeight - 25 ) + "px";
	shadowImg.style.visibility = 'visible';
	autoHidePopup = autoHide;
	inPopup = false;
}

function clearPopupTimer(){
	if( hidePopupTimer != 0 ){
		clearTimeout( hidePopupTimer );
		hidePopupTimer = 0;
	}
}

function overPopup(){
	clearPopupTimer();
	inPopup = true;
}

function outPopup(){
	clearPopupTimer();
	if( autoHidePopup )
		hidePopupTimer = setTimeout( 'hidePopup()', 1000 );
	inPopup = false;
}

function hidePopup(){
	var anounceDiv = document.getElementById('anounce');
	anounceDiv.style.visibility = 'hidden';
	var shadowImg = document.getElementById('anounceBg');
	shadowImg.style.visibility = 'hidden';
	clearPopupTimer();
	inPopup = false;
}

function askQuestion(){
	var content = document.getElementById('askQuestionDiv').innerHTML;
	var anounceDiv = document.getElementById('anounce');
	anounceDiv.innerHTML = content;
	var left = ( getBodyWidth() - anounceDiv.clientWidth )-45-(getBodyWidth()-1024)*0.42;
	var offset;
	/*if (document.all) 
		offset = document.documentElement.scrollTop;
	else
		offset = pageYOffset; */
	/*var top = offset + ( getBodyHeight() - anounceDiv.clientHeight )/5 ;*/
	var top = 150;
	ignoreBodyClick = true;
	showPopup(left, top, content, false );
}

var ignoreBodyClick = false;

function bodyClick(){
/*	var anounceDiv = document.getElementById('anounce');
	if( !ignoreBodyClick && !inPopup && ( anounceDiv.style.visibility == 'visible' ) )
		hidePopup();*/
	ignoreBodyClick = false;
}

function highlightProduct( product_id, highlight ){
	var table = document.getElementById('product_'+product_id);
	if( highlight )
		table.style.backgroundColor = '#E0E1E5';
	else
		table.style.backgroundColor = 'white';
}

function switchSection( section_id ){
	var sectionContent = document.getElementById('section_'+section_id+'_content');
	var sectionTitle = document.getElementById('section_'+section_id+'_title');
	var sectionNumber = document.getElementById('section_'+section_id+'_number');
	if( sectionContent.className == 'closedSectionContent' ){
		sectionContent.className = 'openSectionContent';
		sectionTitle.className = 'openSection';
		sectionNumber.className = 'openNumber';
		setCookie(productListMode+'_'+section_id, '1');
	}
	else{
		sectionContent.className = 'closedSectionContent';
		sectionTitle.className = 'closedSection';
		sectionNumber.className = 'closedNumber';
		setCookie(productListMode+'_'+section_id, '0');
	}
	if( window.event )
		window.event.returnValue = false;
	return false;
}

function setCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue) + ";expires=Mon, 3 Dec 2040 23:59:59 UTC;path=/;";
}

function getCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

function showMidMenu( col, show ){
	if( !midMenuEnabled )
		return false;
	var midCol = document.getElementById('midCol'+col);
	var midPopup = document.getElementById('midPopupMenu');
	if( midMenuTimeout != 0 ){
		clearTimeout( midMenuTimeout );
		midMenuTimeout = 0;
	}
	if( show ){
		if( ( activeMidMenu != col ) || ( midPopup.style.display != 'block' ) ){
			var subMenu = midMenu[col];
			if( subMenu.length > 0 ){
				html = "<div class=normal style='padding: 0px 0px;' id='popupMenu'>\n";
				for( n = 0; n < subMenu.length; n++ ){
					html = html + "<div class=midPopupRow>"+subMenu[n]+"</div>";
				}
				html = html + "</div>";
				midPopup.contentWindow.document.open();
				midPopup.contentWindow.document.writeln( "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\"><link rel='stylesheet' type='text/css' href='/main/style.css'/></head><body class=notPrintable style='background-color: #a8a8a8; margin: 0px; padding: 0px;' onload='parent.resizePopup()'>" );
				midPopup.contentWindow.document.writeln( html );
				midPopup.contentWindow.document.writeln( "</body></html>" );
				midPopup.contentWindow.document.close();
//	if (self.innerWidth) return self.innerHeight;
//	else if (document.documentElement && document.documentElement.clientWidth) return document.documentElement.clientHeight;
//	else if (document.body) return document.body.clientHeight;
//				alert( midPopup.contentWindow.document.getElementById('popupMenu').clientHeight );
//				midPopup.style.height = midPopup.contentWindow.document.clientHeight;
				midPopup.style.top = ( getRelTop( midCol ) + midCol.clientHeight ) + "px";
				if( col == 0 )
					midPopup.style.left = getRelLeft( midCol ) + "px";
				else
					midPopup.style.left = ( getRelLeft( midCol ) + 1 ) + "px";
				midPopup.style.width = midCol.clientWidth + "px";
				midPopup.style.display = 'block';
				setTimeout('resizePopup()', 100);
			}
			activeMidMenu = col;
		}
	}
	else{
		midPopup.style.display = 'none';
	}
}

function resizePopup(){
	var midPopup = document.getElementById('midPopupMenu');
	midPopup.style.height = midPopup.contentWindow.document.getElementById('popupMenu').scrollHeight + "px";
	//alert( midPopup.contentWindow.document.getElementById('popupMenu').scrollHeight );
}

function sendQuestion(){
	var form = document.forms['questionForm'];
	var name = trim( document.getElementById('questionName').value );	
	var email = trim( document.getElementById('questionEmail').value );
	var question = trim( document.getElementById('questionText').value );
	if( name == '' ){
		alert('Заполните Имя');
		return false;
	}
	if( question == '' ){
		alert('Заполните Вопрос');
		return false;
	}
	cp.call("/main/ajax.html", "SendQuestion", sendQuestionResults, name, email, question );
	document.getElementById('questionForm').style.display = 'none';
	document.getElementById('questionSent').style.display = 'block';
}

function showAZSFeedbackForm(){
	var content = document.getElementById('azsFeedbackFormDiv').innerHTML;
	var anounceDiv = document.getElementById('anounce');
	anounceDiv.innerHTML = content;
	var left = ( getBodyWidth() - anounceDiv.clientWidth ) / 2;
	var offset;
	if (document.all) 
		offset = document.documentElement.scrollTop;
	else
		offset = pageYOffset;
	var top = offset + ( getBodyHeight() - anounceDiv.clientHeight ) / 2;
	ignoreBodyClick = true;
	showPopup(left, top, content, false );
}

function sendAZSFeedbackForm(){
	var form = document.forms['azsFeedbackForm'];
	var name = trim( document.getElementById('questionName').value );	
	var email = trim( document.getElementById('questionEmail').value );
	var question = trim( document.getElementById('questionText').value );
	if( question == '' ){
		alert('Заполните Сообщение');
		return false;
	}
	cp.call("/main/ajax.html", "SendAZSFeedbackForm", sendAZSFeedbackFormResults, document.getElementById('azs_id').value, name, email, question );
	document.getElementById('azsFeedbackForm').style.display = 'none';
	document.getElementById('azsFeedbackSent').style.display = 'block';
}

function sendAZSFeedbackFormResults(){
	
}

function sendAZSFeedback( option ){
	cp.call("/main/ajax.html", "SendAZSFeedback", sendAZSFeedbackResults, document.getElementById('azs_id').value, option );
}

function sendAZSFeedbackResults( result ){
	document.getElementById('azsVoteDiv').innerHTML = "<br>Спасибо. Ваше мнение важно для нас.";
}


function sendQuestionResults( results ){
	
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function subscribeClick(){
	var f = document.forms['subscribeForm']; 
	var messageText;
	if( !f['Unsubscribe'].checked ) {
		f['MailAction'].value = 'subscribe'; 
		messageText = '<b>Спасибо за интерес к нашим новостям, Мы будем<br>держать вас в курсе всех важных событий.<br>Ваш e-mail добавлен в список рассылки.</b>';
	}
	else {
		messageText = '<b>Спасибо за интерес к нашим новостям.<br>Ваш e-mail удален из списка рассылки.</b>';
		f['MailAction'].value = 'unsubscribe'; 
	}
	if( ( trim( f['Email'].value ) == '' ) || ( f['Email'].value == 'e-mail' ) ){
		alert('Укажите e-mail');
		return false;
	}
	var re = /^[_a-zA-Z\d\-\.]+@([_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-]+)+)$/i;
	var email = f['Email'].value;
	if( email.search( re ) == -1 ){
		alert('Неверный формат e-mail');
		return false;
	}
	cp.call("/main/ajax.html", "Subscribe", subscribeResults, email, f['MailAction'].value );
	var left = ( getBodyWidth() - 396 ) / 2;
	var top = ( getBodyHeight() - 266 ) / 2;
	ignoreBodyClick = true;
	var s = document.getElementById('messageDiv').innerHTML;
	s = s.replace( '[MessageText]', messageText );
	showPopup(left, top, s, false );
	return true;
}


function subscribeResults( results ){
	
}

function showNewsPic( src, width, height ){
	var big = document.getElementById('bigPhoto');
	big.width = width;
	big.height = height;
	big.style.border = "1px solid #A0A0A4";
	big.style.marginBottom = "20px";
	big.src = src;
}

function selectMapRegion( n ){
	if( n )
		cp.call("/main/ajax.html", "LoadDealers", loadDealersResults, n );
}

function resetMapRegion(){
	cp.call("/main/ajax.html", "LoadDealers", loadDealersResults, 0 );
}

function loadDealersResults( results ){
	document.getElementById('mapContent').innerHTML = results;
}

function openAZS(url){
	document.getElementById('azsFeedbackDiv').style.visibility = 'visible';
	document.getElementById('azsFeedbackDiv').style.height = '200px';
	var mask = /element_id=(\d+)/;
	res = url.match(mask);
	if( res ){
		document.getElementById('azs_id').value = res[1];
	}
	loadSubPage(url);
}

function loadSubPage( url ){
	cp.call("/inc/ajax.html", "LoadPage", loadSubPageResults, url );
}

function loadSubPageResults( results ){
	document.getElementById('contentHTML').innerHTML = results;
}

