// drop down form for add cart
function create_drop_down(stock, minbuy, sale_off, in_cart)
{
   var stocklimit = 99;
   
   if (parseInt(in_cart) > 0)
      document.write('<select class="formSelect" style="font-size:11px" name="quantity" id="quantity" onchange="this.form.submit()">');   
   else
      document.write('<select class="formSelect" style="font-size:11px" name="quantity" id="quantity">');
      
   if (parseInt(sale_off) == 1)
   { 
      stocklimit = parseInt(stock); 
   } 
   
   if (parseInt(minbuy) == 1)
   {
	  for (var i = 1; i <= stocklimit; i++)
	  {		   
	     if (i == parseInt(in_cart))
		    document.write('<option selected="selected">' + i + '</option>'); 
		 else
		    document.write('<option>' + i + '</option>'); 
	  }
   }
   else
   {
	  // minbuy logic
	  
	  // allow stocklimit
	  if (parseInt(stock) > 0 && parseInt(stock) <= parseInt(minbuy))
	  {
		 for (var i = 1; i <= parseInt(stock); i++)
	     {
	        if (i < parseInt(minbuy))
			{
			   if (i == parseInt(in_cart))
		          document.write('<option selected="selected">' + i + '</option>'); 
		       else
		          document.write('<option>' + i + '</option>');   
			}
		 }
	  }	  
	  	  
	  // minbuy step 4 step
	  for (var i = parseInt(minbuy); i <= stocklimit; i+=parseInt(minbuy))
	  {
	     if (i == parseInt(in_cart))
		    document.write('<option selected="selected">' + i + '</option>'); 
		 else
		    document.write('<option>' + i + '</option>');   
	  }	  	  	  
   }   
   document.write('</select>');
}


// copy address in create_account
function copy_address()
{
   document.getElementById('billing_company').value = document.getElementById('delivery_company').value; 
   document.getElementById('billing_name').value = document.getElementById('delivery_name').value; 
   document.getElementById('billing_street').value = document.getElementById('delivery_street').value; 
   document.getElementById('billing_postcode').value = document.getElementById('delivery_postcode').value; 
   document.getElementById('billing_city').value = document.getElementById('delivery_city').value; 
   document.getElementById('billing_country').value = document.getElementById('delivery_country').value;
}

/* swap a div */
function swap_div(div)
{
	if (document.getElementById(div).style.display == "none")
	{
	   document.getElementById(div).style.display = "block";
	}
	else
	{
	   document.getElementById(div).style.display = "none";
	}
}


/* checkout block 1 */
function SubmitCheckout1()
{
   var form = document.getElementById("form_checkout_1");
   
   // get selectec order_type
   var selected = "";
   for (var i = 0; i < document.getElementsByName('order_type').length; i++) 
   {
      if (document.getElementsByName('order_type')[i].checked==true)
	  {
         selected = document.getElementsByName('order_type')[i].value;
	  }
   } 
      
   if (selected == "login")
   {
	  // change url to login url
	  form.action = document.getElementById("form_url_" + selected).value;
	  
	  // reset action
	  document.form_checkout_1.action.value = "login";
   }
   else if (selected == "create_account")
   {
	  // change url to create_account url
	  form.action = document.getElementById("form_url_" + selected).value;
   }
   
   // do submit when a type was selected
   if (selected != "")
      form.submit();
}


/* protect emails */
function CryptMailto() {
	shift = parseInt(document.forms[0].shiftField.value);
	var n=0;
	var r="";
	var s="mailto:"+document.forms[0].emailField.value;
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n+shift); 
	}
	document.forms[0].cyptedEmailField.value = r;
	document.forms[0].HTMLCyptedEmailField.value = "<a href=\"javascript:linkTo_UnCryptMailto('"+r+"', "+shift+");\">eMail-Link</a>";
}

function UnCryptMailto(s, shift) {
	var n=0;
	var r="";
	for(var i=0;i<s.length;i++) { 
		n=s.charCodeAt(i); 
		if (n>=8364) {n = 128;}
		r += String.fromCharCode(n-(shift)); 
	}
	return r;
}
function linkTo_UnCryptMailto(s, shift)	{
	location.href=UnCryptMailto(s, shift);
}


/* country header select box */
$(function() {
	$("*").live('click',function(e) {
                  if($(this).attr("id") != "countrylist" && !$(this).attr("href")) {
			$("#countrylist").hide();
                  }
	});
        $("#countryc").live('click',function(e) {
                e.preventDefault();
                if(!$("#countrylist").hasClass("loaded")) {
		$("#countrylist").load("country_listing.php",function() {
			$("#countrylist").show().addClass("loaded");
                        $("#countrylist").append("<img src=\"images/country_close.gif\" alt=\"\" id=\"closecountrylist\" />");
		});
                }
                else {
                    $("#countrylist").show();
                }
        });
});


/* manufacturer header select box */
$(function() {
	$("*").live('click',function(e) {
                  if($(this).attr("id") != "manufacturerlist" && !$(this).attr("href")) {
			$("#manufacturerlist").hide();
                  }
	});
        $("#manufacturerc").live('click',function(e) {
                e.preventDefault();
                if(!$("#manufacturerlist").hasClass("loaded")) {
		$("#manufacturerlist").load("manufacturer_listing.php",function() {
			$("#manufacturerlist").show().addClass("loaded");
                        $("#manufacturerlist").append("<img src=\"images/country_close.gif\" alt=\"\" id=\"closemanufacturerlist\" />");
		});
                }
                else {
                    $("#manufacturerlist").show();
                }
        });
});
