
function toggleLayer(whichLayer)
{
   if (document.layers){
   		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
   }
   if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
   }
   if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
   }
	style2.display == "none" ? style2.display = "block":style2.display = "none";
}
	
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function guessCompany(e) {
	nameField = $("#name").val();
	keyId = (window.event) ? event.keyCode : e.keyCode;
	if (keyId == 38 || keyId == 40) {
		if(keyId == 38) {
			if(compindex == 0 || compindex == -1) {
				compindex = complength - 1;
			} else {
				compindex--;
			}
		} else if(keyId == 40) {
			if(compindex == complength-1) {
				compindex = 0;
			} else {
				compindex++;
			}
		}
		$("#companySuggest").children().each(function(i) {
			if(i == compindex) {
				$("#name").val($(xdoc).find("company:eq("+i+")").find("name").text());
				this.className = "company_selected";
			} else {
				this.className = "company";
			}
		});
	} else if (keyId == 13) {
		$("#company_"+compindex).trigger('click');
		$("#companySuggest").hide();
		$("#companySuggest").html("");
		return false;
	} else {
		if (nameField.length > 1) {
			$.ajax({
				type: "GET",
				url: "/includes/guessCompany.php",
				data: {guess: nameField},
				dataType: "text",
				success: function(xml) {
					try {
						xdoc = new ActiveXObject("Microsoft.XMLDOM");
						xdoc.async = "false";
						xdoc.loadXML(xml);
					} catch (e) {
						try {
							parser=new DOMParser();
							xdoc=parser.parseFromString(xml,"text/xml");
						} catch (e) { alert(e.message) }
					} // try
					compindex = -1;
					complength = $(xdoc).find("company").length;
					if (complength == 0) {
						$("#companySuggest").hide();
					} else {
						$("#companySuggest").html("");
						$(xdoc).find("company").each(function(i) {
							c_name = $(this).find("name").text();
							c_city = $(this).find("city").text();
							c_state = $(this).find("state").text();
							title = c_name + " - <i>" + c_city + ", " + c_state + "</i>";
							$('<div class=\"company\" id=\"company_' + i + '\"></div>').html(title).appendTo("#companySuggest");
							$("#company_"+i).click( function () {
								k = $(this).attr("id").split("_")[1];
								$("#name").val($(xdoc).find("company:eq("+k+")").find("name").text());
								$("#address").val($(xdoc).find("company:eq("+k+")").find("address").text());
								$("#city").val($(xdoc).find("company:eq("+k+")").find("city").text());
								$("#state").val($(xdoc).find("company:eq("+k+")").find("state").text());
								$("#zip").val($(xdoc).find("company:eq("+k+")").find("zip").text());
								$("#website").val($(xdoc).find("company:eq("+k+")").find("website").text());
								$("#shortname").val($(xdoc).find("company:eq("+k+")").find("shortname").text());
								dscr = $(xdoc).find("company:eq("+k+")").find("description").text();
								msn = $(xdoc).find("company:eq("+k+")").find("mission").text();
								tinyMCE.get("description").setContent(dscr);
								tinyMCE.get("mission").setContent(msn);
							}); // end click
						}); // if ($(xdoc).find("company").length == 0)
						$("#companySuggest").show();
					}
				},
				error: function(xhrq,ats,aerr) {}
			});
		} else {
			$("#companySuggest").hide();
			$("#companySuggest").html("");
		}
	} 
} // function guess company

function popCompany(id) {
	$("#verify_errors").html("");
	$("#verify_errors").html("ID pressed: " + id);
	
	cmp = $(xdoc).find("company")[id].find("address").text();
	$("#address").val(cmp);
}

function showCompanyGuess() {
	if (xmlHttp2.readyState == 4) {
		$("#companySuggest").show();
		$("#companySuggest").html(xmlHttp2.responseText);
	}
}

function hideGuess() {
	compindex = -1;
	//setTimeout('$("#companySuggest").hide()',100);
	setTimeout('hideAndUnbindGuess()',200);
}

function hideAndUnbindGuess() {
    $("#companySuggest").hide();
    $(".company").each(function() {
	    $(this).unbind('click');
	});
    $(".company_selected").each(function() {
	    $(this).unbind('click');
	});
}
