var xmlHttp;

function foundInterest(showWhat) {
	clearOrganization();
	if (showWhat == '') {
		foundSchoolFindRep('','');
	} else if (showWhat == 'none') {
		
	} else {
		document.getElementById(showWhat).style.display='block';
	}
}

function updateType(theDiv) {
	elements = document.getElementsBySelector('.inst_type_div');
	for (i=0; i < elements.length; i++) {
		elements[i].style.display = 'none';
	}
	
	if (theDiv != 'none') {
		document.getElementById(theDiv).style.display = 'block';
	}
}

function handlePIDEnter (field, event) {
		var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
		if (keyCode == 13) {
			var i;
			for (i = 0; i < field.form.elements.length; i++)
				if (field == field.form.elements[i])
					break;
			i = (i + 1) % field.form.elements.length;
			// field.form.elements[i].focus();
			return false;
		}
		else
		return true;
}

function noSchool() {
	document.getElementById('pid_results_div').style.display='none';
	document.getElementById('no_school_div').style.display='block';
}

function homeSchool() {
	document.getElementById('pid_results_div').style.display='none';
	document.getElementById('no_school_div').style.display='block';
}

function foundSchool() {
	document.getElementById('pid_results_div').style.display='none';
	document.getElementById('pid_display').style.display='block';
	document.getElementById('pid_display').innerHTML=document.getElementById('pid').options[document.getElementById('pid').selectedIndex].innerHTML;
}

function foundSchoolFindRep(thePidStateZipCombo, theCmpy) {
	// alert(thePidStateZipCombo);
	var thePidStateZipCombo = thePidStateZipCombo.split("*");
	var thePid=thePidStateZipCombo[0];
	var theState=thePidStateZipCombo[1];
	var theZip=thePidStateZipCombo[2];
	
	document.getElementById('pid_results_div').style.display='none';

	xmlHttp2=GetXmlHttpObject()

	if (xmlHttp2==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="/admin/ajax/rep_finder_query.cfm";
	url=url+"?pid="+thePid;
	url=url+"&cmpy_cd="+theCmpy;
	url=url+"&state="+theState;
	url=url+"&zip="+theZip;
		
	xmlHttp2.onreadystatechange=repStateChanged;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}

function repStateChanged() {
	if (xmlHttp2.readyState == 1 || xmlHttp2.readyState == 2 || xmlHttp2.readyState == 3) {
		document.getElementById("no_school_div").style.display="none";
		document.getElementById("rep_loading_box").style.display="block";
		document.getElementById("pid_results_div").style.display="none";
	}
	if (xmlHttp2.readyState==4) {
		document.getElementById("no_school_div").style.display="none";
		document.getElementById("rep_loading_box").style.display="none";
		document.getElementById("rep_results").style.display="block";
		document.getElementById("rep_results_details").innerHTML = '';
		var xmlDoc2=xmlHttp2.responseXML.documentElement;
		displayReps(xmlDoc2);
	}
}

function displayReps(xmlDoc2) {
	var reps = null;
	var reps = new Array();

	// document.getElementById("rep_results_ul").innerHTML = "";
	document.getElementById("rep_results").innerHTML = "";
	document.getElementById("rep_results_details").innerHTML = "";
	
	if (xmlDoc2.attributes.getNamedItem("repstatus").value == 3) {
		// document.getElementById("rep_results_ul").innerHTML = "<li style='margin-bottom: 3px;'><p>We were unable to locate your sales representative. Please call " + xmlDoc2.attributes.getNamedItem("default_number").value + " for assistance.</p></li>";
		document.getElementById("rep_results").innerHTML = "<div class='rep_details_div' style='margin-left: 40px; display: block;'><p><b>" + xmlDoc2.attributes.getNamedItem("display_label").value + "</b><br/ >" + xmlDoc2.attributes.getNamedItem("default_contact_name").value + "<br />" + xmlDoc2.attributes.getNamedItem("default_number").value + "</p></div>";
	} else {
		for (i=0;i<xmlDoc2.childNodes.length;i++) {
			var thisNode = xmlDoc2.getElementsByTagName("rep")[i];
			var full_name = thisNode.attributes.getNamedItem("fname").value + " " + thisNode.attributes.getNamedItem("lname").value;
			var title = thisNode.attributes.getNamedItem("title").value;
			var email = thisNode.attributes.getNamedItem("email").value;
			var geography = thisNode.attributes.getNamedItem("geography").value;
			var display_label = xmlDoc2.attributes.getNamedItem("display_label").value;

			reps[i] = "<div class='rep_details_div' id='rep" + eval(i+1) + "_details' style='margin-left: 40px; display: block;'>";
				// reps[i] += "<img src='http://www.k12pearson.com/rep_finder_old/rep_photo/uadamc2.jpg' style='float: left; margin-right: 5px;' />";
				reps[i] += "<p><b>" + full_name + "</b><br />" + title + "<br />" + display_label + "</p>";
				reps[i] += "<p><a href='mailto: " + email + "'>" + email + "</a>";

			for (j=0;j<thisNode.childNodes.length;j++) {
				var thisDevice = thisNode.getElementsByTagName("device")[j];
				reps[i] += "<br />" + thisDevice.attributes.getNamedItem("name").value + ": " + thisDevice.attributes.getNamedItem("number").value;
			}

			reps[i] += "</p>";
			reps[i] += "</div>";

			// <div style="width: 325px; margin-top: 35px; background: #feefac; height: 20px; text-align: center; color: #0000cc; font-size: 8px;"><p style="padding-top: 3px;">TEACH & LEARN | ASSESS & INFORM | DEVELOP & LEAD</p></div>
			// document.getElementById("rep_results_ul").innerHTML += "<li style='clear: both; padding-bottom: 5px;'><a onclick='return false;' href='#'>" + full_name + "</a><br />" + title + "<br />" + geography + "</li>";
			// <img src='http://www.k12pearson.com/rep_finder_old/rep_photo/uadamc2.jpg' style='float: left; margin-right: 3px;' />
			document.getElementById("rep_results_details").innerHTML += reps[i];
			document.getElementById('rep_results').innerHTML = document.getElementById("rep_results_details").innerHTML;
		}
	}
}

function showTip(rep) {
	TagToTip('rep'+rep+'_details', WIDTH, 325, FADEIN, 500, FADEOUT, 400, FOLLOWMOUSE, false, CLOSEBTN, true, CLOSEBTNTEXT, 'Close', CLOSEBTNCOLORS, ['', '#0000cc', '', '#0000cc'], STICKY, true, BORDERCOLOR, '#feefac', BGCOLOR, '#ffffff');
}

function checkSchoolFinder() {
	if (document.getElementById("pid") && document.getElementById("pid").value != '') {
		return true;
	} else {
		alert('Please enter either a zip code or a city.');
		return false;
	}
}

function clearOrganization() {
  // clear school stuff
  document.getElementById("no_school_div").style.display="none";
  document.getElementById("loading_box").style.display="none";
  document.getElementById("pid_results_div").style.display="none";
  document.getElementById("pid").options.length = 0;
  document.getElementById('pid_display').style.display='none';
  document.getElementById('pid_display').innerHTML='';

  // clear rep stuff
  document.getElementById("rep_results").style.display="none";
  document.getElementById("rep_results_details").innerHTML = "";
  
  return;
}

function showOrganization(str, searchType, theDiv) {	
	if (str.length==0) { 
		document.getElementById("no_school_div").style.display="none";
		document.getElementById("loading_box").style.display="none";
		document.getElementById("pid_results_div").style.display="none";
		document.getElementById("pid").options.length = 0;
		return;
	} else {
		document.getElementById(theDiv).style.display="block";
	}

	xmlHttp=GetXmlHttpObject()

	if (xmlHttp==null) {
	  alert ("Your browser does not support AJAX!");
	  return;
	}

	var url="/admin/ajax/organizationlist.cfm";
	url=url+"?q="+str;
	url=url+"&c="+searchType;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function stateChanged() {
	if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2 || xmlHttp.readyState == 3) {
		document.getElementById("no_school_div").style.display="none";
		document.getElementById("loading_box").style.display="block";
		document.getElementById("pid_results_div").style.display="none";
	}
	if (xmlHttp.readyState==4) {
		document.getElementById("no_school_div").style.display="none";
		document.getElementById("loading_box").style.display="none";
		document.getElementById("pid_results_div").style.display="block";
		var xmlDoc=xmlHttp.responseXML.documentElement;
		document.getElementById("pid").options.length = 0;
		document.getElementById("pid").options[0] = new Option('I don\'t see my school.', '');
		document.getElementById("pid").options[1] = new Option('I am a homeschool teacher.', '');
		
		// document.write(xmlHttp.responseText);

		for (i=0;i<xmlDoc.childNodes.length;i++) {
			document.getElementById("pid").options[i+2] = new Option(xmlDoc.getElementsByTagName("organization")[i].childNodes[0].nodeValue + ', ' + xmlDoc.getElementsByTagName("organization")[i].attributes.getNamedItem("state").value, xmlDoc.getElementsByTagName("organization")[i].attributes.getNamedItem("pid").value + '*' + xmlDoc.getElementsByTagName("organization")[i].attributes.getNamedItem("state").value + '*' + xmlDoc.getElementsByTagName("organization")[i].attributes.getNamedItem("zip").value);
		}
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}








