// Displays a popup window; takes 5 arguments; url is only the file name; urlvars is a delimited list of url variables; urlvals is a delimited list of the values for those variables (must use same delimiter for both); urldel is the delimiter; name is the name of the window; widgets represents all the other options for a window.open command.

// Example: PopIt('popup.cfm', 'var1|@var2|@var3', '<cf_urlencrypt paramval="1">|@<cf_urlencrypt paramval="2">|@<cf_urlencrypt paramval="3">', '|@', 'popupwin', 'toolbar=no,location=no,directory=no,status=no,menubar=no,scrollbar=yes,resize=no,width=475,height=150,left=300,top=350');

// Requires: WriteQueryString
function PopIt(url, urlvars, urlvals, urldel, name, widgets, noURLEncode) {
	querystring = WriteQueryString(urlvars, urlvals, urldel, noURLEncode);
	encodedurl = url+'?'+querystring;
	popupWin = window.open(encodedurl, name, widgets);
	popupWin.opener.top.name = "opener";
	popupWin.focus();
}

function CloseIt(url) {	
	opener.location = url;
	window.close();
}

// Encodes a value just like URLEncodedFormat in CF
function URLEncode(urlval) {
	escapedurlval = escape(urlval);
	encodedurlval = "";
	for(i=0; i<escapedurlval.length; i++) {
		var currchar = escapedurlval.charAt(i);
		if(currchar == '/') {
			encodedurlval = encodedurlval + ("%2F");
		}
		else if(currchar == '+') {
			encodedurlval = encodedurlval + ('%2B')
		}
		else if(currchar == '.') {
			encodedurlval = encodedurlval + ('%2E')
		}
		else {
			encodedurlval = encodedurlval + (currchar);
		}
	}
	return encodedurlval;
}

// Works just like ListLen in CF
function ListLen(liststr, listdel) {
	if (liststr.length == 0)
		return 0;
	else {
		listcount = 1;
		thisstr = liststr;
		while (thisstr.length > 0) {
			listpos = thisstr.indexOf(listdel);
			if (listpos == -1) {
				thisstr = "";
			}
			else {
				listcount++;
				thisstr = thisstr.substring(listpos+1, thisstr.length);
			}
		}
		return listcount;
	}
}

// Works just like ListGetAt in CF
function ListGetAt(liststr, listpos, listdel) {
	if (ListLen(liststr, listdel) < listpos) {
		alert("ERROR. In function ListGetAt(liststr, listpos, listdel) the value of listpos, which is "+listpos+", is not a valid index for the list given as a first argument (this list has "+ListLen(liststr, listdel)+" elements). Valid indexes are in the range 1 through the number of elements in the list.");
		return '';
	}
	else {
		listcount = 1;
		thisstr = liststr;
		returnstr = "";
		while (listcount <= listpos) {
			currlistpos = thisstr.indexOf(listdel);
			if (currlistpos == -1) {
				returnstr = thisstr;
			}
			else if (listcount == listpos) {
				returnstr = thisstr.substring(0, currlistpos);
			}
			else {
				thisstr = thisstr.substring(currlistpos+listdel.length, thisstr.length);
			}
			listcount++;
		}
		return returnstr;
	}
}

// Writes the query string based on the variables, values, and delimiter passed to it; urlvars is a delimited list of url variables; urlvals is a delimited list of the values for those variables (must use same delimiter for both); urldel is the delimiter.

// Example: WriteQueryString('var1|@var2|@var3', '1|@2|@3', '|@');
// This returns 'var1=1&var2=2&var3=3'

// Requires: URLEncode, ListLen, ListGetAt
function WriteQueryString(urlvars, urlvals, urldel, noURLEncode) {
	querystring = "";
	for (j=1; j<=ListLen(urlvars, urldel); j++) {
		if (querystring == "") {
			querystring = ListGetAt(urlvars, j, urldel) + "=" + ListGetAt(urlvals, j, urldel);
		}
		else {
			querystring = querystring + "&" + ListGetAt(urlvars, j, urldel) + "=" + ListGetAt(urlvals, j, urldel);
		}
	}
	return querystring;
}