var http_request = false;

function makeRequest( method, url, callBackFunction ) {
	http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	if( callBackFunction ){
		http_request.onreadystatechange = callBackFunction;
	}
	http_request.open(method, url , true);
	http_request.send(null);
}

function updateCarrier( subID, newVal ){
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmlDoc = http_request.responseXML;

			var tbody = document.getElementById( "subscribersTable" ).getElementsByTagName("TBODY")[0];
			var x = xmlDoc.getElementsByTagName( 'subscriber' );

			if (x[0]==undefined){
				var row = document.createElement("TR")
				row.style.height = '20px';
				var noSubscriberTD = document.createElement("TD");
				noSubscriberTD.className = 'listTD';
				noSubscriberTD.style.textAlign='center';
				noSubscriberTD.colSpan = '8';
				noSubscriberTD.appendChild( document.createTextNode( ' You do not have any subscribers for this list, create one via the link above. ' ));
				row.appendChild( noSubscriberTD );
				tbody.appendChild(row);
				document.getElementById( 'pageNavPosition' ).style.display = 'none';
				return false;
			}

			for (i=0;i<x.length;i++) {
				document.getElementById( 'subscribersTable' ).style.visibility = 'visible';
				var dataSubscriberID = xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberid' )[0].firstChild.nodeValue;
				xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscribername' )[0].firstChild.nodeValue != null ? dataSubscriberName = xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscribername' )[0].firstChild.nodeValue : dataSubscriberName = '';
				xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberphone' )[0].firstChild != null ? dataSubscriberTele = xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberphone' )[0].firstChild.nodeValue : dataSubscriberTele = '';
				xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberphoneactive' )[0].firstChild != null ? dataSubscriberTeleActive = xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberphoneactive' )[0].firstChild.nodeValue : dataSubscriberTeleActive = '';
				xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberemail' )[0].firstChild != null ? dataSubscriberEmail = xmlDoc.getElementsByTagName( 'subscriber' )[i].getElementsByTagName( 'subscriberemail' )[0].firstChild.nodeValue : dataSubscriberEmail = '';
			}
			
		}			
	}
}

function startSample( ) {
	ph = document.getElementById( 'sampleTextField' );

	phoneDigits = "";
	s = ph.value;
	var ValidChars = "0123456789";
	for (i=0; i < s.length; i++) {
		Char = s.charAt(i);
		if ( ValidChars.indexOf(Char) != -1 ) {
			phoneDigits += s.charAt(i);
		}
	}

	formattedPhone = formatPhone( phoneDigits );

	ph.value =  formattedPhone;
	phoneRegex = /^\(\d{3}\) \d{3}-\d{4}$/;
	if( !formattedPhone.match( phoneRegex ) ) {
		ph.style.backgroundColor="#FFAAAA";
		alert( 'That does not appear to be a valid phone number' );
		return false;
	} else {
		ph.style.backgroundColor="white";
		if( confirm("Please confirm that " + formattedPhone + " is your phone number.  Press \"OK\" to continue.\n\nYour IP address will be logged" ) ){
			Ext.Ajax.request({
				url: '/rd/dao/common/dncdao.php', params: {"action":"getDNCResults", "ph":phoneDigits, "ip":Ext.get( 'ip' ).getValue() },
				success: function( responseObject ) {
					o = Ext.decode( responseObject.responseText );
					if( o.success == true ){
						phoneDigits = o.phonedigits;
						phoneNum = formatPhone( phoneDigits );
						requester_ip = o.requester_ip;
						targetURL = "/rd/dao/blastSample.php?ph=" + phoneDigits;
						window.location.href = targetURL;
					} else {
						if( o.result == 'FAIL DNC' ){
							Ext.listMgmt.msg( 'Error', 'The number you provided ' + formattedPhone + ' is on the Rocket Dispatch DNC list.  Therefore, Rocket Dispatch cannot continue.' );
						} else if( o.result == 'FAIL IP' ){
							Ext.listMgmt.msg( 'Error', 'Although the number provided ' + formattedPhone + ' is not on the Rocket Dispatch DNC List, the IP address ( ' + requester_ip + ' ) from which the request was made has made more than one call that placed a number on the list.  Therefore, Rocket Dispatch cannot continue.' );
						}
					}
				},
				failure: function( responseObject ) {
					o = Ext.decode( responseObject.responseText ); Ext.listMgmt.msg( 'Error', 'There was an Internet connectivity issue, please try again.' );
				}
			});
		}
	}
}

function returnFieldKeyPress( e ){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;

	keycode == 13 ? startSample() : keycode = null;
}

function formatPhone( digits ){
	formattedPhone = "(";
	for (j=0; j < digits.length; j++){
		formattedPhone += digits.charAt(j);
		if (j==2){
			formattedPhone += ") ";
		}
		if (j==5){
			formattedPhone += "-";
		}
	}
	return formattedPhone;
}
