/**
 * Bridge XMLHTTP to XMLHttpRequest in pre-7.0 Internet Explorers
 */
if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function()
{
  try{ return new ActiveXObject("Msxml2.XMLHTTP.6.0") }catch(e){}
  try{ return new ActiveXObject("Msxml2.XMLHTTP.3.0") }catch(e){}
  try{ return new ActiveXObject("Msxml2.XMLHTTP") }catch(e){}
  try{ return new ActiveXObject("Microsoft.XMLHTTP") }catch(e){}
  throw new Error("This browser does not support XMLHttpRequest or XMLHTTP.")
};
 
// ...
 
var xmlhttp = new XMLHttpRequest(); //No conditionals necessary.




function add_wishlist() {

	
	var str = getFormValues(document.product_form);
	
	var message = document.getElementById('message');
	
	var b_wishlist = document.getElementById('b_wishlist');

	var str = str.replace('a=add_cart','a=add_wishlist');
	
	xmlhttp.open("GET","/breadline.php?a=add_wishlist&" + str);	
	
    xmlhttp.onreadystatechange = function() {

      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		
      			//alert(xmlhttp.responseText);
      			
      			message.style.display = "block";
      			b_wishlist.style.display = "none";
      	
				message.innerHTML =  xmlhttp.responseText;
				
				
			
      }
      
    }
    xmlhttp.send(null);
	
}






    function getFormValues(fobj,valFunc)

    {

       var str = "";

       var valueArr = null;

       var val = "";

       var cmd = "";

       for(var i = 0;i < fobj.elements.length;i++)

       {

           switch(fobj.elements[i].type)

           {
           	   case "hidden":
               case "radio":
               case "text":
               case "password":
                    if(valFunc)

                    {

                        //use single quotes for argument so that the value of

                        //fobj.elements[i].value is treated as a string not a literal

                        cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";

                        val = eval(cmd)

                    }

                    str += fobj.elements[i].name +

                     "=" + escape(fobj.elements[i].value) + "&";

               break;
               
               case "select-one":

                    str += fobj.elements[i].name +

                    "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";

                    break;
                    

           }

       }

       str = str.substr(0,(str.length - 1));

       
       
       return str;

    }

    var divName;

   
    function inStock(f,productID,count,divName)
    {
    	if(divName)
	    	massage = document.getElementById(divName);

		xmlhttp.open("GET","/breadline.php?a=inStock&product_id=" + productID + "&count=" + count + "&");	
		
	    xmlhttp.onreadystatechange = function() 
	    {
	      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
	      {
	      	//alert(xmlhttp.responseText);
				if(xmlhttp.responseText==1)
				{
					
					if(divName)
						massage.style.display = "none";

						
					f.submit();
					return true;
				}
				else
				{
					if(divName)
					{
						massage.style.display = "block";
						massage.innerHTML = "You have exceeded the amount of available products";
					}
					return false;
	      		}	      	
			}
	    }
	    xmlhttp.send(null);
    }

    function addToCart(f)
    {
    	inStock(f,f.product_id.value,f.product_quantity.value,'cartMsg');
    	
    }
    
    
	var optionContent = new Object;
	var	optionImage = new Object;
	var	optionMaterial = new Object;
		
    
	var startOptionID;
	var startOptionSetID;
	
    function viewProduct(productid,categoryid,type,optionsetid,optionid)
    {
    	var breadcrumb = document.getElementById('breadcrumbProduct');
    	var productContentDiv = document.getElementById('productContent');
    	var productContentData = document.getElementById('productContentData');
    	
    	loading('productLoad');
		
//    	loading('productContent');
    	
 	 	productContentData.style.display='none';

 	 	
    	var url = "/breadline.php?product_id=" + productid + "&category_id=" + categoryid + "&a=get_name";
    	
    	var nameReq = new XMLHttpRequest();
    	
    	nameReq.open("GET",url);	
		
	    nameReq.onreadystatechange = function() 
	    {
	      if (nameReq.readyState == 4 && nameReq.status == 200) 
	      {
	      	 document.title = nameReq.responseText;
	      }
	    }
	    nameReq.send(null);
	    
 	 	
 	 	
    	var url = "/breadline.php?product_id=" + productid + "&category_id=" + categoryid + "&a=set_options";
    	
    	var ajaxReq = new XMLHttpRequest();
    	
    	ajaxReq.open("GET",url);	
		
	    ajaxReq.onreadystatechange = function() 
	    {
	      if (ajaxReq.readyState == 4 && ajaxReq.status == 200) 
	      {
	      	 optionHash = ajaxReq.responseText;
	      	
	      	 
	      	 var optionSet = optionHash.split('[optionset]');
	      	 
	      	 productName = optionSet[0];
	      	 
	      	 breadcrumb.innerHTML = productName;
	      	 
	      	 /*
	      	 	[optionset]
	      	 		optionsetID
	      	 		[option]1[data]content[data]image[data]material
	      	 		[option]1[data]content[data]image[data]material
	      	 		[option]1[data]content[data]image[data]material
	      	 	[optionset]
	      	 */
	      	 
	      	 // ITERATE THROUGH AVAILABLE SETS
	      	 for(j=1;j<optionSet.length;j++)
	      	 {
	      	 	var options = optionSet[j].split('[option]');

	      	 	optionsetID = options[0]; // option set ID is FIRST row.
	      	 
   			 	optionContent[optionsetID] = new Object;
   				optionImage[optionsetID] = new Object;
   				optionMaterial[optionsetID] = new Object;
	      	 		
				// iterate through Options in SET
		      	 for(k=1;k<options.length;k++)
		      	 {
		      	 	if(options[k])
		      	 	{
			      	 	var optionData = options[k].split('[data]');
			     		var optionID = optionData[0];
			     		
		      	 		if(k==1)
		      	 		{
		      	 			startOptionID = optionID;
		      	 			startOptionSetID = optionsetID;
		      	 		}
		      	 		
		      	 		optionContent[optionsetID][optionID] = optionData[1];
		      	 		optionImage[optionsetID][optionID] = optionData[2];
		      	 		optionMaterial[optionsetID][optionID] = optionData[3];
		      	 	}								      	 	
		      	 }  	 	
	      	 }
	      	 
		    var ajaxReq2 = new XMLHttpRequest();
		    
	    	var url = "/breadline.php?product_id=" + productid + "&category_id=" + categoryid + "&a=view_product_content&optionset_id=" + optionsetid + "&option_id=" + optionid;
	    	
	    	ajaxReq2.open("GET",url);	
			
		    ajaxReq2.onreadystatechange = function() 
		    {
		      if (ajaxReq2.readyState == 4 && ajaxReq2.status == 200) 
		      {
		      	 productContentData.innerHTML = ajaxReq2.responseText;
		      	 
		      	 
		      	 
		      	if((startOptionSetID > 0)&&(startOptionID>0)&&(categoryid==43)) //changed from 43
		      	{
		      	 	if((optionid)&&(optionsetid))
		      	 	{
		   				setOption(optionsetid,optionid);
		      	 	}
		      	 	else
		      	 	{
	 			      	setOption(startOptionSetID,startOptionID);
		      	 	}
		      	}
		      	 
	      	 	
	      //	 	productContentData.style.display='block';
//	      	 	unload('productLoad');
	      	 	
				setTimeout("finishLoad('productLoad','productContentData','" + type + "'," + categoryid + ")",1500);

	      	 	
		      }				
		    }
		    ajaxReq2.send(null);
	    	
	      }				
	    }
	    ajaxReq.send(null);
	    
	    
	    
    }
    
    function finishLoad(loadDivName,contentDiv,type,categoryid)
    {
    	loadDiv = document.getElementById(loadDivName);
    	contentDiv = document.getElementById(contentDiv);
    	loadDiv.innerHTML = '';
    	contentDiv.style.display = 'block';
    	
    	
    	switch(type)
    	{
    		case "init":
		    	loadList(categoryid);
    		default:
    	}
    }
    
    function loadList(categoryid)
    {
    	productListData = document.getElementById('productList');
    	
    	loading("productList");
    	
    	var url = "/breadline.php?a=view_product_list&category_id=" + categoryid;
   	 	
    	
    	var ajaxReq3 = new XMLHttpRequest();
   	 	
    	ajaxReq3.open("GET",url);	
    	
	     ajaxReq3.onreadystatechange = function() 
	    {
	      if (ajaxReq3.readyState == 4 && ajaxReq3.status == 200) 
	      {
	      	 productListData.innerHTML = ajaxReq3.responseText;
		    
		     if(categoryid != 44) 	
		      	 CSBfleXcroll('productOverflowLong');
		     else
   		      	 CSBfleXcroll('productOverflowShort');

	      }				
	    }
	    ajaxReq3.send(null);	
    }
    
    
   	function setOption(optionsetID,optionID)
	{
		
		
		// div = document.getElementById('optionset_' + optionsetID + '_content');
		img = document.getElementById('productImage');
	    mat = document.getElementById('optionset_' + optionsetID + '_material');
    	cont = document.getElementById('optionset_' + optionsetID + '_name');				
		//alert(optionsetID + '_' + optionID);
		
	//	div.innerHTML = optionContent[optionsetID][optionID];
    	img.src = optionImage[optionsetID][optionID];
		$(cont).html('<div style="margin-bottom:1px;"><label>color: </label>' + optionContent[optionsetID][optionID] + '</div>');		
    	$(mat).html('<div style="margin-bottom:1px;"><label>material: </label>' +optionMaterial[optionsetID][optionID] + '</div>');
    		
			
			//var newdiv = document.createElement("div");
//var xhr = new XMLHttpRequest();
//newdiv.innerHTML = xhr.responseText;
//var container = document.getElementById("container");
//container.appendChild(newdiv);
		
    					
    	/* f = document.product_form;
    					
    	for(i=0;i<f.elements.length;i++)
			if(f.elements[i].name == 'optionset_' + optionsetID)
				f.elements[i].value = optionID; */
    }
    
    
    
    
    
    
    
	