var $j = jQuery.noConflict();
function maybe_send_form(form){
	if(!form) form=document.forms[0];
	var elements=form.elements;
	for(var i=0; i<elements.length; i++){
		var element=elements[i];
		var required=element.getAttribute("my_required");
		if(required){
			var title=element.getAttribute("title");
			var name=element.getAttribute("name");
			var value=element.value;
			var title_or_name=title?title:name;
			if(!value){

                                  var data = {
			
						pWinWidth:  '400px',
						pWinHeight: '150px',
						pWinPosition: 'absolute',
						pWinContent: "<div class='p_topBar'><div class='popup-close'></div></div><div class='p_content'>Пожалуйста,  заполните поле \""+title_or_name+"\"!</div><div class='p_footer_with_ok'><a class='p_button_a' id='fill_field'><img src='/images/ok.gif'></a></div>"
						
						};
                                getPosition(data,element);
                                                
				//alert("Пожалуйста,  заполните поле \""+title_or_name+"\"");
                                
				return;
			}
		}
	}
	form.submit();
}

function openPopup(url,w,h){
  win=open(url,
    "popup","toolbar=no,location=no,directories=no,"+
    "status=no,scrollbars=no,resizable=no,"+
    "copyhistory=no,left="+(window.screen.width-w)/2+",top="+
    (window.screen.height-h)/2+",width="+w+",height="+h);
  win.focus();
}
function popImage(imageURL, ttl, width, height){
	if(!width) width=100;
	if(!height) height=100;
	if(!ttl) ttl="...";
        var opt='scrollbars=no,width='+width+',height='+height+',left='+(window.screen.width-width)/2+',top='+(window.screen.height-height)/2+',status=yes';
        imgWin = window.open('','',opt);
        with (imgWin.document) {
                writeln('<html><head><title>'+ttl+'</title><style type="text/javascript">body {overflow: auto; margin: auto; margin:0; padding: 0;}</style>');
                writeln('<sc'+'ript type="text/javascript">');
                writeln('function resizePopup() {');
	                writeln('width = document.images[0].width + 10; ');
        	        writeln('height = document.images[0].height + 50; ');
                	writeln('window.resizeTo(width,height); ');
	                writeln('if (window.innerWidth) { window.innerWidth = width - 10; window.innerHeight = height - 30; }');
	                writeln('window.moveTo((window.screen.width-width)/2, (window.screen.height-height)/2);');
                writeln('}');
                writeln('</s' + 'cript>');
		writeln('</head><body bgcolor="#FFFFFF" onload="resizePopup();">')
                writeln('<img src="'+imageURL+'" alt="Закрыть" onclick="window.close();" border="0" style="position: absolute; left: 0px; top: 0px; cursor: pointer;"/></body></html>');
                close();
        }
}


function getPosition(data,elem)
{
	
	var html = document.documentElement;
	var body = document.body;
	var docClHeight = body.clientHeight;
	var docClWidth = body.clientWidth;
	var allHeight = 0;
	
	xPos = (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0);
    yPos = (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0);
	
	if(html && html.scrollHeight)
		allHeight = html.scrollHeight;
	else if(body && body.scrollHeight)
		allHeight = body.scrollHeight;
	
	
	$j("#back_popup").css('height', allHeight+'px').css('width',(xPos + docClWidth)+"px");

	var popupWin = $j("#popupWin");
	popupWin.css("background-image","url(/images/popup_back.png)");
	popupWin.css('padding-bottom','10px');

	popupWin.css('width', data.pWinWidth);
	popupWin.css('height', data.pWinHeight);
	popupWin.css('border','1px solid #999');
	popupWin.css('position', data.pWinPosition);
	popupWin.css('top',yPos + (docClHeight - parseInt(data.pWinHeight))/2 + 'px');
	popupWin.css('left', xPos + (docClWidth - parseInt(data.pWinWidth))/2 + 'px');
	popupWin.html(data.pWinContent);
	popupWin.css('display', 'block');
	$j('#back_popup').css('display', 'block');
	
        $j("#error_alert_close").click(popupWinClose);
	$j("#continue_shopping").click(popupWinClose);
	$j("#to_shopping_cart").click(goToShopCart);
	$j(".popup-close").click(popupWinClose);
        $j("#fill_field").click(function() {
                       $j("#back_popup").css('display','none');
                       $j("#popupWin").html('').css('display', 'none');
                                      
                       if(elem != null){
                                elem.focus();
                        }
        });
        
}





function popupWinClose()
{
	
	$j("#back_popup").css('display','none');
	$j("#popupWin").html('').css('display', 'none');
}

function field_popupWinClose()
{
	
	$j("#field_back_popup").css('display','none');
	$j("#field_popupWin").html('').css('display', 'none');
}

function goToShopCart()
{
	
	$j("#back_popup").css('display','none');
	$j("#popupWin").html('').css('display', 'none');
	document.location = '/main/cart';
}

function beforeRequestReady()
{
			var data = {
			
						pWinWidth:  '400px',
						pWinHeight: '150px',
						pWinPosition: 'absolute',
                                                pWinContent: "<div style='text-align:center;padding:60px 0'>Подождите!<br><br><br><img src='/images/loading.gif' width='32' height='32' /></div>"
						};
			
			getPosition(data);
}




function add2basket(good_id, amount){
	var requestUrl="/main/add2basket.json?id="+good_id+"&qty="+amount;
	var a = $j.ajax({
		url: requestUrl,
		type:'post', 
                ajaxSend: beforeRequestReady(),

		success: function(msg){
			var response = msg;
			if(response>0){
				if(typeof(korzina_setCount) == "function"){
					korzina_setCount(response);
                                        
                                        var data = {
			
						pWinWidth:  '400px',
						pWinHeight: '150px',
						pWinPosition: 'absolute',
						pWinContent: "<div class='p_topBar'><div class='popup-close'></div></div><div class='p_content'>Товар добавлен в корзину!</div><div class='p_footer'><table  width='100%'><tr><td style='text-align:center;width:50%'><a  href='/main/cart' ><img src='/images/submit.gif' ></a></td><td style='text-align:center;width:50%'><a class='p_button_a' id='continue_shopping' ><img src='/images/prodoljit.gif' ></a></td></tr></table></div>"
						
						};

                                        getPosition(data);
					//alert("Товар добавлен в корзину!");
				}
			}
			else{
				if(typeof(korzina_setCount) == "function"){
					//alert("Ошибка добавления товара в корзину!");
					korzina_setCount(response);
				}
			}
		},
		error: function(){
                        var data = {
			
						pWinWidth:  '400px',
						pWinHeight: '150px',
						pWinPosition: 'absolute',
						pWinContent: "<div class='p_topBar'><div class='popup-close'></div></div><div class='p_content'>Ошибка доступа!</div><div class='p_footer_with_ok'><a  class='p_button_a' id='error_alert_close'><img src='/images/ok.gif'></a></div>"
						
						};
                        getPosition(data);
			//alert('Ошибка доступа!');
		}
	});
}

function PingSession(){
  var a = $j.get("/null.php");
}
//setInterval("PingSession()", 300000);
