var 		UPP_URL = "";
var 		COOKIE_NAME = "UPP_REPLY";
var		submitFlag = false;

function setReply(url, name)
{
	var oneYear = 365 * 7 * 24 * 60 * 1000;
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + oneYear);
	var cookieAttributes = "; expires=" + expDate.toGMTString() + "; path=/";

	var pdpCookie = COOKIE_NAME + "=" + escape(url) + "|" + escape(name);

	document.cookie = pdpCookie + cookieAttributes;
}

function clearReply()
{
	document.cookie = COOKIE_NAME + "=; path=/"
}

function pdpLink(nPub)
{

	if (UPP_URL == "")
		return;
		
	if (!nPub || nPub < 1) {
		alert("You must first create a profile before continuing your message");
	} else {
		window.location = UPP_URL;
	}
}

function getReplyUrl()
{
	return getCookie(COOKIE_NAME);
}

function getReply(nPub)
{
	var		cookieVal = getCookie(COOKIE_NAME);
	
	if (!cookieVal || cookieVal == "")
		return;

	var		valArray = new Array();
	valArray = cookieVal.split('|');

	UPP_URL = valArray[0];
	
	document.write('<table border="0" cellpadding="2" cellspacing="0" bgcolor="#ff0000"><tr><td>\n' +
	               '<table border="0" cellpadding="4" cellspacing="0" bgcolor="#ffffff">\n' +
	               '<tr>\n' + '<td><img src="/img/err.gif"></td>' +
	               '<td class="content">\n' +
	               '<span class="error"><b>Pending Message:</b> </span>' +
	               '<a href="javascript:pdpLink(' + nPub + 
	               ');" class="content"><b>Click Here</b></a>' +
	               ' to continue your message to <b>' + valArray[1] + '</b>.\n' +
	               '</td>\n' +
	               '</tr><tr>\n' +
	               '<td colspan="2" align="right" class="sm"><a href="javascript:clearReply();location.reload();" class="sm">&gt;clear this message</a></td>' + 
	               '</tr></table>\n' +
	               '</td></tr></table>\n<br>\n\n');
	               
}


function limitCb(fVar, limit, name)
{
	var		n;
	
	if (!fVar)
		return true;
	
	if ((n = checkCount(fVar)) > limit) {
		alert('You may select up to ' + limit + ' choices for the question "' + name + '"; ' +
		      'you have selected ' + n + ' choices.\n' +
		      'Please de-select at least ' + (n - limit) + ' choice' + ((n-limit) > 1 ? 's' : '') + '.');
		return false;
	}
	
	return true;
}

function uppBookmark(id)
{
	var		rl = escape(location.pathname + location.search);
	location = '/sqd/search/bookmark?BKM_FID=' + id + '&RL=' + rl;
}

function uppUnBookmark(id)
{
	var		rl = escape(location.pathname + location.search);
	location = '/sqd/search/bookmark?BKM_ID=' + id + '&RL=' + rl;
}


function updForm(fObj)
{
	if (fObj.UPP_HT)
		setHeight(fObj, 'UPP_HT');
	if (fObj.UPD_HT_MIN)
		setHeight(fObj, 'UPD_HT_MIN');
	if (fObj.UPD_HT_MAX)
		setHeight(fObj, 'UPD_HT_MAX');		
}

function checkProfileSubmit(fObj)
{
	var		emailRe = /([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)*@([a-zA-Z0-9_-])+/;
	
	// check for email addresses in "about me"	
	if (fObj.UPP_LDSC && fObj.UPP_LDSC.value.search(emailRe) != -1) {
		alert('It appears you have an e-mail address in your Messsage.\nE-mail addresses are not permitted in personal ads.');
		return false;
	}
	
	// check checkbox limits, textarea sizes
	/*
	if (limitCb(fObj.UPP_ETH, 3, 'Ethnicity') &&
	    limitCb(fObj.PDB_LANG, 5, 'Languages') &&
	    limitCb(fObj.PDB_LAUGH, 3, 'My sense of humor') &&
	    limitCb(fObj.UPD_BUILD, 4, 'Body Type') &&
	    checkFieldSize(fObj.UPP_LDSC, 1000, "About Me")) {
	}
	*/

	// check "looking for" (UPD) location stuff
	if (fObj.UPD_LOC_TYPE[0].checked) {
		// set country to usa
		for (i = 0; i < fObj.UPD_COUNTRY.options.length; i++) {
			if (fObj.UPD_COUNTRY.options[i].value == 'USA') {
				fObj.UPD_COUNTRY.selectedIndex = i;
				break;
			}
		}
	}
	
	if (!submitFlag) {
		submitFlag = true;
		pWin("/sqd/common/parent-submit?FORM_NAME=document." + fObj.name, 500,350,"");
	}
	
	return false;
}

function photoView(upp_id, id)
{
	pWin('/sqd/profile/photoView?UPP_ID=' + upp_id + '&UPI_ID=' + id,570,480,'resize,status');
}

// ######## location stuff ########

function uppZipLookup(evt)
{
	zipLookup(document.frm_basicInfo, 'UPP');
}

function updZipLookup(evt)
{
	zipLookup(document.frm_lookfor, 'UPD');
}

