function getBrowser() {
	var browser = navigator.userAgent;
	if (browser.indexOf('MSIE') != -1) {
		return 'IE'
	}
	else if (browser.indexOf('Firefox') != -1) {
		return 'FF';
	}
	else if (browser.indexOf('Opera') != -1) {
		return 'opera';
	}
	return 'unknown';
}

function getTop(item){
	var myTarget = item;  
	var top=0;
	while(myTarget!= document.body) {
		top += myTarget.offsetTop;     
		myTarget = myTarget.offsetParent;   
	} 
	return top;
}

function getLeft(item){
	var myTarget = item;  
	var left=0;
	while(myTarget!= document.body) {
		left += myTarget.offsetLeft;     
		myTarget = myTarget.offsetParent;   
	} 
	return left;
}

function goPage(link,target){
	if(!target)
		document.location.href=link;
	else
		window.open(link,"","resizable=yes,width=900,height=500,status=yes,location=yes,menubar=yes,scrollbars=yes")
}

function changeImage(image,imID,height,width){
	currentImage=imID;
	var im=document.getElementById('productImage');
	im.src=image;
	im.height=height;
	im.width=width;
}

function popIm(id){

	window.open('popImage.php?id='+id,"","resizable=yes,width=600,height=400,status=yes,location=no,menubar=no,scrollbars=yes")
}

function popPrIm(id){

	window.open('popImage.php?prID='+id,"","resizable=yes,width=600,height=400,status=yes,location=no,menubar=no,scrollbars=yes")
}


function printCart(lang){

	window.open('cart.php?lang='+lang,"","resizable=yes,width=550,height=600,status=yes,location=no,menubar=no,scrollbars=yes")
}

function popColorCard(id){

	if(id)
		window.open('popImage.php?ccID='+id,"","resizable=yes,width=600,height=400,status=yes,location=no,menubar=no,scrollbars=yes")
}

function popWin(url,he,wi){
	if(he<=0)he = '300';
	if(wi<=0)wi = '300';
	
	var kyssa=window.open(url,'','width='+wi+',height='+he+',resizable=yes,toolbar=yes,status=yes,location=yes,menubar=yes,scrollbars=yes,top=10,left=10');	
}
function popWin2(url,he,wi){
	if(he<=0)he = '300';
	if(wi<=0)wi = '300';
	
	var kyssa=window.open(url,'','width='+wi+',height='+he+',resizable=yes,toolbar=nos,status=no,location=no,menubar=no,scrollbars=yes,top=10,left=10');	
}

function cleanInput(input,str){
	if(input.value==str)
		input.value='';
}

function restoreInput(input,str){
	if(input.value=='')
		input.value=str;
}

$(function(){

           // bind 'myForm' and provide a simple callback function 
	function makeHrefUnique(href) {
		if (!href){
			return null;
		}
		var currentTime = new Date(); 
		href += (href.indexOf('?') == -1 ? '?' : '&') +  + currentTime.getTime() ;
		return href;
	}
	if( getBrowser() =='FF') {
		$('#orderForm').attr({method: 'get'});
	}

	$('#orderForm').ajaxForm(function() { 
	   return submitOrderForm();
	}); 
	
	function submitOrderForm() {
		var lang = $('#orderForm #lang').val();

		alert(productAddedToCart);
		$.get(makeHrefUnique('index.php?getCartBox=1&lang='+lang), {}, refreshCartBox);
		searchProducts() ;
		return false;
	}

	function refreshCartBox(data) {
		$('#leftCartBox').replaceWith(data);
		$('#orderForm input').clearFields();
	}
	 
	$('#productSearchFilter').ajaxForm(function() {
		return searchProducts();
	}); 

	$('#productSearchFilter select').change(function(){
		searchProducts();
	});
	

	$('#productSearchFilter input').blur(function(){
		searchProducts();
	});

	function searchProducts() {
		var url = 'index.php?ajax=1&'+
					$('#productSearchFilter input').fieldSerialize()+'&'+
					$('#productSearchFilter select').fieldSerialize();
		$.get(url, {}, refreshProductSearch);
	}

	function refreshProductSearch(data) {

		$('#productSearchList').replaceWith(data);
		
		if( getBrowser() =='FF') {
			$('#orderForm').attr({method: 'get'});
		}
		$('#orderForm').ajaxForm(function() { 
			return submitOrderForm();
		}); 

	}
});