function postSwaplistAd() {

	if (!validateFormData()) {
		return(false);
	}

	document.postAdForm.date.value = getCurrentDate();

	var adString =	replace(document.postAdForm.title.value, '\'', '\\\'') + '|' +
					replace(document.postAdForm.description.value, '\'', '\\\'') + '|' +
					replace(document.postAdForm.tradeForDesc.value, '\'', '\\\'') + '|' +
					replace(document.postAdForm.contName.value, '\'', '\\\'') + '|' +
					replace(document.postAdForm.contEmail.value, '\'', '\\\'') + '|' +
					replace(document.postAdForm.contPhone.value, '\'', '\\\'') + '|' +
					'pswd::' + replace(document.postAdForm.password.value, '\'', '\\\'') + '|' +
					replace(document.postAdForm.date.value, '\'', '\\\'');

	document.adSubmitForm.adContent.value = adString;	

	document.postAdForm.title.value = '';
	document.postAdForm.description.value = '';
	document.postAdForm.tradeForDesc.value = '';
	document.postAdForm.contName.value = '';
	document.postAdForm.contEmail.value = '';
	document.postAdForm.contPhone.value = '';
	document.postAdForm.password.value = '';
	document.postAdForm.password2.value = '';
	document.postAdForm.date.value = '';

	if (!document.getElementById('submitFrame')) {
		writeSubmitFrameDiv();
	}

	document.adSubmitForm.submit();

}

function validateFormData() {

	if (document.postAdForm.title.value == '') {
		alert('Please enter a title for this ad.');
		document.postAdForm.title.focus();
		return(false);
	}

	if (document.postAdForm.description.value == '') {
		alert('Please enter a description for this ad.');
		document.postAdForm.description.focus();
		return(false);
	}

	if (document.postAdForm.contName.value == '') {
		alert('Please enter a contact name for this ad.');
		document.postAdForm.contName.focus();
		return(false);
	}

	if (document.postAdForm.contEmail.value == '' && document.postAdForm.contPhone.value == '') {
		alert('Please enter either a contact email or a contact phone number for this ad.');
		return(false);
	}

	if (document.postAdForm.contEmail.value != '') {
		if (!validateEmail(document.postAdForm.contEmail.value, 'Your', 'document.postAdForm.contEmail')) {
			return(false);
		}
	}

	if (document.postAdForm.password.value == '') {
		alert('Please enter a password for this ad.');
		document.postAdForm.password.focus();
		return(false);
	}

	if (document.postAdForm.password.value != document.postAdForm.password2.value) {
		alert('Your password and confirm password do not match.');
		return(false);
	}

	return(true);
}

function removeSwaplistAd() {
	
	var currAdTitle = document.removeAdForm.adHeadline.value + '';
	var currAdPassword = document.removeAdForm.password.value + '';

	var adData = null;
	
	var adFound = false;

	for (var i=0;i<adsArray.length;i++) {
		adData = adsArray[i].split('|');

		if (adData[0].toLowerCase() == replace(currAdTitle.toLowerCase(), '\'', '\\\'')) {

			if (adData[6] == 'pswd::' + currAdPassword) {
				adFound = true;
				document.getElementById('removeAdLink').href = 'javascript:if(confirmRemove()){void(0);}';
				document.getElementById('removeAdPreviewDiv').innerHTML = '<img src="/img/blank.gif" height="7"><br>' +
				'<span style="font-weight:bold;color:#851F00;">You have chosen to remove this ad:</span>' +
				'<img src="/img/blank.gif" height="7"><br>' +
				writeAdML(adData) + 
				'<img src="/img/blank.gif" height="7"><br>' +
				'<span style="font-weight:bold;color:#851F00;">Click "Remove this Ad" again to confirm removal.</span>';
				document.getElementById('cancelRemoveLink').style.visibility = 'visible';
			}

		}

	}

	if (!adFound) {
		document.getElementById('removeAdPreviewDiv').innerHTML = '<span style="font-weight:bold;color:#851F00;">No ad with this headline and password was found.</span>';
	}

	window.scrollTo(0, findPos(document.getElementById('removeAdLink'))[0]);

}

function confirmRemove() {	
	document.removeAdForm.adHeadline.value = replace(document.removeAdForm.adHeadline.value, '\'', '\\\'');
	document.removeAdForm.password.value = replace(document.removeAdForm.password.value, '\'', '\\\'');

	if (!document.getElementById('submitFrame')) {
		writeSubmitFrameDiv();
	}

	document.removeAdForm.submit();
	document.removeAdForm.adHeadline.value = '';
	document.removeAdForm.password.value = '';
}

function cancelAdRemove() {

	document.getElementById('removeAdLink').href = 'javascript:if(removeSwaplistAd()){void(0);}';
	document.getElementById('removeAdPreviewDiv').innerHTML = '';
	document.getElementById('cancelRemoveLink').style.visibility = 'hidden';
	document.removeAdForm.adHeadline.value = '';
	document.removeAdForm.password.value = '';

}


function writeSwaplistAds() {

	var adsML = '';
	var aCount = 0;

	for (var i=adsArray.length-1;i>=0;i--) {
		
		var adData = null;

		if (adsArray[i] != '') {

			aCount++;

			adData = adsArray[i].split('|');

			adsML += writeAdML(adData);

		}


	}
	document.getElementById('adsListTotalCount').innerHTML = aCount + (aCount>1?' entries':' entry');
	document.getElementById('swaplistAdsDiv').innerHTML = adsML;
}

function writeAdML (adData) {

	adML = ''

	adML = '<div style="position:relative;margin-top:10px;font-family:trebuchet ms,arial,helvetica;">' +
					'<div style="position:relative;border: 1px solid #669C7D;">' + 
						'<div style="position:relative;float:left;color:#fff;font-weight:bold;background-color:#669C7D;padding-left:3px;padding-right:3px;font-size:14px;line-height:20px;">' + replace(adData[0], '\\', '') + '</div>' +
						'<div style="position:relative;float:left;color:#669C7D;padding-left:5px;font-size:12px;line-height:20px;">posted: ' + adData[7] + '</div>' +
							'<div style="position:relative;display:block;clear:both;padding-top:5px;padding-left:10px;padding-right:10px;">'  +
								'<div style="position:relative;display:block;font-size:11px;color:#000;">' +
									'<p style="margin-top:5px;margin-bottom:5px;"><strong style="color:#003A19;">Item(s) to trade: </strong>' + replace(adData[1], '\\', '') + '</p>' +
									'<p style="margin-top:5px;margin-bottom:5px;"><strong style="color:#003A19;">Trading for: </strong>' + (adData[2]!=''?replace(adData[2], '\\', ''):'anything') + '</p>' +
									'<p style="margin-top:5px;margin-bottom:5px;"><strong style="color:#003A19;">Contact: </strong>' + adData[3] + ' &nbsp; ' + (adData[4]!=''?'email: <a href="mailto:' + adData[4] + '?subject=Your%20CNPS%20Native%20Plant%20Swaplist%20Ad">' + adData[4] + '</a>':'') + ' &nbsp; ' + (adData[5]!=''?'phone: ' + adData[5]:'') + '</p>' +
								'</div>' +
							'</div>' +
					 '</div>' +
					'</div>';

	return(adML);
}

function writeSubmitFrameDiv() {

	document.getElementById('submitFrameDiv').innerHTML = '<iframe style="height:90px;width:300px;" src="/blank.html" name="submitFrame"></iframe>';

}


function findPos(obj) {

	obj = document.getElementById(obj.id);

	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}

	return [curleft,curtop];
}
