// Common variable setup
var topcol1='#D72700'; topcol2='#cccccc'
var midcol1='#FF9933'; midcol2='#cccccc'
var midnav1=''; topnav1=''; myrowname='';
cookie_name="textsize"; 
font1="11px"; font2="12px";


//	setnav highlights the bars underneath/next to menu items.  Depending on what is passed in
//	it will highlight the given section by finding the id that is passed in.
function setnav(topnav, midnav, bodnav, sidnav, dropper) { 
	if (topnav!='') {topnav1="ln_"+topnav; topnav2="td_"+topnav;}
	if (midnav!='') {midnav1="ln_"+midnav; midnav2="td_"+midnav;}

	if (topnav!='') {document.getElementById(topnav1).style.background=topcol1}
	if (midnav!='') {document.getElementById(midnav1).style.background=midcol1}
	if (bodnav!='') {document.getElementById(bodnav).className = 'highlight'}
	if (sidnav!='') {	
			namer=sidnav.substring(1,sidnav.length);
			if (dropper=='') {
				//myarr=eval("arr_"+namer); 
				myarr = MM_findObj("arr_"+namer);
				myarr.src="images/shim.gif";
			}

			//myblk=eval("blk_"+namer); 
			myblk=MM_findObj("blk_"+namer);
			myblk.src="images/icon_block.gif"; 
			myrowname="row_"+namer; 
			//myrow=eval(myrowname); 
			myrow=MM_findObj(myrowname);
			colorhi1='#dddddd'; 
			colorhi2='#cccccc';
			mynum=sidnav.substring(0,1); myrow.style.backgroundColor=eval("colorhi"+mynum);
	}
	if (dropper!='') {
			myprompt=dropper.lastIndexOf('_')
			mydrop=eval(dropper.substring(0,myprompt))
			myindex=dropper.substring(myprompt+1,dropper.length); 
			mydrop.selectedIndex=myindex;
	}
}

// navover and navoff are called to change the colors of the bars when the mouse hovers over a link in a nav bar	
function navover(which,col) {document.getElementById("ln_"+which).style.background=col}
function navoff(which,col,nav) {if (("ln_"+which)!=nav) {document.getElementById("ln_"+which).style.background=col}}

//putCookie adds the cookie that is used to store the text size settings
function putCookie() {
	if(document.cookie != document.cookie) {index = document.cookie.indexOf(cookie_name);} else { index = -1;}
	if (index == -1){
		document.cookie=cookie_name+"="+mysize+"; expires=Monday, 04-Apr-2010 05:00:00 GMT; path=/; domain=mcgraw-hill.com";
	} 
}

//	setRule is called to change the size of the font when the user clicks on the link in 
//	the site tools bar in the banner.
function setRule(stylesheet,rule,attribute,value){
	if (! (document.styleSheets || ( window.opera && document.childNodes )) ) return false;
	var lRules = new Array();

	if (document.styleSheets[stylesheet].cssRules) 
		lRules = document.styleSheets[stylesheet].cssRules
	else if (document.styleSheets[stylesheet].rules)
		lRules = document.styleSheets[stylesheet].rules
	else return false;

	//alert(lRules[0].style.fontSize)
 	if (value=="") {if (lRules[0].style.fontSize==font1) {value=font2} else {value=font1}	}

	mysize=value; //alert('value is '+value)
	lSuccess = eval("lRules[" + rule + "].style." + attribute + " = value")
	putCookie();
	return lSuccess;
}

//	getCookie reads the cookie from the user and sets the font size accordingly.
function getCookie() {
	if(document.cookie) {
		index = document.cookie.indexOf(cookie_name);
		if (index != -1) {
			namestart = (document.cookie.indexOf("=", index) + 1);
			nameend = document.cookie.indexOf(";", index);
			if (nameend == -1) {nameend = document.cookie.length;}
			mysize = document.cookie.substring(namestart, nameend);
			setRule(0,0,'fontSize',mysize);
		}
	} //else {alert('no cookie')}
}

//	show_hide is used to show/hide expandable sections in the site.  It also
//	will change the image of the + to a - or vice versa.
function show_hide(the_id,icon) {
	var obj = MM_findObj(the_id);
	if(icon !='') {var arr = MM_findObj(the_id + "_" + icon);}
	if(obj.style.visibility == "hidden"){ // show
		obj.style.visibility = "visible";
		obj.style.display = "block"; // If this looks wrong, try 'inline'
		if (icon!='') {arr.src = "images/nav_minus.gif";}
	} else { // hide
		obj.style.visibility = "hidden";
		obj.style.display = "none";
		if (icon!='') {arr.src = "images/nav_plus.gif"; }
	}
}

//	showH_hideall is used to show/hide all expandable sections on a page in the site.  It also
//	will change the image of the + to a - or vice versa.
function show_hideall(id_start,id_end,icon,the_act) {
for (i=id_start; i<id_end+1; i++) {
	if (i<10) {spacer="0"} else {spacer=""}
	the_id="q"+spacer+i
	var obj = MM_findObj(the_id);
	if(icon !='') {var arr = MM_findObj("q"+spacer+i+"_"+icon);}
	if(the_act=="show"){ // show
		obj.style.visibility = "visible";
		obj.style.display = "block"; // If this looks wrong, try 'inline'
		if (icon!='') {arr.src = "images/nav_minus.gif";}
	} else { // hide
		obj.style.visibility = "hidden";
		obj.style.display = "none";
		if (icon!='') {arr.src = "images/nav_plus.gif"; }
	}
}
}

//	popup will create a popup showing the given URL with the specified width and height.  The user can also
//	choose to show the scrollbar and whether or not to allow the user to resize the popup.
function popup(url, width, height, scrollstat, sizer) {
  window.open(url,"comment",'width=' + width + ',height=' + height + ',resizable=' + sizer + ',scrollbars=' + scrollstat + ',menubar=no,status=no,toolbar=yes,location=yes,top=30,left=85' );
}

//	popupgeneric opens an url in a popup window with a width of 835
function popupgeneric(theurl) {
 	width=835; height=screen.height-250
	sizer='yes'; scrollstat='yes'
	popup(theurl, width, height, scrollstat, sizer)
}

function selecturl(theurl) { //v2.0
  var strlen= theurl.length;
  var pipe_index = theurl.indexOf('|');
  var popup = theurl.substring(pipe_index + 1, strlen);
  if(popup == "yes")
  {
	  	popupgeneric(theurl.substring(0,pipe_index));
  }
  else
  {
	  	MM_goToURL('parent',theurl.substring(0,pipe_index));
  }

}

/* printit is called when the user clicks the print button in site tools */
function printit(){  
	if (window.print) {window.print();}
}

/* forwards a user to the url */
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

/* used to find an object in the DOM */
function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

/* MM_validateForm is used to validate the forms on the site */
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;

  /* loop through the arguments passed in */
  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 we are checking an email field */
      if (test.indexOf('isEmail')!=-1) { 
	  		var err_found = 0; //was it invalidated
			var at="@" // at character
			var dot="." // dot character
			var lat=val.indexOf(at) //find leftmost position of at character
			var lval=val.length     // find length
			var ldot=val.indexOf(dot) // find index of the dot in the string
			// if we didn't find an @ throw error
			if (val.indexOf(at)==-1){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}
			// if we didn't find an at, or it was at position zero or the end of the string
			// throw and error
			if (val.indexOf(at)==-1 || val.indexOf(at)==0 || val.indexOf(at)==lval){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}
			// if we can't find a dot or if it is at pos 0 or end, throw error
			if (val.indexOf(dot)==-1 || val.indexOf(dot)==0 || val.indexOf(dot)==lval){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}
			// if there is a 2nd at after the first throw an error
			if (val.indexOf(at,(lat+1))!=-1){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}
			// Check dot position
			if (val.substring(lat-1,lat)==dot || val.substring(lat+1,lat+2)==dot){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}
			// Check dot position
			if (val.indexOf(dot,(lat+2))==-1){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}
			// Make sure no spaces
			if (val.indexOf(" ")!=-1){
				if( err_found == 0)
				{
					errors+='- '+nm+' must contain an e-mail address.\n';
					err_found = 1;
				}
			}

      } 
	  else if (test.indexOf('isPhone')!=-1)
	  {
		// var regExpObj  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
		var regExpObj  = /^\+?\d(\d|-){7,20}/;
		if(!regExpObj.test(val))
		{
    	     var err_found = 0;
		     if(err_found == 0)
		     {	err_found = 1;
			    errors+='- '+nm+" must contain a valid phone number\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('Please validate the following:\n'+errors);
  document.MM_returnValue = (errors == '');
}

// This function is called when the shop pages load.  It pulls what page you are looking at
// and shows the correct sidebar featured product.  This is used in order to keep 1
// side include instead of 3
function shop_OnLoad()
{
	var url = document.URL; // get URL
	var filename = url.substring(url.lastIndexOf('/') + 1,url.length); //get filename from URL
	var page = filename.substring(0,filename.indexOf('.') ); //get pagename from URL
	if(page != null)
	{
		// show Feature Product table (naming convention is page_fp for id)
		var obj = MM_findObj(page + "_fp");
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
		// show Descriptive text for product (naming is page_desc for id)
		obj = MM_findObj(page + "_desc");
		obj.style.visibility = 'visible';
		obj.style.display = 'block';
	}
}

// The following code is used to detect whether or not the user has the Flash Plugin
// initialize global variables for checking
var detectableWithVB = false;
var pluginFound = false;

// Searches for flash by calling detectPlugin or detectActiveXControl
function detectFlash() {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic (needed for IE on windows)
    if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.6');
    }
    // return pluginFound to caller.  True if flash was detected, otherwise false.
    return pluginFound;
}

// Detect Plugin is used for Netscape/Mozilla plugin checking
function detectPlugin() {
    // allow for multiple checks in a single pass
    var thePlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake (IE has a placeholder that is always empty)
    if (navigator.plugins && navigator.plugins.length > 0) {
		var pluginsArrayLength = navigator.plugins.length;
		// for each plugin...
		for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
		    // loop through all desired names and check each against the current plugin name
		    var numFound = 0;
		    for(namesCounter=0; namesCounter < thePlugins.length; namesCounter++) {
				// if desired plugin name is found in either plugin name or description
				if( (navigator.plugins[pluginsArrayCounter].name.indexOf(thePlugins[namesCounter]) >= 0) || 
					(navigator.plugins[pluginsArrayCounter].description.indexOf(thePlugins[namesCounter]) >= 0) ) {
					if((navigator.plugins[pluginsArrayCounter].description.charAt(navigator.plugins[pluginsArrayCounter].description.indexOf('.') - 1)) > 6)
					{
						// this name was found
				    	numFound++; 
					}
				}   
		    }
		    // if the number we found matches the total number provided then we were successful
			if(numFound == thePlugins.length) {
				pluginFound = true;
				break;
			}
		}
    }
    return pluginFound;
} // detectPlugin

// Here we write out the VBScript block for MSIE for Windows
// this is the only way to detect Flash in IE for windows. DetectPlugin won't work
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');
    document.writeln('</script >');
}

getCookie();