/**
 * @author cashbit
 */

function MxKartSyncroCountryState(){
	
		Ext.get('mxkartcountryfield_invoice').dom.value = Ext.get('mxkartcountryfield_invoice_hidden').getValue() ;
		Ext.get('mxkartcountryfield_shipping').dom.value = Ext.get('mxkartcountryfield_shipping_hidden').getValue() ;
	
		MxKartGetRegions("mxkartcountryfield_invoice","mxkartstatefield_invoice",function(){
			var mxkartstatefield_invoice_hidden = Ext.get("mxkartstatefield_invoice_hidden").getValue() ;
			if (mxkartstatefield_invoice_hidden != ''){
				Ext.get('mxkartstatefield_invoice').dom.value = mxkartstatefield_invoice_hidden ;
			}
		});
	
		MxKartGetRegions("mxkartcountryfield_shipping","mxkartstatefield_shipping",function(){
			var mxkartstatefield_shipping_hidden = Ext.get("mxkartstatefield_shipping_hidden").getValue() ;
			if (mxkartstatefield_shipping_hidden != ''){
				Ext.get('mxkartstatefield_shipping').dom.value = mxkartstatefield_shipping_hidden ;
			}
		});		
		
}

function OnMxKartShipping(htmltext){

	Ext.get('mxkartmenuvoicecontents').update(htmltext);
	
	var newcustomer = Ext.get('newcustomerwarning') ;
	var retcustomer = Ext.get('returningcustomerwarning') ;
	
	var reciduser = Ext.get('mxkartreciduser').getValue() ;
	
	if (MxKartTrim(reciduser) == ''){
		retcustomer.remove() ;
		
	} else {
		newcustomer.remove();	
		Ext.get('mxkartaccounttype').dom.disabled = true ;	
		Ext.get('mxkartvatnumberfield').dom.disabled = true ;	
		Ext.get('mxkartfiscalcodefield').dom.disabled = true ;	
	}
	
	MxKartShippingManagePeopleCompany();
	MxKartSyncroCountryState();

}

function OnMxKartShippingGetRegions(htmltext,mxkartstateobjid){
	Ext.get(mxkartstateobjid+'_td').update(htmltext) ;
}

function OnMxKartSendCompanyData(response){
	var resp = Ext.util.JSON.decode(response);
	if (resp.OK){
		//Ext.get('mxkartreciduser').dom.value = resp.USERRECID;
		MxKartShipping_Transport();
	} else {
		Ext.Msg.alert(mxkartattention,resp.MESSAGE) ;
	}
}

function MxKartIsCompany(){
	return (Ext.get('mxkartaccounttype').getValue() =='C')	
}
function MxKartShippingManagePeopleCompany(){
		Ext.get('mxkartcompanynamefield').dom.disabled = !MxKartIsCompany() ;
}


function MxKartSendCompanyData(){

	var italy = 'MOAS200312191548500468000002' ;
	var country = Ext.get('mxkartcountryfield_invoice').getValue() ;
		
	var invoicestaterequired = Ext.get('mxkartstatefield_invoice').dom.options.length > 0 ;
	var shippingstaterequired = Ext.get('mxkartstatefield_shipping').dom.options.length > 0 ;
	
	var MxKartConditions = 
		{
			styles: {
				ok : 'border-color:#00FF00',
				ko : 'border-color:#FF0000',
				clear : 'border-color:#FFFFFF'
			},
			checklist   : {
				CompanyVAT 	: 1,
				CompanyCF	: 2,
				People		: 3
			},
			combinations: {
				mxkartcompanynamefield:				{	1: true,					2: true, 					3: false 	},
				mxkartfirstnamefield:				{	1: false,					2: false, 					3: true 	},
				mxkartlastnamefield:				{	1: false,					2: false, 					3: true 	},
				mxkartvatnumberfield:				{	1: (country == italy),		2: false, 					3: false 	},
				mxkartfiscalcodefield:				{	1: false,					2: (country == italy), 		3: (country == italy) 	},
				mxkartstreetaddressfield_invoice:	{	1: true,					2: true, 					3: true 	},
				mxkartzipcodefield_invoice:			{	1: true,					2: true, 					3: true 	},
				mxkartcityfield_invoice:			{	1: true,					2: true, 					3: true 	},
				mxkartprovincefield_invoice:		{	1: (country == italy),		2: (country == italy), 		3: (country == italy) 	},
				mxkartcountryfield_invoice:			{	1: true,					2: true, 					3: true 	},
				mxkartstatefield_invoice:			{	1: invoicestaterequired,	2: invoicestaterequired,	3: invoicestaterequired 	},
				mxkartphonenumberfield_invoice:		{	1: true,					2: true, 					3: true 	},
				mxkartfaxnumberfield_invoice:		{	1: false,					2: false, 					3: false 	},
				mxkartstreetaddressfield_shipping:	{	1: true,					2: true, 					3: true 	},
				mxkartzipcodefield_shipping:		{	1: true,					2: true, 					3: true 	},
				mxkartcityfield_shipping:			{	1: true,					2: true, 					3: true 	},		
				mxkartprovincefield_shipping:		{	1: (country == italy),		2: (country == italy), 		3: (country == italy) 	},
				mxkartcountryfield_shipping:		{	1: true,					2: true, 					3: true 	},
				mxkartstatefield_shipping:			{	1: shippingstaterequired,	2: shippingstaterequired, 	3: shippingstaterequired 	},
				mxkartphonenumberfield_shipping:	{	1: true,					2: true, 					3: true 	},
				mxkartfaxnumberfield_shipping:		{	1: false,					2: false, 					3: false 	}
			}
		} ;
		
	if (MxKartIsCompany()){
		ok = (	MxKartCheckForCombination('CompanyVAT', MxKartConditions) || ((country == italy) && MxKartCheckForCombination('CompanyCF', MxKartConditions)) );
	} else {
		ok = MxKartCheckForCombination('People', MxKartConditions) ;
	}
	
	if (!ok){
		Ext.Msg.alert(mxkartattention,mxkartaccountmandatory) ;
		return 
	}
	
	var request = {
		action : 'mxkartsendcompanydata',
		record : {
			mxkartreciduser: Ext.get('mxkartreciduser').getValue(),
			mxkartaccounttype : Ext.get('mxkartaccounttype').getValue(),
			mxkartfirstnamefield: Ext.get('mxkartfirstnamefield').getValue(),
			mxkartlastnamefield: Ext.get('mxkartlastnamefield').getValue(),
			mxkartcompanynamefield: Ext.get('mxkartcompanynamefield').getValue(),
			mxkartvatnumberfield: Ext.get('mxkartvatnumberfield').getValue(),
			mxkartfiscalcodefield: Ext.get('mxkartfiscalcodefield').getValue(),
			
			mxkartstreetaddressfield_invoice: Ext.get('mxkartstreetaddressfield_invoice').getValue(),
			mxkartzipcodefield_invoice: Ext.get('mxkartzipcodefield_invoice').getValue(),
			mxkartcityfield_invoice: Ext.get('mxkartcityfield_invoice').getValue(),
			mxkartprovincefield_invoice: Ext.get('mxkartprovincefield_invoice').getValue(),
			mxkartcountryfield_invoice: Ext.get('mxkartcountryfield_invoice').getValue(),
			mxkartstatefield_invoice: Ext.get('mxkartstatefield_invoice').getValue(),
			mxkartphonenumberfield_invoice: Ext.get('mxkartphonenumberfield_invoice').getValue(),
			mxkartfaxnumberfield_invoice: Ext.get('mxkartfaxnumberfield_invoice').getValue(),
			
			mxkartstreetaddressfield_shipping: Ext.get('mxkartstreetaddressfield_shipping').getValue(),
			mxkartzipcodefield_shipping: Ext.get('mxkartzipcodefield_shipping').getValue(),
			mxkartcityfield_shipping: Ext.get('mxkartcityfield_shipping').getValue(),
			mxkartprovincefield_shipping: Ext.get('mxkartprovincefield_shipping').getValue(),
			mxkartcountryfield_shipping: Ext.get('mxkartcountryfield_shipping').getValue(),
			mxkartstatefield_shipping: Ext.get('mxkartstatefield_shipping').getValue(),
			mxkartphonenumberfield_shipping: Ext.get('mxkartphonenumberfield_shipping').getValue(),
			mxkartfaxnumberfield_shipping: Ext.get('mxkartfaxnumberfield_shipping').getValue()			
		}
	};	
	
	var ShipCountry = Ext.get('mxkartcountryfield_shipping') ;
	var ShipState =  Ext.get('mxkartstatefield_shipping') ;
	
	if (request.record.mxkartcountryfield_shipping == italy) {
		if (ShipState.dom.selectedIndex >= 0)
			this.MxKartSessionData.zoneid = ShipState.dom.options[ShipState.dom.selectedIndex].text ;
	} else {
		this.MxKartSessionData.zoneid = request.record.mxkartcityfield_shipping
	}
	
	this.MxKartSessionData.countryid = ShipCountry.dom.options[ShipCountry.dom.selectedIndex].text ;
	
	/*
	var MxKartShippingMethod = Ext.get('MxKartShippingMethod') ;
	var MxKartShippingMethodList = new Ext.util.MixedCollection();
	var MxKS = MxKartShippingMethod.query('.MxKartShipping') ;
	MxKartShippingMethodList.addAll(MxKS);
	var MxKartShippingMethodSelected = '' ;
	MxKartShippingMethodList.each(function(item,i,l){
		if (item.checked){
			MxKartShippingMethodSelected = item.value ;
		}
	});	
	
	if (MxKartShippingMethodSelected == '') {
		Ext.Msg.alert('Shipping',"Prego indicare un metodo di spedizione/Please select shipping method.");
		return
	}
	
	
	var MxKartPackagingMethod = Ext.get('MxKartPackagingMethod') ;
	var MxKartPackagingMethodList = new Ext.util.MixedCollection();
	var MxKP = MxKartPackagingMethod.query('.MxKartPackaging') ;
	MxKartPackagingMethodList.addAll(MxKP);
	var MxKartPackagingMethodSelected = '' ;
	MxKartPackagingMethodList.each(function(item,i,l){
		if (item.checked){
			MxKartPackagingMethodSelected = item.value ;
		}
	});
	
	if (MxKartPackagingMethodSelected == '') {
		Ext.Msg.alert('Shipping',"Prego indicare un tipo di imballo/Please select packaging type.");
		return
	}	
	
	this.MxKartSessionData.ShippingMethod = MxKartShippingMethodSelected ;
	this.MxKartSessionData.PackagingMethod = MxKartPackagingMethodSelected ; 
	*/
	var lm = new Ext.LoadMask(Ext.getBody()) ;
	lm.show();
	new Ajax.Request(mxkartws, {		
	  method: 'POST',
	  parameters : js2php(request),
	  onSuccess: function(transport) {
		var lm = new Ext.LoadMask(Ext.getBody()) ;
		lm.hide();
		OnMxKartSendCompanyData(transport.responseText) ;
			//MxSendKart();
	  } 
	});		
}

function MxKartGetRegions(mxkartcountryobjid,mxkartstateobjid,SuccessFunction){
	var CountryRecId = Ext.get(mxkartcountryobjid).getValue() ;
	if (CountryRecId){
		var request = {
			action : 'mxkartshippinggetregions',
			CountryRecId: CountryRecId,
			mxkartstateobjid :mxkartstateobjid
		};	
		new Ajax.Request(mxkartws, {		
		  method: 'POST',
		  parameters : js2php(request),
		  onSuccess: function(transport) {
				OnMxKartShippingGetRegions(transport.responseText,mxkartstateobjid) ;
				if(SuccessFunction){
					SuccessFunction() ;
				}
		  } 
		});	
	}
}

function MxKartCopyInvoiceToShipping(){
	
	// first of all: we copy the country
	Ext.get('mxkartcountryfield_shipping').dom.value = Ext.get('mxkartcountryfield_invoice').getValue();
	
	Ext.get('mxkartstreetaddressfield_shipping').dom.value = Ext.get('mxkartstreetaddressfield_invoice').getValue();
	Ext.get('mxkartzipcodefield_shipping').dom.value = Ext.get('mxkartzipcodefield_invoice').getValue();
	Ext.get('mxkartcityfield_shipping').dom.value = Ext.get('mxkartcityfield_invoice').getValue();
	Ext.get('mxkartprovincefield_shipping').dom.value = Ext.get('mxkartprovincefield_invoice').getValue();
	Ext.get('mxkartphonenumberfield_shipping').dom.value = Ext.get('mxkartphonenumberfield_invoice').getValue();
	Ext.get('mxkartfaxnumberfield_shipping').dom.value = Ext.get('mxkartfaxnumberfield_invoice').getValue();
	
	// now we need to call ajax to have same regions in shipping and invoice, when ajax responds, we can start to copy the region
	MxKartGetRegions("mxkartcountryfield_shipping","mxkartstatefield_shipping",function(){
		Ext.get('mxkartstatefield_shipping').dom.value = Ext.get('mxkartstatefield_invoice').getValue() ;
	});
}
