var ProductsGrid;
var grid;
var store1;
var ProductsListingGrid;
var productslisting;
function getSearchResults(frm)
{
}
function getSearchResults1(){
	document.getElementById("list").innerHTML="";
	var keyword = document.getElementById("part").value;
var x;

var myData= new Array();
	var received = function (response) {
	x =  response.responseText ;
	rows=x.split("##");
	for(var i =0;i<rows.length;i++)
	{
		row= rows[i].split(",")
		myData[i] = new Array();
		myData[i]=row;
		
	}
	
	
	
	/*
var myData = [
		['Apple',29],
		['Ext',83],
		['Google',71],
		['Microsoft',52],
		['Yahoo!',29]
	];
*/

  ProductsGrid =  new Ext.grid.EditorGridPanel({
      id: 'ProductsGrid',
      store: store1,
      enableColLock:false,
      selModel: new Ext.grid.RowSelectionModel({singleSelect:false}),
      tbar: [
          { 
            text: 'Quote',
            tooltip: 'Create Quote',
			handler: createQuote,  
            iconCls:'remove'
          }]
    });
  
	var myReader = new Ext.data.ArrayReader({}, [
		{name: 'products_id', type: 'int'},
		{name: 'products_part'},
		{name: 'products_qty', type: 'int'},
		{name: 'products_mfg'},
		{name: 'products_dc'},
		{name: 'products_status'}
	]);
	store1 = new Ext.data.Store({
			data: myData,
			reader: myReader
		});
	
	grid = new Ext.grid.GridPanel({
		store: store1,
		id:"productslisting",
		items:ProductsGrid,
		columns: [	 
			{header: "", width: 25, sortable: true, renderer: status, dataIndex: 'products_id'},

			{id:'Product',header: 'Product', width: 120, sortable: true, dataIndex: 'products_part'},
			{id:'Qty',header: 'Qty', width: 90, sortable: true, dataIndex: 'products_qty'},
			{id:'Mfg',header: 'Mfg', width: 90, sortable: true, dataIndex: 'products_mfg'},
			{id:'Dc',header: 'Dc', width: 90, sortable: true, dataIndex: 'products_dc'},
			{id:'Status',header: 'Status', width: 90, sortable: true, dataIndex: 'products_status'}
		],
		viewConfig: {
			forceFit: true
		},
		renderTo: 'list',
		title: 'Search Results',
		width: 850,
		autoHeight: true,
		frame: true
	});
	
	 function status(val){
		
			return '<input type="checkbox" name="products_id[]" value="'+val+'" onclick="createRFQ(this)" />';
		
        return val;
    }

	}
	Ext.Ajax.request({
		url: 'search.php?keyword='+keyword,
		
		success: received,
		failure: function () {alert("fail"); console.log('failure');}
	});
					
	
	
};
function createQuote()
{
	
	
}
function createRFQ(val)
{//alert(document.getElementById("list").elements.length);
	//alert(val.value);
	//alert(document.getElementById("frmss").elements.length);
	//prodids = new Array();
	//var len=document.elements.length;
	
	/*	alert(len);
	//var prodids = [document.getElementById("products_ids").value];
	var prodidstr = document.getElementById("products_ids").value;
	prodids= prodidstr.split(',');
	keyindex = prodids.indexOf(val);
	
	if(keyindex>=0)
	{
		//alert(prodids[keyindex]);
		//delete from array
		prodids.splice(keyindex, 1);
		document.getElementById("products_ids").value=prodids;
	}
	else
	{
		//alert("else");
		//add to array
		prodids.push(val);
		document.getElementById("products_ids").value=prodids;
		
	}
	//alert(prodids);
	*/
}
function sendToForm(obj,parentdiv)
{	
	id = "hidprodid"+obj.value;	//parentdiv= hidids; obj.value=236236
	container = document.getElementById(parentdiv);
	if(obj.checked==true)
	{
		var input = document.createElement("input");
		input.setAttribute("id",id);
		input.setAttribute("type","hidden");
		input.setAttribute("value",obj.value);
		input.setAttribute("name","hidprodid[]");
		container.appendChild(input); 
	}
	/*else
	{
		alert("hello");
		container.removeChild(document.getElementById(id));
	}*/
	
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	//Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function showhideadd(val)
{
	document.getElementById('row'+val).style.display="none";
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url = "index.php?admin/shoppingcart/addtoshoppingcart/"+val;
	//alert(url);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById('displaymsg').innerHTML="<center>"+xmlHttp.responseText+"</center>";	
		}
	}
}

function updatedata(productid)
{
	var quantity = document.getElementById('quantity'+productid).value;
	var hdnMinQuantity = document.getElementById('hdnMinQuantity'+productid).value;
	/*if((quantity <= 0) || (quantity < hdnMinQuantity))
	{
		 alert('Invalid Quantity!!!');
		 return false;
	}*/
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url = "index.php?admin/shoppingcart/updateshoppingcart/"+productid+"/"+quantity;
	//alert(url);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById('display').innerHTML=xmlHttp.responseText;	
			document.getElementById('quantity'+productid).value=quantity;
			document.getElementById('display').value=quantity;
		}
	}
	
	
}

function deletedata(productid)
{
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url = "index.php?admin/shoppingcart/deleteshoppingcart/"+productid;
	//alert(url);
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById('display').innerHTML=xmlHttp.responseText;	
		}
	}
	
	
}

function validateQuantity(id,val,minQ)
{
	//alert(id);
	if(val <= 0 || val < minQ)
	{
		
		document.getElementById('errMsg').innerHTML = "Invalid Quantity";
		setTimeout('setfocus("'+id+'")',2000);
		//alert(document.getElementById(id).value);
		//document.getElementById(id).value=document.getElementById(id).value;
	}
	
}
function setfocus(id)
{
	var control_obj=document.getElementById(id);
		control_obj.focus();
	
}


