var CNV_QREG_EMAIL_ONLY = 5;
var CNV_QREG_EMAIL_ZIP  = 6;
var CNV_QREG_EMAIL_NAME = 4;
var CNV_QREG_EMAIL_NAME_ZIP = 0;
var CNV_QREG_EMAIL_NAME_STATE = 8;
var CNV_QREG_EMAIL_NAME_ADDRESS = 7;

function CNV_URLEncode(str)
{
        var ms = "%25#23 20?3F<3C>3E{7B}7D[5B]5D|7C^5E~7E`60+2B"
        var msi = 0
        var i,c,rs,ts
        while (msi < ms.length)
        {
                c = ms.charAt(msi)
                rs = ms.substring(++msi, msi +2)
                msi += 2
                i = 0
                while (true)
                {
                        i = str.indexOf(c, i)
                        if (i == -1) break
                        ts = str.substring(0, i)
                        str = ts + "%" + rs + str.substring(++i, str.length)
                }
        }
        return str
}

// This function provides the same conversion as that obtained from
// java.net.URLEncoder.encode().  The function converts a string into
// the x-www-form-urlencoded MIME format.
//
// To convert the string, each character is examined in turn:
//
//     o The ASCII characters 'a' through 'z', 'A' through 'Z',
//       '0' through '9', and '.', '-', '*', '_' remain the same.
//     o The space character (' ') is converted into a plus sign ('+').
//     o All other characters are converted into the 3-character string
//       %XY, where XY is the two-digit hexadecimal representation of the
//       lower 8-bits of the character.
//
// NB: The list of characters that are not encoded have been determined by
//     referencing O'Reilly's "HTML: The Definitive Guide" (page 164).
//
// The implementation is derived from that found at
// <http://summerholiday.org/freecode/JavaScript_URL_Encode.html>
// (which appears to share significant DNA with the code at
// <http://www.blooberry.com/indexdot/html/topics/urlencoding.htm>).
//
function CNV_URLEncodeParamValue (paramValue)
{
        var len       = paramValue.length;
        var i         = 0;
        var newStr    = "";
        var paramChar = "";
        for (i=0;i<len;i++)
        {
                paramChar = paramValue.substring (i, i + 1)
                if (CNV_isUrlOK (paramChar))
                {
                        newStr = newStr + paramChar;
                }
                else if (paramChar.charCodeAt(0) == 32)
                {
                	newStr = newStr + "+";
                }
                else
                {
                        tval1 = paramChar;
                        newStr = newStr + "%" + CNV_decToHex (tval1.charCodeAt(0), 16);
                }
        }
	return newStr;
}

// part of URLEncodeParamValue()
var hexVals = new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function CNV_decToHex(num, radix)
{
        var hexString = "";
        while (num >= radix)
        {
               temp = num % radix;
               num = Math.floor(num / radix);
               hexString += hexVals[temp];
        }
        hexString += hexVals[num];
        return CNV_reversal(hexString);
}

// part of URLEncodeParamValue()
function CNV_reversal (s)
{
        var len = s.length;
        var trans = "";
        for (i=0; i<len; i++)
        {
                trans = trans + s.substring(len-i-1, len-i);
        }
        s = trans;
        return s;
}

// part of URLEncodeParamValue()
function CNV_isUrlOK(compareChar)
{
	var charCode = compareChar.charCodeAt(0);
        if ((charCode >= 97 && charCode <= 122)		// 'a'-'z'
            || (charCode >= 65 && charCode <= 90)	// 'A'-'Z'
            || (charCode >= 48 && charCode <= 57)	// '0'-'9'
            || charCode == 46				// '.'
            || charCode == 45				// '-'
            || charCode == 95				// '_'
            || charCode == 42)				// '*'
        {
                return true;
        }
        else
        {
                return false;
        }
}

function CNV_QuickRegUsage() {
	alert("Usage: popupQuickRegForm(style, <domain>, <nextUrl>, <includeInterests>, <buttonText>, <width>, <height>)");
}

function CNV_popupQuickRegForm(style, domain, nextUrl, interests, buttonText, width, height){
	//
	// Check for required paramaters
	//
	if (style == null) {
		CNV_QuickRegUsage();
		return false;
	}

	if (style != 0 && style != 4 && style != 5 && style != 6 && style != 7 && style != 8) {
		alert("Style must be one of these values: 0, 4, 5, 6, 7, 8");
		return false;
	}
	if (domain == null || domain.length == 0) {
		path = "";
	}
	else {
		if (domain.indexOf("http://") == -1) {
			domain = "http://" + domain;
		}
		if (domain.indexOf('/site/') == -1) {
			path = domain + '/site/';
		}
		else {
			path = domain;
		}
	}


	//
	// Set defaults for optional parameters
	//
	if (nextUrl == null) {
		nextUrl = "" + window.top.location;
	}

	if (interests == null) includeInterests = "FALSE";
	else if (interests == true) includeInterests = "TRUE";
	else if (interests == false) includeInterests = "FALSE";
	else if (interests == 'true') includeInterests = "TRUE";
	else if (interests == 'false') includeInterests = "FALSE";
	else if (interests == 'TRUE') includeInterests = "TRUE";
	else includeInterests = "FALSE";

	if (buttonText == null) {
		buttonText = "Sign Up";
	}
	if (width == null) {
		if (style == 0) width=440;
		else if (style == 4) width=425;
		else if (style == 5) width=425;
		else if (style == 6) width=425;
		else if (style == 7) width=460;
		else if (style == 8) width=430;
		else width=450;
	}
	if (height == null) {
		if (style == 0) height=400;
		else if (style == 4) height=400;
		else if (style == 5) height=400;
		else if (style == 6) height=400;
		else if (style == 7) height=500;
		else if (style == 8) height=400;
		else height=400;
	}

	//
	// Set variables that have standard values
	//
	var externalContent = 'TRUE';
	var complete = 'FALSE';

	//
	// Build the URL to the QuickReg Servlet
	//
	nextUrl = CNV_URLEncode(nextUrl);
	buttonText = CNV_URLEncode(buttonText);
	var theTime = new Date().getTime();	// used to make URL unique
	var url = path + "QuickReg?QREG_STYLE=" + style + "&QREG_INCLUDE_INTERESTS=" + includeInterests + "&QREG_EXTERNAL_CONTENT=" + externalContent + "&QREG_BUTTON_TEXT=" + buttonText + "&QREG_COMPLETE=" + complete + "&TS=" + theTime + "&NEXTURL=" + nextUrl;

	//
	// Open it up
	//
	window.top.open(url, '_blank', 'scrollbars=yes,resizable=yes,alwaysRaised=yes,dependent=yes,width=' + width + ',height=' + height)
}


function CNV_SurveyUsage() {
	alert("Usage: CNV_popupSurveyForm(surveyid, <domain>, <nextUrl>, <width>, <height>)");
}

function CNV_popupSurveyForm(surveyid, domain, nextUrl, width, height){
	//
	// Check for required paramaters
	//
	if (surveyid == null) {
		CNV_SurveyUsage();
		return false;
	}

	if (domain == null || domain.length == 0) {
		path = "";
	}
	else {
		if (domain.indexOf("http://") == -1) {
			domain = "http://" + domain;
		}
		if (domain.indexOf('/site/') == -1) {
			path = domain + '/site/';
		}
		else {
			path = domain;
		}
	}

	//
	// Set defaults for optional parameters
	//
	if (nextUrl == null) {
		nextUrl = "" + window.top.location;
	}

	if (width == null) {
		width=450;
	}
	if (height == null) {
		height=400;
	}

	//
	// Build the URL to the Survey Servlet
	//
	nextUrl = CNV_URLEncode(nextUrl);
	var theTime = new Date().getTime();	// used to make URL unique
	var url = path + "Survey?ACTION_REQUIRED=URI_ACTION_USER_REQUESTS&SURVEY_ID=" + surveyid + "&mfc_popup=true" +  "&TS=" + theTime + "&NEXTURL=" + nextUrl;

	//
	// Open it up
	//
	window.top.open(url, '_blank', 'scrollbars=yes,resizable=yes,alwaysRaised=yes,dependent=yes,width=' + width + ',height=' + height)
}
