// @(#) $Id: main.js,v 1.1.1.1 2004/05/07 09:14:29 tom Exp $

function WOpen(name,url,w,h,s,r)
{
	return(window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+s+",resizable="+r+",fullscreen=no,channelmode=no,width="+w+",height="+h));
}

function ImgOpen(url) 
{
	win = window.open(url, '', 'location=no toolbars=no width=200 height=200 left=100 top=100 resizable=no');
	win.document.open();
	win.document.write('<head><title>Image</title><style>body { margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px;</style>');
	win.document.write('<script language="javascript"> function autosize() { img=document.obrazek; window.resizeTo(img.width + 10, img.height + 29); }</script></head>');
	win.document.write('<body onload="autosize()"><img name="obrazek" onclick="window.close()" border="0" src="' + url + '"></body>');
	win.document.close();
}



function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
	i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}







function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


function add_to_cart (p_id, p_name, lang) 
{
	 dtime = "36000";
	 var exp = new Date();
	 exp.setTime(exp.getTime() + dtime);
	 pname = "pid" + p_id;
	 // adding items of one kind
	 
	/*
	 amount_entered = prompt("Quantity to add:", "1");
	 if(amount_entered==null)
		 return;
	 amount_entered = parseInt(amount_entered);
	*/
	amount_entered = 1;

	 if(amount_entered<0)
	 {
		alert("Value has to be positive");
	 }
	 else if(amount_entered==0)
	 {
		alert("No action has been taken!");
	 }
	 else
	 {
		amount = GetCookie(pname);
		
		amount = parseInt(amount);
		if(!amount) amount = 0;
		amount += amount_entered;

		SetCookie(pname, amount);
		
		icounter = GetCookie('itemscounter');
		icounter = parseInt(icounter);
		if(!icounter) icounter = 0;
		icounter += amount_entered;
		SetCookie('itemscounter', icounter);

		/*
		if(amount_entered==1)
			message = amount_entered + ' item of ' + p_name + ' has been added to your cart.';
		else
			message = amount_entered + ' items of ' + p_name + ' have been added to your cart.';
		*/
		message = '';
		switch(lang)
		{
			case 'pl': message = 'Dodano do koszyka: ' + p_name; break;
			case 'en': message = 'Added to cart: ' + p_name; break;
			case 'ba': message = 'Added to cart: ' + p_name; break;
		}
		alert(message);
		actualize_items();
		
		//if(lang == 'ba') tmp = '&debug=ba';
		
		document.location.href = '?m=10&a=1';
	}

/*
// adding prompt which shows cart content, not just amount to add
	 dtime = "36000";
	 var exp = new Date();
	 exp.setTime(exp.getTime() + dtime);
	 pname = "pid" + p_id;
	 // adding items of one kind
	 amount_already = GetCookie(pname);
	 amount_already = parseInt(amount_already);
	 if(!amount_already)
	 {
		 amount_in_cart = 0;
		 amount_to_prompt = 1;
	 }
	 else
	 {
		amount_in_cart = amount_already;
		amount_to_prompt = amount_already;
	 }
	 amount_entered = prompt("Quanity in cart:", amount_to_prompt);
	 if(amount_entered==null)
		 return;
	 amount_entered = parseInt(amount_entered);
	 if(amount_entered<0)
	 {
		alert("Value has to be positive");
	 }
	 else
	 {
		 difference = amount_entered - amount_in_cart;
		 icounter = GetCookie('itemscounter');
		 icounter = parseInt(icounter);
		// if(icounter >= 1) icounter+=difference;
		// else icounter = amount_entered;
		icounter+=difference;
		 SetCookie('itemscounter', icounter);

		 SetCookie(pname, amount_entered);
		 if(amount_entered==0 && amount_in_cart>0)
			message = p_name + '\n\nhas been removed from your cart.'; 
		 else if(amount_entered==0 && amount_in_cart==0)
			message = p_name + '\n\nNo action has been taken!'; 
		 else
		 {
			if(amount_entered==1)
				message = amount_entered + ' item of ' + p_name + ' is now in your cart.';
			else
				message = amount_entered + ' items of ' + p_name + ' are now in your cart.';
		 }
		 alert(message);

		 actualize_items();
	}
*/
}

function actualize_items() 
{
 dtime = "360000";
 var exp = new Date();
 exp.setTime(exp.getTime() + dtime);

 icounter = GetCookie('itemscounter');
 icounter = parseInt(icounter);
 if (!icounter) { icounter=0; }

 SetCookie('itemscounter', icounter);
 document.getElementById("items_number").innerHTML = icounter;
 // document.getElementById("items_number2").innerHTML = icounter;
}

function IsNumeric(sText)
{
	if(sText == '' || sText==null) return false;
	//if(sText < 0) return false;
	if(!sText) return false;

   var ValidChars = " -0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
}

function CheckCardNumber(url)
{
	ptr = document.forms.TheCardForm;
	ptr.action = url + "&a=6";
	ptr.submit();
}

function checkDataForm()
{
	ptr = document.forms.TheNameForm;

	if(ptr.bname.value=='' 
		|| (ptr.baddr1.value=='')
		|| (ptr.bcity.value=='')
		|| (ptr.bzip.value=='')
		|| (ptr.email.value=='')
		|| (ptr.phone.value==''))
	{
		alert("Proszę wypełnić wszystkie pola.");
	}
	else if(ptr.DstCB.checked)
	{
		if((ptr.sname.value=='')
			|| (ptr.saddr1.value=='')
			|| (ptr.scity.value=='')
			|| (ptr.szip.value==''))
		{
			alert("Proszę wypełnić wszystkie pola odbiorcy.");
		}
		else
			ptr.submit();
	}
	else
		ptr.submit();

/*
	if(ptr.bname.value=='')
		alert("Please fill customer's name field.");
	else if(ptr.baddr1.value=='' && ptr.baddr2.value=='')
		alert("Please fill customer's address field.");
	else if(ptr.bcity.value=='')
		alert("Please fill customer's city field.");
	else if(ptr.bzip.value=='')
		alert("Please fill customer's zip field.");
	else if(ptr.email.value=='')
		alert("Please fill customer's e-mail field.");
	else if(ptr.phone.value=='')
		alert("Please fill customer's phone field.");
	else if(ptr.DstCB.checked)
	{
		if(ptr.sname.value=='')
			alert("Please fill shipping name field.");
		else if(ptr.saddr1.value=='' && ptr.saddr2.value=='')
			alert("Please fill shipping address field.");
		else if(ptr.scity.value=='')
			alert("Please fill shipping city field.");
		else if(ptr.szip.value=='')
			alert("Please fill shipping zip field.");
		else
			ptr.submit();
	}
	else
		ptr.submit();
*/
}

function goBackToForm(url)
{
	ptr = document.forms.TheCardForm;	
	ptr.action = url + "&a=4";
	ptr.submit();
}

function GetDisplayShow() 
{
	if (navigator.userAgent.search('Gecko')>0)
	{
		return 'table-row';
	}
	else
	{
		return 'block';
	}
}

function ShowHideShipping()
{
	document.getElementById('DstLayer').style.display = document.TheNameForm.DstCB.checked ? GetDisplayShow() : 'none';
}