--- asterisk-1.2.0-beta1/channels/chan_zap.c	2005-08-19 05:23:23.000000000 +0100
+++ asterisk-1.2.0-beta1-new/channels/chan_zap.c	2005-09-28 20:22:45.000000000 +0100
@@ -576,6 +576,7 @@
 #ifdef PRI_ANI
 	char cid_ani[AST_MAX_EXTENSION];
 #endif
+
 	char cid_num[AST_MAX_EXTENSION];
 	int cid_ton;					/* Type Of Number (TON) */
 	char cid_name[AST_MAX_EXTENSION];
@@ -2501,9 +2502,9 @@
 	case SIG_FXOKS:
 		/* Pick up the line */
 		ast_log(LOG_DEBUG, "Took %s off hook\n", ast->name);
-		if(p->hanguponpolarityswitch) {
-			gettimeofday(&p->polaritydelaytv, NULL);
-		}
+		//if(p->hanguponpolarityswitch) {
+		//	gettimeofday(&p->polaritydelaytv, NULL);
+		//}
 		res =  zt_set_hook(p->subs[SUB_REAL].zfd, ZT_OFFHOOK);
 		tone_zone_play_tone(p->subs[index].zfd, -1);
 		p->dialing = 0;
@@ -3434,7 +3435,8 @@
 							ast_setstate(ast, AST_STATE_UP);
 							p->subs[index].f.frametype = AST_FRAME_CONTROL;
 							p->subs[index].f.subclass = AST_CONTROL_ANSWER;
-						}
+							p->polarity=POLARITY_IDLE;
+						} else p->polarity=POLARITY_IDLE;
 					}
 				}
 			}
@@ -3692,10 +3694,11 @@
 
 				/* If we get a ring then we cannot be in 
 				 * reversed polarity. So we reset to idle */
-				ast_log(LOG_DEBUG, "Setting IDLE polarity due "
-					"to ring. Old polarity was %d\n", 
-					p->polarity);
-				p->polarity = POLARITY_IDLE;
+				//Not working in Spain
+				//ast_log(LOG_DEBUG, "Setting IDLE polarity due "
+				//	"to ring. Old polarity was %d\n", 
+				//	p->polarity);
+				//p->polarity = POLARITY_IDLE;
 
 				/* Fall through */
 			case SIG_EM:
@@ -4006,30 +4009,25 @@
 			 * mark the channel as UP or if this is an indication
 			 * of remote end disconnect. 
 			 */
-			if (p->polarity == POLARITY_IDLE) {
-				p->polarity = POLARITY_REV;
-				if (p->answeronpolarityswitch &&
-				    ((ast->_state == AST_STATE_DIALING) ||
-				     (ast->_state == AST_STATE_RINGING))) {
-					ast_log(LOG_DEBUG, "Answering on polarity switch!\n");
-					ast_setstate(p->owner, AST_STATE_UP);
-				} else 
-					ast_log(LOG_DEBUG, "Ignore switch to REVERSED Polarity on channel %d, state %d\n", p->channel, ast->_state);
-			} else if(p->hanguponpolarityswitch &&
-				(p->polarityonanswerdelay > 0) &&
-			        (p->polarity == POLARITY_REV) &&
-				(ast->_state == AST_STATE_UP)) {
-
-				if(ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
-					ast_log(LOG_DEBUG, "Hangup due to Reverse Polarity on channel %d\n", p->channel);
-					ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
-					p->polarity = POLARITY_IDLE;
-				} else {
-					ast_log(LOG_DEBUG, "Ignore Reverse Polarity (too close to answer event) on channel %d, state %d\n", p->channel, ast->_state);
-				}
+			if (	p->hanguponpolarityswitch &&
+				p->polarity == POLARITY_REV &&
+				( ast->_state == AST_STATE_UP ||
+				  ast->_state == AST_STATE_RING )) {
+				//hangup
+				ast_log(LOG_VERBOSE, "Hangup due to Reverse Polarity on channel %d\n", p->channel);
+				ast_softhangup(p->owner, AST_SOFTHANGUP_EXPLICIT);
+			} else if (p->answeronpolarityswitch && 
+					p->polarity==POLARITY_IDLE &&
+					(ast->_state == AST_STATE_DIALING ||
+					 ast->_state == AST_STATE_RINGING)) {
+				//answer
+				ast_log(LOG_VERBOSE, "Answering on polarity switch on channel %d\n", p->channel);
+	                        p->subs[index].f.frametype = AST_FRAME_CONTROL;
+	                        p->subs[index].f.subclass = AST_CONTROL_ANSWER;
+				ast_setstate(p->owner, AST_STATE_UP);
+				p->polarity=POLARITY_REV;
 			} else {
-				p->polarity = POLARITY_IDLE;
-				ast_log(LOG_DEBUG, "Ignore switch to IDLE Polarity on channel %d, state %d\n", p->channel, ast->_state);
+				ast_log(LOG_VERBOSE, "Ignoring polarity switch on channel %d. Polarity is unchanged (%s)\n",p->channel,p->polarity==POLARITY_IDLE?"POLARITY_IDLE":"POLARITY_REV");
 			}
 			break;
 		default:
@@ -6170,8 +6168,8 @@
 		case SIG_FXSLS:
 		case SIG_FXSKS:
 		case SIG_FXSGS:
+			i->polarity = POLARITY_REV;
 			if (i->cid_start == CID_START_POLARITY) {
-				i->polarity = POLARITY_REV;
 				ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
 					    "CID detection on channel %d\n",
 					    i->channel);
