--- chan_zap.c.orig	2005-11-14 15:54:38.526656854 +0100
+++ chan_zap.c	2005-11-14 16:00:03.818163815 +0100
@@ -607,6 +607,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];
@@ -2656,9 +2657,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;
@@ -3605,7 +3606,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;
 					}
 				}
 			}
@@ -3863,10 +3865,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:
@@ -4190,35 +4193,29 @@
                          * 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);
-			} 
-			/* Removed else statement from here as it was preventing hangups from ever happening*/
-			/* Added AST_STATE_RING in if statement below to deal with calling party hangups that take place when ringing */
-			if(p->hanguponpolarityswitch &&
-				(p->polarityonanswerdelay > 0) &&
-			       (p->polarity == POLARITY_REV) &&
-				((ast->_state == AST_STATE_UP) || (ast->_state == AST_STATE_RING)) ) {
-                                /* Added log_debug information below to provide a better indication of what is going on */
-				ast_log(LOG_DEBUG, "Polarity Reversal event occured - DEBUG 1: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
-			
-				if(ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) > p->polarityonanswerdelay) {
-					ast_log(LOG_DEBUG, "Polarity Reversal detected and now Hanging up on channel %d\n", p->channel);
+			if (	p->hanguponpolarityswitch &&
+				p->polarity == POLARITY_REV &&
+				( ast->_state == AST_STATE_UP ||
+				  ast->_state == AST_STATE_RING )) {
+				/*hangup
+					Polarity was REV
+					Channel was up or was still ringing*/
+				ast_log(LOG_VERBOSE, "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, "Polarity Reversal detected but NOT hanging up (too close to answer event) on channel %d, state %d\n", p->channel, ast->_state);
-				}
+			} else if (p->answeronpolarityswitch && 
+					p->polarity==POLARITY_IDLE &&
+					(ast->_state == AST_STATE_DIALING ||
+					 ast->_state == AST_STATE_RINGING)) {
+				/*answer:
+					Polarity was IDLE
+					We were dialing or ringing the other party*/
+				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, "Ignoring Polarity switch to IDLE 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");
 			}
                      	/* Added more log_debug information below to provide a better indication of what is going on */
 			ast_log(LOG_DEBUG, "Polarity Reversal event occured - DEBUG 2: channel %d, state %d, pol= %d, aonp= %d, honp= %d, pdelay= %d, tv= %d\n", p->channel, ast->_state, p->polarity, p->answeronpolarityswitch, p->hanguponpolarityswitch, p->polarityonanswerdelay, ast_tvdiff_ms(ast_tvnow(), p->polaritydelaytv) );
@@ -6366,8 +6363,8 @@
 		case SIG_FXSLS:
 		case SIG_FXSKS:
 		case SIG_FXSGS:
-			if (i->cid_start == CID_START_POLARITY) {
 				i->polarity = POLARITY_REV;
+			if (i->cid_start == CID_START_POLARITY) {
 				ast_verbose(VERBOSE_PREFIX_2 "Starting post polarity "
 					    "CID detection on channel %d\n",
 					    i->channel);

