var o_big_pic;
var a_big_imgs = [];
var a_big_imgs2 = [];
var a_big_imgs2_large = [];
var i_cut_rash;
var i_count_in_notepad = 0;
var next_url, back_url;
function check_orders_compare(product_id)
{
	if(typeof product_id != 'undefined')
	{
            
        JsHttpRequest.query(
            '/check_orders_compare.php', // backend
			{
                'product_id': product_id
            },
            // Function is called when an answer arrives. 
            function(result, errors) {
                document.getElementById("slect").value = result['value'];
                if(result['value'] == 0)
                {
                	i_count_in_notepad--;
                }
                else
                {
                	i_count_in_notepad++;
                }
            },
            true
        );
	}
};

function captchaRefresh()
{
	try{
		var oCaptcha=document.getElementById('captcha');
		if(oCaptcha != null)
		{
			var sRandom=Math.floor((Math.random()*100000000))+'';
			if(sRandom.length!=8)
			{
				var n='';
				for(var i=0;i<(8-sRandom.length);i++)
				{
					n='0'+n;
				}
				sRandom=n+sRandom;
			}
			oCaptcha.src = '/captcha/'+sRandom+'.jpg'; 
			document.getElementById('captcha_file').value=sRandom; 
		}
	}
	catch(e){};
}

function faq(oSubmit)
{
	var oForm=oSubmit.form;
	var aData={
		'name':oForm.name.value,
		'email':oForm.email.value,
		'message':oForm.message.value,
		'captcha':oForm.captcha.value,
		'captcha_file':oForm.captcha_file.value
	};
	
	var req = new JsHttpRequest();
    // Code automatically called on load finishing.
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
        	
        	if(isset(req.responseJS.status))
        	{
        		if(req.responseJS.status == 'error_captcha')
        		{
        			captchaRefresh();
        			document.getElementById('error').innerHTML = 'Ошибка! Неправильный текст с картинки<br/><br/>';
        			document.getElementById('captcha_error').innerHTML = 'Неправильный текст с картинки'; 
        		}
	        	else if(req.responseJS.status == 'success')
	        	{
	        		oForm.innerHTML = 'Спасибо! Ваш вопрос принят!';
	        	}
	        	else if(req.responseJS.status == 'empty')
	        	{
	        		document.getElementById('error').innerHTML = ' Все поля обязательны для заполнения!<br/><br/>';
	        	}	        	
        	}
        }
    }
    // Prepare request object (automatically choose GET or POST).
    req.open('POST', '/question.php', true);
    // Send data to backend.
    req.send(aData);	
    return false;
}

function isset(obj)
{
	if(typeof obj != 'undefined')return true;
	else return false
}
function init()
{
	try{
		setProdHeight();
		
		captchaRefresh();
		o_big_pic1 = document.getElementById('big_pic1');
		o_big_pic2 = document.getElementById('big_pic2');
		o_uses_span = document.getElementById('uses_span');
		showMap();
		if(i_cut_var != 0)
		{
			set_var(i_cut_var)
		}
	}
	catch(e){}
}

function set_rash(i_num)
{
	try{
		i_cut_rash = i_num;
		o_big_pic1.src = a_big_imgs1[i_cut_rash];
	}
	catch(e){}
}

function set_var(i_num)
{
	try{
		i_cut_var = i_num;
		o_big_pic2.src = a_big_imgs2[i_cut_var];
		if(isset(o_uses_span))
		{
			set_uses(i_num);			
		}
	}
	catch(e){}
}
function set_uses(i_num)
{
	try{
		o_uses_span.innerHTML = a_uses_span[i_cut_var];
	}
	catch(e){}
}

function go_next()
{
	try{	
		if(isset(a_big_imgs2[i_cut_var+1]))
		{
			set_var(i_cut_var+1);
		}
		else
		{
			if(isset(next_url))
			{
				location = next_url;
			}
		}
	}
	catch(e){}
}

function go_back(link_flag)
{
	try{
		if(isset(a_big_imgs2[i_cut_var-1]))
		{
			set_var(i_cut_var-1);
		}
		else
		{
			if(isset(back_url))
			{
				location = back_url;
			}
		}	
	}
	catch(e){}
}
function notepad() 
{
	var i_height = 730;
	try{
		if(i_count_in_notepad <= 4)
		{
			i_height = 490;
		}
		win_notepad=window.open('/notepad/?rand='+Math.round((Math.random()*10000)), 'notepad', 'scrollbars=1,resizable=1,width=810,height='+i_height+',toolbar=0');
		win_notepad.window.focus();
	}
	catch(e){}
}
function right_direct(url)
{ 
	var err=0;
	win=window.open(url,'');
	first_win=win;
	win.focus();
} 
function email (login, serv)
{
	if((typeof serv == 'undefined')||(serv=="")) serv="idealstone.ru";
	eml = login +  "@" + serv;
	return eml;
}


function mylo (login, serv)
{
 document.write (email(login, serv));
}

function namylo (login, serv, subj)
{
eml = "mailto:" + email(login, serv);
re= /\s/;
if(typeof subj != 'undefined')
{
	while(subj.search(re)>0)
	subj=subj.replace(re, "%20");
	
	if (subj != "") eml += "?subject=" + subj;
}
window.location.href = eml;
} 

function openBig(url, win_w,win_h) {
main_big=window.open(url,'','scrollbars=0,resizable=1,width='+win_w+', height='+win_h+',toolbar=0');
}

function setProdHeight()
{
	var tag = 'div', // тег с товаром
	className = 'gB', // класс тега с товаром
	tags = document.getElementsByTagName(tag), i, maxHeight = 0, gtags = [];
	for(i in tags)
	{
		if(tags[i].className == className)
		{
			gtags[gtags.length] = tags[i];
			if(maxHeight < tags[i].offsetHeight)
			{
				maxHeight = tags[i].offsetHeight;
			}
		}
	}
	for(i in gtags)
	{
		gtags[i].style.height = (maxHeight) + "px";
//		gtags[i].style.minHeight = maxHeight + 100;
	}		
	
}
function showBigImg()
{
	try{
		if(isset(a_big_imgs2_large[i_cut_var]))
		{
			openBig(a_big_imgs2_large[i_cut_var]['src'], a_big_imgs2_large[i_cut_var]['width'], a_big_imgs2_large[i_cut_var]['height']);
		}
	}
	catch(e){}
}

