	function dispatchGeoState( res ){		
				
		switch( res.data.sCity ){		
			case null:      break;
			case undefined: break;
			case -1: $("select#sCity").append('<option value="-1">&mdash;</option>'); break;
			default:			
				cities = res.data.sCity;			
				$("select#sCity").append('<option value="-1">' + reqSelectCity + '</option>');			
				$.each( cities , function(cityId , cityParams){				
					$("select#sCity").append('<option value="' + cityId + '">' + cityParams.name + '</option>');				
					if( cityParams.is_active == 0 ) { $("select#sCity option[value=" + cityId + "]").attr('disabled' , 'disabled').attr('class' , 'inactive'); }				
				});			
			break;		
		}
		
		switch( res.data.sMStation ){		
			case null:        break;
			case undefined: break;
			case -1: $("select#sMStation").append('<option value="-1">&mdash;</option>');  break;
			default:					
				mStations = res.data.sMStation;			
				$("select#sMStation").append('<option value="-1">' + reqSelectMStation + '</option>');			
				$.each( mStations , function(stationId , stationParams){				
					$("select#sMStation").append('<option value="' + stationId + '">' + stationParams.name + '</option>');				
					if( stationParams.is_active == 0 ) { $("select#sMStation option[value=" + stationId + "]").attr('disabled' , 'disabled').attr('class' , 'inactive');	}
				});			
			break;		
		}
		
		switch( res.data.sPOS ){		
			case null:        break;
			case undefined:   break;
			case -1: $("select#sPOS").append('<option value="-1">&mdash;</option>');  break;
			default:			
				POS = res.data.sPOS;			
				/*$("select#sPOS").append('<option value="-1">' + reqSelectPOS + '</option>');*/
				$.each( POS , function(posId , posParams){					
					$("select#sPOS").append('<option value="' + posId + '">' + posParams.name + '</option>');				
					if( posParams.is_active == 0 ) { $("select#sPOS option[value=" + posId + "]").attr('disabled' , 'disabled').attr('class' , 'inactive');	}
				});			
			break;		
		}
		
		if( res.data.sPos == 1 ) { shipPOSEnable = 1; shipPOSCost = res.data.sPosCost; }
		if( res.data.sPost == 1 ) { shipPostEnable = 1; shipPostCost = res.data.sPostCost; }
		if( res.data.sCourier == 1 ) { shipCourierEnable = 1; shipCourierCost = res.data.sCourierCost; }
		
		return;
		
	}
	//------------------------------------------------------------------------
	function dispatchCartState( res ){		
		
		if( res.data.items == undefined ){
		
			location.href = '/cart.contents'; 
			return false;
		
		}else{
		
			items = res.data.items;		
		
			$.each(items , function(index , value){
				
				//alert( value.cost );
				if( value.is_preorder ) {				
					$("tr#row-" + index  + " .price").empty().append( '-' );
					$("tr#row-" + index  + " input.q").val( value.amount );
					$("tr#row-" + index  + " .cost").empty().append( '<span style="color:#c00;">предзаказ!</span>' );				
				} else {				
					$("tr#row-" + index  + " .price").empty().append( value.price + currencyLabel );
					$("tr#row-" + index  + " input.q").val( value.amount );
					$("tr#row-" + index  + " .cost").empty().append( value.cost + currencyLabel );				
				}
		
			});
		
		}
		
		overallItemsCost = res.data.overallItemsCost;
		
		if( overallCost != -1 ) { overallCost = overallItemsCost + overallServicesCost; }		
		
		updateOverallSaldos();

		return;
		
	}
	//------------------------------------------------------------------------
	function updateOverallSaldos(){		
		if( overallItemsCost != -1 ) { 			
			$("tr#overallItems .cost").empty().append( overallItemsCost + currencyLabel ); 		
		}else{		
			$("tr#overallItems .cost").empty().append( "&mdash;" ); 		
		}		
		if( overallServicesCost != -1 ) {			
			$("tr#overallServices .cost").empty().append( overallServicesCost + currencyLabel ); 
			overallCost = overallItemsCost + overallServicesCost;			
		}else{					
			overallCost = -1;			
			$("tr#overallServices .cost").empty().append( "&mdash;" ); 	
		}		
		if( overallCost != -1 ) { 			
			$("tr#overallSaldo #cost").empty().append( overallCost + currencyLabel ); 		
		}else{		
			$("tr#overallSaldo #cost").empty().append( "&mdash;" );	
		}		
	}
	//------------------------------------------------------------------------
	function resetShippingOptions(){		
		
		shipPOSEnable = 0;
		shipPOSCost = -1;
		shipPostEnable = 0;
		shipPostCost = -1;
		shipCourierEnable = 0;
		shipCourierCost = -1;
			
		shippingCost = -1;
		
		shippingOptionsSetUp();
		recalcOverallServicesCost();
		updateOverallSaldos();
		
	}
	//------------------------------------------------------------------------
	function resetPaymentOptions(){		
		
		payCashEnable = 0;
		payPreEnable = 0;
		payClearingEnable = 0;
		
		paymentOptionsSetUp();
		
	}
	//------------------------------------------------------------------------
	function recalcOverallServicesCost(){
		
		overallServicesCost = 0;
		
		if( shippingCost >= 0 ) { 
		
			overallServicesCost = overallServicesCost + shippingCost; 
			overallServicesCost = Number(overallServicesCost);
			
		}else{ overallServicesCost = -1; }
		
	}
	//------------------------------------------------------------------------
	function blockGeoSwitchers( nodeId ){
		
		$("select#sRegion").attr('disabled','disabled');
		$("select#sCity").attr('disabled','disabled');
		$("select#sMStation").attr('disabled','disabled').hide();
		$("select#sPOS").attr('disabled','disabled').hide();
		$("select#sMixed").attr('disabled','disabled').show();			
		
		switch( nodeId ){
		
			case "sRegion":  
				$("select#sCity").empty();
				$("select#sMStation").empty();
				$("select#sPOS").empty();				
			break;
			case "sCity":  
				$("select#sMStation").empty();
				$("select#sPOS").empty();				
			break;
		
		}
		
	}
	//------------------------------------------------------------------------
	function geoSwitchersSetUp(){
		
		$("select#sRegion").removeAttr('disabled').show();
		$("select#sCity").attr('disabled' , 'disabled').show();
		$("select#sMStation").attr('disabled' , 'disabled').hide();
		$("select#sPOS").attr('disabled' , 'disabled').hide();
		$("select#sMixed").attr('disabled' , 'disabled').show();
		
	}
	//------------------------------------------------------------------------
	function updatePaymentOptions( curPoint ){
		
		curUTStatus = $(".userType input:checked").attr("id");
		
		switch( curPoint ){
					 	
			case "deliv_courier": 
				payCashEnable = 1;
				if( curUTStatus == "uCorporate" ) { payPreEnable = 0; } else { payPreEnable = 1; }
				if( curUTStatus == "uPrivate" ) { payClearingEnable = 0; } else { payClearingEnable = 1; }
			break;
			case "deliv_post":    
				payCashEnable = 0;
				if( curUTStatus == "uCorporate" ) { payPreEnable = 0; } else { payPreEnable = 1; }
				if( curUTStatus == "uPrivate" ) { payClearingEnable = 0; } else { payClearingEnable = 1; }
			break;
			case "deliv_pos": 
				payCashEnable = 1;
				if( curUTStatus == "uCorporate" ) { payPreEnable = 0; } else { payPreEnable = 1; }
				if( curUTStatus == "uPrivate" ) { payClearingEnable = 0; } else { payClearingEnable = 1; }			 
			break;
						
		}
		
		paymentOptionsSetUp();
		
	}
	//------------------------------------------------------------------------
	function updateShippingOptions( curPoint ){
		
		switch( curPoint ){
					 	
			case "deliv_courier": 
				shippingCost = shipCourierCost; 
				if( $("select#sMStation option").size() > 1 ){					
					$("select#sMixed").attr('disabled','disabled').hide();
					$("select#sPOS").attr('disabled','disabled').hide();					
					$("select#sMStation").removeAttr('disabled').show();					
				}else{					
					$("select#sPOS").attr('disabled','disabled').hide();					
					$("select#sMStation").attr('disabled','disabled').hide();
					$("select#sMixed").attr('disabled','disabled').show();
				}
			break;
			case "deliv_post":    
				shippingCost = shipPostCost; 
			break;
			case "deliv_pos": 
				shippingCost = shipPOSCost;
				if( $("select#sPOS option").size() > 0 ){					
					$("select#sMixed").attr('disabled','disabled').hide();									
					$("select#sMStation").attr('disabled','disabled').hide();
					$("select#sPOS").removeAttr('disabled').show();						
				}else{														
					$("select#sMStation").attr('disabled','disabled').hide();
					$("select#sPOS").attr('disabled','disabled').hide();
					$("select#sMixed").attr('disabled','disabled').show();					
				}				 
			break;
						
		}
		
		shippingOptionsSetUp();
		recalcOverallServicesCost();
		updateOverallSaldos();
		updatePInfoStack( curPoint );
		
	}
	//------------------------------------------------------------------------
	function shippingOptionsSetUp(){
		
		if( shipPOSEnable == 1 ) { 		
			$(".points input#deliv_pos").removeAttr('disabled');
			$(".points label[for=deliv_pos]").removeAttr('class'); 			
		} else { 		
			$(".points input#deliv_pos").removeAttr('checked').attr('disabled','disabled');
			$(".points label[for=deliv_pos]").attr('class','inactive');			
		}
		
		if( shipPostEnable == 1 ) { 
			$(".points input#deliv_post").removeAttr('disabled');
			$(".points label[for=deliv_post]").removeAttr('class'); 			
		} else { 
			$(".points input#deliv_post").removeAttr('checked').attr('disabled','disabled'); 
			$(".points label[for=deliv_post]").attr('class','inactive');
		}
		
		if( shipCourierEnable == 1 ) { 
			$(".points input#deliv_courier").removeAttr('disabled'); 
			$(".points label[for=deliv_courier]").removeAttr('class'); 
		} else { 
			$(".points input#deliv_courier").removeAttr('checked').attr('disabled','disabled'); 
			$(".points label[for=deliv_courier]").attr('class','inactive');
		}	
		
		if( $(".points input:checked").attr("id") == undefined ){
		
			if( shipPOSEnable == 1 && shipPostEnable == 0 && shipCourierEnable == 0 ) { $("input#deliv_pos").attr('checked' , 'checked').trigger("click"); }
			if( shipPOSEnable == 0 && shipPostEnable == 1 && shipCourierEnable == 0 ) { $("input#deliv_post").attr('checked' , 'checked').trigger("click"); }
			if( shipPOSEnable == 0 && shipPostEnable == 0 && shipCourierEnable == 1 ) { $("input#deliv_courier").attr('checked' , 'checked').trigger("click"); }

		}
			
		if( shippingCost == -1 ){			
			$("td#shippingCost").empty().append("&mdash;");			
		}else{			
			$("td#shippingCost").empty().append( shippingCost + currencyLabel );			
		}
		
		recalcOverallServicesCost();
		updateOverallSaldos();
		
	}
	//------------------------------------------------------------------------
	function paymentOptionsSetUp(){
		
		if( payCashEnable == 1 ) { 		
			$(".points_payment input#payment_cash").removeAttr('disabled');
			$(".points_payment label[for=payment_cash]").removeAttr('class'); 			
		} else { 		
			$(".points_payment  input#payment_cash").removeAttr('checked').attr('disabled','disabled');
			$(".points_payment  label[for=payment_cash]").attr('class','inactive');			
		}
		
		if( payPreEnable == 1 ) { 
			$(".points_payment  input#payment_prepay").removeAttr('disabled');
			$(".points_payment  label[for=payment_prepay]").removeAttr('class'); 			
		} else { 
			$(".points_payment  input#payment_prepay").removeAttr('checked').attr('disabled','disabled'); 
			$(".points_payment  label[for=payment_prepay]").attr('class','inactive');
		}
		
		if( payClearingEnable == 1 ) { 
			$(".points_payment  input#payment_clearing").removeAttr('disabled'); 
			$(".points_payment  label[for=payment_clearing]").removeAttr('class'); 
		} else { 
			$(".points_payment  input#payment_clearing").removeAttr('checked').attr('disabled','disabled'); 
			$(".points_payment  label[for=payment_clearing]").attr('class','inactive');
		}	
		/*
		if( shipPOSEnable == 1 && shipPostEnable == 0 && shipCourierEnable == 0 ) { $("input#deliv_pos").attr('checked' , 'checked').trigger("change"); }
		if( shipPOSEnable == 0 && shipPostEnable == 1 && shipCourierEnable == 0 ) { $("input#deliv_post").attr('checked' , 'checked').trigger("change"); }
		if( shipPOSEnable == 0 && shipPostEnable == 0 && shipCourierEnable == 1 ) { $("input#deliv_courier").attr('checked' , 'checked').trigger("change"); }
		
		if( shippingCost == -1 ){			
			$("td#shippingCost").empty().append("&mdash;");			
		}else{			
			$("td#shippingCost").empty().append( shippingCost );			
		}
		
		recalcOverallServicesCost();
		updateOverallSaldos();
		*/
		
	}
	//------------------------------------------------------------------------
	function freeGeoSwitchers(){
		
		$("select#sRegion").removeAttr('disabled');
		
		if( $("select#sCity option").size() > 1 ) { $("select#sCity").removeAttr('disabled'); }		
		if( $("select#sMStation option").size() > 1 ) { $("select#sMStation").removeAttr('disabled'); }	
		if( $("select#sPOS option").size() > 0 ) { $("select#sPOS").removeAttr('disabled'); }
		
	}
	//------------------------------------------------------------------------
	function cartCommunicate(sAction , nodeId , setValue) {
		
		$.ajax({
			url:      'async',
			type:     'POST',
			data:     '_SENDID=' + sAction + '&node_id=' + nodeId + '&set_value=' + setValue + '&s=' + sid,
			dataType: 'json',
			timeout:  5000,
			beforeSend: function(){
				
				switch( sAction ){
					case "delete":  $("tr#" + nodeId).fadeTo( 300 , 0.3 ); break;
					case "recount":	$("tr#" + nodeId).fadeTo( 300 , 0.3 ); break;
					case "retrieve_geo": blockGeoSwitchers( nodeId );resetShippingOptions(); break;
				}
				
			},
			complete: function(){				
			},
			success: function( response ){
				
				switch( sAction ){
				
					case "delete": 
					
						if( response.status == 'failure' ){
						
							$('form').prepend( msgConnectionError );
						
						}else{
						
							dispatchCartState( response ); 
							$("tr#" + nodeId).fadeTo( 100 , 0 );
							$("tr#" + nodeId).remove();	

						}
						
					break;
					
					case "recount": 
					
						if( response.status == 'failure' ){
						
							$('form').prepend( msgConnectionError );
						
						}else{
						
							dispatchCartState( response ); 
							$("tr#" + nodeId).fadeTo( 300 , 1 );	

						}
						
					break;
					case "retrieve_geo":
						
						dispatchGeoState( response );						
						freeGeoSwitchers();
						shippingOptionsSetUp();
						
					break;
					
				}
 
				return false;
				
			},
			error: function(){
				
				$('form').prepend( msgConnectionError );
				
				switch( sAction ){
					case "delete":						
						$("tr#" + nodeId).fadeTo( 300 , 1 );						
					break;
				}
				
				return false;
			}
			
		});
		
	}
	
	// p Info functions ---------------------------------------
	function blockPInfoRows( rowStack ){		
		
		$.each( rowStack , function(index , rowId){
			
			$(".pInfo tr#" + rowId).fadeTo( 300 , 0.3 );
			$(".pInfo tr#" + rowId + " input").attr('disabled' , 'disabled');
			if( '\v'=='v' ) { $(".pInfo tr#" + rowId + " input").attr('class' , 'w250 inactive'); } // IE
			
		});
	
	}
	function freePInfoRows( rowStack ){
	
		$.each( rowStack , function(index , rowId){
			
			$(".pInfo tr#" + rowId + " input").removeAttr('disabled');
			$(".pInfo tr#" + rowId).fadeTo( 300 , 1 );
			if( '\v'=='v' ) { $(".pInfo tr#" + rowId + " input").attr('class' , 'w250'); } // IE
		
		});
	
	}
	function updatePInfoStack( curPoint ){
		
		curUTStatus = $(".userType input:checked").attr("id");
		
		switch( curPoint ){
		
			case "deliv_courier":  							
				switch( curUTStatus ){
				
					case "uPrivate":
						/*blockFields = '["zipCode" , "passportData"]';*/
						blockFields = '[]';
						freeFields  = '["street" , "house" , "entrance" , "passCode" , "level" , "room" , "companyName"]';	
					break;
					case "uCorporate":
						/*blockFields = '["zipCode" , "passportData" , "companyName"]';*/
						blockFields = '[]';
						freeFields  = '["legal_company_name" , "legal_inn_kpp" , "legal_jur_address" , "legal_okpo" , "legal_bank_name" , "legal_rs" , "legal_ks" , "legal_bik" ,"street" , "house" , "entrance" , "passCode" , "level" , "room"]';	
					break;

				}
			break;	
			case "deliv_post":  			
				switch( curUTStatus ){
				
					case "uPrivate":
						/*blockFields = '["entrance" , "passCode" , "level"]';*/
						blockFields = '[]';
						freeFields  = '["zipCode" , "street" , "house" , "room" , "companyName"]';		
					break;
					case "uCorporate":
						/*blockFields = '["entrance" , "passCode" , "level"]';*/
						blockFields = '[]';
						freeFields  = '["legal_company_name" , "legal_inn_kpp" , "legal_jur_address" , "legal_okpo" , "legal_bank_name" , "legal_rs" , "legal_ks" , "legal_bik" ,"zipCode" , "street" , "house" , "room" , "companyName"]';			
					break;

				}
	
			break;
			case "deliv_pos": 
				switch( curUTStatus ){
				
					case "uPrivate":
						/*blockFields = '["zipCode" , "street" , "house" , "entrance" , "passCode" , "level" , "room" , "companyName"]';*/
						blockFields = '[]';
						freeFields  = '[]';	
					break;
					case "uCorporate":
						/*blockFields = '["zipCode" , "street" , "house" , "entrance" , "passCode" , "level" , "room" , "companyName"]';*/
						blockFields = '[]';
						freeFields  = '["legal_company_name" , "legal_inn_kpp" , "legal_jur_address" , "legal_okpo" , "legal_bank_name" , "legal_rs" , "legal_ks" , "legal_bik"]';				
					break;

				} 			
		
			break;			
			default:
				blockFields = null;
				freeFields  = null;
			break;
		
		}
		
		pInfoSetUp();
		
	}
	function pInfoSetUp(){		
		freePInfoRows( eval(allFields) );
		/*
		if( blockFields == null ) { blockPInfoRows( eval(allFields) ); } else { blockPInfoRows( eval(blockFields) ); }
		if( freeFields == null ) { freePInfoRows( eval(initFields) ); } else { freePInfoRows( eval(freeFields) ); }
		*/
		
	}
	
	$(document).ready(function(){
		
		//Init form elements and data
		$(".cart-body").show();
		$(".back-to-catalogue").fadeTo(15000 , 1).fadeOut();
		$(".err").fadeTo(15000 , 1).fadeOut();
		
		geoSwitchersSetUp();
		resetShippingOptions();
		shippingOptionsSetUp();
		resetPaymentOptions();
		
		updatePInfoStack();
		
		if( curUTStatus == 'uCorporate' ){
				$("#legal-details").show();
			}else{
				$("#legal-details").hide();
			}
		
		//Amount controller
		$("input.q").bind("modAmount" , function(){
			
			curValue = $(this).val();
			
			if( !parseInt( curValue ) ) { curValue = 0; }
			
			row_id = $(this).parent("td").parent("tr").attr("id");
					
			if( !curValue ){
				$(this).val( tempPrevValue );
			}else{
				cartCommunicate("recount" , row_id , sid + "," + curValue );
				tempPrevValue = curValue;				
			}		
					
			return false;					
					
		});				
		
		$("input.q").bind("keyup" , function(){	
			if( $(this).val() === "0" ) { 
				row_id = $(this).parent("td").parent("tr").attr("id");					
				cartCommunicate( "delete" , row_id , sid );					
				return false;
			} 
			if( $(this).val() != "" ) { 
				$(this).trigger("modAmount"); 
				return false;
			} 
		});				
		$("input.q").bind("focus" , function(){	tempPrevValue = $(this).val(); return false; });				
		$("input.q").bind("blur"  , function(){ 
			if( $(this).val() == "" ) { 
				$(this).val(tempPrevValue); 
			}
			tempPrevValue = 0; 
			return false; 
			}
		);
				
		//Go next controller
		$("input#go_next").bind("click" , function(){
					
			errText = "";
					
			if( !$(".points input:checked").size() ) { errText += errFShipping + "\n"; }
			if( !$(".points_payment input:checked").size() ) { errText += errFPayment + "\n"; }
			if( !$(".userType input:checked").size() ) { errText += errFUserType + "\n"; }
			
			if( $("tr#name input").val() == "" || $("tr#name input").val() == null )   { errText += errFName + "\n";  }
			if( $("tr#email input").val() == "" || $("tr#email input").val() == null ) { errText += errFEmail + "\n"; }
			if( $("tr#phone input").val() == "" || $("tr#phone input").val() == null ) { errText += errFPhone + "\n"; }
			
			if( $(".userType input:checked").attr("id") == "uCorporate" ){
				
				if( $("tr#legal_company_name input").val() == "" || $("tr#legal_company_name input").val() == null ) { errText += errFLegalCompanyName + "\n"; }
				if( $("tr#legal_inn_kpp input").val() == "" || $("tr#legal_inn_kpp input").val() == null ) { errText += errFLegalInnKpp + "\n"; }
				if( $("tr#legal_okpo input").val() == "" || $("tr#legal_okpo input").val() == null ) { errText += errFLegalOkpo + "\n"; }
				if( $("tr#legal_jur_address input").val() == "" || $("tr#legal_jur_address input").val() == null ) { errText += errFLegalJurAddress + "\n"; }
				if( $("tr#legal_bank_name input").val() == "" || $("tr#legal_bank_name input").val() == null ) { errText += errFLegalBankName + "\n"; }
				if( $("tr#legal_rs input").val() == "" || $("tr#legal_rs input").val() == null ) { errText += errFLegalRs + "\n"; }
				if( $("tr#legal_ks input").val() == "" || $("tr#legal_ks input").val() == null ) { errText += errFLegalKs + "\n"; }
				if( $("tr#legal_bik input").val() == "" || $("tr#legal_bik input").val() == null ) { errText += errFLegalBik + "\n"; }
				
			}
			
			if( $("input#terms:checked").size() == 0 ) { errText += errFTerms + "\n"; }
					
			if( !errText ){
				return;
			}else{
				errText = errDetected + "\n" + errText;						
				alert( errText );
				return false;
			}
					
		});
				
		//Delete controller
		$("img.del").parent("a").bind("click" , function(){
		
			row_id = $(this).parent("td").parent("tr").attr("id");					
			cartCommunicate( "delete" , row_id , sid );					
			return false;
					
		});
				
		//Geo controller
		$("select#sRegion").change( function(){ curGeoNodeId = $(this).attr("id");$(".selector").trigger("modGeo");return false; } );
		$("select#sCity").change( function(){ curGeoNodeId = $(this).attr("id");$(".selector").trigger("modGeo");return false; } );
		$(".selector").bind("modGeo" , function(){
					
			regionId   = $("select#sRegion").children("option:selected").val();
			cityId     = $("select#sCity").children("option:selected").val();
			mStationId = $("select#sMStation").children("option:selected").val();
			posId      = $("select#sPOS").children("option:selected").val();
					
			curShippingOption = $(".points input:checked").val();
					
			SQ = regionId + "," + cityId + "," + mStationId + "," + posId + ",," + curShippingOption ;
					
			if( curGeoNodeId != -1 ){ cartCommunicate( "retrieve_geo" , curGeoNodeId , SQ ); }
					
				return false;
					
		});	
		
		$(".points input").bind("click" , function(){ 
			updateShippingOptions( $(".points input:checked").attr("id") ); 
			updatePaymentOptions( $(".points input:checked").attr("id") ); 
		});

		$(".userType input").bind("click" , function(){ 			
			curUTStatus = $(".userType input:checked").attr("id");
			if( curUTStatus == 'uCorporate' ){
				$("#legal-details").slideDown();
			}else{
				$("#legal-details").slideUp();
			}
			
			blockGeoSwitchers("sRegion");	
			$("select#sRegion").children("option[value='-1']").attr('selected','selected');			
			geoSwitchersSetUp();
			resetShippingOptions();
			shippingOptionsSetUp();
			resetPaymentOptions();
		});	
		
	});
