var message = "";
var di = "";
var name = new Array();
var params = new Array();
var width = 0;
var height = 0;
var relay = null;
var copyright = null;
var privacy = null;
var window_count = 0;
var window_name = "";
var popupWin = null;

//  Determines if the 3-digit number supplied is a valid NPA.
function isValidNPA(npa) {
	var valid = new Array("011", "111", "411", "511", "611", "711", "811", "911", "000", "100", "555", "976");
	//var valid = new Array("111", "411", "511", "611", "711", "811", "911", "000", "100", "555", "976", "204", "250", "289", "306", "403", "416", "418", "450", "506", "514", "519", "604", "613", "647", "705", "709", "778", "780", "807", "819", "867", "902", "905");
	for (var i=0; i < valid.length; i++) {
		if (npa == valid[i]) { 
			return false;
		}
	}
	return true;
}
//  Determines if the 3-digit number supplied is a valid NXX.
function isValidNXX(nxx) {
	var valid = new Array("011", "111", "511", "611", "000", "100");
	//var valid = new Array("011", "111", "211", "311", "511", "611", "811", "000", "100");
	for (var i=0; i < valid.length; i++) {
		if (nxx == valid[i]) { 
			return false;
		}
	}
	return true;
}
//  Determines if the number is a valid toll free.
function isValidTollFree( npa, nxx) 
{
	
	var validNxx = new Array("511", "611");
	var validToll = new Array("800", "888", "877", "866");
	var flag =0;

	for (var i=0; i < validNxx.length; i++) 
	{
		if (nxx == validNxx[i]) 
		{ 
			flag = 1;
			break;
		}
	}

	if(flag == 1)
	{
		for (var i=0; i < validToll.length; i++) 
		{
			if (npa == validToll[i]) 
			{ 
				return true;
			}
		}
	}
	
	return false;
}
//  Determines if the 3-digit number supplied is an international NPA.
function isInternational(npa) {
	var valid = new Array("204", "204", "250", "289", "306", "403", "416", "418", "450", "506", "514", "519", "604", "613", "647", "670", "705", "709", "780", "778", "807", "819", "867", "902", "905", "242", "246", "264", "268", "284", "345", "441", "473", "649", "664", "758", "767", "784", "809", "868", "869", "876");
	for (var i=0; i < valid.length; i++) {
		if (npa == valid[i]) { 
			return true;
		}
	}
	return false;
}


function goodToConnect() {
	var ntd = document.forms['relay'].DIALTO1.value;
	/*
	-------------------------------------------------------------------------------
	BEGIN SCREENING
	-------------------------------------------------------------------------------
	*/
	var valid = "0123456789";
	var ltrvalid = "ABCDEFGHIJKLMNOPQRSTVWXYZ";
	var badnbr = new Array("000", "100", "011", "111", "211", "311", "411", "511", "611", "711", "811", "911", "555", "976");
	var newstr = "";
	var temp;		
	
	//  REMOVE INVALID CHARACTERS AND REPLACE ALPHANUMERIC CHARACTERS
	//-----------------------------------------------------------------------------
	for (var i=0; i < ntd.length; i++) {
		temp = "" + ntd.substring(i, i+1);
		if (valid.indexOf(temp) != "-1") { 
			newstr = newstr + temp; 
		} else if (ltrvalid.indexOf(temp.toUpperCase()) != "-1") {
			newstr = newstr + ltrToNbr(temp.toUpperCase());
		}
	}
	ntd = newstr;

	//  VALIDATE NUMBER TO DIAL (NTD) AND PROCEED OR DENY CALL
	//-----------------------------------------------------------------------------
	switch(ntd.length) {
		case 3:
			//Allow 911 calls
			if (ntd == "911") {
				dial(ntd);
			} else {
					alert("The number you entered is an invalid number. Please check the number and try again.");
			}
			break;				
		case 7:
			alert("The number seems to be missing the Area Code. Use the following format: (xxx) xxx-xxxx.");
			break;
		case 10:
			if ( isValidNPA(ntd.substring(0,3)) && isValidNXX(ntd.substring(3,6)) ) {
				if( isInternational(ntd.substring(0,3)) ) {
					alert("INTERNATIONAL CALLS ARE NOT AVAILABLE THROUGH SPRINT RELAY ON LINE AT THIS TIME. TO COMPLETE A SPRINT RELAY CALL VIA TTY, ASCII OR VOICE PLEASE CALL 1-800-877-8973.\r\n\r\nLLAMADAS INTERNACIONALES NO SE PERMITEN POR MEDIO DE SPRINT RELAY ONLINE EN ESTE MOMENTO. PARA COMPLETAR UNA LLAMADA POR SPRINT RELAY USANDO APARATOS DE TTY, ASCII O VOZ POR FAVOR LLAME A 1-800-435-8590.\r\n\r\nLES APPELS INTERNACIONAUX NE SONT PAS DISPONIBLES EN FRANCES ATRAVERS SPRINT RELAY ON LINE C`EST MOMENT.");
					break;
				} else {
					dial(ntd);
				}
			} else {

				if(isValidTollFree(ntd.substring(0,3), ntd.substring(3,6))) {
					dial(ntd);
				} else {
					alert("The number you entered is an invalid number. Please check the number and try again.");
				}
			}
			break;
		case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18:
			if ( ntd.length == 11 ) {
				if ( isValidNPA(ntd.substring(1,4)) && isValidNXX(ntd.substring(4,7)) ) {
					if( isInternational(ntd.substring(1,4)) ) {
						alert("INTERNATIONAL CALLS ARE NOT AVAILABLE THROUGH SPRINT RELAY ON LINE AT THIS TIME. TO COMPLETE A SPRINT RELAY CALL VIA TTY, ASCII OR VOICE PLEASE CALL 1-800-877-8973.\r\n\r\nLLAMADAS INTERNACIONALES NO SE PERMITEN POR MEDIO DE SPRINT RELAY ONLINE EN ESTE MOMENTO. PARA COMPLETAR UNA LLAMADA POR SPRINT RELAY USANDO APARATOS DE TTY, ASCII O VOZ POR FAVOR LLAME A 1-800-435-8590.\r\n\r\nLES APPELS INTERNACIONAUX NE SONT PAS DISPONIBLES EN FRANCES ATRAVERS SPRINT RELAY ON LINE C`EST MOMENT.");
					} else {
						dial(ntd.substring(1,ntd.length));
					}
				} else {

					if(isValidTollFree(ntd.substring(1,4), ntd.substring(4,7)))
					{
						dial(ntd.substring(1,ntd.length));
					}
					else
					{
						alert("The number you entered is an invalid number. Please check the number and try again.");
					}
				}
			} else {
				if ( ntd.substring(0,3) == "011" ) {
					if(ntd.substring(3,6) == "684") {
						dial(ntd);
					} else {
						alert("INTERNATIONAL CALLS ARE NOT AVAILABLE THROUGH SPRINT RELAY ON LINE AT THIS TIME. TO COMPLETE A SPRINT RELAY CALL VIA TTY, ASCII OR VOICE PLEASE CALL 1-800-877-8973.\r\n\r\nLLAMADAS INTERNACIONALES NO SE PERMITEN POR MEDIO DE SPRINT RELAY ONLINE EN ESTE MOMENTO. PARA COMPLETAR UNA LLAMADA POR SPRINT RELAY USANDO APARATOS DE TTY, ASCII O VOZ POR FAVOR LLAME A 1-800-435-8590.\r\n\r\nLES APPELS INTERNACIONAUX NE SONT PAS DISPONIBLES EN FRANCES ATRAVERS SPRINT RELAY ON LINE C`EST MOMENT.");
					}
				} else {
					alert("The number you entered is an invalid number. Please check the number and try again.");
				}
			}
			break;
		default:
			alert("The number you entered is an invalid number. Please check the number and try again.");
	}
	/*
	-------------------------------------------------------------------------------
	END SCREENING
	-------------------------------------------------------------------------------
	*/
}


function connect() {

	var ntodial = document.forms['relay'].DIALTO1.value;
	if (ntodial == "" || ntodial == null) {
		alert("Please enter number to dial");
		document.forms['relay'].DIALTO1.focus();
	} else if (ntodial == "911") {
        // If the user is not logged in, then we have to prompt for the 
        // address information needed for emergency calls...
        if( readCookie("userid") == null ) {
		    prompt911('These four fields are required for emergency calls', 'myfunction');
        } else {
            // The user is logged in, so blank out the fields and place the call...
            var x = document.forms["relay"];
            x.yourName.value = "";
            x.callbackNumber.value = "";
            x.streetAddress.value = "";
            x.zipCode.value = "";
            //Place the call
            goodToConnect();
        }
	} else {
		//Clear out any previous 911 information and call goodToConnect
		myfunction("", "", "", ""); 
	}
}

function wait_for_call() {

    // Set the user_id and user_number fields in the FORM, based on the 
    // cookies that were set by the validation servlet.
	var doc = document.forms["relay"];

    doc.user_id.value     = readCookie( "userid" );
    doc.user_number.value = readCookie( "phone" );

    dial( "wait" );
}


function dial(ntd) {
	//document.write("Dialing: "+ntd)
	
	if(ntd == "cust") {	
		document.forms['relay'].STATE.value = "CSV"; 
		ntd = "";
	} else { 
		document.forms['relay'].STATE.value = "ALL"; 
	}
    if( ntd != "wait" ) {
		document.forms['relay'].user_id.value = readCookie( "userid" );
		document.forms['relay'].user_number.value = null;
    }

	checkBrowser();
	document.forms['relay'].DIALTO1.focus();
	window_name = "InternetRelay" + window_count;
	document.forms['relay'].target = window_name;
	var width = 670;
	var height = 600;
	var leftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	// Removing 10 from topPosition to compensate for IE status bar...
	var topPosition = (screen.height) ? ((screen.height-height)/2 - 25) : 0;
	
	window.open('',window_name, 'toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,width='+width+',height='+height+',top='+topPosition+',left='+leftPosition+'');
	
	document.forms['relay'].DNIS.value = "800";
	document.forms['relay'].DIALTO1.value = ntd;
	document.forms['relay'].DIALTO.value = ntd;
	document.forms['relay'].DIALINFO.value = document.forms['relay'].DIALINFO1.value;
	document.forms['relay'].DIALINFO1.value = "";
	document.forms['relay'].DIALTO1.value = "";
	document.forms['relay'].APPCODENAME.value = navigator.appCodeName;
	document.forms['relay'].APPNAME.value = navigator.appName;
	document.forms['relay'].APPMINORVERSION.value = navigator.appMinorVersion;
	document.forms['relay'].OSPLATFORM.value = navigator.platform;
	document.forms['relay'].APPVERSION.value = navigator.appVersion;

	//document.forms['relay'].target = window_name;
	window_count++;
	document.forms['relay'].submit();
}


function checkBrowser()
{
	HM_DOM = (document.getElementById) ? true : false;
	HM_NS4 = (document.layers) ? true : false;
	HM_IE  = (document.all) ? true : false;
	HM_IE4 = HM_IE && HM_DOM;
	HM_MAC = (navigator.appVersion.indexOf("Mac") != -1);
	HM_IE4M = HM_IE4 && HM_MAC;

	if(HM_NS4) { document.forms['relay'].BROWSER.value = 'netscape'; }
	if(HM_IE) { document.forms['relay'].BROWSER.value = 'ie'; }
}

function ltrToNbr(ltrnbr) {
	switch (ltrnbr)	{
		case 'A': case 'B': case 'C':
			return "2";
			break;
		case 'D': case 'E': case 'F':
			return "3";
			break;
		case 'G': case 'H': case 'I':
			return "4";
			break;
		case 'J': case 'K': case 'L':
			return "5";
			break;
		case 'M': case 'N': case 'O':
			return "6";
			break;
		case 'P': case 'Q': case 'R': case 'S':
			return "7";
			break;
		case 'T': case 'U': case 'V':
			return "8";
			break;
		case 'W': case 'X': case 'Y': case 'Z':
			return "9";
			break;
	}
}

function loadPage() {
	//alert("Service will be temporarily unavailable between 5:00 A.M. and 5:15 A.M. C.S.T.");
	document.forms['relay'].DIALTO1.value = parseURL('ntd');
	document.forms['relay'].DIALTO1.focus();
}
function WinOpen(page,w,h) {
	if(privacy != null) { privacy.close(); }
	if(popupWin != null) { popupWin.close(); }
	popupWin=open(page,"popup","status=yes,toolbar=no,directories=no,scrollbars=yes,menubar=no,resizable=yes,width=" + w + ",height=" + h + ",left=20,top=20");
	popupWin.focus();
}
function openURL(url, title, params) {
	if(popupWin != null) { popupWin.close(); }
	if(privacy != null) { privacy.close(); }
	privacy = window.open(url, title, params, false);	
	privacy.focus();
}
function WinOpenClose(page,w,h) {
	if(privacy != null) { privacy.close(); }
	if(popupWin != null) { popupWin.close(); }
	popupWin=open(page,"popup","status=yes,toolbar=no,directories=no,scrollbars=yes,menubar=no,resizable=yes,width=" + w + ",height=" + h + ",left=20,top=20");
	popupWin.focus();
	window.close();
}
function openWindow(url) {
	if(privacy != null) { privacy.close(); }
	if(popupWin != null) { popupWin.close(); }
	popupWin = window.open(url, name, 'scrollbars,resizable,width=420,height=400,left=20,top=20');
	popupWin.focus();
}
function showFlash() {
	var now = new Date();
	var sec = now.getSeconds();
	document.write("											<!-- URL's used in the movie-->");	
	if(sec < 30) {
		document.write("											<A HREF=FSCommand:asl_video></A> <!-- text used in the movie-->");
	} else {
		document.write("											<A HREF=FSCommand:smart_button></A> <!-- text used in the movie-->");
	}
	document.write("											<!--Sprint Relay OnlineNEED HELP?ASLGET HELP INview help inClick here to begin--><OBJECT classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"");
	document.write("											  codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\"");
	document.write("											 ID=\"sro\" WIDTH=\"175\" HEIGHT=\"112\" ALIGN=\"\">");
	if(sec < 30) {
		document.write("											 <PARAM NAME=movie VALUE=\"combined.swf\"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src=\"combined.swf\" quality=high bgcolor=#FFFFFF  WIDTH=\"175\" HEIGHT=\"112\" swLiveConnect=true ID=\"sroasl\" NAME=\"sroasl\" ALIGN=\"\"");
	} else {
		document.write("											 <PARAM NAME=movie VALUE=\"combined.swf\"> <PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#FFFFFF> <EMBED src=\"combined.swf\" quality=high bgcolor=#FFFFFF  WIDTH=\"175\" HEIGHT=\"112\" swLiveConnect=true ID=\"smartbutton\" NAME=\"smartbutton\" ALIGN=\"\"");
	}
	document.write("											 TYPE=\"application/x-shockwave-flash\" PLUGINSPAGE=\"http://www.macromedia.com/go/getflashplayer\"></EMBED>");
	document.write("											</OBJECT>");
}
function parseURL(param) {
	var queryString = document.location + "";
	var val = "";
	param = param + "=";
	if(queryString.indexOf('?') != -1) {
		if(queryString.indexOf('&',(queryString.indexOf(param) + param.length)) != -1) { 
			val = queryString.substring((queryString.indexOf(param) + param.length),queryString.indexOf('&',(queryString.indexOf(param) + param.length)));
		} else {
			val = queryString.substring((queryString.indexOf(param) + param.length),queryString.length);
		}
	}
	return val;
}
function buildHelp(title,img,text,name,top) {
	document.write("			<table width=\"100%\" bgcolor=\"#FFFFFF\" border=\"0\"><tr><td align=\"left\">");
	document.write("			<span class=\"t12k-b\"><a name=\"" + name + "\">" + title + "</a></span><br><br>");
	document.write("			<img src=\"images\\" + img + "\">");
	document.write("			<p>" + text + "</p></td></tr>");
	document.write("			<tr><td align=\"right\"><span class=\"t10k\" align=\"right\">" + top + "</a></td></tr></table><br><br>");
}
function getHelpPage(clip,title,player,speed) {
	document.close();
	document.open();
	title = title.replace(/ /, "%20");
	window.open("help_asl_clips.htm?player=" + player + "&clip=" + clip + "&title=" + title + "&speed=" + speed,"popup");
}
function getRaidoValue(radio,defaultvalue) {
	var val = "";
	for(var i=0; i<radio.length; i++) {
		if(radio[i].checked) {
			return radio[i].value;
		}
	}
	return defaultvalue; 
}



function prompt911(prompttitle, sendto) { 
	promptbox = document.createElement('div'); 
	promptbox.setAttribute ('id' , 'prompt'); 
	document.getElementsByTagName('body')[0].appendChild(promptbox); 

	promptbox = eval("document.getElementById('prompt').style");
	promptbox.position = 'absolute'; 
	promptbox.top = 82; 
	promptbox.left = 230; 
	promptbox.width = 350; 
	promptbox.border = 'outset 1 #bbbbbb'; 

	var tempHTML = '';

	//Title & Spacer
	tempHTML = "<table cellspacing='0' cellpadding='0' border='0' width='100%' class='promptbox'><tr><td class='titlebar'>" + prompttitle + "</td></tr><tr><td>&nbsp;</td></tr></table>";

	//Inputs
	tempHTML = tempHTML + "<table cellspacing='0' cellpadding='0' border='0' width='100%' class='promptbox'><tr><td>Your Name:</td></tr><tr><td><input type='text' id='yName' size=\"40\" class='promptbox'></td></tr><tr><td>A 10 digit TTY call back number or VRS VP call back number:</td></tr><tr><td><input type='text' id='cNumber' class='promptbox'></td></tr><tr><td align='right'><tr><td>Address: (Used to locate the 911 service in your area)</td></tr><tr><td>Street:</td></tr><tr><td><input type='text' id='sAddress' class='promptbox'></td></tr><tr><td>Zip Code:</td></tr><tr><td><input type='text' id='zCode' class='promptbox'></td></tr>";

	//Spacer
	tempHTML = tempHTML + "<tr><td>&nbsp;</td></tr>";

	//Buttons
	tempHTML = tempHTML + "<tr><td><input type='button' class='submit' value='Submit' onMouseOver='this.style.border=\"1 outset #dddddd\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='" + sendto + "(document.getElementById(\"yName\").value, document.getElementById(\"cNumber\").value, document.getElementById(\"sAddress\").value, document.getElementById(\"zCode\").value); document.getElementsByTagName(\"body\")[0].removeChild(document.getElementById(\"prompt\"))'> <input type='button' class='prompt' value='Not an Emergency' onMouseOver='this.style.border=\"1 outset transparent\"' onMouseOut='this.style.border=\"1 solid transparent\"' onClick='document.getElementsByTagName(\"body\")[0].removeChild(document.getElementById(\"prompt\"))'></td></tr>";

	//Spacer
	tempHTML = tempHTML + "<tr><td>&nbsp;</td></tr>";

	//Disclaimer
	tempHTML = tempHTML + "<tr><td class='disclaimer'>Sprint hereby advises all current Sprint Relay customers that 911 calling services are available at this time, but may/or may not function the same as traditional 911 services.</td></tr></table>";
	
	document.getElementById('prompt').innerHTML = tempHTML;
	document.getElementById('prompt').focus();
} 

function myfunction(yourName, callbackNumber, streetAddress, zipCode) { 

	var x = document.forms["relay"];
	x.yourName.value = yourName;
	x.callbackNumber.value = callbackNumber;
	x.streetAddress.value = streetAddress;
	x.zipCode.value = zipCode;

	//Place the call
	goodToConnect();
}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

