//	Copyright © Coretomic, 2005

//*********************************************
//*  ?????????? ? ??????? "????? ?????????"   *
//*********************************************
//alert("00");
dom = (document.getElementById) ? true : false;
nn4 = (document.layers) ? true : false;
nn6 = (navigator.userAgent.indexOf("Gecko")!=-1);
ie = (document.all) ? true : false;
ie4 = ie && !dom;
ie5 = ie && !ie4;
Mac = (navigator.appVersion.indexOf("Mac") != -1);
ie4mac = ie4 && Mac;
opera = (navigator.userAgent.indexOf("Opera")!=-1);
Konqueror = (navigator.userAgent.indexOf("Konqueror")!=-1);
function empty() {}

var stdMouseX = -1, stdMouseY = -1;
var MouseMoved = 0;
var stdHint="";
var MouseMove = empty;
var Init = empty;

if (nn4) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = stdMouseMove;

function stdMouseMove(e) {
//	stdMouseX = (nn4) ? (e.pageX):(event.x + document.body.scrollLeft);
//	stdMouseY = (nn4) ? (e.pageY):(event.y + document.body.scrollTop);
	MouseMoved++;
	MouseMove(e);
	HintMouseMove();
}

function HintMouseMove() {
	if (stdHint)moveElem(stdHint, stdMouseX+10, stdMouseY+10);
}

function stdInit() {
	Init();
}

//*********************************************
//* ???????????? ??????? ??? ?????? ?? ?????? *
//*********************************************

function showElem(elemId) {
if (dom) document.getElementById(elemId).style.visibility = "visible";
	else if (ie4) document.all[elemId].style.visibility = "visible";
		else if (nn4) document.layers[elemId].visibility = "show";
}

function hideElem(elemId) {
if (dom) document.getElementById(elemId).style.visibility = "hidden";
	else if (ie4) document.all[elemId].style.visibility = "hidden";
		else if (nn4) document.layers[elemId].visibility = "hide";
}

function putElem(elemId) {
if (dom) document.getElementById(elemId).style.display = "block";
	else if (ie4) document.all[elemId].style.display = "block";
}

function removeElem(elemId) {
if (dom) document.getElementById(elemId).style.display = "none";
	else if (ie4) document.all[elemId].style.display = "none";
}

function moveElem(idname,x,y) {
if (dom)
	with (eval(idname)) {
		style.left = x;
		style.top = y;
	}
else if (nn4) {
		document.layers[idname].left=x;
		document.layers[idname].top=y;
	}
}

function changeElem(elemId) {
if (dom)
	if (document.getElementById(elemId).style.display == "block")
		document.getElementById(elemId).style.display = "none";
	else
		document.getElementById(elemId).style.display = "block";

else if (ie4)
	if (document.all[elemId].style.display == "block")
		document.all[elemId].style.display = "none";
	else
		document.all[elemId].style.display = "block";
}

function exchangeElem(elemId) {
if (dom)
	if (document.getElementById(elemId).style.visibility == "hidden")
		document.getElementById(elemId).style.visibility= "visible";
	else
		document.getElementById(elemId).style.visibility= "hidden";

else if (ie4)
	if (document.all[elemId].style.visibility== "hidden")
		document.all[elemId].style.visibility= "visible";
	else
		document.all[elemId].style.visibility= "hidden";
else if (nn4)
	if (document.layers[elemId].visibility=="hide")
		document.layers[elemId].visibility="show";
	else
		document.layers[elemId].visibility="hide";
}

function relPosX(which) {
if (nn4) {
return document.layers[which].pageX;
} else {
var elem = (dom)? document.getElementById(which) : document.all[which];
var pos = elem.offsetLeft;
while (elem.offsetParent != null) {
elem = elem.offsetParent;
pos += elem.offsetLeft;
if (elem.tagName == 'BODY') break;
} return pos;
}
}

function relPosY(which) {
if (nn4) {
return document.layers[which].pageY;
} else {
var elem = (dom)? document.getElementById(which) : document.all[which];
var pos = elem.offsetTop;
while (elem.offsetParent != null) {
elem = elem.offsetParent;
pos += elem.offsetTop;
if (elem.tagName == 'BODY') break;
} return pos;
}
}

//*********************************************
//*           ??????????? ???????             *
//*********************************************

function iLayer(nameid,width,text) {
if (!width)width=1;
if (!text)text='';
if (nn4)
document.write("<ilayer name='"+nameid+"'width='"+width+"'>"+text+"</ilayer>");
else
document.write("<span id='"+nameid+"' style='position: relative; width: "+width+"px;'>"+text+"</span>");
}

function checkmail(email) {
if (email == "") return (false);
 if (email.indexOf(".") == -1) return (false);
 dog = email.indexOf("@");
 if (dog == -1) return (false);
 if ((dog < 1) || (dog > email.length - 5))return (false);
 if ((email.charAt(dog - 1) == '.') || (email.charAt(dog + 1) == '.')) return (false);
 if (email.charAt(email.length - 1) == '.') return (false);
 return true;
}

function startHint(nameid) {
	if (stdHint)hideElem(stdHint);
	stdHint=nameid;
	moveElem(stdHint, stdMouseX+10, stdMouseY+10);
	showElem(stdHint);
}

function stopHint() {
	hideElem(stdHint);
	stdHint="";
}

// Added

function CheckCount(value) {
	var re = new RegExp("^\\d+$");
	return (value.match(re) && parseInt(value) > 0);
}

function VCartAction(form_name) {
	var form = document[form_name];
	form.submit();
}

function SelectVProduct(form_name, vCartID) {
//	try	{
	var form = document[form_name];
	var added_params = form["added_params"];
	var price;
	var price = (added_params.selectedIndex >= 0) ? added_params[added_params.selectedIndex].value : added_params.value;

	if (!CheckCount(form["_count"].value)) {
		//alert("Please select correct count.");
		form["_count"].focus();
	} else {
		var item = "" + vCartID +"^" + price + "^" + form["_count"].value;
		form["item"].value = item;
		form.submit();
	}
/*	} catch (err) {
		//alert(err.description);
	}*/
}

function ShowLarge(id) {
	window.open('/home/product/' + id + '.html', null, 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=499,height=530');
}

function setLocation(NH) {
	var _href;
	if (ie) {
		_href='results.html?q='+(NH.substring(NH.indexOf('?')+3,NH.indexOf('&')))+NH.substring(NH.indexOf('&'),NH.length);
		location.href=_href;
	} else {
		_href='results.html?q='+(NH.substring(NH.indexOf('?')+3,NH.indexOf('&')))+NH.substring(NH.indexOf('&'),NH.length);
		location.href=_href;
	}
}

function strPad(val)
{
	var offset = arguments.length > 1 && arguments[1] ? arguments[1] : 2;
	
	val = val + "";
	while(val.length < offset)
		val = "0"+val;
	return val;
}

function intVal(val)
{
	val = parseInt(val);
	if(isNaN(val))
		val = 0;
	return val;
}

/************************ RESIZE CONTENT IMAGES **************************/
	function getResizeImages(a)
	{
		aImage = document.getElementsByName(a);
		for(var i = 0; i < aImage.length; i++)
		{
			setTimeout(returnedClosure(i),1000);
		}
	}

	function returnedClosure(indx)
	{
		return function()
		{
			setImageSize(aImage[indx]);
		}
	}

	function setImageSize(obj)
	{
		if (!obj)
		{
			setTimeout(function(){setImageSize(obj)},100);
			return;
		}
		if (obj.offsetWidth > imageSize)
			obj.width = imageSize;
	}
/************************ /RESIZE CONTENT IMAGES **************************/

//lixlpixel tooltips     © 2005 http://lixlpixel.com/  //
////////////////////////////////////////////////////////////
//position of the tooltip relative to the mouse in pixel //
var offsetx = 12;										  //
var offsety =  8;										  //
////////////////////////////////////////////////////////////
function newelement(newid)
{ 
if(document.createElement)
{ 
var el = document.createElement('div'); 
el.id = newid;     
with(el.style)
{ 
display = 'none';
position = 'absolute';
} 
el.innerHTML = '&nbsp;'; 
document.body.appendChild(el); 
} 
} 
var ie5 = (document.getElementById && document.all); 
var ns6 = (document.getElementById && !document.all); 
var ua = navigator.userAgent.toLowerCase();
var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);
function getmouseposition(e) // thanks to Grant O. Anderson from Arizona State University
{
if(document.getElementById)
{
var iebody=(document.compatMode && 
document.compatMode != 'BackCompat') ? 
document.documentElement : document.body;
pagex = (isapple == 1 ? 0 : (ie5)?iebody.scrollLeft:window.pageXOffset);
pagey = (isapple == 1 ? 0 : (ie5)?iebody.scrollTop:window.pageYOffset);
mousex = (ie5)?event.x:(ns6)?clientX = e.clientX:false;
mousey = (ie5)?event.y:(ns6)?clientY = e.clientY:false;

var lixlpixel_tooltip = document.getElementById('tooltip');
lixlpixel_tooltip.style.left = (mousex+pagex+offsetx) + 'px';
lixlpixel_tooltip.style.top = (mousey+pagey+offsety) + 'px';
}
}
function tooltip(tipdiv)
{
var tip = document.getElementById(tipdiv).innerHTML;
if(!document.getElementById('tooltip')) newelement('tooltip');
var lixlpixel_tooltip = document.getElementById('tooltip');
lixlpixel_tooltip.innerHTML = tip;
lixlpixel_tooltip.style.display = 'block';
document.onmousemove = getmouseposition;
}
function ttexit()
{
document.getElementById('tooltip').style.display = 'none';
}
/////////////////////// end tooltips ///////////////////////

//*********************************************
//*  cookie manage  *
//*********************************************

function setCookie(name, value)
{
	var date = new Date();
	date = new Date(date.getFullYear(), date.getMonth() + 3, date.getDate());
	document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString();
}

function getCookie(name)
{
	var cookies = document.cookie.split("; ");
	for(var i = 0; i < cookies.length; i++)
	{
		var cookie = cookies[i].split("=");
		if(name == cookie[0])
			return unescape(cookie[1]);
	}
	return null;
}

function deleteCookie(name)
{
	if(getCookie(name))
	{
		document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}




var downloadedblocks = [];

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curright = obj.offsetRight
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curtop];
}

function HTMLBlockPopup(evt, parent, obj_id)
{	

	//var left_menu = document.getElementById("left_menu");

	//var w = left_menu.offsetWidth;
        //var h = left_menu.offsetHeight;
    
	//var l = left_menu.offsetLeft;
        //var t = left_menu.offsetTop; 
	
	if(window.is_loading == 1)return;
	window.is_loading = 1;
	var e = evt || event;
	var left = 0;
	o = parent;
	
	while (o.tagName != 'P')o = o.parentNode;
	parent.o = o;
	
	var top = o.offsetHeight;
	//o.style.position = 'relative';

	//alert("0");
	left = left + 'px';
	top = top + 'px';
	
	for(var i = 0; i < downloadedblocks.length; i++){
		if(downloadedblocks[i] != parent.inpopup){
			downloadedblocks[i].style.display = 'none';
			//downloadedblocks[i].parentNode.o.style.position = 'static';
		}
	}
	
	if(parent.is_downloaded == 1){
		window.is_loading = 0;
		if(!parent.inpopup)return;
		if(parent.inpopup.style.display == 'block')return;
		//parent.inpopup.style.left = left;
		
		//parent.inpopup.style.top = top;
		parent.inpopup.style.display = 'block';
		return;
	};
	
	parent.is_downloaded = 1;
//alert("1");
	
    var popup = function(data)
	{	
		window.is_loading = 0;
		var main = document.createElement('DIV');
		
		var corner_top_wrap = document.createElement('div');
		var corner_top = document.createElement('div');
		var corner_bottom_wrap = document.createElement('div');
		var corner_bottom = document.createElement('div');
	
		downloadedblocks.push(main);
		
		var content = document.createElement('DIV');
		var header = document.createElement('DIV');
		var body = document.createElement('DIV');
		var closer = document.createElement('SPAN');
		
		
		
		closer.innerHTML = '<span>Close x</span>';	
		header.innerHTML = '';
		body.innerHTML = data;

		main.className = "popup";
		main.style.position = 'absolute';
		main.style.display = 'block';
		//main.style.zIndex = 101;

		
		
		corner_top_wrap.className = "corner_top_wrap";
		corner_top.className = "corner_top";
		corner_bottom_wrap.className = "corner_bottom_wrap";
		corner_bottom.className = "corner_bottom";
		content.className = "content";
		header.className = "header";
		body.className = "txt";
		closer.className = "close";
		
		

		main.appendChild(corner_top_wrap);
		corner_top_wrap.appendChild(corner_top);
		main.appendChild(content);
		content.appendChild(header);
		header.appendChild(closer);
		content.appendChild(body);
		main.appendChild (corner_bottom_wrap);
		corner_bottom_wrap.appendChild (corner_bottom);
		parent.appendChild(main);
		
	
		parent.inpopup = main;
		
		closer.onclick = function(){
			main.style.display = 'none';
			var ov = parent.onmouseover;
			parent.onmouseover = function(){};
			window.setTimeout(function(){parent.onmouseover = ov},1000);
			
		};	
		
	};
	
	var url = '/doaction.php?action=GetHTMLBlock&id=' + obj_id;
	downloadBlock(url,function(data){popup(data);});
} 

function downloadBlock(url, callback){
				var req = null;
		if (window.ActiveXObject) {
			var a = ['MICROSOFT.XMLHTTP'];
			for (var i = 0; i < a.length; i++) {
				try {
					req = new ActiveXObject(a[i]);
					break;
				} catch (e) { }
			}
			req.open( "GET", url, true);
						
		} else if (window.XMLHttpRequest) {
			req = new XMLHttpRequest;
			req.open( "GET", url, true);
		}

		req.onreadystatechange = function() {
						if (4 == req.readyState){
							// if "OK"
							if (200 == req.status){
								var xml = null;
									xml = req.responseText;
								callback(xml);
							} else {
								alert("Problem retrieving XML data");
							}
						}
		}
		req.send(null);
	}
