function getHTTPObject()
{
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
  		xmlhttp=new XMLHttpRequest()
	// code for IE
	else if (window.ActiveXObject)
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	return xmlhttp;
}

var http = getHTTPObject();

var attr_selected_str='';
function AddProduct(mode, productid, qty, price, name, maxqty, contshp,offeraccept)
{
//	alert(" mode = "+mode+" pid= "+productid+" qty= "+qty+" price= "+price+" name= "+name+" mqty= "+maxqty+" constsp= "+contshp);return false;
	var val ;
	var counter;
	val = 0 ;
	counter = 0;
	var js_attr_val='';
    var js_attr_arr='';
    var js_attr_val_array='';
    var js_attr_val_ids='';
    var csstr='';
    var csvalstr='';
   // var size_count=document.getElementById('size_count').value;
    //var color_count=document.getElementById('color_count').value;
     var size_count='';
    var color_count='';
    attr_wise='No';
    
    if(attr_wise=='Yes')
    {
	   if(size_arr['val_str']!='' && typeof(size_arr['val_str'])!='undefined') 
	   {
	       csstr+=size_arr['size_str'];
           csvalstr+=size_arr['val_str'];
	   }
        else
        {
            //alert(parseInt(size_count))
            if(parseInt(size_count)!='' && parseInt(size_count)!='undefined' && parseInt(size_count)>0)
            {
                alert(JS_MSG_SELECT_SIZE);
                return false;
            }   
        }
        if(color_arr['var_str']!='' && typeof(color_arr['var_str'])!='undefined') 
	   {
	       if(size_arr['val_str']!='' && typeof(size_arr['val_str'])!='undefined')
           {
                csstr+=",";
                csvalstr+=",";
           }
	       csstr+=color_arr['color_str'];
           csvalstr+=color_arr['var_str'];
	   }
       else
       {
            //alert(color_count)
            if(parseInt(color_count)!='' && parseInt(color_count)!='undefined' && parseInt(color_count)>0)
            {
                alert(JS_MSG_SELECT_COLOR);
                return false;
            }    
       }
       attr_selected_str=csstr+"#"+csvalstr;
       js_attr_val=attr_selected_str;
	   js_attr_arr=js_attr_val.split("#");
	   js_attr_val_array=js_attr_arr[0];
	   js_attr_val_ids=js_attr_arr[1];
     }   		
    
	
	//alert(attr_selected_str)
    product_comment_js_var="";
	if(document.getElementById('product_comment'))
    {
        product_comment_js_var=document.getElementById('product_comment').value;
    }
    if(document.getElementById('img_main'))
	       var selgallery_img = encode64(document.getElementById('img_main').src);
	if(parseInt(qty) <= 0)
	{
		alert(JS_MSG_QTY_GREATER_THEN_ZERO)
		return false;
	}
	 if(qty == '')
		qty = '1';
	
	var url = ajax_url + "ajax_shoppingcart.php";
	url = url + "?productid="+ escape(productid)+"&qty="+ escape(qty)+"&price="+escape(price)+"&name="+escape(name)+"&mode="+escape(mode)+"&maxqty="+maxqty+"&contshp="+contshp+"&selimage="+selgallery_img+"&getatt="+js_attr_val_array+"&offeraccept="+offeraccept+"&js_attr_val_ids="+js_attr_val_ids+"&pro_com="+product_comment_js_var;
	//alert(url);return false;
	
	http.open("GET", url, true);
	
	http.onreadystatechange = getCartDataAdd;
	http.send(null);
}

function getCartDataAdd()
{
	
	if (http.readyState == 4)
	{
		
   		var xmlDocument = http.responseXML;
      	
		var set_key  = xmlDocument.getElementsByTagName('set_key').item(0).firstChild.data;
		//alert(set_key);
		if(set_key == 'yes')
			tb_show(REDUCE_QTY_DISP,"#TB_inline?height=80&width=450&inlineId=reduceqty_message","");
		else if(set_key == '3' || set_key == '5')
			tb_show(PRODUCT_NOT_DISP,"#TB_inline?height=80&width=450&inlineId=no_product","");	
		else if(set_key == '4')
			tb_show(GREATER_QTY_DISP,"#TB_inline?height=80&width=450&inlineId=greater_quantity","");	
                else if(set_key == '6')
			tb_show(MIN_QTY_DISP,"#TB_inline?height=80&width=450&inlineId=min_qty_message","");    
		else
        {
            product_added='Yes';
			tb_show(ADD_TO_CART,"#TB_inline?height=75&width=450&inlineId=cart_message","");
            
        }    

		return false;
	}
}

function removeone(id, val,js_key_accept)
{
	var qty = 'qty_'+ val;
	document.getElementById(qty).value = 0;
	document.frmcart.cartmode.value = "RemoveOne";
	document.frmcart.productid.value = id;
    document.frmcart.key_accept.value = js_key_accept;
	document.frmcart.submit();
}

function refreshshopping()
{
	window.location = site_url;
  	return false;
}
function continue_shopping()
{
    window.location = site_url;
  	return false;
}

function view_cart()
{
	window.location = site_url + 'ShoppingCart/';
	return false;
}
function continueshopping(url)
{
	window.location = url;
}
function removeall()
{
	document.frmcart.cartmode.value = "RemoveAll";
	document.frmcart.submit();
}

function updatecart()
{
	var flag = 0;
	
	for(i = 0 ; i < document.getElementById('total_item_cart').value ; i++)
	{  
		if(document.getElementById("qty_"+i))
			qyt_entered = document.getElementById("qty_"+i).value;
		if(document.getElementById("qtyinstock_"+i))
			qty_available = document.getElementById("qtyinstock_"+i).value;
		if(document.getElementById("qtyflag_"+i))
			qty_flag = document.getElementById("qtyflag_"+i).value;
		  
		if(parseInt(qyt_entered) > parseInt(qty_available) && qty_flag =='No')
		{
		 	document.getElementById("qty_"+i).value = document.getElementById("qtyinstock_"+i).value;
			if(flag == '0')
			{	
			   document.getElementById("qty_"+i).className = 'input-red';
         		tb_show(REDUCE_QTY_DISP,"#TB_inline?height=75&width=450&inlineId=reduceqty_message","");
         		return false;
			}
			flag++;
		}
	}
	document.frmcart.cartmode.value = "UpdateCart";
	document.frmcart.submit();
}

function qty_reduced()
{
	var flag = 0;
	for(i = 0 ; i < document.getElementById('total_item_cart').value ; i++)
	{
		qyt_entered = document.getElementById("qty_"+i).value;
		qty_available = document.getElementById("qtyinstock_"+i).value;
		
		if(parseInt(qyt_entered) > parseInt(qty_available))
		{
			document.getElementById("qty_"+i).value = document.getElementById("qtyinstock_"+i).value;
			flag++;
		}
	}
	document.frmcart.cartmode.value = "UpdateCart";
	document.frmcart.submit();
}

function gotocheckout()
{
	window.location = site_url + 'Billing-Shipping/';
	return false;
}

function removeone_gc(id, val)
{
	var gcamount = 'gcamount_'+ val;
	
	document.getElementById(gcamount).value = 0;
	document.frmcart.cartmode.value = "GCRemoveOne";
	document.frmcart.vendorid.value = id;
	document.frmcart.submit();
}

function removeone_charity(id, val)
{
	var charityamount = 'charityamount_'+ val;
	
	document.getElementById(charityamount).value = 0;
	document.frmcart.cartmode.value = "CharityRemoveOne";
	document.frmcart.charityid.value = id;
	document.frmcart.submit();
}

function updatecart_gc()
{
	document.frmcart.cartmode.value = "UpdateCart_GC";
	document.frmcart.submit();
}

function removeall_gc()
{
	document.frmcart.cartmode.value = "RemoveAll_GC";
	document.frmcart.submit();
}

function updatecart_charity()
{
	document.frmcart.cartmode.value = "UpdateCart_Charity";
	document.frmcart.submit();
}

function removeall_charity()
{
	document.frmcart.cartmode.value = "RemoveAll_Charity";
	document.frmcart.submit();
}

var http_attrib;
var passed_prodid;
function fillattributeval(optval,iProductId) 
{
	var url = ajax_url+"ajax_attributes.php?iAttrOptId="+optval+"&iProductId="+iProductId+"";
	//alert(url);
    passed_prodid=iProductId;
	var isWorking = false;
	
	if (!isWorking)
	{
		isWorking = true;
		if (window.XMLHttpRequest)
	  	{
			//alert(url);
	  		http_attrib=new XMLHttpRequest()
	  		http_attrib.open("GET",url, true);
		  	http_attrib.onreadystatechange=handleHttpResponse_attribute
		  	http_attrib.send(null)
	  	}
		// code for IE
		else if (window.ActiveXObject)
	  	{
	  		http_attrib=new ActiveXObject("Microsoft.XMLHTTP")
	    	if (http_attrib)
	    	{
	    		http_attrib.open("GET",url, true);
	    		http_attrib.onreadystatechange=handleHttpResponse_attribute
	    		http_attrib.send()
	    	}
	  	}
  	}
}
function handleHttpResponse_attribute() 
{ 
	if (http_attrib.readyState == 4) 
	{	
      	isWorking = false;
    	if (http_attrib.responseText.indexOf('invalid') == -1) 
		{
			//alert(http_attrib.responseText)
            //return false;
			var xmlDocument111 = http_attrib.responseXML;
			//var no = xmlDocument111.getElementsByTagName('tot').item(0).firstChild.data;
			//alert(no)
            //alert(xmlDocument111.getElementsByTagName('colorhtmlnew').item(0).firstChild.data)
            //color_html= xmlDocument111.getElementsByTagName('colorhtmlnew').item(0).firstChild.data;
            document.getElementById('colorhtmlid').innerHTML=http_attrib.responseText;
			//if(parseInt(no)>0)
			//{
				//Attr_Obj.length=0;
				//Attr_Obj.length=parseInt(no);
				//alert(Attr_Obj.length)
                /*
				for(j=0;j<Attr_Obj.length;j++)
				{
		   			var iPId = xmlDocument.getElementsByTagName('id').item(j).firstChild.data;
		   			//alert(iPId)
	   		   		var PName =xmlDocument.getElementsByTagName('text').item(j).firstChild.data;
	   		   		//alert(PName)
	   				Attr_Obj[j].value	= iPId;
	   				Attr_Obj[j].text 	= PName;
   					
  				}
                */
                //var color_html;
                //color_html= xmlDocument111.getElementsByTagName('color_html_new').item(0).firstChild.data;
                //alert(color_html)
                //document.getElementById('colorhtmlid').innerHTML=color_html;
                //isWorking = false;
    		}
            //alert(color_html)
            //color_value=document.getElementById('color_combo').value;
	        //size_value=document.getElementById('size_combo').value;
            /*
            if(size_arr['val_str']!='' && typeof(size_arr['val_str'])!='undefined' && color_arr['var_str']!='' && typeof(color_arr['var_str'])!='undefined')
	        {
		      //alert(prod_id)
		      checkstocksituation(color_arr['var_str'],size_arr['val_str'],prod_id);
	        }
            */
            //checkstocksituation(color_value,size_value,passed_prodid)			
		}
  	
}
function hidecheckout(js_out_of_stock,js_current_stock,comb_not_avail)
{
	//alert(js_out_of_stock)
	if(js_out_of_stock=='Yes' || comb_not_avail=='1'){
		document.getElementById('checkoutbtnid').style.display='none';
		document.getElementById('lblqtyid').style.display='none';
		document.getElementById('qty').style.display='none';
		
	}else{
		document.getElementById('checkoutbtnid').style.display='';
		document.getElementById('lblqtyid').style.display='';
		document.getElementById('qty').style.display='';
	}
	
}
var http_stock;
function checkstocksituation(color_value,size_value,iProductId) 
{
	//alert(attr_str)
	//alert(getattrid)
	//atrr_array=new Array();
	//atrr_array=attr_str.split(":");
	//alert(atrr_array);
	var url = ajax_url+"ajax_attributes_stock.php?iProductId="+iProductId;
    if(color_value!="" && typeof(color_value)!='undefined')
    {
        url+="&color_value="+color_value;
    }
    if(size_value!="" && typeof(size_value)!='undefined')
    {
        url+="&size_value="+size_value;
    }
	//alert(url);return false;
	var isWorking = false;
	
	if (!isWorking)
	{
		isWorking = true;
		if (window.XMLHttpRequest)
	  	{
			//alert(url);
	  		http_stock=new XMLHttpRequest()
	  		http_stock.open("GET",url, true);
		  	http_stock.onreadystatechange=handleHttpResponse_attributestock
		  	http_stock.send(null)
	  	}
		// code for IE
		else if (window.ActiveXObject)
	  	{
	  		http_stock=new ActiveXObject("Microsoft.XMLHTTP")
	    	if (http_stock)
	    	{
	    		http_stock.open("GET",url, true);
	    		http_stock.onreadystatechange=handleHttpResponse_attributestock
	    		http_stock.send()
	    	}
	  	}
  	}
}
function handleHttpResponse_attributestock() 
{ 
	if (http_stock.readyState == 4) 
	{	
      	isWorking = false;
    	if (http_stock.responseText.indexOf('invalid') == -1) 
		{
		//	alert(http_stock.responseText)
			var xmlDocument = http_stock.responseXML;			
			var stock_situation=xmlDocument.getElementsByTagName('stock_situation').item(0).firstChild.data;
			var js_out_of_stock=xmlDocument.getElementsByTagName('outofstock').item(0).firstChild.data;
			var js_current_stock=xmlDocument.getElementsByTagName('currentstock').item(0).firstChild.data;
            var comb_not_avail=xmlDocument.getElementsByTagName('comb_not_avail').item(0).firstChild.data;
            var stock_situation_desc=xmlDocument.getElementsByTagName('stock_situation_desc').item(0).firstChild.data;
            
            if(comb_not_avail=='1')
            {
              alert("Combination not available")
              //return false;
            }
			hidecheckout(js_out_of_stock,js_current_stock,comb_not_avail);
			if(document.getElementById('stockid') && comb_not_avail=='0')
				document.getElementById('stockid').innerHTML=stock_situation+'&nbsp;&nbsp;&nbsp;<a href="javascript://" onmouseover="showToolTip(event,\'\',\'No\');return false" onmouseout="hideToolTip()"><img src="images/que2.png" align="absmiddle"  /></a>';
            else
                document.getElementById('stockid').innerHTML='';
                
            if(document.getElementById('bubble_tooltip_content'))
            {
            //    alert('1')
                document.getElementById('bubble_tooltip_content').innerHTML=stock_situation_desc;
            }
			isWorking = false;
		}
  	}
}

var Attr_Obj='';
function setdynamicstock(iProductId,attr_option)
{
	//alert(iProductId);return false;
	color_obj=document.getElementById('color_combo');
	size_obj=document.getElementById('size_combo');
	var color_value='';
	var size_value='';	
    var function_called='';
	if(color_obj)
	{
		color_value=color_obj.options[color_obj.selectedIndex].value;
		if(size_obj && attr_option=='color')
		{
            function_called='yes';
			Attr_Obj=size_obj
			fillattributeval(color_value,iProductId);
		}
	}
	if(size_obj)
	{
		size_value=size_obj.options[size_obj.selectedIndex].value;
	}
    if(function_called!='yes')
	   checkstocksituation(color_value,size_value,iProductId)
}
var final_str='';
var attr_str='';
var color_arr=Array();

var size_arr=Array();
var last_color_div_obj='';
var last_size_div_obj='';
function getAttributeStr(divobj,attr_option,atrr_text,attr_val,textobj,fillop)
{
    //alert(attr_option)
	
    //alert(last_color_div_obj.id)
    
	if(attr_option=='color')
	{
		if(last_color_div_obj!='')
		{
			//last_color_div_obj.style.border='none';
            last_color_div_obj.className='';
            last_color_div_obj.className='size-box'
            		
		}
        if(divobj)
        {
            //alert(divobj.id)
            //divobj.style='';
		    divobj.className='size-box-selected';
	    }
		last_color_div_obj=divobj;
		textobj.innerHTML='Color : You selected <span style="color:red;"><b>'+atrr_text+'</b></span> color';
		color_text_value=atrr_text;
		color_value=attr_val;
		
		js_color_str='Color:'+color_text_value;
		color_arr['color_str']='';
		color_arr['color_str']=js_color_str;
		color_arr['var_str']='';
		color_arr['var_str']=color_value;
        //fillattributeval(color_value,prod_id)
		
	}
	if(attr_option=='size')
	{
		if(last_size_div_obj!='')
		{
			//last_size_div_obj.style.border='none';		
			last_size_div_obj.className='size-box';
		}
        if(divobj)
        {
            //alert(divobj.id)
            divobj.className='';
		    divobj.className='size-box-selected';
	    }
		last_size_div_obj=divobj
		textobj.innerHTML='Size : You selected <span style="color:red;"><b>'+atrr_text+'</b></span> size';
		size_text_value=atrr_text;
		size_value=attr_val;
		js_size_str='Size:'+size_text_value;
		size_arr['size_str']='';
		size_arr['size_str']=js_size_str;
		size_arr['val_str']='';
		size_arr['val_str']=size_value;
        //alert(prod_id)
        //alert(size_value)
        //alert(fillop)
        if(fillop!='No' && typeof(fillop) !='undefined')
        {
            color_arr['color_str']='';
		    color_arr['var_str']='';
            fillattributeval(size_value,prod_id)
        }
            
	}
    
    //alert(size_arr['val_str']+","+color_arr['var_str']);
	if((size_arr['val_str']!='' && typeof(size_arr['val_str'])!='undefined') || (color_arr['var_str']!='' && typeof(color_arr['var_str'])!='undefined'))
	{
		//alert(prod_id)
		checkstocksituation(color_arr['var_str'],size_arr['val_str'],prod_id);
	}
}
/*
function getAttributeStr()
{
	color_obj=document.getElementById('color_combo');
	size_obj=document.getElementById('size_combo');
	var color_text_value='';
	var size_text_value='';
	var js_color_str='';
	var js_size_str='';
	var final_str='';	
	var attr_str='';
	if(color_obj)
	{
		color_text_value=color_obj.options[color_obj.selectedIndex].text;
		color_value=color_obj.options[color_obj.selectedIndex].value;
		js_color_str='Color:'+color_text_value;
		final_str=js_color_str;
		attr_str=color_value;
	}
	if(size_obj)
	{
		size_text_value=size_obj.options[size_obj.selectedIndex].text;
		size_value=size_obj.options[size_obj.selectedIndex].value;
		js_size_str='Size:'+size_text_value;
		if(Trim(final_str)!='' && Trim(final_str)!='undefined')
		{
			final_str+=",";		
		}		
		final_str+=js_size_str;
		
		if(Trim(attr_str)!='' && Trim(attr_str)!='undefined')
		{
			attr_str+=",";		
		}		
		attr_str+=size_value;
	}
	return final_str+"#"+attr_str;
}
*/

