

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, object, param1 )
{
	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 'soa':
			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/job/j_soa/';
			break;
		case 'viewuser':
			param = encodeURIComponent( 'object' );
			param += "=" + encodeURIComponent( object );
			params.push(param);
			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/job/j_viewuser/';
			break;
		case 'bookmark':
			param = encodeURIComponent( 'pid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'object' );
			param += "=" + encodeURIComponent( object );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/job/j_bookmark/';
			break;
		case 'delbookmark':
			param = encodeURIComponent( 'pid' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'object' );
			param += "=" + encodeURIComponent( object );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			sbody = params.join("&");
			address = host+'/index.php/job/j_delbookmark/';
			break;
		case 'salary':
			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/job/j_salary/';
			break;
		case 'login':
			param = encodeURIComponent( 'login' );
			param += "=" + encodeURIComponent( pid );
			params.push(param);
			param = encodeURIComponent( 'pwd' );
			param += "=" + encodeURIComponent( object );
			params.push(param);
			param = encodeURIComponent( 'time' );
			param += "=" + encodeURIComponent( time+Math.random() );
			params.push(param);
			param = encodeURIComponent( 'first' );
			param += "=" + encodeURIComponent( param1 );
			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 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);
	   if(nameDiv.className == 'nobg')
	   {
		nameDiv.className = 'bg';
	   }
    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;
    }

	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();
}
