var IsLoaded=false;
var Host;

function SetVariables(_Host){
	IsLoaded=true;
	Host=_Host;
}

//Fuels equipment packages positions (in the table)
function FuelPositions(){
	Count=0;
	while(Position=document.getElementById('left_pos_name_'+Count))
		document.getElementById('right_pos_name_'+Count++).innerHTML='<div><div class="position_name">'+Position.innerHTML+'</div></div>';
	Count=0;
	while(Position=document.getElementById('pos_header_'+Count++))
		document.getElementById('position_header').innerHTML+=Position.innerHTML;		
	Count=0;
	while(Position=document.getElementById('left_section_header_'+Count))
		document.getElementById('right_section_header_'+Count++).innerHTML+='<div><div class="position_name">'+Position.innerHTML+'<div></div>';		
}

//Changes option price and calculates total quipment package price
function ChangePrice(PriceX,PriceY){
	JSTotalPrices[PriceX]+=JSPrices[PriceX][PriceY];
	JSOptionPrices[PriceX]+=JSPrices[PriceX][PriceY];
	JSPrices[PriceX][PriceY]=-JSPrices[PriceX][PriceY];

	document.getElementById('option_price_'+PriceX).innerHTML=DisplayPrice(JSOptionPrices[PriceX])+'&nbsp;$';
	if(JSBasePrices[PriceX])
		document.getElementById('total_price_'+PriceX).innerHTML=DisplayPrice(JSTotalPrices[PriceX])+'&nbsp;$';
}

//Displays price with whitespaces between thousands and other
function DisplayPrice(Price){
	Thousands=(Math.floor(Price/1000));
	if(Thousands){
		Others=new String('00'+(Price%1000));
		Others=Others.substr(Others.length-3,3);
	}	
	else
		Others=Price%1000;
	return (Thousands?Thousands+' ':'')+Others;
}

//Opens wallpaper window
function DisplayWallpaper(URL){
	var Top=(screen.availWidth-1024)/2;
	var Left=(screen.availHeight-768)/2;
	Wallpaper=window.open(URL,'wallpaper','top='+Top+',left='+Left+',width=1024,height=768,menubar=no,toolbar=no,resizable=no,scrolling=no,location=no,status=no');
	Wallpaper.moveTo(Top,Left);
	Wallpaper.focus();
}

//Stars
function StarOver(Group,Position){
	Count=1;
	while(Star=document.getElementById('star_'+Group+'_'+Count))
		Star.className=(Position>Count++)?"star star_hover":"star";
}
function StarOut(Group,Position){
	Count=1;
	while(Star=document.getElementById('star_'+Group+'_'+Count))
		Star.className=(document.getElementById('star_'+Group).value>=Count++)?"star star_hover":"star";
}
function StarFix(Group,Position){
	document.getElementById('star_'+Group).value=Position;
	document.getElementById('checker_'+Group).className="checker checked";
	return false;
}
function StarUnfix(Group){
	document.getElementById('star_'+Group).value=0;
	document.getElementById('checker_'+Group).className="checker unchecked";
	StarOut(Group,0);
	return false;
}
function UnfixAll(){
	Group=0;
	while(Star=document.getElementById('star_'+Group))
		StarOut(Group++,0);
}

//Symbols
function CountSymbols(){
	Response=document.getElementById('response');
	Symbols=document.getElementById('symbols');
	Text=new String(Response.value);
	Text=Text.replace(/(^\s+)|(\s+$)/g,'');
	Words=Text.split(/\s+/g);
	WordsCount=(Text=='')?0:Words.length;
	Symbols.innerHTML='Введено слов: '+WordsCount;
	return false;
}

function CheckWords(){
	if(MinWordsCount>WordsCount){
		alert('Вы должны ввести как минимум '+MinWordsCount+' слов для отзыва.');
		return false;
	}
	return true;
}

function SubmitNewOpinion(){
	if(!(AddOpinion=document.getElementById('add_opinion_form')))
		return false;
	if(CheckWords()) 
		AddOpinion.submit();
	return false;
}

//Popups
function ShowPopup(ID){
	if(!(Block=document.getElementById('popup_'+ID)))
		return false;
	Block.style.visibility='visible';
	return true;
}
function HidePopup(ID){
	if(!(Block=document.getElementById('popup_'+ID)))
		return false;
	Block.style.visibility='hidden';
	return true;
}

//CarSelections
var FormProducerID;
var FormModelID;
var FormEngineID;
var ProducerPosition=-1;
var ModelPosition=-1;

function FuelCarSelections(){
	ProducerSelection=document.getElementById('manufact_id');
	ModelSelection=document.getElementById('model_id');
	EngineSelection=document.getElementById('engine_id');
	if(!(ProducerSelection && ModelSelection && EngineSelection))
		return false;

	ProducerSelection.options.length=JSProducers.length+1;
	ProducerSelection.options[0]=new Option('Сделайте выбор',0);
	for(i=1;i<JSProducers.length+1;i++){
		ProducerSelection.options[i]=new Option(
			JSProducers[i-1]['manufact_name'],
			JSProducers[i-1]['manufact_id'],
			(JSProducers[i-1]['manufact_id']==FormProducerID)?true:false
			);
	}

	if(FormModelID){
		ChangeProducer();	
		if(FormEngineID)
			ChangeModel();
		else {
			EngineSelection.disabled=true;
			EngineSelection.options.length=0;
		}
	}
	else {
		ModelSelection.disabled=true;
		ModelSelection.options.length=0;
		EngineSelection.disabled=true;
		EngineSelection.options.length=0;
	}
	return true;
}
function ChangeProducer(){
	ProducerSelection=document.getElementById('manufact_id');
	ModelSelection=document.getElementById('model_id');
	EngineSelection=document.getElementById('engine_id');
	if(!(ProducerSelection && ModelSelection && EngineSelection))
		return false;

	ProducerID=ProducerSelection.options[ProducerSelection.options.selectedIndex].value;
	for(i=0;i<JSProducers.length;i++)
	if(JSProducers[i]['manufact_id']==ProducerID){
		ModelSelection.options.length=JSModels[i].length+1;
		ModelSelection.options[0]=new Option('Сделайте выбор',0);
		for(j=1;j<JSModels[i].length+1;j++){
			ModelSelection.options[j]=new Option(
				JSModels[i][j-1]['model_name'],	
				JSModels[i][j-1]['model_id'],
				(JSModels[i][j-1]['model_id']==FormModelID)?true:false
				);
		}
		ProducerPosition=i;
		ModelSelection.disabled=false;
		EngineSelection.disabled=true;
		EngineSelection.options.length=0;
		return true;
	}
	ProducerPosition=-1;
	ModelSelection.disabled=true;	
	ModelSelection.options.length=0;
	EngineSelection.disabled=true;
	EngineSelection.options.length=0;
	return false;
}
function ChangeModel(){
	ModelSelection=document.getElementById('model_id');
	EngineSelection=document.getElementById('engine_id');
	if(!(ModelSelection && EngineSelection))
		return false;

	ModelID=ModelSelection.options[ModelSelection.options.selectedIndex].value;
	for(i=0;i<JSModels[ProducerPosition].length;i++)
	if(JSModels[ProducerPosition][i]['model_id']==ModelID){
		EngineSelection.options.length=JSEngines[ProducerPosition][i].length+1;
		EngineSelection.options[0]=new Option('Сделайте выбор',0);
		for(j=1;j<JSEngines[ProducerPosition][i].length+1;j++){
			EngineSelection.options[j]=new Option(
				JSEngines[ProducerPosition][i][j-1]['engine_name'],
				JSEngines[ProducerPosition][i][j-1]['engine_id'],
				(JSEngines[ProducerPosition][i][j-1]['engine_id']==FormEngineID)?true:false
				);
		}
		EngineSelection.options[j]=new Option(
			'Свой вариант...',
			'advanced',
			FormEngineID=='advanced'?true:false
			);
		ModelPosition=i;
		EngineSelection.disabled=false;
		return true;
	}
	ModelPosition=-1;
	EngineSelection.disabled=true;	
	EngineSelection.options.length=0;
	return false;
}

/*--------------------------------------------------------*/

function HandleAdvanced(){
	if(!(EngineID=document.getElementById('engine_id')))
		return false;
	if(!(Advanced=document.getElementById('advanced')))
		return false;
	if(EngineID.options[EngineID.options.selectedIndex].value=='advanced')
		Advanced.style.display='block';
	else
		Advanced.style.display='none';
	return false;
}

/*--------------------------------------------------------*/

function SubmitSearch(){
return true;
	if(!IsLoaded){
		setTimeout("SubmitSearch()",500);
		return false;
	}
	if(!(String=document.getElementById('search_string')))
		return false;

if(String.value.indexOf('123')==0)
alert(String.value+'|'+encodeURI(String.value));

	document.location=Host+'search/'+String.value+'/';
	return false;
}

/*--------------------------------------------------------*/

function CollapseSelection(SelectionID,Link){
	if(!(S=document.getElementById('selection_'+SelectionID)))
		return false;
	if(S.style.display=='none'){
		S.style.display='block';
		Link.className='minus';
	}
	else {
		S.style.display='none';
		Link.className='plus';
	}
	return false;
}
