// Get the parameters to send to update.php
// This should be called by onChange of the sponsors dropdown or the feed type dropdown

function updateSites() {
	var sField = document.forms['feeds']['feedOptions'];
	var s = sField.options[sField.selectedIndex].value;
	var tField = document.forms['feeds']['feedType'];
	var params = "s=" + s;
	if (tField.options[tField.selectedIndex].value == 'pst') {
		params = params + "&p=true";
	}
	// ajaxRead(params);
}

function ajaxRead(params){
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Unable to process Ajax");
				return false;
			}
		}
	}
	// Here's where we do the thing...
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var options = ajaxRequest.responseText;
			var wField = document.forms['feeds']['feedSite'];
			if(options){
				wField.innerHTML = options;
			}	else {
				options = "<option value=''>No Sites Available</option>";
			}
			// IE has a bug that causes the first in a group of options to be truncated when using innerHTML.
			// This provides a workaround (see http://muffinresearch.co.uk/archives/2006/07/19/bug-when-creating-select-options-using-innerhtml-in-ie/)
			if(document.all) {
				options = '<option>truncatethis</option>' + options;
			}
			// End IE hack part 1
			wField.innerHTML = options;
			// IE hack part 2
			if(document.all) { // Or any IE testing
				wField.outerHTML = wField.outerHTML;
			}
			// End IE hack part 2
		}
	}
//	file = "/webmasters/aff-files/update.php?" + params;
	file = "aff-files/update.php?" + params;
	ajaxRequest.open("GET", file, true);
	ajaxRequest.send(null);
}

// Update the feed link
//function updateFeedLink() {

function updateFeedLink(id) {
	var fForm = document.forms['feeds'];
	var tField = fForm['feedType'];
	var t = tField.options[tField.selectedIndex].value;
	var oField = fForm['feedOptions'];
	var o = oField.options[oField.selectedIndex].value;

	var link = 'http://www.nscash.com/rss-feed.php?t=' + t + ';o=' + o + ';w=' + id;
	var linkField = document.forms['feeds']['feedLink'];
	linkField.value = link;
	return link;
}
/*
function updateFeedLink(id) {
	//var id = <?php echo $webmaster->id; ?>;
	var fForm = document.forms['feeds'];
	var tField = fForm['feedType'];
	var t = tField.options[tField.selectedIndex].value;
	var sField = fForm['feedSponsor'];
	var s = sField.options[sField.selectedIndex].value;
	var wField = fForm['feedSite'];
	var w = wField.options[wField.selectedIndex].value;
	var cField = fForm['feedCamp'];
	var c = cField.options[cField.selectedIndex].value;
	var nField = fForm['feedLimit'];
	var n = nField.value;
	var link = 'http://syndication.nscash.com/feed.php?pid=' + id + "&doc=" + t;
	if (s) {
		// Prevent creation of the PS param if sponsor has no sites with teasers
		var wVal = wField.options[wField.selectedIndex].text;
		if(wVal != 'No Sites Available') {
			link = link + "&PS=" + s;
		}
	}
	if (w) {
		link = link + "&sid=" + w;
	}
	if (c) {
		link = link + "&c=" + c;
	}
	if (n) {
		if (n > 50) {
			n = 50;
			// Update the # of Entries field
			nField.value = n;
		}
		link = link + "&n=" + n;
	}
	var linkField = document.forms['feeds']['feedLink'];
	linkField.value = link;
	return link;
}
*/

function testFeedLink(id) {
	var testLink = updateFeedLink(id);
	if (testLink) {
		window.open(testLink);
	}
}

function setDisplay(section, button) {
	var thisSection = document.getElementById(section);
	var thisButton = document.getElementById(button);
	if (thisSection.style.display == "none") {
		thisSection.style.display = "block";
		thisButton.innerHTML = '<span style="font-size:9px;"><img src="/nsicons/arrow_up.png" title="Close this section" alt="Close" style="border:none;"/></span>'
	} else {
		thisSection.style.display = "none";
		thisButton.innerHTML = '<span style="font-size:9px;"><img src="/nsicons/add.png" title="Open this section" alt="Open" style="border:none;"/></span>';
	}
	writeHiddenField();
}

function updatetours( id ) {
  var site = $("#linksite").val();
  if( site > 0 ) {
    $("#linktours").removeOption(/./);
//    $("#linktours").ajaxAddOption("/webmasters/rpc/getTours.php?s="+site, null, false,
    $("#linktours").ajaxAddOption("rpc/getTours.php?s="+site, null, false,
      function() {
        $("#linktours").val('main'); updatelinkcode( id );
      }
    );
  } else {
    $("#linktours").removeOption(/./);
    $("#linktours").addOption({'none':'Select Site First'});
    updatelinkcode( id );
  }
}

function updatelinkcode( id ) {
  var site = $("#linksite").val();
  var tour = $("#linktours").val();
  var prog = $("#linkprogram").val();
  var camp = $("#linkcampaign").val();

  if( site == 0 || site == "") {
    $("#linkingcode").val("");
    return link;
  }

  if( tour == 'none' ) { tour = 'main'; }

  var link = "http://www.nscash.com/hit.php?w="+id+"&s="+site+"&p="+prog+"&t="+tour;
  if( camp > 0 ) link = link+"&c="+camp;

  $("#linkingcode").val(link);

  return link;
}

function testlinkcode() {
  var link = $("#linkingcode").val();
  if( link ) {
    window.open( link );
  }
}

