function getMyOrders(){
	var winKey='gridOrder';
	var winObj = new Ext.Window({
	    layout:'fit',
	    width:500,
	    height:400,
	    id:winKey,
	    listeners:{
	    	destroy:function(){arrWindows[winObj.id]=null;}
	    },
	    closeAction:'destroy',
	    plain: true});
	
	arrWindows[winKey]=winObj; 
	winObj.show();
	winObj.getPositionEl().anchorTo(document, 'c-c', false, false, true);
	winObj.el.mask('Siparişleriniz listeleniyor, lütfen bekleyiniz', 'x-mask-loading');

    var url="order.php?progress=getMyOrders";
    Ext.Ajax.request({url: url,
					   success: getMyOrdersComplete
					});
}

function getMyOrdersComplete(conn,response){
	
	var arrResponseText=conn.responseText.split("|");
	eval(arrResponseText[1]);
    
	var store=new Ext.data.SimpleStore({orderId:'storeOrders',
										fields:[{name:'orderID'},
													{name:'categoryID'},
													{name:'categoryName'},
													{name:'orderTime'},
													{name:'integrationStatu'}]}
										);    
	store.loadData(arrResponse['gridData']);
    var grid = new Ext.grid.GridPanel({
        store: store,
        autoExpandColumn:'orderTime',
        trackMouseOver:true,
        listeners:{rowdblclick:gridOrder_rowdblclick},
        columns:[
        			{id:'orderID',dataIndex:'orderID',header:'Sipariş No',sortable:true},
        			{id:'categoryName',dataIndex:'categoryName',header:'Kategori',sortable:true},
        			{id:'orderTime',dataIndex:'orderTime',header:'Sipariş Tarihi',sortable:true},
        			{id:'integrationStatu',dataIndex:'integrationStatu',header:'Durum',sortable:true}
        		],
        stripeRows: true,
        height:350,
        width:500
        });
	var winObj=arrWindows["gridOrder"]; 
	winObj.setTitle("Sipariş Listesi");
	winObj.add(grid);

	winObj.render();
	winObj.el.unmask();
}

function gridOrder_rowdblclick(grid,rowIndex,e ){
	
	grid.el.mask('Siparişiniz açılıyor, lütfen bekleyiniz', 'x-mask-loading');
	
	var store=grid.getStore();
	var orderID=store.data.items[rowIndex].data["orderID"];
	var categoryID=store.data.items[rowIndex].data["categoryID"];
	
	var url="order.php?progress=getOrderForm&orderID="+orderID+"&categoryID="+categoryID;
    Ext.Ajax.request({url: url,
					   success:gridOrder_rowdblclick_complete.createDelegate(null, [grid], true)
					});
	
}

function gridOrder_rowdblclick_complete(conn,response,grid){
	var arrResponseText=conn.responseText.split("|");
	eval(arrResponseText[1]);
	eval(arrResponse['js']);
	eval(arrResponse['orderFormJS']);
	eval(arrResponse['orderFormLineJS']);
	eval(arrResponse['orderLineData']);

	grid.el.unmask();
	
	if (!arrResponse['success']){
		Ext.MessageBox.alert("Hata...",arrResponse["message"]); 
		return;
	}
	
	var orderFormKey='form_'+orderFormID;
	var orderLineFormKey='form_'+orderLineFormID;
	var winKey='win_'+orderFormID;
	
	var orderFormObj=formDesign[orderFormKey];
	var formHTML=orderFormObj.generateHTML(false,false);
	
	var colSpan=(orderFormObj.colCount*2);
	var html="<div name='divForm_"+orderFormObj.id+"' id='divForm_"+orderFormObj.id+"'>"+formHTML+"</div>";
	$("hiddenDummy").style.cssText=orderFormObj.formStyle.cssText;
	
	winObj = new Ext.Window({
	    layout:'fit',
	    id:winKey,
	    width:700,
	    autoHeight:true,
	    id:winKey,
	    listeners:{
	    	destroy:function(){arrWindows[winObj.id]=null;}
	    },
	    closeAction:'destroy',
	    plain: true});
	
	arrWindows[winKey]=winObj;

    var fieldSet= new Ext.form.FieldSet();
	fieldSet.html=html;
	fieldSet.autoHeight=true;

	fieldSet.addButton(new Ext.Button({text:'Kaydet',id:'buttonSave',hidden:true,handler:function(){saveFormData(orderFormKey);}}));
	fieldSet.addButton(new Ext.Button({text:'Vazgeç',id:'buttonCancel',hidden:true,handler: function(){cancelFormEdit(orderFormKey);}}));
	fieldSet.addButton(new Ext.Button({text:'Güncelle',handler: function(){setFormToEditMode(orderFormKey);}}));
	fieldSet.addButton(new Ext.Button({text:'Kapat',id:'buttonClose',hidden:false,handler: function(){winObj.destroy();}}));

	winObj.add(fieldSet);
	
    var orderLineFormObj=formDesign[orderLineFormKey];
    var orderLineStore = new Ext.data.SimpleStore({
        fields: eval(orderLineFormObj.getJsonStrForStore())});
	orderLineStore.loadData(arrResponse['orderLineData']);
	
    var orderLineGrid = new Ext.grid.GridPanel({
        store: orderLineStore,
        autoSize:true,
        autoExpandColumn:"f_"+orderLineFormObj.elements[1].elementID,
        trackMouseOver:true,
        columns: eval(orderLineFormObj.getJsonStrForGrid(false)),
        stripeRows: true,
        height:350
        });
    
	winObj.add(orderLineGrid);
	winObj.show();
}

function getNewOrder(categoryID){
	var url="order.php?progress=getNewOrder&categoryID="+categoryID;
	new Ajax.Request(url, {method: 'get',onComplete:getNewOrderComplete});			
}

function getNewOrderComplete(request){
	var arrRequest=request.responseText.split("|");
	eval(arrRequest[1]);
	var html=formDesign[formKey].generateHTML(false,true);
	html+=arrRequest[0];
	
	$("divFormArea").innerHTML="<form id='Gform' method='post'>"+html+"</form>";
	formDesign[formKey].initialize();
}

function getProductList(formID,categoryID,pageNumber){
	if (readCookie("prodCountPerPage")!=null)
		prodCountPerPage=readCookie("prodCountPerPage");
	else prodCountPerPage=50;
	
	if ($("prodCountPerPage")) $("prodCountPerPage").value=prodCountPerPage;
	
	var orderBy="";
	if ($("orderBy")) orderBy=$("orderBy").value; 
	
	var url="order.php?progress=getProductList&formID="+formID+"&categoryID="+categoryID+
				"&pageNumber="+pageNumber+"&prodCountPerPage="+prodCountPerPage+"&orderBy="+orderBy;
	//Sipariş ana formundan çağrıldığında Gform ile ana form bilgileri post ediliyor.
	if ($('Gform'))
		new Ajax.Request(url, {method: 'post',parameters: $('Gform').serialize(true),onComplete:getProductListComplete});
	else
		new Ajax.Request(url, {method: 'post',onComplete:getProductListComplete});			
}

function getProductListComplete(request){
	var arrRequest=request.responseText.split("|");
	
	var success=(arrRequest[0]=="true");
	var jsStr=arrRequest[2];
	
	if (!success){
		alert(arrRequest[1]);
	}
	else{
		$("divFormArea").innerHTML=arrRequest[1];
		eval(jsStr);
	}

	if (readCookie("prodCountPerPage")!=null)
		$("prodCountPerPage").value=readCookie("prodCountPerPage");
	else $("prodCountPerPage").value=50;
	
}
function addToOrderList(index,categoryID){
	if (!validate(index)) return false;
	
	$("imgBasket_"+index.toString()).src="/images/ajaxloader1.gif";
		
	var url="order.php?progress=addToOrderList&formID&index="+index+"&categoryID="+categoryID;
	new Ajax.Request(url, {method: 'post',parameters: $('form_'+index).serialize(true),onComplete:addToOrderListComplete});			
}

function addToOrderListComplete(request){
	var index=request.responseText;
	$("imgBasket_"+index.toString()).src="/UserFiles/tasarim/sepette.gif";
}

function addAllToOrderList(categoryID){
	
	var rowCount=formRowList.rows.size();
	var validated=false;
	var elementID="";
	var reqFieldIndexes="";

	//Zorunlu alanları tespit et.
	for(var ii=0;ii<formFieldList.fieldList.size();ii++){
		var field=formFieldList.fieldList[ii];
		if (field.required){
			reqFieldIndexes+="#"+(field.index-1).toString()+"#";
		}
	}

	//Zorunlu alanı dolu olan alanların diğer alanları validate et.
	//Eğer validate başarılı olursa sepete at.
	for(kk=0;kk<rowCount;kk++){
		for(var ii=0;ii<formFieldList.fieldList.size();ii++){
			var field=formFieldList.fieldList[ii];
			if (reqFieldIndexes.indexOf("#"+(field.index-1).toString()+"#")>-1){
				elementID="field#"+kk+"#"+field.elementID+"_"+(field.index-1).toString();
				if ($(elementID).value.trim()!=""){
					if (validate(kk)){
						addToOrderList(kk,categoryID);
					}
				}
			}
		}
	}
		
}

function validate(index){
	
	for(var ii=0;ii<formRowList.rows[index].columnList.size();ii++){
		validObj=formRowList.rows[index].columnList[ii].validObj;
		
		if (validObj!=null){
			if (!validObj.validate()) return false;
		}
	}
	return true;
}

function showMyOrderList(){
	GB_showCenter("Sipariş Listem", siteURLRoot+"/order.php?progress=showMyOrderList",600,900);
}

function queryForm(reportID,formID,categoryID){
	var url="order.php?progress=getProductList&formID="+formID+"&categoryID="+categoryID+"&reportID="+reportID+"&pageNumber=1&$prodCountPerPage=50";
	new Ajax.Request(url, {method: 'post',parameters: $('Gform').serialize(true),onComplete:getProductListComplete});			
}

function queryFormDesign(categoryID,formID){
	$("divQueryForm").style.display="";
	url="order.php?progress=getReportDesign&categoryID="+categoryID+"&formID="+formID;
			
	new Ajax.Request(url, {method: 'post',onComplete:queryFormDesignComplete});			
}

function queryFormDesignComplete(request){
	$("divQueryForm").innerHTML=request.responseText;
}





