function searchClick(){
	var query = trim( document.getElementById('query').value );
	if ( (query == '') || (query == 'Поиск') ){
		alert('Введите одно или несколько слов');
		return false;
	}
	/*var fieldAll = searchFieldValue('All');
	var fieldAuthor = searchFieldValue('Author');
	var fieldTitle = searchFieldValue('Title');
	var fieldKeyWords = searchFieldValue('KeyWords');
	if( !fieldAll && !fieldAuthor && !fieldTitle && !fieldKeyWords ){
		alert("Выберите поля поиска");
		return false;
	} */
	document.location.href = "/main/searchResults.html?Query="+query;
}

function searchFieldValue( field ){
	var cb = document.getElementById('field'+field);
	if( cb.checked )
		return 1;
	else
		return 0;
}

function selectDistrict( district ){
	document.location.href = "/main/index.html?id=9&District=" + district;
}

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

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

function loadSubPageMenu(){
	var leftMenu = document.getElementById('subPageMenu');
	if( leftMenu ){
		document.getElementById('leftMenuDiv').innerHTML = leftMenu.innerHTML;
	}
}

function pageLoaded(){
	loadSubPageMenu();
}


function selectModel( id ){
	loadSubPage( "/index.html?id="+id);
	var InternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
	var m;
	if( InternetExplorer )
		m = document.getElementById('flash');
	else
		m = document.embeds[0];
	m.SetVariable("ActiveModelID", id);
}

var selectedColor;

function selectCarColor( rgb ){
	var c = document.getElementById('color'+rgb);
	if( c )
		selectedColor = c.innerHTML;
}

function colorClickNext(){
	if( !selectedColor ){
		alert('Выберите цвет');
		return false;
	}
	var url = document.getElementById('colorBaseURL').innerHTML + selectedColor;
	var re = /\&amp\;/g;
	url = url.replace(re,"&");
	loadSubPage(url);	
}

function sendTestDrive( pageId, modelId, form ){
	if( trim( form['first_name'].value ) == '' ){
		alert('Поле Фамилия должно быть заполнено');
		form['first_name'].focus();
		return false;
	}
	if( trim( form['last_name'].value ) == '' ){
		alert('Поле Имя должно быть заполнено');
		form['last_name'].focus();
		return false;
	}
	if( trim( form['phone'].value ) == '' ){
		alert('Поле Телефон должно быть заполнено');
		form['phone'].focus();
		return false;
	}
	form.submit;
	/*cp.call("/main/ajax.html", "SendOrder", sendTestDriveResults, 
		form['car'].value, 
		form['first_name'].value,
		form['last_name'].value,
		form['phone'].value,
		form['email'].value,
		form['note'].value );*/
}

function sendTestDriveResults( results ){
	document.getElementById('modulesContent').innerHTML = results;
}


function cfgTextClickNext(){
	var nextValue = radioValue( document.forms['configurator'], 'cfgRadio' );
	if( nextValue == '' ){
		alert('Выберите опцию');
		return false;
	}
	var href = document.forms['configurator']['radio'+nextValue].value;
	loadSubPage( href );
}

function cfgShowPackageDetails(){
	var selected = radioValue( document.forms['configurator'], 'cfgRadio' );
	var s = document.getElementById('radioPackageDiv').innerHTML;
	document.getElementById('radioPackageDiv').innerHTML = document.getElementById('longPackageDiv').value;
	document.getElementById('longPackageDiv').value = s;
	setRadioValue( document.forms['configurator'], 'cfgRadio', selected );
}

function calcCredit(){
	var firstPay = document.getElementById('creditFirstPay').value;
	var monthes = document.getElementById('creditMonthes').value;
	var payment = document.getElementById('creditPayment');
	payment.innerHTML = document.getElementById('monthPay_'+firstPay+'_'+monthes).value;
	var form = document.forms['configurator'];
	var firstPayText = document.getElementById('creditFirstPay').options[firstPay].text;
	var monthesText = document.getElementById('creditMonthes').options[monthes].text;
	var paymentText = payment.innerHTML;
	var nextLink = document.getElementById('nextButtonLink');
	nextLink.href = document.getElementById('nextLinkBase').value
	+ '&credit=1&firstPay=' + url_encode( firstPayText ) 
	+ '&monthes=' + url_encode( monthesText )
	+ '&payment=' + url_encode( paymentText );
}

  function subfm(f,v,t)
  {                    
    f.upfile.value = v;
    f.upfile_type.value = t;
    f.check.value = "";
    f.submit();
  }

function printOrder(){
	openPrintWindow( '/main/printOrder.html', 900, 650 );
}
  
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 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 viz = document.getElementById('vizDiv');
	var vizBg = document.getElementById('topLogo');
	viz.style.left = getRelLeft( vizBg ) + 180;
	hideViz();
	viz.style.visibility = "visible";
}

function showViz(){
	var viz = document.getElementById('vizDiv');
	viz.style.top = 0;
	var vizOpenArrow = document.getElementById('vizOpenArrow');
	vizOpenArrow.src = '/img/viz-arrow-up.gif';
}

function hideViz(){
	var viz = document.getElementById('vizDiv');
	var vizTable = document.getElementById('vizTable');
	viz.style.top = -1 * vizTable.clientHeight + 33;
	var vizOpenArrow = document.getElementById('vizOpenArrow');
	vizOpenArrow.src = '/img/viz-arrow-down.gif';
}
