if (document.images) {

  //img26on=new Image();
  //img26on.src=imgdir + "/tabs/26on.gif";

  //img26off=new Image();
  //img26off.src=imgdir + "/tabs/26.gif";
}

function imgOn(imgName) {
        if (document.images) {
            document[imgName].src = eval(imgName + "on.src");
        }
}

function imgOff(imgName) {
        if (document.images) {
            document[imgName].src = eval(imgName + "off.src");
        }
}

function select_all(field)
{
	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
}

function clear_all(field)
{
	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
}

function check_removed(field)
{
	var str = '';
	// if there is only one element
	// then it doesn't come through as an array
	if (field.length == undefined) {
		if (field.checked==false) { 
			str += field.value + '|'; 
		}
	}
	else {
		for (i = 0; i < field.length; i++) {
			if (field[i].checked == false) {
				str += field[i].value + '|';
			}
		}
	}

	document.main_form.off_values.value = str;
}

function NewWindow(mypage, myname, w, h, scroll) {
  var winl = (screen.width - w) / 2;
  var wint = (screen.height - h) / 4;
  winprops = 'height=' + h + ',width=' + w
       + ',top=' + wint + ',left=' + winl
       + ',scrollbars=' + scroll + ',resizable=yes,status=yes';
  
  win = window.open(mypage, myname, winprops);
  if (win) {
     if (parseInt(navigator.appVersion) >= 4) {
  win.window.focus(); 
     }
  }
} 

function popUp(url, width, height)
{ 
	if (width == undefined) { width = 500; }
	if (height == undefined) { height = 520; }

	var newWin = window.open(url,'win',
	"toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=" 
	+ width + ",height=" + height);
	newWin.creator = self;
  newWin.focus();
}

function get_text(field)
{
  var url = 'thesaurus.tt2';
  url += '?type=';
  url += field;
  url += '&input=';

  for(var i = 0; i < document.main_form.length; i++)
  {
    var e = document.main_form.elements[i];
    if ( e.type== "text" && e.name == field )
    {
      url += e.value;
    }
  }

  return url;
}

function set_text(field, text_value)
{
  for(var i = 0; i < document.main_form.length; i++)
  {
    var e = document.main_form.elements[i];
    if ( ((e.type== "text") || (e.type == 'textarea')) && e.name == field )
    {
      e.value = text_value;
    }
  }
}

// figure out if a check box is checked or unchecked
// before we pop a company detail
function get_state(url, element_id)
{
	var state = '';
	if (document.main_form.companies[element_id].checked == true)
	{
		state = 'checked';
	}

	return url + "&status=" + state + "&element_id=" + element_id;;
}

function set_state(element_id)
{
	if (document.main_form.company.checked == true)
	{
		window.opener.document.main_form.companies[element_id].checked = true;
	}
	else 
	{ 
		window.opener.document.main_form.companies[element_id].checked = false;

	}
}

// Special HREF function to make sure submits happen
function cs_href(f, url) {
   if (void 0 == f) {
      alert("No form object passed to cs_href");
      return;   
   }
  
   if (f.action == "") { 
      alert("Form has no ACTION properity and will not work in IE");
   }
  
   // weird, look for the form index
   var trg = -1;
   for (var i=0; i < document.forms.length; i++) {
      if (document.forms[i].name == f.name) {
         trg = i; 
         break;
      }
   }

   if (trg == -1) {
     alert("Couldn't find the form");
   } else {
     // alert("Form index is " + trg);
     document.forms[trg].redirect.value = url;
     document.forms[trg].submit();
   }
}

function open_url(url)
{
	opener.location.href=url;
	self.close();
}
// ***************** //
// AJAX methods
// ***************** //

function limit_alert() {
	var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open("GET", '/perl/get_selected_count', false);
	xmlhttp.send(null);
	var results = xmlhttp.responseText.split("\|");
	if (results[1])	{
		alert(results[1]);
  }
}

function update(field) {

  // for this field, determine what's checked and unchecked

  var str = '';

	if (field.type == 'select-one') {
		if (field.name == 'ownership') {
			str += 'keywords|ownership|' + document.main_form.ownership.selectedIndex;
		}
		else if (field.name == 'founded') {
			str += 'keywords|founded|' + document.main_form.founded.selectedIndex;
		}
		else if (field.name == 'site_type') {
			str += 'keywords|site_type|' + document.main_form.site_type.selectedIndex;
		}
    else if (field.name == 'order_by') {
      str += 'order_by|' + document.main_form.order_by.selectedIndex;
    }
	}
	else if (field.type == 'text') {
		if (field.name == 'express_locations') {
			str = 'locations' + '|' + field.name + '|' + escape(field.value);
		} else {
			str = 'keywords' + '|' + field.name + '|' + escape(field.value);
		}
	}
	else {
  	if (field.length == undefined) {

			if ( field.name == 'records_with_web' ) {
				str += 'keywords|';
			}
			if ( field.name == 'records_with_email' ) {
				str += 'keywords|';
			}
			if ( field.name == 'records_with_fax' ) {
				str += 'keywords|';
			}
			if ( field.name == 'records_with_contacts' ) {
				str += 'keywords|';
			}
			
    	str += field.name + '|';
    	if (field.checked==false) {
      	str += '~' + field.value + '|';
    	}
    	else {
      	str += field.value + '|';
    	}
  	}
  	else {
    	str += field[0].name + '|';
    	for (i = 0; i < field.length; i++) {
      	if (field[i].checked == false) {
        	str += '~' + field[i].value + '|';
      	}
      	else {
        	str += field[i].value + '|';
      	}
    	}

    	if (field[0].name == 'companies') {

    	  str += '?select_all_pages=' + document.main_form.select_all_pages.value + 
				'?clear_all_pages=' + document.main_form.clear_all_pages.value;
    	}

  	}

	} // else field type

  // New XMLHttpRequest object
  try {

    // Moz supports XMLHttpRequest. IE uses ActiveX.
    // browser detection is bad. object detection works for any browser
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
    //alert("This browser doesn't support AJAX");
    // browser doesn't support ajax. handle however you want
  }

  // the xmlhttp object triggers an event everytime the status changes
  // triggered() function handles the events
  xmlhttp.onreadystatechange = triggered;
  // open takes in the HTTP method and url.
  xmlhttp.open("GET", '/perl/ajax?string=' + str);
  // send the request. if this is a POST request we would have
  // sent post variables: send("name=aleem&gender=male)
  // Moz is fine with just send(); but
  // IE expects a value here, hence we do send(null);
  xmlhttp.send(null);
}

function update_intern(field) {

  // for this field, determine what's checked and unchecked

  var str = '';

  if (field.type == 'select-one') {

    if (field.name == 'major') {
      str += 'major|' + document.main_form.major.selectedIndex;
    }
    else if (field.name == 'compensation') {
      str += 'compensation|' + document.main_form.compensation.selectedIndex;
    }
    else if (field.name == 'hours') {
      str += 'hours|' + document.main_form.hours.selectedIndex;
    }
    else if (field.name == 'timeframe') {
      str += 'timeframe|' + document.main_form.timeframe.selectedIndex;
    }
    else if (field.name == 'state') {
      str += 'state|' + document.main_form.state.selectedIndex;
    }
    else if (field.name == 'order_by') {
      str += 'order_by|' + document.main_form.order_by.selectedIndex;
    }
  }
	else if (field.type == 'select-multiple')	{
		str = 'major';
		for (var i = 0; i < document.main_form.major.length; i++) { 
      if (document.main_form.major.options[i].selected) { 
        str += '|' + document.main_form.major.options[i].text; 
      } 
    } 
	}
		
  else if (field.type == 'text') {
    str = field.name + '|' + escape(field.value);
  }
  else {
		if (field.length == undefined) {
      str += field.name + '|';
      if (field.checked==false) {
        str += '~' + field.value + '|';
      }
      else {
        str += field.value + '|';
      }
		} 
		else {
    	str += field[0].name + '|';
    	for (i = 0; i < field.length; i++) {
      	if (field[i].checked == false) {
        	str += '~' + field[i].value + '|';
      	}
      	else {
        	str += field[i].value + '|';
      	}
    	}

    	if (field[0].name == 'companies') {

      	str += '?select_all_pages=' + document.main_form.select_all_pages.value +
      	'?clear_all_pages=' + document.main_form.clear_all_pages.value;
    	}
		}
  }

  try {
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
    // browser doesn't support ajax. handle however you want
  }

  xmlhttp.onreadystatechange = triggered;
  xmlhttp.open("GET", '/perl/ajax_intern?string=' + str);
  xmlhttp.send(null);
}

function triggered() {
  // if the readyState code is 4 (Completed)
  // and http status is 200 (OK) we go ahead and get the responseText
  // other readyState codes:
  // 0=Uninitialised 1=Loading 2=Loaded 3=Interactive
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
    // xmlhttp.responseText object contains the response.
		var results = xmlhttp.responseText.split("\|");

		// in the case of company_detail.tt2 we're refreshing the count 
		// of the opener
		if (results[0] == 'opener_count')	{
			window.opener.document.getElementById("selected_count").innerHTML = results[1];
		}
		else { 
    	document.getElementById(results[0]).innerHTML = results[1];
			// on the SIC/NAICS page both Keywords and Industries can be updated
			if (results[2]) {
    		document.getElementById(results[2]).innerHTML = results[3];
    		//var result = xmlhttp.responseText;
			}
      if (results[4]) {
        document.getElementById(results[4]).innerHTML = results[5];
        //var result = xmlhttp.responseText;
      }
		} // else
  } // if read state

} // end of function

function get_industries(field) {

  var str = '';
  str += 'major|major|' + document.main_form.major.selectedIndex;
  try {
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
    alert("This browser doesn't support AJAX");
  }

  xmlhttp.onreadystatechange = triggered;
  xmlhttp.open("GET", '/perl/tabs_major?string=' + str);
  xmlhttp.send(null);
}

function log_cb(username,client_id,session_id,co_id,cb_id,context,vendor)
{
  var str;

  str = 'username=' + username + '&client_id=' + client_id + '&session_id=' + session_id +
  '&co_id=' + co_id + '&cb_id=' + cb_id +'&context=' + context + '&vendor=' + vendor;

  try {
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
    alert("This browser doesn't support AJAX");
  }

  xmlhttp.onreadystatechange = triggered;
  xmlhttp.open("GET", '/perl/log_cb?' + str);
  xmlhttp.send(null);
}

function log_classic_cs(client_id,username,session_id)
{
  var str;
  
  str = 'client_id=' + client_id + '&username=' + username + '&session_id=' + session_id; 

  try {
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  }
  catch (e) {
    alert("This browser doesn't support AJAX");
  }

  xmlhttp.onreadystatechange = triggered;
  xmlhttp.open("GET", '/perl/log_classic_cs?' + str);
  xmlhttp.send(null); 
} 

function refresh(url)
{
  window.location.href=url;
  window.focus();
}

function check_selected(field, val)
{
	// ***** will tell us if the box in question is checked or not ***** //
	var status = '';
  if (field.checked==false) {
		status = 'false';
  } else { status='true'; }

	// ***** the field are we manipulating in the opener ***** //
  var f = window.opener.document.main_form.companies;

	// ***** two cases, first when there is one result on the page ***** //
	// ***** second when there is more than one checkbox on the result page ***** //
 	if (f.length == undefined) {
		if (status == 'true') { f.checked = true; } 
		else { f.checked = false; }
 	}
	else {
		for (i = 0; i < f.length; i++)	{
			if (f[i].value == val) {
     		if (status == 'true') { f[i].checked = true; }
     		else { f[i].checked = false; }

			} // if

		} // for

	} // else

} // </check selected>



