var platnoscipl = "https://www.platnosci.pl/paygw/ISO/NewPayment";
window.document.onkeypress = CheckEnter;
//document.oncontextmenu = function(){return false;}

function Open(file,title,width,height)
{
	var left=(screen.availWidth-width)/2;
	var top=(screen.availHeight-height)/2;
	window.open(file,title,'toolbar=no,menubar=no,location=no,personalbar=no,status=no,resizable=yes,scrollbars=yes,width='+width+',height='+height+',top='+top+',left='+left+'');
}
function Open2(file,title,width,height)
{
	var left=(screen.availWidth-width)/2;
	var top=(screen.availHeight-height)/2;
	w=window.open(file,title,'toolbar=no,menubar=no,location=no,personalbar=no,status=no,resizable=no,scrollbars=no,width='+width+',height='+height+',top='+top+',left='+left+'');
}

function Open3(file,title,width,height)
{
	var left=(screen.availWidth-width)/2;
	var top=(screen.availHeight-height)/2;
	w=window.open(file,title,'toolbar=no,menubar=no,location=no,personalbar=no,status=no,resizable=yes,scrollbars=no,width='+width+',height='+height+',top='+top+',left='+left+'');
}
function Open4(file,title,width,height)
{
	var left=(screen.availWidth-width)/2;
	var top=(screen.availHeight-height)/2;
	w=window.open(file,title,'toolbar=no,menubar=no,location=no,personalbar=no,status=no,resizable=no,scrollbars=yes,width='+width+',height='+height+',top='+top+',left='+left+'');
}

function Reminder(email,page,msg)
{
	if(document.getElementById(email)==null || document.getElementById(email).value=="") 
	{
		alert(msg);
	}
	else
	{	
		SubmitForm(page);
	}
}

function GetPayment(pagedest,optID,demoVersion)
{
	var obj = document.getElementsByName(optID);

	for(var i = 0; i < obj.length; i++)
	{
		if(obj[i].checked)
		{
			if(obj[i].value == 3) //platnosci.pl
			{
				if(demoVersion==1)
				{
					alert(language.Get("key1"));
					return false;
				}
				else
				{
					return SubmitForm(pagedest);
				}
			}
			else
			{
				return SubmitForm(pagedest);			
			}
		}
	}

	alert(language.Get("key2"));
}
function GetPaymentBackup(pagedest,optID,demoVersion)
{
	var obj = document.getElementsByName(optID);

	for(var i = 0; i < obj.length; i++)
	{
		if(obj[i].checked)
		{
			if(obj[i].value == 3) //platnosci.pl
			{
				if(demoVersion==1)
				{
					alert(language.Get("key1"));
					return false;
				}
				else
				{
					return SubmitForm(pagedest);
				}
			}
			else
			{
				return SubmitForm(pagedest);			
			}
		}
	}

	alert(language.Get("key2"));
}
function ChangeUrl(obj, newUrl)
{
	if(obj.href != null)
	{
		obj.href = newUrl;
	}
}

function DeleteProduct(id,page)
{
	document.forms.form.hdnDel.value=id; 	
	SubmitForm(page);
}

function SubmitForm(page,enctype)
{
	if(enctype !=null)
	{
		document.forms.form.enctype = enctype;
		document.forms.form.encoding = enctype;
	}
	
	document.forms.form.method = "post";
	document.forms.form.action = page;
	document.forms.form.submit(); 	
}

function ChangeImg(uniqueName)
{	
	try
	{
		var parent = document.getElementById("spanImgBig");	
		var tmp = imgprd.GetImage(uniqueName);
		
		if(tmp != null && tmp != undefined)
		{
			parent.innerHTML = "<img id='"+tmp.id+"' src='"+tmp.src+"' alt='' width='"+tmp.width+"' height='"+tmp.height+"'>"; //width='"+tmp.width+"' height='"+tmp.height+"'
		}
	}catch(e){}
}

function GetImgID(idobj)
{
		var img= document.getElementById(idobj).getElementsByTagName("IMG");
		return img[0].id;
}

function Img()
{
	this.uniqueName = "";
	this.img = null;	
}

function ImagePrd(http_host,img_type,water_mark)
{
	this.arrImages = new Array();
	this.host = http_host;
	this.imgType = img_type;
	this.waterMark = water_mark;
			
	this.Add = function(id,uniqueName,fileName2Disc,width,height)
	{
		var im = new Img();
		im.uniqueName = uniqueName;
		var img = new Image();
		img.src = this.GetUrl(fileName2Disc);
		img.alt = "Picture";
		img.id = id;
		img.width = width;
		img.height = height;
		im.img = img;
		this.arrImages.push(im);
	}
	
	this.GetUrl = function(fileName2Disc)
	{
		if(this.waterMark == 1)
		{
			return "image.php?src=images/upload/"+fileName2Disc;
		}
		else
		{
			return "../images/upload/"+fileName2Disc;
		}
	}
	
	this.GetImage = function(uniqueName)
	{
		for(var i=0; i< this.arrImages.length;i++)
		{
			if((this.arrImages[i]).uniqueName == uniqueName)
			{
				return this.arrImages[i].img;
			}
		}
	}	
}

function SetPrice(src,dest,token)
{
	var src = document.getElementById(src);
	var dest = document.getElementById(dest);
	dest.innerHTML = src.value+" "+token;
}

function CheckPrice(price,dest,token)
{
	var dest = document.getElementById(dest);
	if(dest.innerHTML == "")
	{
		dest.innerHTML = price +" "+token;	
	}
}

function SetLogin(val)
{
	if(val.length == 0)
	{
		var email = document.getElementById("Login");
		val = email.value;
	}

	try
	{
		var login = document.getElementById("PassEmail");
		login.value = val;
	}
	catch(e){};
}

function CheckEnter(e)
{
	if (!e) {var e = window.event;}
		
	if(IsMSIE())
	{
		if(e.srcElement.type == "textarea")
		{
			return true;
		}
	}
	else
	{
		var node = e.target;
		if(node.type == "textarea")
		{
			return true;
		}
	}
	
	if(e.keyCode == 13)
	{
		return false;
	}
	
	return true;
}

function ChangeOrder(obj,url,mode)
{
	if(mode==1)//url friendly
	{
		var arr = Array();
		arr = url.split(",");
		arr[7] = obj[obj.selectedIndex].value;
		url = arr.join(",");
		window.location = url;
	}
	else
	{
		var m = obj[obj.selectedIndex].value;
		url = url.replace(/order=([0-9]+)/,"order="+m);
		window.location = url;
	}
}
function ValidateQuantity(control,unit,unittype)
{
	var quantity = control;
	var v = new Validate();
	
	if(unittype==0)
	{			
		v.Add(quantity.id,"intplus",language.Get("key3"),"1, 2, 10");
		if(!v.IsValid())
		{
			quantity.value = 1;
			quantity.style.background="red";
			alert(v.message);
		}		
	}
	else if(unittype == 1)
	{
		v.Add(quantity.id,"floatplus",language.Get("key3"),"0.3, 0.6, 1, 1.5");
		quantity.value = quantity.value.replace(/,/g,".");
		if(!v.IsValid())
		{
			quantity.value = 0.1;
			quantity.style.background="red";
			alert(v.message);
		}		
	}	
}

function AddToBag(id,prdexist,unit,unittype)//id = idprodukt, unit - jednostka miary produktu
{	
	if(prdexist == 0)
	{
		alert(language.Get("key4"));
		return false;
	}
	
	if(CheckBagAccount(id) == false)
	{
		return false;
	}
	
	//textbox z ilosci¹ produktow do dodania  - jesli istnieje
	var quantity = document.getElementById("txtq"+id);	
	if(quantity!=null) 
	{
		var v = new Validate();
		if(unittype == 0)
		{			
			v.Add("txtq"+id,"intplus",language.Get("key3"),"1, 2, 10");			
		}
		else if(unittype == 1)
		{
			v.Add("txtq"+id,"floatplus",language.Get("key3"),"0.3, 0.5, 1, 1.5");
			quantity.value = quantity.value.replace(/,/g,".");
		}
		
		if(!v.IsValid())
		{
			quantity.style.background="red";
			alert(v.message);
			return false;
		}
		else
		{
			SetCookie("bagPrdQuantity", quantity.value);
		}		
	}
	
	//zapis wybranych typow do koszyka
	SaveBagAccount(id);
}

function GetWindowWidth()
{
    if(window.document.documentElement && ( window.document.documentElement.clientWidth || window.document.documentElement.clientHeight ))
    {
        return window.document.documentElement.clientWidth;
    } 
    else if(window.document.body && ( window.document.body.clientWidth || window.document.body.clientHeight ))
    {
        return window.document.body.clientWidth;
    }
    else
    {
        return 0;
    }
}

function GetWindowHeight()
{
    if( window.document.documentElement && ( window.document.documentElement.clientWidth || window.document.documentElement.clientHeight ))
    {
        return window.document.documentElement.clientHeight;
    } 
    else if( window.document.body && ( window.document.body.clientWidth || window.document.body.clientHeight ))
    {
        return window.document.body.clientHeight;
    }
    else
    {
        return 0;
    }
}

function IsMSIE()
{
	var userAgent = window.navigator.userAgent;
	if(userAgent.indexOf("MSIE")>=1)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function GoTo(dest)
{
	location.href = "#a"+dest;
}
function AddComment(page)
{
	SubmitForm(page);
}
function GetScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function SetScrollXY(scrOfX,scrOfY) 
{
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    window.pageYOffset = scrOfY;
    window.pageXOffset = scrOfX;
  } 
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    document.body.scrollTop = scrOfY;
    document.body.scrollLeft = scrOfX;
  } 
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    document.documentElement.scrollTop = scrOfY;
    document.documentElement.scrollLeft = scrOfX;
  }
}
function AddToFavorite()
{
	var url=document.location;
	var title=document.title;
	
    //FireFox
    if (window.sidebar) { 
        window.sidebar.addPanel(title, url, ""); 
    //IE
    } else if (window.external) {
        window.external.AddFavorite(url, title); 
    //Opera
    } else if (window.opera && window.print) {
        var a = document.createElement("a");
        a.setAttribute("href", url);
        a.setAttribute("title", title);
        a.setAttribute("rel","sidebar");
        a.click();
    }		
}

function CheckZipCode(obj)
{	
	var t = obj.value;
	if (t.match(/^\d+$/) == null)
	{
		obj.value = t.substring(0,t.length-1);
	}
}
function CheckZipLength(obj)
{
	if(obj.value.length == 2)
	{
		document.getElementById('txtplugzipcode2').focus();
	}
}

function MakeRequest(url) 
{
    http_request = false;
	
    if (window.XMLHttpRequest) // Mozilla, Safari,...
	{ 
        http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml;');
		}		
    } 
	else if (window.ActiveXObject) // IE
	{ 
        try 
		{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
		catch (e) 
		{
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) 
	{
        return false;
    }
	else
	{
		return http_request;
	}
}

function CheckRating(msg,enabled)
{
	if(enabled==1)
	{
		if(document.getElementById('optcomment1').checked==false && document.getElementById('optcomment2').checked==false && document.getElementById('optcomment3').checked==false && document.getElementById('optcomment4').checked==false && document.getElementById('optcomment5').checked==false)
		{
			alert(msg);
			return false;
		}
	}
	return true;
}
function SaveProductComment(idprd,msgok,msgerr)
{
	var comment = document.getElementById('txtacomment').value;
	var author = document.getElementById('txtcomment').value;
	var ratings = document.getElementsByName('optcomment');
	var email = document.getElementById('txtcommentemail').value;	
	var rat = 0;

	for(var k=0;k < ratings.length;k++)
	{
		var rating = ratings[k];
		if(rating.checked == true)
		{
			rat = rating.value;
		}
	}
	var url = 'prdcommentajax.php?idprd='+idprd+'&rating='+rat+'&comment='+encodeURIComponent(comment)+'&author='+encodeURIComponent(author)+'&email='+(email)+'';	
	var http_request = MakeRequest(url);
	if(http_request != null)
	{
		http_request.onreadystatechange = function() {return true;};
		http_request.open('GET', url, true);
		http_request.send(null);		
		alert(msgok);
	}
	else
	{
		alert(msgerr);
	}
	
	document.getElementById('txtacomment').value = "";
	document.getElementById('txtcomment').value = "";
	document.getElementById('txtcommentemail').value = "";
}
function SetSrcSearch(src)
{
	document.getElementById('hdnSearchSrc').value = src;
}
//Funkcja do zapisu w cookie stanu formularza wyszukiwania zaawansowanego na liscie produktow 
function SaveSearch()
{
	if(objValid!=null)
	{
		if(!objValid.IsValid())
		{
			alert(objValid.message);
			return false;
		}
	}	

	var obj = document.getElementsByName("chksrch");//kolekcja checkboxow
	var tmp = "";
	
	for(var i = 0; i < obj.length; i++)
	{
		if(obj[i].checked)
		{
			tmp += "" + obj[i].getAttribute("id").replace("chk","") +";";
		}		
	}
	if(tmp.length>0)
	{
		tmp = tmp.substring(0,tmp.length-1);
	}
	
	SetCookie("search[idatr]", tmp);
	SetCookie("search[pricefrom]", document.getElementById("txtPriceFrom").value);
	SetCookie("search[priceto]", document.getElementById("txtPriceTo").value);
	return true;
}
//Kasuje stan formularza wuszukiwania
function DeleteSearch()
{
	DeleteCookie("search[idtree]");
	DeleteCookie("search[idatr]");
	DeleteCookie("search[pricefrom]");
	DeleteCookie("search[priceto]");
	DeleteCookie("search[text]");
}
function DSS()
{
	return DeleteSearch();
}

function SubmitSearch(idControl,page,src)
{
	var s = document.getElementById(idControl);	
	if(s != null && s.value.length>0)
	{
		SetSrcSearch(src);
		SubmitForm(page);
	}
	else
	{
		return false;
	}
}

function SubmitSearchExt(page,src)
{
	var onsub = true;
	
	if(objValid!=null)
	{
		if(!objValid.IsValid())
		{
			onsub = false;
			alert(objValid.message);			
		}
	}	
	
	if(onsub)
	{
		SetSrcSearch(src);
		SubmitForm(page);
	}
}

function SearchShowHide(obj,objIdToShow,textshow,texthide)
{
	var objToShow = document.getElementById(objIdToShow);
	if(objToShow.style.display=="none")
	{
		objToShow.style.display="block";
		obj.innerText = texthide;
		obj.className = "clssearchshow searchvisible";
	}
	else
	{
		objToShow.style.display="none";
		obj.innerText = textshow;
		obj.className = "clssearchshow searchhide";
	}
}
function ShowPopAdvertising(wait)
{
	if(wait==null)
	{
		setTimeout('ShowPopAdvertising(1)', 1000);
	}
	else
	{
		var advert = document.getElementById("popAdvertising");
		var left=(GetWindowWidth() - advert.style.width.replace("px",""))/2;
		var top=(GetWindowHeight() - advert.style.height.replace("px",""))/2;	
		advert.style.left = left+"px";
		advert.style.top = top+"px";
	}
}
function SetTemplateFolder(obj)
{
	SetCookie("templateFolder", obj[obj.selectedIndex].value);
	SubmitForm("home");
}
function GoProducer(obj,url)
{
	DeleteSearch();
	var name = obj[obj.selectedIndex].innerHTML;
	name = name.replace(" ","-");
	name = name.toLowerCase();
	var url = name + url;
	location.href = url.replace("{ID}",obj[obj.selectedIndex].value);	
}

function SaveUserForm(idform,msgok,msgerr)
{
	var name = document.getElementById('txtform'+idform+'_name');
	var email = document.getElementById('txtform'+idform+'_email');
	var subject = document.getElementById('txtform'+idform+'_subject');
	var body = document.getElementById('txtform'+idform+'_body');	
	
	var url = 'userformajax.php?idform='+idform+'&name='+encodeURIComponent(name.value)+'&body='+encodeURIComponent(body.value)+'&subject='+encodeURIComponent(subject.value)+'&email='+(email.value)+'';	
	var http_request = MakeRequest(url);
	if(http_request != null)
	{
		http_request.onreadystatechange = function() {return true;};
		http_request.open('GET', url, true);
		http_request.send(null);		
		alert(msgok);
	}
	else
	{
		alert(msgerr);
	}
	
	name.value = "";
	email.value = "";
	subject.value = "";
	body.value = "";
}
function FloatFormat(no)
{
	var number=new String(no);
	if(number.search(/\./)!=-1)
	{
		var arr = new Array();
		arr = number.split(".");
		if(arr[1].length==0)		
		{
			return arr[0]+".00";
		}
		else if(arr[1].length==1)
		{
			return arr[0]+"."+arr[1]+"0";
		}
		else
		{
			return number;
		}
	}
	else
	{
		return number+".00";
	}
}
function CheckRules()
{
	var chk = document.getElementById('chkrules');
	if(chk == null){return true;}
	if(!chk.checked){alert(language.Get("key13"));return false;}
	return true;
}
