// The Spotlight Project: targeting functions (c)2006 Solideco LLC

function createXmlHttpObj()
{
   if (window.XMLHttpRequest)                 // Mozilla, etc.
   {
      xmlHttpObj = new XMLHttpRequest()
      xmlHttpObj.abort;
   }
   else if (window.ActiveXObject)             // IE
   {
      xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP")
   }

   return xmlHttpObj;
}

function uncheckAllCB(obj)
{
	var allCB;
	
	allCB = document.getElementById("targetTypeAllCB");
	if (obj.checked == true && allCB != obj)
		allCB.checked = false;
}

function clearList(list)
{
	while (list.length)
		list[0] = null;
}

function listContainItem(list, item)
{
	var i;
	
	for (i = 0; i < list.length; i++)
	{
		if (list[i].text == item.text && list[i].value == item.value)
			return true;
	}
	
	return false;
}

function checkListLength(list)
{
	var limit = 10;
	
	if (list.length > limit)
	{
		alert('The "Selected Targets" list it limited to 10 media contacts.');
		return false;
	}
	else
		return true;
}


function addFromList()
{
    var i, nSrc, nDst, flag;
	
	//alert ("addFromList()");
	
	srcList = document.forms[0]["srcTargetList"];
	dstList = document.forms[0]["dstTargetList"];
	
	nSrc = srcList.length;
	nDst = dstList.length;

	if (nSrc == 0)
	{
		alert("Specify target criteria and click 'Update'.");
		return false;
	}

	flag = 0;	
	for (i = 0; i < nSrc; i++)
	{
		if (srcList[i].selected)
		{
			flag = 1;
			
			if ( ! listContainItem(dstList, srcList[i]))
			{
				dstList.options[nDst] = new Option(srcList[i].text, srcList[i].value);
				dstList.options[nDst].title = srcList.options[i].title;
				nDst++;
			}
		}
	}
	
	if (flag == 0)
		alert("Please select one or more 'Potential Targets'.");
	else
		checkListLength(dstList);

	return true;
}

function addFromCustom()
{
   //alert("addFromCustom()");
   
   // dstTargetList
}

function remove()
{
	var i, flag;
	var dstList;
	
	dstList = document.forms[0]["dstTargetList"];
	if (dstList.length == 0)
	   return false;

	flag = 0;
    for (i = (dstList.length - 1); i >= 0; i--)
    {
       if (dstList[i].selected)
	   {
          dstList[i] = null;
		  flag = 1;
       }
    }

	if (flag == 0)
	{
		alert("Please highlight Selected Targets to remove.");
		return false;
	}
}

function clearDstList(obj)
{
   clearList(document.forms[0]["dstTargetList"]);
}

function getFormData(objName)
{
	var i, elements, str;
	var srcList;
	
	elements = document.forms[0].elements;
	
	str = "";
	
	for (i = 0; i < elements.length; i++)
	{
		if (elements[i].name == objName)
		{
			if ((elements[i].type == "checkbox" || elements[i].type == "radio") &&
		        elements[i].checked)
			{
				if (str.length)
					str = str + ",";
				str = str + elements[i].value;
			}
			if (elements[i].type == "text")
			{
				if (str.length)
					str = str + ",";
				str = str + elements[i].value;
			}
		}
	}
	str = objName + "=" + str;
	
	return str;
}

function filterChange()
{
	var postData;
	var url;
	var lines;
	var srcList;
	
	//alert("filterChange()");
   
   regionObj = document.getElementById("range_ZipCodeState");
   region = regionObj.value;

   if (region.length != 0 && region.length != 2 && region.length != 5)
   {
	   alert("You must enter a two letter state code or five digit zip code.");
	   return false;
   }
   
   postData = getFormData("targetType");
   postData = postData + "&" + getFormData("range");
   postData = postData + "&" + getFormData("region");
   postData = postData + "&" + getFormData("orgSize");
   postData = postData + "&" + getFormData("orgName");
   postData = postData + "&" + getFormData("contactName");

   url = "getTargetList2.php" + "?" + postData;

//   alert(url);

   xmlHttpObj = createXmlHttpObj();

   xmlHttpObj.open('get', url, false);

   xmlHttpObj.send("");

//   alert("post send");
//   alert(xmlHttpObj.status);
//   alert(xmlHttpObj.statusText);

   document.body.setAttribute("style", "cursor:wait;");
   document.forms[0]["srcTargetList"].setAttribute("style", "cursor:wait;") 
   document.forms[0]["dstTargetList"].setAttribute("style", "cursor:wait;") 

   if (xmlHttpObj.status == 200)
   {
      //alert(xmlHttpObj.responseText);

      document.body.setAttribute("style", "cursor:wait;");
      document.forms[0]["srcTargetList"].setAttribute("style", "cursor:wait;") 
      document.forms[0]["dstTargetList"].setAttribute("style", "cursor:wait;") 

      lines = xmlHttpObj.responseText.split("\n");
      srcList = document.forms[0]["srcTargetList"];
      clearList(srcList);
	  
	  for (i = 0; i < lines.length; i++)
	     if (lines[i].length)
		 {
		 	ary = lines[i].split("\t");
            srcList.options[i] = new Option(ary[1], ary[0]);
			srcList.options[i].setAttribute("title", ary[1]);
			srcList.options[i].setAttribute("class", ary[2]);
         }
   }
   else
      alert(xmlHttpObj.statusText);
	  
   document.body.setAttribute("style", "cursor:default;");
   document.forms[0]["srcTargetList"].setAttribute("style", "cursor:default;") 
   document.forms[0]["dstTargetList"].setAttribute("style", "cursor:default;") 
}

function submitAimForm(formObj)
{
   var i, x;
	
   dstList = formObj["dstTargetList"];
   if (dstList.length == 0)
   {
      alert("There are no items in the 'Selected Targets' list.\n\nAdd some by selecting items in the 'Potential Targets' list and pressing 'Add'.");
	  return false;
   }

   if (checkListLength(dstList))
   {
      x = "";
      for (i = 0; i < dstList.length; i++)
      {
         if (x.length)
         x = x + ",";
         x = x + dstList[i].value;
      }

      formObj["targets"].value = x;
      formObj.submit();
   }
}


function toggleSearchOpts(x)
{
	var searchOptToggle = document.getElementById('searchOptToggle');
	var extraSearchOpts = document.getElementById('extraSearchOpts');

	if (extraSearchOpts.style.display == 'none') {
		extraSearchOpts.style.display = 'block';
		searchOptToggle.innerHTML = 'less &laquo;';
	} else {
		extraSearchOpts.style.display = 'none';
		searchOptToggle.innerHTML = 'more &raquo;';

		// If the extra search options pane is being collapsed
		// then reset the form field to the original state
		// to ensure that they fields do not contain values
		// without the user knowing.
		//
		document.forms["aimForm"].elements[9].checked = true;
		document.forms["aimForm"].elements[11].value = "";
		document.forms["aimForm"].elements[12].value = ""
	}

	// cancel onclick event
	return false;	
}

