var interval;
var isIE;
var myHeight;
var topHeight;
var myBanner;
var myLM;
var pageHeight;
var etalon = 340;
var etalon1 = 350; //386
var currencyHeigh = 115;

var mypage_content = '';

var nbrb = new Array;
var cbrf = new Array;
var date;

var mouse_x;
var mouse_y;
var text1 = "Поиск по базе товары ДЛЯ БИЗНЕСА [Business to Business] ";
var text2 = "Поиск по базе товары/услуги ДЛЯ ПОТРЕБИТЕЛЯ [Businss to Clients] ";


var iTopHeight;
var iMypageHeight;
var iArrowHeight;
 



/**
 *
 * @access public
 * @return void
 **/
function $(id){
	return document.getElementById(id);
}


function detect() {
	var sUserAgent = navigator.userAgent;
	var isOpera = sUserAgent.indexOf("Opera") > -1;
	isIE = sUserAgent.indexOf("compatible") > -1 && sUserAgent.indexOf("MSIE") > -1 && !isOpera;
}

function show(el) {
	$(el).style.visibility = "visible";
}
function hide(el) {
   $(el).style.visibility="hidden";
}
var iTimeoutId = setTimeout('',100);

function warning(){
	var n;
	var text = document.getElementById("iField").value;
	n = text.indexOf(' ');
	if( n != -1){
		text = text.substr(n+1);
		//alert(text);
		n = text.indexOf(' ');
		if( n != -1){
			show("warning");
			clearTimeout(iTimeoutId);
			iTimeoutId = setTimeout("document.getElementById('warning').style.visibility='hidden';",2000);
		};
	};
}

function init_xy(e)
{
	if(isIE){
	mouse_x = window.event.x - 5;
	mouse_y = window.event.y + 5;
	}else{
	mouse_x = e.clientX + 5 - myLM ;
	mouse_y = e.clientY + 5 - myBanner;
	}
}
function showInfo(e){
	myBanner = document.getElementById("banner").clientHeight;
	//if ( document.getElementById("LM") ){
		myLM = document.getElementById("LM").clientWidth;
	//};
	init_xy(e);
	$("b2_info").style.visibility = "visible";
	$("b2_info").style.left = mouse_x + "px";
	$("b2_info").style.top = mouse_y + "px";
	$("b2_info").innerHTML = ( mouse_x < (85) ) ? text1 : text2;
}

function hideInfo(){
	document.getElementById("b2_info").style.visibility = "hidden";
	document.getElementById("b2_info").style.left = "50px";
	document.getElementById("b2_info").style.top = "200px";
}

/***********************************/
function set_myH (bH,banH,byH) {
        var sbH = $("poisk2bg").clientHeight+122+banH+50;
	bH = bH > sbH ? bH : sbH;
	myHeight = bH - byH - 177 - banH ;
	
}

function set_tH (myH,byH) {
	topHeight = myH+81;
	iTopHeight = myH+81;
}

function drawElements(resize) {
	setWidth();

	if( (iTopHeight && iMypageHeight) ) return true;
	
	var bodyHeight = $("size").clientHeight;
	var banHeight = $("banner").clientHeight;
	
	if ( $("mypage")) {
		var byHeight = $("c_by").clientHeight;
		
		set_myH(bodyHeight,banHeight,byHeight);
		
		if (isIE) {
			$("mypage").style.height = myHeight + "px" ;}
		else {
			$("mypage").style.height = "auto";
			$("mypage").style.minHeight = myHeight + "px";};
		var pHeight = $("mypage").clientHeight;
		set_tH(pHeight,byHeight);
		$("list").style.height = topHeight + "px";
		iMypageHeight = $("mypage").clientHeight-20;
		//iTopHeight  = $("list").clientHeight+37-20;
	};
	//results
	if ($("res")){
		
                var sbH = $("poisk2bg").clientHeight+122+banHeight+50;
	        bodyHeight = bodyHeight > sbH ? bodyHeight : sbH;
	        
		var ifHeight = bodyHeight - 200 - banHeight;
		if (ifHeight > etalon1)
			{$("res").height = ifHeight}
		else {$("res").height = etalon1 + "px"}
	};
}


//********************************************************************************

function showEl(el) {
	$(el).style.visibility = "visible";
}

function hideEl(el) {
   if($(el)){
		$(el).style.visibility="hidden";
		
		if(el=='f1') $(el).innerHTML='';
		if(el=='f2') $(el).innerHTML='';
		if(el=='f3') $(el).innerHTML='';
		if(el=='f4') $(el).innerHTML='';
		if(el=='res') $(el).innerHTML='';
	}
}

function loadEl(myF,newSrc,search) {
	if(myF!='res')
		showEl(myF);
	var info = newSrc.split('?');
	var aj = new ajax_object(info[0],'post', 'text');
	var data = new Array();
	var aTemp = info[1].split('&');
	for (var i=0;i<aTemp.length;i++)
	{
		data[i] = aTemp[i].split('=');
	}
	if(myF!='res')
	{
		$(myF).innerHTML = aj.request(data);
	}
	else
	{
		var result = aj.request(data);
		if(result=='NO_RESULT|Q' || result=='NO_RESULT|S')
		{
			var aParam = result.split('|');
			Destroy(search,aParam[1]);
		}
		else
		{
			if (document.getElementById("res"))
			{
				$(myF).innerHTML = result;
			}
			else 
			{
				var bH = $("size").clientHeight;
				var banHeight = $("banner").clientHeight;
				var sbH = $("poisk2bg").clientHeight+122+banHeight+50;
				bH = bH > sbH ? bH : sbH;
				
				var ifH = bH - 200 - banHeight + 1;
		
				if (ifH > etalon) { iframeH = ifH }else { iframeH = etalon};
				$("content").innerHTML = "<div id='caption'><img src='pictures/c-topLeft.gif' height='7' width='7' id='leftTot' alt=''><div id='top'></div><p>Выберите интересующий Вас товар/услугу<br>для перехода на следующую страницу</p><div id='sorting'><img id='left' height='26' width='6' src='pictures/res-bCentr.gif'><p>по алфавиту столбец №</p><div id='sel_div'></div></div></div><div id='res' style='overflow:scroll;height:" + ifH + "px'></div>  ";
				$("content").style.border = " solid #000000 1px";
				$("content").style.backgroundColor = "#FFFFFF";
				$(myF).innerHTML = result;
				Create_sel("sel",$('opt_count').innerHTML,"sel_div")
				Sort_goods('res','goods_content','a',0);
			}	
		}

	}
	if(myF=='f1') fr1source = newSrc;
	if(myF=='f2') fr2source = newSrc;
	if(myF=='f3') fr3source = newSrc;
	if(myF=='f4') fr4source = newSrc;
	if(myF=='res') ressource= newSrc;
}


function rePoisk (arrow) {

		var iTh = iTopHeight;
		var iMh = iMypageHeight;
	
	if (($("res")) || ($("newPage"))) {
		
		var mpc = arrow ? $("arrow_div").innerHTML : mypage_content;		
		if(arrow)
		{
			var h = $('content').clientHeight > $('poisk2bg').clientHeight ? $('content').clientHeight : $('poisk2bg').clientHeight;
			iTh = h - 55;
			iMh = h - 157;
		}
		
		// write
		$("content").style.border = "none";
                $("content").innerHTML = "<div id='top100'><img alt=' TOP 100 ' id='up' src='pictures/top100-top.gif' width='120' height='35'><div style='height:" + iTh + "px;overflow:scroll' id='list'></div><img alt=' TOP 100 ' id='bottom' src='pictures/top100-bot.gif' width='120' height='20'>	</div>" +
		"<div id='mypage' class='myPage' style='height:" + iMh + "px;min-height:" + iMh + "px;'><div id='top'></div><img alt='' src='pictures/c-topLeft.gif' height='7' width='7' id='leftTot'>"+mpc+"</div>" +
		"<div id='c_by' class='currency'>" +
		$('tab_head').innerHTML+"<div id='informer'>"+$('currency').innerHTML+"</div></div>";
		
		setWidth();
		$("content").style.backgroundColor = "";
		
		loadEl('list','top100ajax.php?');
	}
	else if(arrow)
	{

		
		var bH = $("size").clientHeight;
		var banHeight = $("banner").clientHeight;
		
		var sbH = $("poisk2bg").clientHeight+122+banHeight+50;
	        bH = bH > sbH ? bH : sbH;
	        
		var ifH = bH - 200 - banHeight + 50;
		
		var h = ifH;
		
		var iTh = h - 55;
		var iMh = h - 157;
		
		$('list').style.height = iTh+'px';
		$('mypage').style.height = iMh+'px';
		$('mypage').style.minHeight = iMh+'px';
		$('mypage').innerHTML = $("arrow_div").innerHTML;
			
		
	}
	else
	{
                if(iTh)
                {
		  $('list').style.height = iTh+'px';
		  $('mypage').style.height = iMh+'px';
		  $('mypage').style.minHeight = iMh+'px';	
		}
	}
}


function clear_P1(){
	$("iField").value='';
}

function hide_inf(par,num){
	for(var i=0;i<num;i++){
		var id = 'inf'+i;
		var informer = document.getElementById(id);
		if(informer){
		document.getElementById("counters").removeChild(informer);
		informer.style.visibility= par ? "hidden" : "visible";
		}
	}
}



function start1(myLink) {
	
	hideEl("f3");
	hideEl("f4");
	hideEl("lang_en");
	showEl("lang_ru");
	showEl("step2");
	showEl("f2");
	hideEl("step1");
	hideEl("step3");
	
	if($("mypage")){
		if(mypage_content!=''){
			$("mypage").innerHTML = mypage_content;
		}
	}else{
		rePoisk();
		if(mypage_content!=''){
			$("mypage").innerHTML = mypage_content;
		}
	}
	rePoisk();
	loadEl("f2",myLink);
	
	superLink = myLink;
	
	if( $("inf0") )
		hide_inf(false,5);
	
	clear_P1();
}

function start2 (myLink) {
	hideEl("f4");
	hideEl("step2");
	showEl("step3");
	if($("mypage")){
	if(mypage_content!=''){
		$("mypage").innerHTML = mypage_content;
		
	}
	}else{
		rePoisk();
		if(mypage_content!=''){
			$("mypage").innerHTML = mypage_content;
		}
	}
	rePoisk();
	loadEl("f3",myLink);
	clear_P1();

}

function start3 (myLink) {
	hideEl("step3");
	
	clearInterval(interval);
	
	rePoisk(true);
	
	loadEl("f4",myLink);
	clear_P1();
}

function start4 (myLink,search) {
	clear_P1();
	loadEl("res",myLink,search);
}

//* B2B & B2C ********************************************************************
function add_to_url_b(parametr,value,url){
	if(url.indexOf("?")==-1){
		url=url+"?"+parametr+"="+value;
	}else{
		if(url.indexOf(parametr)==-1){
			url = url+"&"+parametr+"="+value;
		}else{
			var param = "&"+parametr+"=";
			var last =url.substr(url.indexOf(param)+param.length,1);
			url = url.replace("&"+parametr+"="+last,param+value)
		}
	}
	return url;
}

	function change_biz(obj,biz_){
		if(obj=='f1') src = add_to_url_b('biz',biz_,fr1source);
		if(obj=='f2') src = add_to_url_b('biz',biz_,fr2source);
		if(obj=='f3') src = add_to_url_b('biz',biz_,fr3source);
		if(obj=='f4') src = add_to_url_b('biz',biz_,fr4source);	
		if(obj=='res') src = add_to_url_b('biz',biz_,fr4source);			
		if(obj=='res') src = add_to_url_b('biz',biz_,ressource);
		loadEl(obj,src);
	}

function b2c_b2b (biz) {
	
	biz ? showEl("b2c"): showEl("b2b");
	biz ? hideEl("b2b"): hideEl("b2c");

	loadEl("f1","regions.php?biz="+biz);

	if($("mypage"))
	if(mypage_content!=''){
		$("mypage").innerHTML=mypage_content;
	}

	if ($("res")){
			change_biz('res',biz);
			if(fr4source!='') change_biz('f4',biz);
			if(fr3source!='') change_biz('f3',biz);
			if(fr2source!='') change_biz('f2',biz);
			if(fr1source!='') change_biz('f1',biz);
	
	}else {
		hideEl("f2");
		hideEl("f3");
		hideEl("f4");
		hideEl("step2");
		hideEl("step3");
		showEl("step1");
		rePoisk();
	}
}

function Upd_search(string){
	var reg1 = /^[А-Яа-яёЁ_-]+$/; //Для русских слов
	var reg2 = /^[A-Za-z_-]+$/; // Для английских слов
	var str_arr = string.split(' '); //разбиваем на составляющие слова
	var right = new Array;
	for (var i=0; i < str_arr.length;i++){
		if(str_arr[i].length>2){
			if ((reg1.test(str_arr[i]))||(reg2.test(str_arr[i]))){
				right.push(str_arr[i]);
			}
		}
	}
	right = right.slice(0,2);
return right.join(' ');
}

function submit_form(){
	if(document.getElementById("inf0"))
	hide_inf(false,5);
	clearInterval(interval);
	hideEl("f2");
	hideEl("f3");
	hideEl("f4");
	hideEl("step2");
	hideEl("step3");
	showEl("step1");
	if(document.getElementById("mypage")){
		if(mypage_content=='')mypage_content=document.getElementById("mypage").innerHTML;
	}
	var search = document.getElementById('iField').value;
	search = Upd_search(search);
	var region = document.getElementById('reg_sel').value=="0" ? "region0" : document.getElementById('reg_sel').value ;
	var biz = document.getElementById('b2c').style.visibility=='hidden' ? "0" : "1";
	var param = "?search="+search+"&region="+region+"&biz="+biz+"&but1=true";
	start4("goods.php"+param,true);
	document.getElementById('iField').value = search;
}

function reset_colors(obj){
	var doc = obj.parentNode;
	var els = doc.getElementsByTagName('a');

	for(var i=0;i<els.length;i++){
		els[i].style.background = '#FFFFFF';
	}
	obj.style.background = '#EEEEEE';
}
function Create_sel(id_,opt,parent_el){
	var obj;
	if(obj=document.getElementById(id_)){
		document.getElementById(parent_el).removeChild(obj);
	}
	var oSel;
	try{
	 oSel = document.createElement("<select id='sel' name='numb' onchange=\"Sort_goods('res','goods_content','a',this.value)\" >" );
	}catch(e){
	}
	if(!oSel){
	 oSel = document.createElement("select");
	oSel.setAttribute('id',id_);
	oSel.setAttribute('name','numb');
	oSel.setAttribute('onChange',"Sort_goods('res','goods_content','a',this.value)");
	}
	var option = new Array;
	for(var i=0;i<opt;i++){
		option[i]=document.createElement('option');
		option[i].setAttribute("value",i);
		option[i].appendChild(document.createTextNode(i+1));
	}
	for (var i=0;i<opt;i++){
		oSel.appendChild(option[i]);
	}
	document.getElementById(parent_el).appendChild(oSel);

}



function Destroy(search,sT){
		
		var error_cont = sT=='Q' ?$('error_content_q').innerHTML : $('error_content').innerHTML;
		
		if(!$('res'))
		{
			$('mypage').innerHTML = '<div id="top"/></div><img width="7" height="7" id="leftTot" alt="" src="pictures/c-topLeft.gif"/>'+error_cont;
			drawElements();
		}
		else
		{

		var h = $('content').clientHeight;
		var iTh = h - 55;
		var iMh = h - 157;
		
		// write
		$("content").style.border = "none";
        	$("content").innerHTML = "<!-- TOP 100 -->" +
		"<div id='top100'><img alt=' TOP 100 ' id='up' src='pictures/top100-top.gif' width='120' height='35'><div style='height:" + iTh + "px;overflow:scroll' id='list'></div><img alt=' TOP 100 ' id='bottom' src='pictures/top100-bot.gif' width='120' height='20'>	</div>" +
		"<!--PAGE --> 	<div id='mypage' class='myPage' style='height:" + iMh + "px;min-height:" + iMh + "px;'><div id='top'></div><img alt='' src='pictures/c-topLeft.gif' height='7' width='7' id='leftTot'>"+error_cont+"</div>" +
		"<!--CURRENCY --><div id='c_by' class='currency'>" +
		$('tab_head').innerHTML+"<div id='informer'>"+$('currency').innerHTML+"</div></div>";
		setWidth();
		$("content").style.backgroundColor = "";
		loadEl('list','top100ajax.php?');
		}
}



function setWidth()
{
    var w = document.body.clientWidth;
    if(w>1320)
    {
      $('top100').style.width = '18%';
      var wt = $('top100').clientWidth;
      $('c_by').style.marginRight = wt+10+'px';
      $('mypage').style.marginRight = wt+10+'px';
    }
    else
    {
      $('top100').style.width = '130px';
      $('c_by').style.marginRight = '140px';
      $('mypage').style.marginRight = '140px';   
    }
}

function getCounters(){
	var aj = new ajax_object('counters.php','post', 'text');
	$('counters').innerHTML = aj.request(Array());
}
