getqty = function(){
	var getlist = null;
	
	function getview(thetype,theid){
		var urlvalue = "";
		if(thetype){
			urlvalue = 'getdata.php?Type='+thetype+'&ID='+theid;
		}else{
			urlvalue = 'getdata.php?Type=GETALL';
		}
		return search_tabledata = new Ext.data.Store({
			proxy: new Ext.data.HttpProxy({url: urlvalue}),
			reader: new Ext.data.JsonReader({
				root: 'results',
				totalProperty: 'total',
				id: 'ID'
			},['ID', 'KindID', 'IDName', 'KindName', 'Imgsrc', 'Detail']),
			remoteSort: false
		});
	}
	
	function creatlist(type){
		var listhtml = '';
		fcount = getlist.getCount(getlist);
		if(fcount > 0){
			for (var i=1; i<fcount+1;i++) {
				var listunit = getlist.getAt(i-1).data;
				if (type == 'plist'){
					listhtml += listbox1(listunit);
				}
			}
		}else{
			if (type == 'plist'){
				listhtml = '<table width="705" border="0" cellspacing="0" cellpadding="0"><tr><td width="705" height="300" align="center" valign="middle" class="about_qa_td">您目前沒有諮詢任何產品</td></tr></table>';
				Ext.get('endbuttom').hide();
				Ext.get('endbuttom').dom.width = 0;
			}
		}
		Ext.get('TotalItem').dom.innerHTML = fcount;
		Ext.get(type).dom.innerHTML = "";
		zeroCart = Ext.DomHelper.append(Ext.get(type).dom,
		{	
			tag:'span',
			html:listhtml,
			style:'visibility:hidden;'
		},true)
		zeroCart.show(true);
	}
	
	function listbox1(pdtlist){
		var detaillist = "";
		for(var i=0;i<pdtlist.Detail.length;i++){
			var info = pdtlist.Detail[i];
			var select1 = info[1].split('|');
			var select2 = info[2].split('|');
			detaillist += ['<div class="products_div_111">',
				'<table width="100%" border="0" cellspacing="0" cellpadding="0">',
				'<tr><td class="presscenter_div_3">'+info[0]+' &gt; '+select1[1]+'  &gt; '+select2[1]+' &gt; Qty '+info[3]+'...<a href="javascript:getqty.deldetail(\''+pdtlist.ID+','+i+'\',\''+info[0]+'\')" class="link006">Cancel</a> | <a href="products.php?Type=more&KindID='+pdtlist.KindID+'&ID='+pdtlist.ID+'&Select='+select1[0]+','+select2[0]+'" class="link006">Edit</a></td></tr>',
				'<tr><td><img src="images/products_inquiry_27.jpg" width="619" height="5" border="0" /></td></tr>',
				'</table>',
				'</div>'].join('');
		}
		return ['<div class="products_div_111">',
				'<table width="100%" border="0" cellspacing="0" cellpadding="0">',
				'<tr><td>',
				'<table width="100%" border="0" cellpadding="0" cellspacing="3" bgcolor="#6ea900"><tr><td bgcolor="#f2f2f2">',
				'<table border="0" cellspacing="5" cellpadding="0"><tr>',
				'<td width="65" valign="top"><div class="index_div_x_2"><a href="products.php?Type=more&KindID='+pdtlist.KindID+'&ID='+pdtlist.ID+'"><img src="'+pdtlist.Imgsrc+'" width="59" height="59" border="0" /></a></div></td>',
				'<td valign="top">',
				'<table width="619" border="0" align="right" cellpadding="0" cellspacing="0">',
				'<tr><td width="624" align="left" valign="top">',
				'<table width="619" border="0" cellspacing="0" cellpadding="0"><tr>',
				'<td width="553" align="left" valign="top"><div class="products_div_110">[ '+pdtlist.KindName+' ] <a href="products.php?Type=more&KindID='+pdtlist.KindID+'&ID='+pdtlist.ID+'" class="link004">'+pdtlist.IDName+'</a></div></td>',
				'<td width="40"><a href="javascript:getqty.delall(\''+pdtlist.ID+'\',\''+pdtlist.IDName+'\')"><img src="images/products_inquiry_24.jpg" width="40" height="17" border="0" /></a></td>',
				'<tr></table>',
				'<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td height="10"><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#CCCCCC"><tr><td height="1"></td></tr></table></td></tr></table>',
				'</td></tr><tr><td align="left" valign="top">',
				detaillist,
				'</td></tr></table>',
				'</td></tr></table>',
				'</td></tr></table>',
				'</td></tr>',
				'<tr><td>&nbsp;</td></tr></table>',
				'</div>'].join('');
	}
	
	return {
		init : function(){
			if(Ext.get('plist')){
				getlist = getview();
				getlist.on('load', function() {
					creatlist('plist');
				});	
				getlist.load();
			}
			
			if(Ext.get('total')){
				for(var i=0;i<Ext.get('total').dom.value;i++){
					var thename = new Ext.form.TextField({
						emptyText:'QTY',
						fieldClass:'',
						invalidClass:'',
						emptyClass:'',
						focusClass:''
					});
					thename.applyTo(Ext.get('qty'+i));
					thename.getEl().removeClass('x-form-text')
					thename.getEl().removeClass(' x-form-empty-field')
				}
			}
		},
		
		submitForm: function(formTable){
			var cb = function(a,b,c){
				if(b){
					var json = eval(c.responseText);
					if(json == 'true'){ 
						if(Ext.get('OverWord')) alert(Ext.get('OverWord').dom.value);
						formTable.submit();
					}else{
						alert('Your Confirm is error!!');
					}
				}
			}
			var cn = new Ext.data.Connection({url:"getdata.php?Type=Verify"}).request({
				params: {"ID":Ext.getDom('Verify').value},
				callback:cb
			});
		},
		
		delall: function(theid,thename){
			if(confirm('是否確定要刪除'+thename)){
				getlist = getview('DELALL',theid);
				getlist.on('load', function() {
					creatlist('plist');
				});	
				getlist.load();
			}
		},
		
		deldetail: function(theid,thename){
			if(confirm('是否確定要刪除'+thename)){
				getlist = getview('DELDETAIL',theid);
				getlist.on('load', function() {
					creatlist('plist');
				});	
				getlist.load();
			}
		}
	};
}();

Ext.onReady(getqty.init, getqty);

function checkdata(theform){
	theform.selectrow.value = "";
	if(theform.select3.value == ""){
		alert ('Please Select Material!!');
		return false;
	}
	if(theform.select4.value == ""){
		alert ('Please Select Surface Finish!!');
		return false;
	}
	var total = theform.total.value;
	for(var i=0;i<total;i++){
		var deside = Ext.get('qty'+i).dom.value;
		if(deside != "" && isNaN(deside) === false && deside != 0){
			theform.selectrow.value += "," + i;
		}
	}
	if (theform.selectrow.value == ""){
		alert('Please Enter QTY');
		return false;
	}
	return true;
}

function changemenu()
{
	var thepm = document.getElementById("droplist1");
	var thep = document.getElementById("droplist2");
	for (var m= thep.options.length; m > 0; m--) {
		thep.options[m] = null;
	}
	var thedata = thepm.value.split(',');
	employee_data = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({url: 'getdata.php?Type=SELECT&ID='+thedata[1]}),
		reader: new Ext.data.JsonReader({
			root: 'results',
			totalProperty: 'total',
			id: 'ID'
		},[ 'ID', 'ProductName']),
		remoteSort: false
	});
	employee_data.on('load', function() {
		pcount = employee_data.getCount(employee_data);
		for (var i=1; i < pcount+1; i++) {
			thep.options[i] = new Option(employee_data.getAt(i-1).data.ProductName,'products.php?Type=more&KindID='+thedata[0]+'&ID='+employee_data.getAt(i-1).data.ID);
		}
	});
	employee_data.load();
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function changeselect(selection,sv){
	for (var i=0;i<selection.length;i++){
		if(selection.options[i].value == sv){
			selection.options[i].selected = true;
			break;
		}
	}
	return sv;
}

function getQTY(s1,s2){
	if(s1){
		var material = changeselect(document.getElementById("select3"),s1);
		var surface = changeselect(document.getElementById("select4"),s2);
	}else{
		var material = Ext.get("select3").dom.value;
		var surface = Ext.get("select4").dom.value;
	}
	var thepdt = Ext.get("pdtInfo").dom.value.split(",+");
	var total = parseInt(Ext.get("total").dom.value);
	if(material != "" && surface != ""){
		var cb = function(a,success,rspn){
			if(success){
				var thevalue = eval(rspn.responseText);
				for(var j=0;j<total;j++){
					Ext.get('qty'+j).dom.value = "QTY";
				}
				for(var i=0;i<thevalue.length;i++){
					for(var j=0;j<total;j++){
						if(Ext.get('on'+j).dom.value == thevalue[i][0]){
							Ext.get('qty'+j).dom.value = thevalue[i][3];
							break;
						}
					}
				}
			}else{
				alert(rspn.responseText);
			}
		}
		var cn = new Ext.data.Connection({url:'getdata.php?Type=GETQTY&ID='+thepdt[2]}).request({
			params:{Material: material, 
				Surface: surface},
			scope: this,
			callback:cb
		});  
	}
}