function doNothing(){
	return true;
}

function getXMLHttpRequest()
{
    var req;
	if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return req;
}


function sendRequest(req, url)
{
	req.open("GET", url, true);
	if (window.XMLHttpRequest) {
		req.send(null);
	}else{
		req.send();
	}
}



function getElem(id)
{
	if (document.getElementById && document.getElementById(id)) {
		return document.getElementById(id);
	}
	else if (document.all && document.all[id]) {
		return document.all[id];
	}
	else {
		return false;
	}
}


function popupWindow(name, url, width, height) {
	var left = (screen.width - width)/2;
	var top = (screen.height - height)/2;
	eval(name + " = window.open(url, '" + name + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=yes,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + "');");
}






function ltrim( value ) {

	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

// Removes ending whitespaces
function rtrim( value ) {

	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");

}

// Removes leading and ending whitespaces
function trim( value ) {

	return ltrim(rtrim(value));

}



function showRssFeedPrice(show, id)
{
	if(show == true)
		getElem('rss_price_'+id).style.display = 'inline';
	else
		getElem('rss_price_'+id).style.display = 'none';
}



function showWebPrice(show, id)
{
	if(show == true)
		getElem('web_price_'+id).style.display = 'inline';
	else
		getElem('web_price_'+id).style.display = 'none';
}



function searchInLinks()
{
	var query_elem = $('inlinks_query');
	var query = trim(query_elem.value);

	if(query == '') return;

	//query_elem.disabled = true;
	$('inlink_results').innerHTML = '<p style="font-size: 1.15em; color: #888;" align="center">Searching for posts containing &quot;<strong>'+query+'</strong>&quot;...</p>';

	new Ajax.Request("global.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			$('inlink_results').innerHTML = result;
			//query_elem.disabled = false;
		},

		onFailure : function(resp) {
			$('inlink_results').innerHTML = '<p style="font-size: 1.15em; color: #888;" align="center"><strong>We\'re sorry, an issue has occurred. Please try again.</strong></p>';
			//query_elem.disabled = false;
		},

		parameters : 'action=search_inlinks&query=' + query + '&group_by_domain=' + $('group_by_domain').checked

	});

	return false;
}




function updateStep1Fields()
{
	var webYes 		= $('web_enabled_yes');
	var webNo  		= $('web_enabled_no');
	var rssYes 		= $('rss_enabled_yes');
	var rssNo  		= $('rss_enabled_no');
	var marketNo  	= $('rss_market_no');
	var wpYes  		= $('wordpress_yes');
	var wpOptions	= $('wordpress_options');
	var plNo		= $('post_level_enabled_no');

	if(wpYes.checked == true){
		wpOptions.style.display = 'block';
	}else{
		wpOptions.style.display = 'none';
		webYes.checked = true;
		rssNo.checked = true;
		marketNo.checked = true;
		plNo.checked = true;
	}

	var rssPersonlFields 	= getElem('rss_personal_options');

	if(webNo.checked == true && rssNo.checked == true){
		rssYes.checked = true;
	}

	if(rssYes.checked == true){
		rssPersonlFields.style.display = 'block';
	}else{
		rssPersonlFields.style.display = 'none';
	}

}


function checkUrlAvailable()
{
	if($('actionType').value == 'update') return submitStep1();

	removeDomainWarning();

	$('continueStep1').disabled = true;
	$('continueStep1').value = 'Please Wait...';

	new Ajax.Request("submit_site.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			if(result == 'available'){
				removeDomainWarning();
				submitStep1();
			}else{
				showDomainWarning();
			}

		},

		onFailure : function(resp) {
			showDomainWarning();
		},

		parameters : 'action=check_url&domain=' + $("domain").value

	});
}


function showDomainWarning()
{
	$('domain_row').style.backgroundColor = '#ecc';
	$('site_url_note').style.display = 'none';
	$('domain_notice').style.display = 'block';
	$('continueStep1').disabled = false;
	$('continueStep1').value = 'Continue';
}


function removeDomainWarning()
{
	$('domain_row').style.backgroundColor = '';
	$('domain_notice').style.display = 'none';
	$('site_url_note').style.display = 'block';
}


function submitStep1()
{

	var inventoryStep1 = $('inventoryStep1');
	var inventoryStep2 = $('inventoryStep2');
	var inventoryStep3 = $('inventoryStep3');

	var webYes 		= $('web_enabled_yes');
	var rssYes 		= $('rss_enabled_yes');
	var marketYes  	= $('rss_market_yes');
	var marketNo  	= $('rss_market_no');


	if(webYes.checked == true && $('inlinks_enabled').value == 'N'){
		$('inventory_web_fields').style.display = 'block';
		$('web_slot_fields').style.display = 'block';
	}else{
		$('inventory_web_fields').style.display = 'none';
		$('web_slot_fields').style.display = 'none';
	}

	if(rssYes.checked == true){
		$('inventory_rss_fields').style.display = 'block';
	}else{
		$('inventory_rss_fields').style.display = 'none';
		marketYes.checked = false;
		marketNo.checked = true;
	}

	if(marketYes.checked == true){
		$('rss_slot_fields').style.display = 'block';
	}else{
		$('rss_ads_marketplace').selectedIndex = 0;
		$('rss_ads_private').selectedIndex = $('rss_max_ads').selectedIndex + 1;
		$('rss_slot_fields').style.display = 'none';
	}

	inventoryStep1.style.display = 'none';
	$('continueStep1').disabled = false;
	$('continueStep1').value = 'Continue';

	if(webYes.checked == true || (rssYes.checked == true && marketNo.checked == false)){
		$('pageHeader').innerHTML = 'Step 2 - Tell us about your site...';
		inventoryStep2.style.display = 'block';
	}else{
		$('pageHeader').innerHTML = 'Step 3 - Set your number of ads...';
		inventoryStep3.style.display = 'block';
	}

}


function hideNumPages()
{
	if($('site_wide_yes').checked == true){
		$('siteNumPagesRow').style.display = 'block';
	}else{
		$('siteNumPagesRow').style.display = 'none';
		$('siteNumPages').value = '1';
	}
}


function submitStep2()
{

	if($('cat_id1').options[$('cat_id1').selectedIndex].value == ''){
		$('siteCat1').style.backgroundColor = '#ecc';
		return false;
	}else{
		$('siteCat1').style.backgroundColor = '';
	}

	if($('cat_id2').options[$('cat_id2').selectedIndex].value == ''){
		$('siteCat2').style.backgroundColor = '#ecc';
		return false;
	}else{
		$('siteCat2').style.backgroundColor = '';
	}

	if($('siteTitle').value == ''){
		$('siteTitleRow').style.backgroundColor = '#ecc';
		return false;
	}else{
		$('siteTitleRow').style.backgroundColor = '';
	}

	if($('siteDescription').value == ''){
		$('siteDescriptionRow').style.backgroundColor = '#ecc';
		return false;
	}else{
		$('siteDescriptionRow').style.backgroundColor = '';
	}

	if($('web_enabled_yes').checked == true && $('site_wide_yes').checked == true && $('siteNumPages').value == ''){
		$('siteNumPagesRow').style.backgroundColor = '#ecc';
		return false;
	}else{
		$('siteNumPagesRow').style.backgroundColor = '';
	}

	if($('inlinks_enabled').value == 'Y'){
		$('backStep2').disabled = true;
		$('continueStep2').disabled = true;
		$('continueStep2').value = 'Please Wait...';

		submitStep3();
		return true;
	}


	var inventoryStep2 = getElem('inventoryStep2');
	var inventoryStep3 = getElem('inventoryStep3');

	$('pageHeader').innerHTML = 'Step 3 - Set your number of ads...';

	inventoryStep2.style.display = 'none';
	inventoryStep3.style.display = 'block';

	return true;
}


function submitStep3()
{

	$('backStep3').disabled = true;
	$('continueStep3').disabled = true;
	$('continueStep3').value = 'Please Wait...';

	new Ajax.Request("submit_site.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			$('inventoryStep4').innerHTML = result;
			$('inventoryStep2').style.display = 'none';
			$('inventoryStep3').style.display = 'none';
			$('inventoryStep4').style.display = 'block';
			$('pageHeader').innerHTML = 'Step 4 - Site details and instructions';
		},

		onFailure : function(resp) {
			$('inventoryStep4').innerHTML = "<p>We're sorry, but there has been a problem processing your submission. Please try again later</p>";
			$('inventoryStep2').style.display = 'none';
			$('inventoryStep3').style.display = 'none';
			$('inventoryStep4').style.display = 'block';
			$('pageHeader').innerHTML = 'Step 4 - A problem has occured';
		},

		parameters : Form.serialize($("create_account"))

	});
}


function declineSpecialInvitation(inventory_key)
{
	new Ajax.Request("submit_site.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			$('inventoryStep4').innerHTML = result;
		},

		onFailure : function(resp) {
			window.location.href = "/my_account.php?view=generateLink&inventory_key="+inventory_key;
		},

		parameters : "action=decline_invitation&inventory_key="+inventory_key

	});
}


function getAjaxResponseVal(resp)
{
	var doc = resp.responseXML.documentElement;
	if(doc.hasChildNodes()) {
		return doc.childNodes[0].firstChild.data;
	}
	return '';
}




function goBackStep2()
{
	var inventoryStep1 = getElem('inventoryStep1');
	var inventoryStep2 = getElem('inventoryStep2');

	$('pageHeader').innerHTML = 'Step 1 - Where do you want to publish ads?';

	inventoryStep2.style.display = 'none';
	inventoryStep1.style.display = 'block';
}


function goBackStep3()
{
	var inventoryStep1 = getElem('inventoryStep1');
	var inventoryStep2 = getElem('inventoryStep2');
	var inventoryStep3 = getElem('inventoryStep3');

	var webYes 		= getElem('web_enabled_yes');
	var rssYes 		= getElem('rss_enabled_yes');
	var marketNo  	= getElem('rss_market_no');

	inventoryStep3.style.display = 'none';

	if(webYes.checked == true || (rssYes.checked == true && marketNo.checked == false)){
		$('pageHeader').innerHTML = 'Step 2 - Tell us about your site...';
		inventoryStep2.style.display = 'block';
	}else{
		$('pageHeader').innerHTML = 'Step 1 - Where do you want to publish ads?';
		inventoryStep1.style.display = 'block';
	}
}



function limitRssAdGrouping(changed)
{
	var rss_max_ads 		= $('rss_max_ads');
	var rss_ads_marketplace = $('rss_ads_marketplace');
	var rss_ads_private 	= $('rss_ads_private');

	var total 	= parseInt(rss_max_ads.options[rss_max_ads.selectedIndex].value);
	var market 	= parseInt(rss_ads_marketplace.options[rss_ads_marketplace.selectedIndex].value);
	var priv 	= parseInt(rss_ads_private.options[rss_ads_private.selectedIndex].value);

	if(total == (market + priv)) return;

	if(changed == 'market'){
		if(market > total) market = total;
		priv = total - market;
	}else{
		if(priv > total) priv = total;
		market = total - priv;
	}

	rss_ads_marketplace.selectedIndex = market;
	rss_ads_private.selectedIndex = priv;
}

function reportError(request)
{
	alert('Sorry. There was an error.');
}

function editCartItem (package_id, add, web, rss) {

	if ($('mini_cart_items')) {
		$('mini_cart_items').innerHTML = '<p style="text-align: center; font-size: 1.1em; color: #888;"><strong>Updating Cart... Please wait...</strong><br /><br /><br /></p>';
	} else {
		$('mini_cart').innerHTML = '<div class="mini_cart_header"></div>';
		$('mini_cart').innerHTML += '<div class="box_bottom_white">';
		$('mini_cart').innerHTML += '<center>';
		$('mini_cart').innerHTML += '<strong>Updating Cart... Please wait...</strong><br /><br />';
		$('mini_cart').innerHTML += '</center>';
		$('mini_cart').innerHTML += '</div>';
	}

	var query = '/global.ajax.php';
	var pars = 'action=edit_cart_items&package_id=' + package_id + '&add=' + add + '&web=' + web + '&rss=' + rss;

	var myAjax = new Ajax.Updater(
				{success: 'mini_cart'},
				query,
				{
					method: 'get',
					parameters: pars,
					onFailure: reportError
				});
}

function editCartPost (inventory_post_id, add, link_text) {

	if ($('mini_cart_items')) {
		$('mini_cart_items').innerHTML = '<p style="text-align: center; font-size: 1.1em; color: #888;"><strong>Updating Cart... Please wait...</strong><br /><br /><br /></p>';
	} else {
		$('mini_cart').innerHTML = '<div class="mini_cart_header"></div>';
		$('mini_cart').innerHTML += '<div class="box_bottom_white">';
		$('mini_cart').innerHTML += '<p style="text-align: center; font-size: 1.1em; color: #888;"><strong>Updating Cart... Please wait...</strong><br /><br /><br /></p>';
		$('mini_cart').innerHTML += '</div>';
	}

	new Ajax.Request('/global.ajax.php', {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			$('mini_cart').innerHTML = result;
			try { showSideBar(); } catch(e) { }
		},

		onFailure : function(resp) {},

		parameters : 'action=edit_cart_posts&inventory_post_id=' + inventory_post_id + '&add=' + add + '&link_text=' + link_text

	});

	try { showSideBar(); } catch(e) { }
}

function showTooltip(id, show, text, extra)
{
	var tooltip = getElem(id);

	if(text == 'web_links'){
		tooltip.innerHTML = 'You can opt into your text link appearing on the actual website mentioned in this listing for an additional fee that will show up when you click the box.';
		tooltip.style.top = '230px';
		tooltip.style.left = '358px';
	}else if(text == 'rss_links'){
		tooltip.innerHTML = 'Your rss ad will appear exclusively on the bottom of one out of every '+extra+' posts from this blog for a 30 day period.';
		tooltip.style.top = '220px';
		tooltip.style.left = '280px';
	}else if(text == 'web_links_cart'){
		tooltip.innerHTML = 'You can opt into your text link appearing on the actual website mentioned in this listing for an additional fee that will show up when you click the box.';
		tooltip.style.top = '220px';
		tooltip.style.left = '161px';
	}else if(text == 'rss_links_avail'){
		tooltip.innerHTML = 'Your rss ad will appear exclusively on the bottom of one out of every '+extra+' posts from this blog for a 30 day period.';
		tooltip.style.top = '204px';
		tooltip.style.left = '445px';
	}else if(text == 'rss_links_cart'){
		tooltip.innerHTML = 'Your rss ad will appear exclusively on the bottom of one out of every '+extra+' posts from this blog for a 30 day period.';
		tooltip.style.top = '252px';
		tooltip.style.left = '112px';
	}else if(text == 'num_pages'){
		tooltip.innerHTML = 'This is the approximate number of pages on your website that will be displaying Text Link Ads.';
		tooltip.style.top = '340px';
		tooltip.style.left = '158px';
	}else if(text == 'ad_spots'){
		tooltip.innerHTML = 'This is the total number of Text Link Ads you want sold on your page.';
		tooltip.style.top = '30px';
		tooltip.style.left = '-90px';
	}else if(text == 'rss_max_ads'){
		tooltip.innerHTML = 'This is the total number of rss ads you want running in rotation in your feed.  Note: only one ad will run below each post.  So if you set this number at "6" the most ads running in rotation will be 6 running evenly, one ad per unique post.';
		tooltip.style.top = '20px';
		tooltip.style.left = '-90px';
	}else if(text == 'rss_ads_marketplace'){
		tooltip.innerHTML = 'This is the maximum number of spots in your rss feed that you would like to make available for sale in our marketplace.  You can choose to sell all your ad spots in our marketplace or opt to keep some spots reserved for your own use.  You can also opt to keep your site 100% out of our marketplace and set all spots for private use only.';
		tooltip.style.top = '40px';
		tooltip.style.left = '-90px';
	}else if(text == 'rss_ads_private'){
		tooltip.innerHTML = 'This is the number of links you would like to reserve for your own private use and not offer these spots in our marketplace.  You can choose to reserve some for your own personal use, all, or none, it\'s all up to you!';
		tooltip.style.top = '120px';
		tooltip.style.left = '-90px';
	}else if(text == 'keywords'){
		tooltip.innerHTML = 'Associate keywords to your listing that will help potential advertisers find your listing. Separate your keywords describing your sites with commas.';
		tooltip.style.top = '200px';
		tooltip.style.left = '158px';
	}else if(text == 'site_wide'){
		tooltip.innerHTML = 'You have the option of publishing Text Link Ads on every page of your site, this is "site wide" or just publishing them on the single page you submitted on Step 1.';
		tooltip.style.top = '260px';
		tooltip.style.left = '158px';
	}else if(text == 'package_type'){
		tooltip.innerHTML = 'We offer two types of advertising options: our Text Link Ads for website based static HTML links and our Feedvertising option for ads served on RSS feeds.';
		tooltip.style.top = '30px';
		tooltip.style.left = '85px';
	}else if(text == 'paypal_benefits_aff'){
		tooltip.innerHTML =  'Affiliates electing to receive payment via PayPal will not be charged ANY PayPal fees, and will receive payment promptly on the first day of each month.<br /><br />  Affiliates located outside of the United States are encouraged to select PayPal to avoid having checks slowed down or lost by the postal service.';
		tooltip.style.top = '580px';
		tooltip.style.left = '585px';
	}else if(text == 'paypal_benefits'){
		tooltip.innerHTML =  'Publishers electing to receive payment via PayPal will not be charged ANY PayPal fees, and will receive payment promptly on the first day of each month.<br /><br />  Publishers located outside of the United States are encouraged to select PayPal to avoid having checks slowed down or lost by the postal service.';
		tooltip.style.top = '610px';
		tooltip.style.left = '585px';
	}else if(text == 'post_level'){
		tooltip.innerHTML = 'This will allow you to sell a single text link ad at the bottom of blog posts you have previously written.';
		tooltip.style.top = '260px';
		tooltip.style.left = '158px';
	}




	if(show)
		tooltip.style.display = 'block';
	else
		tooltip.style.display = 'none';
}






function markAlertSeen(alert_id)
{
	new Ajax.Request("global.ajax.php", {
		onSuccess : function(resp) {},
		onFailure : function(resp) {},
		parameters : 'action=mark_alert_seen&alert_id='+alert_id
	});

	$('alert_'+alert_id).style.display = 'none';
}



function showOldAlerts()
{

	new Ajax.Request("global.ajax.php", {

		onSuccess : function(resp) {
			var result = getAjaxResponseVal(resp);
			$('old_alerts').innerHTML = result;
		},

		onFailure : function(resp) {
			$('old_alerts').innerHTML = "<p>We're sorry, but your old alerts cannot be retrieved at this time. Please try again later</p>";
		},

		parameters : 'action=get_old_alerts_html'

	});

}