var loading = new Image();
loading.src = host + "/sitedesign/images/load.gif";

var colors = new Array();
colors[0] = "#000";
colors[1] = "#111";
colors[2] = "#222";
colors[3] = "#333";
colors[4] = "#444";
colors[5] = "#555";
colors[6] = "#666";
colors[7] = "#777";
colors[8] = "#888";
colors[9] = "#999";
colors[10] = "#AAA";
colors[11] = "#BBB";
colors[12] = "#CCC";
colors[13] = "#DDD";
colors[14] = "#EEE";
colors[15] = "#FFF";
var borders = new Array();
borders[0] = "#F7EB11";
borders[1] = "#E2DA2C";
borders[2] = "#E3DD33";
borders[3] = "#E5E03D";
borders[4] = "#E7E448";
borders[5] = "#E8E755";
borders[6] = "#EAE962";
borders[7] = "#EBEC70";
borders[8] = "#EDEF7E";
borders[9] = "#EFF08E";
borders[10] = "#F0F29D";
borders[11] = "#F2F5AC";
borders[12] = "#F4F7BC";
borders[13] = "#F7F9CD";
borders[14] = "#F9FCCD";
borders[15] = "#FFF";

var isIE = false; // надеемся на лучшее
if(navigator.userAgent.indexOf('MSIE') != -1 && navigator.userAgent.indexOf('Opera') == -1)
{
	isIE = true;// но сталкиваемся с суровой реальностью
}

// объект vxml
var vxml = {
	useActiveX: typeof ActiveXObject != 'undefined',
	useXMLHTTP: typeof XMLHttpRequest != 'undefined'
}

vxml.XMLHTTP_Versions = [ 'MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' ];

// объект vXMLHTTP, посредством этого объекта происходит вся работа
function vXMLHTTP() {}

vXMLHTTP.createRequest = function()
{
	if( vxml.useXMLHTTP == true )
	{
		return new XMLHttpRequest();
	}
	else if( vxml.useActiveX == true )
	{
		if( !vxml.XMLHTTP_Version )
		{
			for( var i = 0; i < vxml.XMLHTTP_Versions.length; i++ )
			{
				try
				{
					new ActiveXObject( vxml.XMLHTTP_Versions[i] );
					vxml.XMLHTTP_Version = vxml.XMLHTTP_Versions[i];
					break;
				}
				catch( error )
				{}
			}
		}
		if( vxml.XMLHTTP_Version )
		{
			return new ActiveXObject( vxml.XMLHTTP_Version );
		}
		else
		{
			throw new Error( 'Невозможно создать запрос XMLHTTP' );
		}
	}
	else
	{
		throw new Error( 'Ваш браузер не поддерживает AJAX' );
	}
}

function sendRequest( action, pid, del )
{
	if( isIE == false )
	{
		var n = document.getElementById('loading');
		if( n )
		{
			n.parentNode.removeChild( n );
		}
		h = window.scrollY + screen.availHeight / 2 - 54;
		var icon = document.createElement('div');
		icon.id = 'loading';
		icon.style.top = h+'px';
		icon.style.left = '50%';
		icon.style.position = 'absolute';
		icon.style.zIndex = '10000';
		icon.style.border = '2px solid #F7EB11';
		icon.style.width = '50px';
		icon.style.height = '40px';
		icon.style.marginLeft = '-27px';
		icon.style.background = '#FFF';
		icon.style.textAlign = 'center';
		icon.style.paddingTop = '10px';
		var animation = document.createElement('img');
		animation.src = loading.src;
		icon.appendChild(animation);
//	var b = document.getElementById('page');
//	b.appendChild(icon);
		var b = document.getElementsByTagName( 'body' );
		b[0].appendChild( icon );
	}
	else
	{
		var n = document.getElementById('loading');
		if( n )
		{
			n.parentNode.removeChild( n );
		}
//		h = window.scrollY + screen.availHeight / 2 - 54;
		h = (document.documentElement.scrollTop+300)+'px';
		var icon = document.createElement('div');
		icon.id = 'loading';
		var b = document.getElementsByTagName( 'body' );
		b[0].appendChild( icon );
		icon.style.top = h;
		icon.style.left = '50%';
		icon.style.position = 'absolute';
		icon.style.zIndex = '10000';
		icon.style.border = '2px solid #F7EB11';
		icon.style.width = '50px';
		icon.style.height = '40px';
		icon.style.marginLeft = '-27px';
		icon.style.background = '#FFF';
		icon.style.textAlign = 'center';
		icon.style.paddingTop = '10px';
		var animation = document.createElement('img');
		animation.src = loading.src;
		icon.appendChild(animation);
//	var b = document.getElementById('page');
//	b.appendChild(icon);
		/**/
	}

	var params = new Array();
	var address = '';
	var param = '';
	var sbody = '';
	switch(action)
	{
		case 'add':
			var pcount = document.getElementById('addProduct'+pid).value;
			pcount = pcount.replace( /,/, '.' );
			pcount = ( pcount == '' || NaN == parseFloat(pcount) ) ? 1 : parseFloat(pcount);
			document.getElementById('addProduct'+pid).value = '';
			param = encodeURIComponent( 'pid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'pcount' );
			param += "=" + encodeURIComponent( pcount );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			param = encodeURIComponent( 'del' );
			param += "=" + encodeURIComponent( del );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j-add2cart/';//?pid='+pid+'&pcount='+pcount+ua+'&time='+time+Math.random();
			break;
		case 'remove':
			if (confirm("Вы действительно хотите удалить?")) {
			param = encodeURIComponent( 'pid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			param = encodeURIComponent( 'del' );
			param += "=" + encodeURIComponent( del );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j_removefromcart/';//?pid='+pid+ua+'&time='+time+Math.random();
			}
			else {
				setTimeout( "{ var n = document.getElementById('loading'); if( n ) { n.parentNode.removeChild( n ); } }", 2000 );
				return true;
			}
			break;
		case 'emptyCart':
			address = host+'/index.php/trade/j_emptycart/';
			break;
		case 'getModels':
			param = encodeURIComponent( 'cid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j_getmodels/';
			break;
		case 'getZone':
			
			param = encodeURIComponent( 'cid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j_getzone/';
			break;
		case 'getSmallZone':
			
			param = encodeURIComponent( 'cid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j_getsmallzone/';
			break;
		case 'loadSubcategories':
			param = encodeURIComponent( 'cid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j-loadsubcategories/';
			break;
		case 'loadBrands':
			param = encodeURIComponent( 'cid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j-loadbrands/';
			break;
		case 'add2compare':
			param = encodeURIComponent( 'pid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j-add2compare/';
			break;
		case 'clear-list':
			param = encodeURIComponent( 'gid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j-clear-list/';
			break;
/*		case 'vote':
			param = encodeURIComponent( 'pid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'mark' );
			param += "=" + encodeURIComponent(document.getElementById('mark').value);
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j_vote/';
			break;*/
		case 'login':

var replace = new Array('%C0', '%C1', '%C2', '%C3', '%C4', '%C5', '%A8', '%C6', '%C7', '%C8', '%C9', '%CA', '%CB', '%CC', '%CD', '%CE', '%CF', '%D0', '%D1', '%D2', '%D3', '%D4', '%D5', '%D6', '%D7', '%D8', '%D9', '%DC', '%DB', '%DA', '%DD', '%DE', '%DF', '%E0', '%E1', '%E2', '%E3', '%E4', '%E5', '%B8', '%E6', '%E7', '%E8', '%E9', '%EA', '%EB', '%EC', '%ED', '%EE', '%EF', '%F0', '%F1', '%F2', '%F3', '%F4', '%F5', '%F6', '%F7', '%F8', '%F9', '%FC', '%FB', '%FA', '%FD', '%FE', '%FF' );
var search = new Array('А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ё', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М', 'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ь', 'Ы', 'Ъ', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ё', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ы', 'ъ', 'э', 'ю', 'я' );
pid = str_replace(search, replace, pid);
del = str_replace(search, replace, del);

			param = encodeURIComponent( 'login' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'pwd' );
			param += "=" + encodeURIComponent( del );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/user/j_login/';
			break;

	}

	var oXmlHTTP = vXMLHTTP.createRequest();
	oXmlHTTP.open( "post", address, true );
	oXmlHTTP.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	oXmlHTTP.onreadystatechange = function()
	{
		if(oXmlHTTP.readyState == 4 )
		{
			if(oXmlHTTP.status == 200)
			{
//				setTimeout( saveRequest(oXmlHTTP.responseText), 10000 );
				saveRequest(oXmlHTTP.responseText);
			}
			else
			{
				saveRequest("Error: " + oXmlHTTP.statusText);
			}
		}
	}
	oXmlHTTP.send(sbody);
}
function updatehref( action )
{

	var params = new Array();
	var address = '';
	var param = '';
	var sbody = '';
	switch(action)
	{
		case 'orderlegalize':
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/trade/j_orderlegalize/';
			break;
		case 'userorder':
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/user/j_userorder/';
			break;
	}

	var oXmlHTTP = vXMLHTTP.createRequest();
	oXmlHTTP.open( "post", address, true );
	oXmlHTTP.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	oXmlHTTP.onreadystatechange = function()
	{
		if(oXmlHTTP.readyState == 4 )
		{
			if(oXmlHTTP.status == 200)
			{
//				setTimeout( saveRequest(oXmlHTTP.responseText), 10000 );
				saveRequest(oXmlHTTP.responseText);
			}
			else
			{
				saveRequest("Error: " + oXmlHTTP.statusText);
			}
		}
	}
	oXmlHTTP.send(sbody);
}


function sendLogin( action, login, password )
{
	if( isIE == false )
	{
		var n = document.getElementById('loading');
		if( n )
		{
			n.parentNode.removeChild( n );
		}
		h = window.scrollY + screen.availHeight / 2 - 54;
		var icon = document.createElement('div');
		icon.id = 'loading';
		icon.style.top = h+'px';
		icon.style.left = '50%';
		icon.style.position = 'absolute';
		icon.style.zIndex = '10000';
		icon.style.border = '2px solid #F7EB11';
		icon.style.width = '50px';
		icon.style.height = '40px';
		icon.style.marginLeft = '-27px';
		icon.style.background = '#FFF';
		icon.style.textAlign = 'center';
		icon.style.paddingTop = '10px';
		var animation = document.createElement('img');
		animation.src = loading.src;
		icon.appendChild(animation);
//	var b = document.getElementById('page');
//	b.appendChild(icon);
		var b = document.getElementsByTagName( 'body' );
		b[0].appendChild( icon );
	}
	else
	{
		var n = document.getElementById('loading');
		if( n )
		{
			n.parentNode.removeChild( n );
		}
//		h = window.scrollY + screen.availHeight / 2 - 54;
		h = (document.documentElement.scrollTop+300)+'px';
		var icon = document.createElement('div');
		icon.id = 'loading';
		var b = document.getElementsByTagName( 'body' );
		b[0].appendChild( icon );
		icon.style.top = h;
		icon.style.left = '50%';
		icon.style.position = 'absolute';
		icon.style.zIndex = '10000';
		icon.style.border = '2px solid #F7EB11';
		icon.style.width = '50px';
		icon.style.height = '40px';
		icon.style.marginLeft = '-27px';
		icon.style.background = '#FFF';
		icon.style.textAlign = 'center';
		icon.style.paddingTop = '10px';
		var animation = document.createElement('img');
		animation.src = loading.src;
		icon.appendChild(animation);
//	var b = document.getElementById('page');
//	b.appendChild(icon);
		/**/
	}

	var params = new Array();
	var address = '';
	var param = '';
	var sbody = '';
	switch(action)
	{
		case 'login':
			param = encodeURIComponent( 'login' );
			param += "=" + encodeURIComponent( login );
			params.push(param);
			param = encodeURIComponent( 'password' );
			param += "=" + encodeURIComponent( password );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/user/j_userbox/';//?pid='+pid+'&pcount='+pcount+ua+'&time='+time+Math.random();
			break;
	}

	var oXmlHTTP = vXMLHTTP.createRequest();
	oXmlHTTP.open( "post", address, true );
	oXmlHTTP.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	oXmlHTTP.onreadystatechange = function()
	{
		if(oXmlHTTP.readyState == 4 )
		{
			if(oXmlHTTP.status == 200)
			{
//				setTimeout( saveRequest(oXmlHTTP.responseText), 10000 );
				saveRequest(oXmlHTTP.responseText);
			}
			else
			{
				saveRequest("Error: " + oXmlHTTP.statusText);
			}
            closePopup();
		}
	}
	oXmlHTTP.send(sbody);
}



function handleRequestStateChange()
{
	if( xmlHttp.readyState == 4 )
	{
		if( xmlHttp.status == 200 )
		{
			try
			{
				handleServerResponse();
            }
            catch(e)
			{
	            alert( 'Ошибка чтения ответа '+ e.toString() );
            }
        }
        else
		{
	        alert( 'Возникли проблемы при получении данных:\n'+ xmlHttp.statusText+'\nПопробуйте сменить браузер\n Рекомендуем использовать Mozilla Firefox' );
        }
	}
}
function saveRequest( response )
{
	
	//if( isIE == false )
	setTimeout( "{ var n = document.getElementById('loading'); if( n ) { n.parentNode.removeChild( n ); } }", 2000 );
	var parts = response.split( '|||' );
//alert(response);
	var id = parts[0].substr( 1, parts[0].length - 2 );
	   nameDiv=document.getElementById(id);
    switch(id)
    {
        case 'minicart':
            showMessage('<p>Товар добавлен в корзину</p>');
            break;
        case 'compare-list':
            if(parts[1].substr( 1, parts[1].length - 2 ).length != '')
            {
                showMessage('<p>Товар добавлен в список сравнения</p>');
            }
            break;
         case 'errorregistr':
            if(parts[1].substr( 1, parts[1].length - 2 ).length != '')
            {
                showMessage('<p style="font-size: 14px; color: red;">Ошибка! Пользователя с таким логином и паролем не существует</p>');
				var lochref= host + '/index.php/user/login/error/';
				window.location.href = lochref;
				return false;
           }
            break;
   }

	contentHolder = document.getElementById( id );
	if( !contentHolder )
	{
		contentHolder = window.opener ? window.opener.document.getElementById( parts[0].substr( 1, parts[0].length - 2 ) ) : 'undef';
	}

	if( contentHolder != 'undef' )
	{
		if( isIE == false )
		{
			contentHolder.innerHTML = escapeToNormal( parts[1].substr( 1, parts[1].length - 2 ) );
		}
		else
		{
			contentHolder.innerHTML = '';
			var str = escapeToNormal( parts[1].substr( 1, parts[1].length - 2 ) );
			if( str.indexOf( 'option' ) != -1 )
			{
				contentHolder.outerHTML = contentHolder.outerHTML.replace( /<\/select>/gi, str+"</select>" );
			}
			else
			{
				contentHolder.innerHTML = escapeToNormal( parts[1].substr( 1, parts[1].length - 2 ) );
			}
		}
	}
	useTarget();
//	element('doc').removeChild(element('lwShadow');
//	element('doc').removeChild(element('loginWindow');
}
var i = 0;
function showMessage( msgText )
{
	if( isIE == false )
	{
		var n = document.getElementById('addedmsg');
		if( n )
		{
			n.parentNode.removeChild( n );
		}
		h = window.scrollY + screen.availHeight / 2 - 54;
		var msg = document.createElement('div');
		msg.id = 'addedmsg';
		msg.style.top = h+'px';
		msg.style.left = '50%';
		msg.style.position = 'absolute';
		msg.style.zIndex = '10000';
		msg.style.border = '2px solid #F7EB11';
		msg.style.width = '250px';
		msg.style.height = '40px';
		msg.style.marginLeft = '-127px';
		msg.style.background = '#FFF';
		msg.style.textAlign = 'center';
		msg.style.paddingTop = '10px';
		msg.innerHTML = msgText;
		var b = document.getElementsByTagName( 'body' );
		b[0].appendChild( msg );
	}
	else
	{
		var n = document.getElementById('addedmsg');
		if( n )
		{
			n.parentNode.removeChild( n );
		}
		h = (document.documentElement.scrollTop+300)+'px';
		var msg = document.createElement('div');
		msg.id = 'addedmsg';
		var b = document.getElementsByTagName( 'body' );
		b[0].appendChild( msg );
		msg.style.top = h;
		msg.style.left = '50%';
		msg.style.position = 'absolute';
		msg.style.zIndex = '10000';
		msg.style.border = '2px solid #F7EB11';
		msg.style.width = '250px';
		msg.style.height = '40px';
		msg.style.marginLeft = '-127px';
		msg.style.background = '#FFF';
		msg.style.textAlign = 'center';
		msg.style.paddingTop = '10px';
		msg.innerHTML = msgText;
	}
    setTimeout( 'fade( 0 )', 200 );
}
function fade( j )
{
	var msg = document.getElementById( 'addedmsg' );
	msg.style.borderColor = borders[j];
	msg.style.color = colors[j];
	i++;
	if( i < 16 )
	{
		setTimeout( 'fade( ' + i + ' )', 100);
	}
	else
	{
		msg.style.display = "none";
		if( isIE )
		{
			msg.removeNode(true);
		}
		i = 0;
	}
}

function escapeToNormal( str )
{
	str = str.replace( /%C0/g, 'А' );
	str = str.replace( /%C1/g, 'Б' );
	str = str.replace( /%C2/g, 'В' );
	str = str.replace( /%C3/g, 'Г' );
	str = str.replace( /%C4/g, 'Д' );
	str = str.replace( /%C5/g, 'Е' );
	str = str.replace( /%C6/g, 'Ж' );
	str = str.replace( /%C7/g, 'З' );
	str = str.replace( /%C8/g, 'И' );
	str = str.replace( /%C9/g, 'Й' );
	str = str.replace( /%CA/g, 'К' );
	str = str.replace( /%CB/g, 'Л' );
	str = str.replace( /%CC/g, 'М' );
	str = str.replace( /%CD/g, 'Н' );
	str = str.replace( /%CE/g, 'О' );
	str = str.replace( /%CF/g, 'П' );
	str = str.replace( /%D0/g, 'Р' );
	str = str.replace( /%D1/g, 'С' );
	str = str.replace( /%D2/g, 'Т' );
	str = str.replace( /%D3/g, 'У' );
	str = str.replace( /%D4/g, 'Ф' );
	str = str.replace( /%D5/g, 'Х' );
	str = str.replace( /%D6/g, 'Ц' );
	str = str.replace( /%D7/g, 'Ч' );
	str = str.replace( /%D8/g, 'Ш' );
	str = str.replace( /%D9/g, 'Щ' );
	str = str.replace( /%DA/g, 'Ъ' );
	str = str.replace( /%DB/g, 'Ы' );
	str = str.replace( /%DC/g, 'Ь' );
	str = str.replace( /%DD/g, 'Э' );
	str = str.replace( /%DE/g, 'Ю' );
	str = str.replace( /%DF/g, 'Я' );
	str = str.replace( /%E0/g, 'а' );
	str = str.replace( /%E1/g, 'б' );
	str = str.replace( /%E2/g, 'в' );
	str = str.replace( /%E3/g, 'г' );
	str = str.replace( /%E4/g, 'д' );
	str = str.replace( /%E5/g, 'е' );
	str = str.replace( /%E6/g, 'ж' );
	str = str.replace( /%E7/g, 'з' );
	str = str.replace( /%E8/g, 'и' );
	str = str.replace( /%E9/g, 'й' );
	str = str.replace( /%EA/g, 'к' );
	str = str.replace( /%EB/g, 'л' );
	str = str.replace( /%EC/g, 'м' );
	str = str.replace( /%ED/g, 'н' );
	str = str.replace( /%EE/g, 'о' );
	str = str.replace( /%EF/g, 'п' );
	str = str.replace( /%F0/g, 'р' );
	str = str.replace( /%F1/g, 'с' );
	str = str.replace( /%F2/g, 'т' );
	str = str.replace( /%F3/g, 'у' );
	str = str.replace( /%F4/g, 'ф' );
	str = str.replace( /%F5/g, 'х' );
	str = str.replace( /%F6/g, 'ц' );
	str = str.replace( /%F7/g, 'ч' );
	str = str.replace( /%F8/g, 'ш' );
	str = str.replace( /%F9/g, 'щ' );
	str = str.replace( /%FA/g, 'ъ' );
	str = str.replace( /%FB/g, 'ы' );
	str = str.replace( /%FC/g, 'ь' );
	str = str.replace( /%FD/g, 'э' );
	str = str.replace( /%FE/g, 'ю' );
	str = str.replace( /%FF/g, 'я' );
	str = str.replace( /%A8/g, 'Ё' );
	str = str.replace( /%B8/g, 'ё' );

	return str;
}

function escapeToWin1251(str){
str=str.replace(/&/g, '&');
str=str.replace(/ /g, ' ');
str=str.replace(/=/g, '=');
str=str.replace(/\+/g, '+');
str=str.replace(/№/g, '№');
str=str.replace(/\;/g, ';');
str=str.replace(/Ё/g, 'Ё');
str=str.replace(/ё/g, 'ё');
str=str.replace(/©/g, '©');
str=str.replace(/[А-Яа-я]/g, function(symbol){return '%'+Number(symbol.charCodeAt(0)-0x0350).toString(16);});
return str;
}
function showImage( address, alt, width, height )
{
	var newWindow = window.open('','newWindow','width='+width+',height='+height);
	newWindow.document.open();
	newWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	newWindow.document.write('<html>');
	newWindow.document.write('<head>');
	newWindow.document.write('<title>'+alt+'</title>');
	newWindow.document.write('<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />');
	newWindow.document.write('<meta name="generator" content="220V, ~400Hz" />');
	newWindow.document.write('<style type="text/css">');
	newWindow.document.write('body { padding:  0; margin:  0; background:  #FFF;}');
	newWindow.document.write('</style>');
	newWindow.document.write('</head>');
	newWindow.document.write('<body>');
	newWindow.document.write('<img src="'+host+address+'" alt="'+alt+'" title="'+alt+'" />');
	newWindow.document.write('</body>');
	newWindow.document.write('</html>');
	newWindow.document.close();
}
function selectPhones()
{
	var vendor = document.getElementById('vendor').value;
	if( vendor != '-1' )
	{
		sendRequest( 'getModels', vendor );
	}
}
//-------------функция вывода микрорайонов--------------
function selectZone()
{
	document.getElementById('price').innerHTML =" ";

	var vendor = document.getElementById('vendor').value;
	sendRequest( 'getZone', vendor );
}	
//------------------------------------------------------

//------------функция вывода цены доставки--------------
function selectSmallZone()
{
	var vendor = document.getElementById('smallregion').value;
	sendRequest( 'getSmallZone', vendor );
}	
//------------------------------------------------------
function changeSorting()
{
	document.getElementById('sorting').value = document.getElementById('sortBy').value;
	document.getElementById('filterFormSelectBtn').click();
}
function checkAll()
{
    if(document.getElementById)
    {
        var cart = document.getElementById('productlist');
		if( !cart )
		{
			cart = document.getElementById('tableDecor');
		}
		if( !cart )
		{
			cart = document.getElementById('orders');
		}
        var tb = cart.getElementsByTagName('tbody');
        var inputs = tb[0].getElementsByTagName('input');
        var chb = document.getElementById('pall');
        var chb2 = document.getElementById('pall2');
        var chckd = chb.checked ? 'checked' : '';
		chb2.checked = chb.checked;
        for( var i = 0; i < inputs.length; i++ )
        {
            if( inputs[i].type == "checkbox" )
            {
                inputs[i].checked = chckd;
            }
        }
    }
}
function checkAll2()
{
    if(document.getElementById)
    {
        var cart = document.getElementById('productlist');
		if( !cart )
		{
			cart = document.getElementById('tableDecor');
		}
		if( !cart )
		{
			cart = document.getElementById('orders');
		}
        var tb = cart.getElementsByTagName('tbody');
        var inputs = tb[0].getElementsByTagName('input');
        var chb = document.getElementById('pall2');
        var chb2 = document.getElementById('pall');
        var chckd = chb.checked ? 'checked' : '';
		chb2.checked = chb.checked;
        for( var i = 0; i < inputs.length; i++ )
        {
            if( inputs[i].type == "checkbox" )
            {
                inputs[i].checked = chckd;
            }
        }
    }
}
function toggle(what)
{
    if(document.getElementById)
    {
        var chb = document.getElementById('pall');
        var chb2 = document.getElementById('pall2');
        if( !what.checked )
        {
            chb.checked = '';
            chb2.checked = '';
        }
        else
        {
            var cart = document.getElementById('productlist');
			if( !cart )
			{
				cart = document.getElementById('tableDecor');
			}
            var tb = cart.getElementsByTagName('tbody');
            var inputs = tb[0].getElementsByTagName('input');
            var inum = 0;
            var chnum = 0;
            for( var i = 0; i < inputs.length; i++ )
            {
                if( inputs[i].type == "checkbox" )
                {
                    inum++;
                    if( inputs[i].checked )
                    {
                        chnum++;
                    }
                }
            }
            if( chnum > 0 && inum == chnum )
            {
                chb.checked = 'checked';
                chb2.checked = 'checked';
            }
        }
    }
}
function open_w(newURL)
{
        id=0;
        numbers = "width=500, height=500, left=4, top=4, toolbar=0, statusbar=0, scrollbars=0, resizable=1,border=thin,directories=0,status=0,menubar=0";
        Wid=window.open(newURL, id, numbers );
}

function show_me(id,ttype )
{
        newURL=host+"/index_noframe.php/trade/show/"+id+"/"+ttype;
		if(Wid)
		{
			Wid.close();Wid=null;
		}
        open_w(newURL );
}
function closeW( )
{
			window.close();
}
function closeS( )
{
			window.parent.reload();
			window.close();
}

function popup (url, target )
{
        document.all.java_collector.www_dir=host;
    numbers = "width=500, height=400, left=4, top=4, toolbar=1, statusbar=0, scrollbars=1, resizable=1";
        if(Wid){Wid.close();}
    Wid = window.open ( url, target, numbers );
}
function escapeToUrl( str )
{
	str = str.replace( 'А', '%C0' );
	str = str.replace( 'Б', '%C1' );
	str = str.replace( 'В', '%C2' );
	str = str.replace( 'Г', '%C3' );
	str = str.replace( 'Д', '%C4' );
	str = str.replace( 'Е', '%C5' );
	str = str.replace( 'Ж', '%C6' );
	str = str.replace( 'З', '%C7' );
	str = str.replace( 'И', '%C8' );
	str = str.replace( 'Й', '%C9' );
	str = str.replace( 'К', '%CA' );
	str = str.replace( 'Л', '%CB' );
	str = str.replace( 'М', '%CC' );
	str = str.replace( 'Н', '%CD' );
	str = str.replace( 'О', '%CE' );
	str = str.replace( 'П', '%CF' );
	str = str.replace( 'Р', '%D0' );
	str = str.replace( 'С', '%D1' );
	str = str.replace( 'Т', '%D2' );
	str = str.replace( 'У', '%D3' );
	str = str.replace( 'Ф', '%D4' );
	str = str.replace( 'Х', '%D5' );
	str = str.replace( 'Ц', '%D6' );
	str = str.replace( 'Ч', '%D7' );
	str = str.replace( 'Ш', '%D8' );
	str = str.replace( 'Щ', '%D9' );
	str = str.replace( 'Ъ', '%DA' );
	str = str.replace( 'Ы', '%DB' );
	str = str.replace( 'Ь', '%DC' );
	str = str.replace( 'Э', '%DD' );
	str = str.replace( 'Ю', '%DE' );
	str = str.replace( 'Я', '%DF' );
	str = str.replace( 'а', '%E0' );
	str = str.replace( 'б', '%E1' );
	str = str.replace( 'в', '%E2' );
	str = str.replace( 'г', '%E3' );
	str = str.replace( 'д', '%E4' );
	str = str.replace( 'е', '%E5' );
	str = str.replace( 'ж', '%E6' );
	str = str.replace( 'з', '%E7' );
	str = str.replace( 'и', '%E8' );
	str = str.replace( 'й', '%E9' );
	str = str.replace( 'к', '%EA' );
	str = str.replace( 'л', '%EB' );
	str = str.replace( 'м', '%EC' );
	str = str.replace( 'н', '%ED' );
	str = str.replace( 'о', '%EE' );
	str = str.replace( 'п', '%EF' );
	str = str.replace( 'р', '%F0' );
	str = str.replace( 'с', '%F1' );
	str = str.replace( 'т', '%F2' );
	str = str.replace( 'у', '%F3' );
	str = str.replace( 'ф', '%F4' );
	str = str.replace( 'х', '%F5' );
	str = str.replace( 'ц', '%F6' );
	str = str.replace( 'ч', '%F7' );
	str = str.replace( 'ш', '%F8' );
	str = str.replace( 'щ', '%F9' );
	str = str.replace( 'ъ', '%FA' );
	str = str.replace( 'ы', '%FB' );
	str = str.replace( 'ь', '%FC' );
	str = str.replace( 'э', '%FD' );
	str = str.replace( 'ю', '%FE' );
	str = str.replace( 'я', '%FF' );
	str = str.replace( 'Ё', '%A8' );
	str = str.replace( 'ё', '%B8' );
	alert(str);
	return str;
}
function str_replace ( search, replace, subject ) {
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){
        replace[replace.length]='';
    }
    if(subject instanceof Array){
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
    return subject;
}
function reloadPage ( ) {
	window.reload();
}
