function setTFValue(number,section) {
	if(section=="part") {
		document.getElementById('part_an').value=number;
		document.forms["partEntry"].submit();
	}
	else {
		document.getElementById('chair_tf').value=number;
		//jQuery loading function for account info page.
		if(document.getElementById('acctInfo')){
			loadNext();
		//If the account info form is not found, user is on front page, send to CC.
		}else{
			document.forms["chairEntry"].submit();
		}
	}
}

function multiTF_display(tf_numbers,section) {
	var tf_array = new Array();
	tf_array = tf_numbers.split(":");
	var tf_radio_text='<p style="margin-bottom: 4px;">Please select the appropriate Dial in number below:</p>';

	if(section=="part") {
		for(var i=0;i<tf_array.length;i++) {
			tf_radio_text = tf_radio_text + '<p style="margin-bottom: 4px; line-height:1em;"><input style="margin:0 6px 0 0 !important; padding:0; line-height: 1em;" type="radio" name="radio_an" onclick="setTFValue(\''+tf_array[i]+'\',\'part\')">'+tf_array[i]+'</p>';
		}
		document.getElementById('part_mutliTF_view').innerHTML=tf_radio_text;
		document.getElementById('part_mutliTF_view').style.visibility="visible";
	}
	else {
		for(var i=0;i<tf_array.length;i++) {
			tf_radio_text = tf_radio_text + '<p style="margin-bottom: 4px; line-height:1em;"><input style="margin:0 6px 0 0 !important; padding:0; line-height: 1em;" type="radio" name="radio_an" onclick="setTFValue(\''+tf_array[i]+'\',\'chair\')">'+tf_array[i]+'</p>';
		}
		document.getElementById('chair_mutliTF_view').innerHTML=tf_radio_text;
		document.getElementById('chair_mutliTF_view').style.visibility="visible";
		
	}

}

function setTF(tf_numbers,section) {
	var tf_array = new Array();
	tf_array = tf_numbers.split(":");
	
	if(tf_numbers == 0) {
		if(section=="part") {
			showInvalid('part');
		}
		else {
			showInvalid('chair');
		}
	}
	
	else if(tf_array.length > 1) {
		multiTF_display(tf_numbers,section);
	}
	else if(tf_array.length == 1) {
		
		if(section=="part") {
			document.getElementById('part_an').value=tf_numbers;
			document.forms["partEntry"].submit();
		}
		
		else { //chair section login
			document.getElementById('chair_tf').value=tf_numbers;
			//jQuery loading function for account info page.
			if(document.getElementById('acctInfo')){
				loadNext();
			//If the account info form is not found, user is on front page, send to CC.
			}else{
				document.forms["chairEntry"].submit();
			}
		}
	}
}

function showInvalid(section) {
	if(section=="part") {
		document.getElementById('part_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">Invalid Access Code</div>";
		document.getElementById('part_mutliTF_view').style.visibility="visible";
	}
	else {
		document.getElementById('chair_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">Invalid Login</div>";
		document.getElementById('chair_mutliTF_view').style.visibility="visible";
	}
}

function showSystemMaintenanceNotice(section) {
	if(section=="part") {
		document.getElementById('part_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">ReadyTalk web conferencing services are currently unavailable due to scheduled system maintenance. Please try again later.";
		document.getElementById('part_mutliTF_view').style.visibility="visible";
	}
	else {
		document.getElementById('chair_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">ReadyTalk web conferencing services are currently unavailable due to scheduled system maintenance. Please try again later.";
		document.getElementById('chair_mutliTF_view').style.visibility="visible";
	}
}

function showSystemError(section) {
	if(section=="part") {
		document.getElementById('part_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">An error has occurred.<br>Please contact support.<br>U.S. and Canada: 800.843.9166<br>International: 303.209.1600</div>";
		document.getElementById('part_mutliTF_view').style.visibility="visible";
	}
	else {
		document.getElementById('chair_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">An error has occurred.<br>Please contact support.<br>U.S. and Canada: 800.843.9166<br>International: 303.209.1600</div>";
		document.getElementById('chair_mutliTF_view').style.visibility="visible";
	}
}

function checkTF(section,ac) {
	// Obtain an XMLHttpRequest instance
	var req = newXMLHttpRequest();
	if(section == 'part') {
		var ac  = document.getElementById('part_ac').value;
		for(var i=0;i<ac.length;i++) {
			if(ac.charCodeAt(i) < 48 || ac.charCodeAt(i) > 57) {
				if(ac.charCodeAt(i) == 32) { //this is a space, so we'll strip it out and correct form value
					var regexp = / /;
					document.getElementById('part_ac').value=ac.replace(regexp,"");
					ac=document.getElementById('part_ac').value; //reset value for check
				}
				//else {  //any other chars found, we'll warn and not send them in
				//		alert("Bad character found.\nPlease check entry");
				//		return false;
				//}
			}
		}		
	}
	else  { //chair value to be looked up
		//Check if ac was passed in (likely for account info page).  Otherwise retrieve from doc.
		if(!ac){
			var ac  = document.getElementById('chair_ac').value;
		}
		for(var i=0;i<ac.length;i++) {
			if(ac.charCodeAt(i) < 48 || ac.charCodeAt(i) > 57) {
				if(ac.charCodeAt(i) == 32) { //this is a space, so we'll strip it out and correct form value
					//alert('space at ' + i);
					var regexp = / /;
					document.getElementById('chair_ac').value=ac.replace(regexp,"");
					ac=document.getElementById('chair_ac').value  //reset value for check
					i--; //we have to decrement one to keep counter inline
				}
				else {  //any other chars found, we'll warn and not send them in
					alert("Bad character found.\nPlease check entry");
					return false;
				}
			}
		}		
	}

	if(document.getElementById('chair_passcode').value == '' && section != 'part') { 
		document.getElementById('chair_mutliTF_view').innerHTML="<div style=\"color:red;font-weight:bold;\">Please enter passcode.</div>";
		document.getElementById('chair_mutliTF_view').style.visibility="visible";
	}
	else {
		var url = "/legacy/check_tollfree.php?ac="+ac;
		req.onreadystatechange = function() { retrieveTF(req,section); };
		req.open("GET", url, true);
		req.send(null);
	}
	return false;
}

function retrieveTF(req,section) {
	if (req.readyState == 4) {
		if (req.status == 200) {
			//alert('in 200');
			if(req.responseText == 'lookup error') {
				showSystemError(section);
				return false;
			}
			else if(req.responseText == 'maint') {
				showSystemMaintenanceNotice(section);
				return false;
			}
			else if(req.responseText.substr(0,13) == 'send to event') {
				var textvar = req.responseText.split("|||");
				var geturl=textvar[1];
				document.getElementById('er').value=document.getElementById('part_ac').value;
				document.getElementById('ev_form').action=geturl;
				document.ev_form.submit()
				return false;
			}				
			else {
				setTF(req.responseText,section)
				if(section=="part") {
					//document.forms["partEntry"].submit();
				}
			}
		}
		else {
			alert('There was a problem with the request.\nPlease contact customer support at 1-800-843-9166');
		}
	}
}

