

function viewpic(file, name, gender, age){
	MyWin = window.open("","","scrollbars=no,resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width=100,height=100");
	with(MyWin.document){
	open();
	write("<html>\n<head><scr"+"ipt>awidth=(document.layers)?0:8;awidth+=(document.all)?4:0;aheight=(document.layers)?22:51;</scr"+"ipt>\n<title>"+name+"</title>\n</head>\n<body onLoad=\"window.resizeTo(document.images[0].width+awidth,document.images[0].height+aheight)\"marginwidth=0 marginheight=0 leftmargin=0 topmargin=0 rightmargin=0 style=\"overflow:hidden;\">\n");
	write("<a href=\"#\" onclick=\"window.close();\"><img src=\"http://beastwear.se/_images/products/"+file+"\" border=\"0\"></a>\n");
	write("</body>\n</html>\n");
	close();
	}
}
function CorrectEmailaddress(SubscriberForm){
  if (SubscriberForm.pf_Email.value.length > 255)
  {
    alert("Ange som mest 255 tecken i \"Epostadress\" Fält.");
    SubscriberForm.pf_Email.focus();
    return (false);
  }
  if (SubscriberForm.pf_Email.value == "")
  {
    alert("Ange värdet för \"Epostadress\" Fält.");
    SubscriberForm.pf_Email.focus();
    return (false);
  }
  if (SubscriberForm.pf_Email.value.length < 7)
  {
    alert("Ange minst 7 tecken i \"Epostadress\"Fält.");
    SubscriberForm.pf_Email.focus();
    return (false);
  }
  pf_Email=document.SubscriberForm.pf_Email.value
  at=pf_Email.indexOf("@");
  lastat=pf_Email.lastIndexOf("@");
  dot=pf_Email.lastIndexOf(".");
  if(at<1||at!=lastat||dot<at){
    alert("Din epostadress är inte rätt. Försök igen.");
    return false;}
}


  /*
   * Popup info boxes
   */
  function popup_box(uri) {
   w = window.open(uri, 'bar', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,' + center_popup(top, 320, 300));
   w.focus();
  }

  /*
   * Popup centering function
   */
  function center_popup(ref,w,h) {
   var xpos, ypos;
   if (ref.window.document.body) {
    xpos = Math.round(ref.window.screenLeft + (ref.window.document.body.clientWidth / 2) - ((w + 5) / 2));
    ypos = Math.round(ref.window.screenTop + (ref.window.document.body.clientHeight / 2) - ((h + 25) / 2));
   }
   else {
    xpos = 150
    ypos = 150
   }
   xpos = xpos + Math.round(Math.random()*200-100);
   ypos = ypos + Math.round(Math.random()*200-100);
   return 'width='+w+',height='+h+',left='+xpos+',top='+ypos;
  }  
  
  /*
   * Array improvement
   */
  Array.prototype.unique = function ()
  {
    var vic = new Object();
    for(i=0; i < this.length; i++) {
      vic[this[i]] = "";
    }
    this.length = 0;
    for(i in vic) {
      this[this.length] = i;
    }
  };
  
  /*
   * For printons listing
   */
  
  document.write('<form action="" method="post" id="cart-form">');
  document.write('<input type="hidden" id="cart-form-do" name="do" value="" />');
  document.write('<input type="hidden" id="cart-form-item-id" name="item_id" value="" />');
  document.write('</form>');
  
  function update_price_based_on_value() {
    var idData = color_box.options[color_box.selectedIndex].value.split("#");
  }
  
  function add_item(printon_id) {
    
    // Define select box variables
    product_box = document.getElementById('product-' + printon_id);  
    color_box = document.getElementById('color-' + printon_id);            
    size_box = document.getElementById('size-' + printon_id);
    
    if ( product_box.selectedIndex == 0 ) {
      if ( show_exchange_rates == true ) { // Ugly fix to make sure we're in the english version
        alert('Before adding to your cart, you must pick garment, size and color.');
      } else {
        alert('Före du köper måste du välja plagg, storlek och färg.');
      }
      return false;
    }
    
    if ( size_box.selectedIndex == 0 ) {
      if ( show_exchange_rates == true ) { // Ugly fix to make sure we're in the english version
        alert('Before adding to your cart, you must pick size and color.');
      } else {
        alert('Före du köper måste du välja storlek och färg.');
      }
      return false;
    }
    
    if ( color_box.selectedIndex == 0 ) {
      if ( show_exchange_rates == true ) { // Ugly fix to make sure we're in the english version
        alert('Before adding to your cart, you must pick color.');
      } else {
        alert('Före du köper måste du välja färg.');
      }
      return false;
    }
    
    var idData = color_box.options[color_box.selectedIndex].value.split("#");
    id = idData[0];
    cart_form = document.getElementById('cart-form');
    cart_form.action = document.location.href;
    document.getElementById('cart-form-do').value = 'add-item';
    document.getElementById('cart-form-item-id').value = id;
    cart_form.submit();
  }
  
  function add_product_item(product_id) {
    cart_form = document.getElementById('cart-form');
    cart_form.action = location.href;
    document.getElementById('cart-form-do').value = 'add-product-item';
    document.getElementById('cart-form-item-id').value = product_id;
    cart_form.submit();
  }
  
  function remove_item() {
    cart_box = document.getElementById('cart'); 
    cart_form = document.getElementById('cart-form');
    cart_form.action = location.href;
    document.getElementById('cart-form-do').value = 'remove-item';
    document.getElementById('cart-form-item-id').value = cart_box.value;
    cart_form.submit();
  }
  
  function remove_item_special(product_id) {
    cart_box = document.getElementById('cart'); 
    cart_form = document.getElementById('cart-form');
    cart_form.action = location.href;
    document.getElementById('cart-form-do').value = 'remove-item';
    document.getElementById('cart-form-item-id').value = product_id;
    cart_form.submit();
  }
    
  function createXMLHttpRequest() {
    var ua;
    if(window.XMLHttpRequest) {
      try {
        ua = new XMLHttpRequest();
      } catch(e) {
        ua = false;
      }
    } else if(window.ActiveXObject) {
      try {
        ua = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        ua = false;
      }
    }
    return ua;
  }
  
  var req = createXMLHttpRequest();
  
  function getGarments(lang, printon_id) {
    product_box = document.getElementById('product-' + printon_id);
    product_box.options.length = 1;
    color_box = document.getElementById('color-' + printon_id);
    color_box.options.length = 1;
    size_box = document.getElementById('size-' + printon_id);
    size_box.options.length = 1;
    
    req.open('get', 'index.php?module=webshop&action=ajax&' + 
                    'type=garment&' + 
                    'lang=' + lang + '&' + 
                    'printon_id=' + printon_id);
    req.onreadystatechange = handleResponse;
    req.send(null);
  }
  
  function getSizes(lang, printon_id, garment) {
    if ( garment == 0 ) {  
      color_box = document.getElementById('color-' + printon_id);
      color_box.options.length = 1;
      size_box = document.getElementById('size-' + printon_id);
      size_box.options.length = 1;
      return false; 
    }
    
    garment = garment.split("#");
    price = garment[1];
    garment = garment[0];
    
    document.getElementById('price-' + printon_id).innerHTML = price;
    updateCurrencies(price, printon_id);  
    req.open('get', 'index.php?module=webshop&action=ajax&' + 
                    'type=size&' + 
                    'lang=' + lang + '&' + 
                    'printon_id=' + printon_id + '&' + 
                    'garment=' + garment);
    req.onreadystatechange = handleResponse;
    req.send(null);
  }
  
  function updatePrice(printon_id) {
    val = document.getElementById('color-' + printon_id).value.split("#");
    updateCurrencies(val[1], printon_id);
    document.getElementById('price-' + printon_id).innerHTML = val[1];
  }
  
  function updateCurrencies(val, printon_id) {
    if ( show_exchange_rates != true ) {
      // Don't try to update any fields
      return;
    }
    var re = new RegExp('([0-9 ]+.[0-9]{2})');
    var m = re.exec(val);
    if ( m != null ) {
      m = m[0];
      m = m.replace(',', '.');
      m = m.replace(' ', '');
      m = parseFloat(m);
      document.getElementById('currencies-' + printon_id).innerHTML = 'Approx.: ' + Math.round(m / rate_usd) + ' USD - ' + Math.round(m / rate_euro) + '  EURO - ' + Math.round(m / rate_gbp) + ' GBP';
    }  
  }
  
  function getColors(lang, printon_id, garment, size) {
    if ( garment == 0 || size == 0 ) { 
      color_box = document.getElementById('color-' + printon_id);
      color_box.options.length = 1;
      return false; 
    }
    
    garment = garment.split("#");
    price = garment[1];
    garment = garment[0];
    
    req.open('get', 'index.php?module=webshop&action=ajax&' + 
                    'type=color&' + 
                    'lang=' + lang + '&' + 
                    'printon_id=' + printon_id + '&' + 
                    'garment=' + escape(garment) + '&' + 
                    'size=' + escape(size));
    req.onreadystatechange = handleResponse;
    req.send(null);
  }
  
  function handleResponse() {
    if(req.readyState == 4){
      var response = req.responseText;
      var lines = response.split("\n");
      
      var type       = lines[0];
      var lang       = lines[1];
      var printon_id = lines[2];
      var garment    = lines[3];
      var size       = lines[4];
      
      // Define select box variables
      product_box = document.getElementById('product-' + printon_id);  
      color_box = document.getElementById('color-' + printon_id);
      size_box = document.getElementById('size-' + printon_id);
      
      // Loop data
      if ( type == 'size' ) {
        // Changed product (empty colors, empty sizes, populate sizes)
        color_box.options.length = 1;
        size_box.options.length = 1;
        
        // Loop through response
        for ( i = 5; i < lines.length - 1; i++ ) {
          size_box.options[size_box.options.length] = new Option(lines[i], lines[i]);
        }
        
      } else if ( type == 'color' ) {
        // Changed size (empty colors, populate colors)
        color_box.options.length = 1;
        
        // Loop through response
        for ( i = 5; i < lines.length - 1; i++ ) {
          var lineData = lines[i].split("|");
          color_box.options[color_box.options.length] = new Option(lineData[1], lineData[0]);
        }
        
      } else if ( type == 'garment' ) {
        // Changed product (empty garments, empty colors, empty sizes, populate garments)
        product_box.options.length = 1;
        color_box.options.length = 1;
        size_box.options.length = 1;
        
        // Loop through response
        for ( i = 5; i < lines.length - 1; i++ ) {
          product_box.options[product_box.options.length] = new Option(lines[i], lines[i]);
        }
      }
    }
  }


  function popup_custom_design_preview(id) {
    foo = window.open("http://www.beastwear.se/design_loader.swf?design_link=" + id,"","scrollbars=no,resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,width=340,height=425");
  }