__ShippingMethod = function(){
	this.details = $_('#ShippingMethodDetails')
	
	this.cSelect = $_('#OSCShippingSelect')
	this.cError = $_('#OSCShippingNotEnoughFields')
	
	this.URLGetContent = SmartCheckout.APIURL+'?ax=1&function=get_shipping_method_html';
	this.URLUpdate = SmartCheckout.APIURL+'?ax=1&function=update_shipping_method&methodId=';
	
	this.attachHandlers();
	
	this.disabled = !this.cSelect
}

__ShippingMethod.prototype.attachHandlers = function(){
	if(this.disabled) return;
	var self = this;
	if(this.details.$_('@shippingid').attach){
		this.details.$_('@shippingid').attach('click', function(){
			$_('#OSCShippingIndicator'+this.value).style.visibility = 'visible';
			self.update();
		})
	}else{
		self.update();
	}
}

__ShippingMethod.prototype.getContent = function(){
	var self = this;
	if(this.disabled) return;
	$_(this.URLGetContent).GET(function(data){
		self.cSelect.innerHTML = data.stdout
		self.enable();
		if(data.stderr.errOccured && data.stderr.errCode == 'NO_SHIPPING'){
			Totals.getContents();
			Order.disable();
			
		}else{
			self.attachHandlers();
			// Recalc totals
			Totals.getContents();
			Order.enable()
		}
	})
}


__ShippingMethod.prototype.putContent = function(content){
	if(this.disabled) return;
	var self = this;
	$_(this.URLGetContent).POST(
		content,
		function(data){
			self.cSelect.innerHTML = data.stdout
			self.enable()
			if(data.stderr.errOccured && data.stderr.errCode == 'NO_SHIPPING'){
				Totals.getContents();
				Order.disable()
			}else{
				self.attachHandlers();
				// Recalc totals
				Totals.getContents();
				Order.enable()
			}
		}
	)
}

__ShippingMethod.prototype.update = function(){
	if(this.disabled) return;
	var self = this;
	
	if(this.details.$_('@shippingid').VAL){
		var val = this.details.$_('@shippingid').VAL()
		if(val){	
			$_('@shippingid').apply(function(el){el.disabled = true;})
			$_(this.URLUpdate + val).GET(function(data){
				$_('.OSCShippingIndicator').CSS({visibility:'hidden'})
				Totals.getContents();
				self.details.$_('@shippingid').apply(function(el){el.disabled = false;})		
			})
		}else{
			// No shipping method selected, but there are some
		}
	}else{
		// No shipping methods found for location, show dialog
	}
}

__ShippingMethod.prototype.disable = function(){
	// Disables shipping method 
	if(this.disabled) return;
	this.cSelect.style.display = 'none';
	this.cError.style.display = '';
	//Order.disable()
	
	this.status = false;
}
__ShippingMethod.prototype.enable = function(){
	if(this.disabled) return;
	// Disables shipping method 
	this.cSelect.style.display = '';
	this.cError.style.display = 'none';
	Order.enable()
	this.status = true;
}

__ShippingMethod.prototype.hide = function(){
	// Hides shipping method 
	this.details.style.display = 'none'
}


__ShippingMethod.prototype.$TEST = function(cbf){
	if(!this.cSelect && !this.cError) return cbf();
	if(!this.cError.offsetHeight) return cbf();
}
