
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(page_num)
{
	var query_elem = $('inlinks_query');
	var query = trim(query_elem.value);

	if(query == '') return;

	$('inlink_results').innerHTML = '<p style="font-size: 1.15em; color: #888;" align="center"><img src="/images/ajax_spinner.gif" /><br /><br />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;
		},

		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>';
		},

		parameters : 'action=search_inlinks&query=' + query + '&page=' + page_num + '&group_by_domain=' + $('group_by_domain').checked

	});

	if(page_num == 0){

		new Ajax.Request("global.ajax.php", {

			onSuccess : function(resp) {
				var result = getAjaxResponseVal(resp);
				$('related_suggestions').innerHTML = result;
			},

			onFailure : function(resp) {
				$('related_suggestions').innerHTML = '';
			},

			parameters : 'action=related_suggestions&query=' + query

		});

	}


	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 if(result == 'blocked'){
				removeDomainWarning();
				showDomainBlocked();
			}else{
				removeDomainBlocked();
				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 showDomainBlocked()
{
	$('domain_row').style.backgroundColor = '#ecc';
	$('site_url_note').style.display = 'none';
	$('domain_blocked').style.display = 'block';
	$('continueStep1').disabled = false;
	$('continueStep1').value = 'Continue';
}


function removeDomainBlocked()
{
	$('domain_row').style.backgroundColor = '';
	$('domain_blocked').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 editCartWishlistItem (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_wislist_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 editHotList (package_id, add) {

	var query = '/global.ajax.php';
	var pars = 'action=edit_hotlist&package_id=' + package_id + '&add=' + add;

	var myAjax = new Ajax.Updater(
				{},
				query,
				{
					method: 'get',
					parameters: pars,
					onFailure: reportError
				});
}




function editCartPost (inventory_post_id, add, link_text, link_url) {

	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 + '&link_url=' + link_url

	});

	try { showSideBar(); } catch(e) { }
}

function selectMultiplePosts(ids_string, check)
{
	var ids = ids_string.split(',');
	var num = ids.length;
	for(var i = 0; i < num; i++){
		$('chk-'+ids[i]).checked = check;
	}
}

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';
	}else if(text == 'manage_posts'){
		tooltip.innerHTML = 'The <strong>Update Posts</strong> button will update your posts to be available or unavailable to the advertisers in the marketplace.  This button will <strong>not</strong> activate our spiders to re-crawl your site.  The spiders are systematically scheduled to crawl your site on a schedule.<br /><br />If you want a post to <strong>not</strong> be available for an advertiser to buy an advertisement on, please <strong>uncheck</strong> the box next to the post title.  By default, all posts will be available unless you uncheck the box and click Update Posts.';
		tooltip.style.top = '-187px';
		tooltip.style.left = '111px';
	}else if(text == 'cache_explain'){
		tooltip.innerHTML = 'Is this page actively crawled by the major search engines and currently in their index.';
		tooltip.style.top = '-65px';
		tooltip.style.left = '375px';
	}




	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'

	});

}


function searchBoxTip(input, show)
{
	if(show && input.value == ''){
		input.value = 'Enter Keywords Here';
	}else if(!show && input.value == 'Enter Keywords Here'){
		input.value = '';
	}
}


function premiumUserAgree()
{
	new Ajax.Request("global.ajax.php", {
		onSuccess : function(resp) {},
		onFailure : function(resp) {},
		parameters : 'action=premium_agree'

	});

	return false;
}


function checkWidgetName()
{
	var name = $('widget_name');
	if(name){
		var title = name.value.toLowerCase();
		
		if(	!isAlphaNumeric(title) ||
			isFirstCharNumeric(title) || 
			title == ''	||
			title == 'sponsored links' || 
			title == 'paid ads' || 
			title == 'inlinks' || 
			title == 'in links' || 
			title == 'tla inlinks' || 
			title == 'tlainlinks' ||
			title == 'tla-inlinks' ||  
			title == 'ads' || 
			title == 'text link ads' || 
			title == 'sponsors' || 
			title == 'tla' || 
			title == 'advertisements'
		){
			alert("Please choose a different title. We suggest your website title.\n\nNote: Your title should be alpha-numeric and cannot begin with a number.");
			return false;
		}else{
			return true;
		}
	}
	
	return true;
}

function isFirstCharNumeric(str)
{
	var chr = str.charCodeAt(0);
	if(chr >= 48 && chr <= 57) return true;
	return false;
}


function isAlphaNumeric(str)
{
	for(var i = 0; i < str.length; i++){
		var chr = str.charCodeAt(i);
		if( chr == 32 || (chr >= 48 && chr <= 57) || (chr >= 65 && chr <= 90) || (chr >= 97 && chr <= 122) ) continue;
		return false;
	}
	return true;
}
