// JavaScript for many fb_*.php pages

	// definitions / constants
	var getElem = false;
	var getLayer = false;
	if (document.getElementById) {
		getElem = true;
	}
	else if (document.layers) {
		getLayer = true;
	}
	else {
		getElem = true;
	}
	//var ns4 = (document.layers)? true:false
	//var ie4 = (document.all)? true:false
	var ProductNone = 0;
	var ProductInstaller = 1;
	var ProductTextbook = 2;
	var ProductVirtualLab = 3;
	var ProductAlgebraBook = 4;
	var ProductWebSite = 5;
	var origProduct = 0;

	var InquiryNone = 0;
	var InquiryQuestion = 1;
	var InquiryTechnical = 2;
	var InquiryPhysics = 3;

	// no longer needed
	// window.onload=theOnLoadStuff;

// hides or displays the chapter select based upon if the the Textbook is selected

//function theOnLoadStuff(){
	//frc: May-2006 removed some code that couldn't be global.
//}

function showChapter() {

	origProduct = document.errform.product.selectedIndex;

	// if using layers
	if (getLayer) {
		// Textbook option
		if (document.errform.product.selectedIndex == ProductTextbook) {
			if (origProduct == ProductAlgebraBook) {CheckSubmit(0);}
			document.layers['chapterselection'].visibility='visible';
			document.layers['vlselection'].visibility='hidden';
		}
		if (document.errform.product.selectedIndex == ProductAlgebraBook) {
			if (origProduct == ProductTextbook) {CheckSubmit(0);}
			document.layers['chapterselection'].visibility='visible';
			document.layers['vlselection'].visibility='hidden';
		}
		// Virtual Lab option
		if (document.errform.product.selectedIndex == ProductVirtualLab) {
			document.layers['chapterselection'].visibility='hidden';
			document.layers['vlselection'].visibility='visible';
		}
		// Other options
		if ( (document.errform.product.selectedIndex == ProductNone) || (document.errform.product.selectedIndex == ProductWebSite) ) {
			document.layers['chapterselection'].visibility='hidden';
			document.layers['vlselection'].visibility='hidden';
		}
	}

	// if using div tags
	if (getElem) {
		// Textbook option
		if (document.errform.product.selectedIndex == ProductTextbook){
			if (origProduct == ProductAlgebraBook) {CheckSubmit(0);}
			changeElementsByClass('chselectioncl_hide', 'chselectioncl_show');
			changeElementsByClass('vlselectioncl_show', 'vlselectioncl_hide');
		}
		if (document.errform.product.selectedIndex == ProductAlgebraBook) {
			if (origProduct == ProductTextbook) {CheckSubmit(0);}
			changeElementsByClass('chselectioncl_hide', 'chselectioncl_show');
			changeElementsByClass('vlselectioncl_show', 'vlselectioncl_hide');
		}
		// Virtual Lab option
		if (document.errform.product.selectedIndex == ProductVirtualLab) {
			changeElementsByClass('chselectioncl_show', 'chselectioncl_hide');
			changeElementsByClass('vlselectioncl_hide', 'vlselectioncl_show');
		}
		// Other options
		if ( (document.errform.product.selectedIndex == ProductNone) || (document.errform.product.selectedIndex == ProductWebSite) ) {
			changeElementsByClass('vlselectioncl_show', 'vlselectioncl_hide');
			changeElementsByClass('chselectioncl_show', 'chselectioncl_hide');
		}
	}
}

function getReq(){
 var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
	return xmlhttp;
}

function getBookDoc(){
	var bookDocField = document.getElementById("kbProduct")[document.getElementById("kbProduct").selectedIndex].value;
	return bookDocField;
}

function getChapters(e){
/*	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
*/

	var selectedprod = document.getElementById("kbProduct")[document.getElementById("kbProduct").selectedIndex].value;
	//if you are switching products, always remove the sectionrow
	if(document.getElementById("sectionrow")) document.getElementById("sectionrow").parentNode.removeChild(document.getElementById("sectionrow"));
	if(document.getElementById("labsrow")) document.getElementById("labsrow").parentNode.removeChild(document.getElementById("labsrow"));

	if(selectedprod == "Virtual Lab" || selectedprod == "Installer" || selectedprod == "Web Site"){
		if(document.getElementById("chapterrow")) document.getElementById("chapterrow").parentNode.removeChild(document.getElementById("chapterrow"));
		if(selectedprod == "Virtual Lab"){
			var labrow = document.createElement("tr");
			var labpaddleft =document.createElement("td");
			var labpaddright =document.createElement("td");
			var lablabel = document.createElement("td");
			var labcell = document.createElement("td");
			var labfield = document.createElement("input");

			labfield.type = "text";
			labfield.name="lab_number";

			labrow.id = "labsrow";
			labcell.id = "labcell";
			lablabel.className="label"
			labcell.className="fields"
			lablabel.innerHTML = "Lab Name:";

			labcell.appendChild(labfield);
			labrow.appendChild(labpaddleft);
			labrow.appendChild(lablabel);
			labrow.appendChild(labcell);
			labrow.appendChild(labpaddright);
			document.getElementById("issuerow").parentNode.insertBefore(labrow,document.getElementById("issuerow"));
		}
	}
	else{
		var bookdoc = getBookDoc();
		var xmlhttp = getReq();
		xmlhttp.open("POST", "chapters.php",true);
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xmlhttp.send("bookdoc="+bookdoc+"&type=chapter");
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				var sel = document.createElement("select");
				sel.id = "kbChapter";
				sel.name = "ChId";
				if(sel.addEventListener){
					sel.addEventListener("change",getSections,false);
				}
				else{
					sel.attachEvent("onchange",getSections);
				}
				var opts = xmlhttp.responseXML.getElementsByTagName("option");
				for (var i = 0; i < opts.length; i ++){
					var opt = document.createElement("option");
					opt.value = opts[i].getAttribute("value");
					opt.innerHTML = opts[i].firstChild.data;
					sel.appendChild(opt);
				}
				if(!document.getElementById("chaptercell")){
					var secrow = document.createElement("tr");
					var secpaddleft = document.createElement("td");
					var secpaddright = document.createElement("td");

					var seclabel = document.createElement("td");
					var seccell = document.createElement("td");
					secrow.id = "chapterrow";
					seccell.id = "chaptercell";
					seclabel.className="label"
					seccell.className="fields"

					seclabel.innerHTML = "Chapter:";
					secrow.appendChild(secpaddleft);
					secrow.appendChild(seclabel);
					secrow.appendChild(seccell);
					secrow.appendChild(secpaddright);
					document.getElementById("issuerow").parentNode.insertBefore(secrow,document.getElementById("issuerow"));
				}
				document.getElementById("chaptercell").innerHTML = "";
				document.getElementById("chaptercell").appendChild(sel);
			}
		}
	}
}

function getSections(e){
	var bookdoc = getBookDoc();
	var xmlhttp = getReq();
	var chapter = document.getElementById("kbChapter")[document.getElementById("kbChapter").selectedIndex].value;
	xmlhttp.open("POST", "chapters.php",true);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xmlhttp.send("bookdoc="+bookdoc+"&type=sections&chapter="+chapter);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			var sel = document.createElement("select");
			sel.id = "selsections";
			sel.name = "SecId";
			var opts = xmlhttp.responseXML.getElementsByTagName("option");
			for (var i = 0; i < opts.length; i ++){
				var opt = document.createElement("option");
				opt.value = opts[i].getAttribute("value");
				opt.innerHTML = opts[i].firstChild.data;
				sel.appendChild(opt);
			}
			if(!document.getElementById("sectioncell")){
				var secrow = document.createElement("tr");
				var secpaddleft =document.createElement("td");
				var secpaddright =document.createElement("td");
				var seclabel = document.createElement("td");
				var seccell = document.createElement("td");
				secrow.id = "sectionrow";
				seccell.id = "sectioncell";
				seclabel.innerHTML = "Section:";
				seclabel.className="label"
				seccell.className="fields"
				secrow.appendChild(secpaddleft);
				secrow.appendChild(seclabel);
				secrow.appendChild(seccell);
				secrow.appendChild(secpaddright);

				document.getElementById("chapterrow").parentNode.insertBefore(secrow,document.getElementById("chapterrow").nextSibling);
			}
			document.getElementById("sectioncell").innerHTML = "";
			document.getElementById("sectioncell").appendChild(sel);
		}
	}
}

// hides or displays the chapter select based upon if the the Textbook is selected
function showInquirySelection() {

	//There was junk here, not used anymore though (frc 4/2006)
}


function changeElementsByClass(classname, newclassname) {
 var inc=0;
 //var alltags=document.all? document.all : document.getElementsByTagName("div");
 var alltags= document.getElementsByTagName("div");
 for (i=0; i<alltags.length; i++){
   if (alltags[i].className==classname) {
     alltags[i].className=newclassname;
   }
 }
}


// Validation of form elements
function CheckSubmit(goForth) {
	// sets the hidden value to indicate whether to submit changes to the db and email
	// or just redraw the page with the new options selected.
	document.errform.formsubmit.value=goForth;
	var error = "";
	// form validation
	var reallygo = false;
	if (goForth==1) {
		// put code here to do form validation if you want to.
		//There is code in past vss revs.  (frc 4/2006)
		reallygo = true;
		var elms = document.forms["errform"].elements;
		var elen = elms.length
		var emailFilter=/^.+@.+\..{2,3}$/;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/

		for (var i = 0; i < elen; i++){
			elm = elms[i];
			
			if(elm.name == "description" && elm.value.length==0){
				elm.style.background="yellow";
				error += "<li>You have not provided any information about your issue.</li>"
				reallygo=false;
			}
			else if (elm.name === 'description' && elm.value.length>0){
				elm.style.background = 'white';
			}
			
			if(elm.name == 'email'){
				if(elm.value==""){
					elm.style.background = "yellow";
					error += "<li>You have not provided an email address.  We require your email address in order to provide you with further assistance.</li>"
					reallygo=false;
				}
				else{
					if (!(emailFilter.test(elm.value))) {
						elm.style.background = "yellow";
						error += "<li>Please enter a valid email address.</li>";
						reallygo=false;
					}
					if (elm.value.match(illegalChars)) {
						elm.style.background = "yellow";
						error += "<li>The email address contains illegal characters.</li>";
						reallygo=false;
					}
				}
			}
			
			else if(elm.name == 'upid'){
				var selectedProductOption = document.errform.upid.selectedIndex;
				var selectedPid = document.errform.upid.options[selectedProductOption].value;
				var selectedVer = document.errform.uver.selectedIndex;

				//if the user selects the "select your product" or "-----" options, add info to the error display
				if((parseInt(selectedPid) == 0 || selectedPid == "none") && parseInt(selectedVer) == 0){
					document.errform.upid.style.background = "yellow";
					document.errform.uver.style.background = "yellow";
					error += "<li>Please select a product and version from the drop-down menus below.</li>";
					reallygo=false;
				}
				if((parseInt(selectedPid) == 0 || selectedPid == "none") && !(parseInt(selectedVer) == 0)){
					document.errform.upid.style.background = "yellow";
					document.errform.uver.style.background = "white";
					error += "<li>Please select a product from the drop-down menu below.</li>";
					reallygo=false;
				}
				//if the user select the "select your version" option, add info to the error display
				if(!(parseInt(selectedPid) == 0 || selectedPid == "none") && parseInt(selectedVer) == 0){
					document.errform.upid.style.background = "white";
					document.errform.uver.style.background = "yellow";
					error += "<li>Please select a version from the drop-down menu below.</li>";
					reallygo=false;
				}
				//if the user selects valid options, set the product name field and return true
				if((parseInt(selectedPid) > 0) && (parseInt(selectedVer) > 0)){
					document.errform.upid.style.background = "white";
					document.errform.uver.style.background = "white";
					document.errform.upid.value = document.errform.upid.options[selectedProductOption].text;
				}
			}
			
			else if(elm.name == 'issue'){
			  var selectedIssueOption = document.errform.issue.selectedIndex;
			  
			  if(selectedIssueOption == 0){
					elm.style.background = "yellow";
					error += "<li>Please select an issue type from the drop-down menu below.</li>";
					reallygo=false;
			  }
			  else{
				elm.style.background = "white";
			  }
			}
			
			//else if(elm.type!='button'){
				//elm.style.background="white";
			//}
		//	if(elms[i].selectedIndex!=null && elms[i].selectedIndex==0){alert(elms[i].selectedIndex);}
		}
	}
	else {
		reallygo = true;
	}

	// now it can be submitted and put into the database
	if (reallygo) {

		return true;
	}
	else{
		document.getElementById("errdisplay").innerHTML = error;
		return false;
	}
}

function wJAR(code,archive,wt,ht,params){
	var str = "<OBJECT classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" ";
	str += "width=\""+wt+"\" height=\""+ht+"\" align=\"right\" ";
	str += "codebase=\"http://java.sun.com/products/plugin/autodl/jinstall-1_4-windows-i586.cab#Version=1,4,0,0\">";
	str += "<PARAM name=\"code\" value=\""+code+"\">";
	str += "<PARAM name=\"type\" value=\"application/x-java-applet;jpi-version=1.4.2\">";
	str += "<PARAM name=\"scriptable\" value=\"true\"><PARAM NAME=\"archive\" VALUE=\""+archive+"\">";
	if (typeof(params)!="undefined") str += params;
	str += "</OBJECT>";
	//prompt("fds",str);
	document.write(str);
	//return str;
}

function wSWFT(swf,width,height,id){
	var str = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" ';
	str += 'id="'+id+'" ';
	str += 'width="'+width+'" height="'+height+'" align="right">';
	str += '<param name="quality" value="high">';
	str += '<param name="movie" value="'+swf+'">';
	str += '<embed src="'+swf+'" quality="high" width="'+width+'" height="'+height+'" ';
	str += 'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" align="right">';
	str += '</embed></object>';
	document.write(str);
}

function wQWT(mov,width,height,id){
	var str = '<object id="'+id+'" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" height="165" width="200" align="right">';
	str += '<param name="autoplay" value="true">';
	str += '<param name="controller" value="true">';
	str += '<param name="loop" value="false">';
	str += '<param name="src" value="'+mov+'">';
	str += '<embed loop="false" src="'+mov+'" id="'+id+'" backgroundcolor="#FFFFFF" width="'+width+'" controller="true" height="'+height+'"  pluginspage="http://www.apple.com/quicktime/download/" autoplay="true" align="right">';
	str += '</embed></object>';
	document.write(str);
}

function updatePidSelect(selectObj){
	var loc = location.href;
	var si = selectObj.selectedIndex;
	if (selectObj.options[si].value!="none"){
		if (loc.indexOf("pid=")>0){
			//strip the pid value in the url, set it with a cookie, refresh.
			loc = loc.replace(/pid=[^&]*&|&pid=[^&]*$/g,"");
		}
		createCookie("fbvars_pid",selectObj.options[si].value,0);
		window.location.replace(loc);
	}
}
