// Scholastic Teacher Store
// main javascript functions
// (c) 2001 j.blecher / digital pulp

// BEGIN ###############################################################
// code to adjust nav links via DOM

var dom = (document.getElementById) ? true : false;
var mOverClass = "mainNavCellBg1";
var mOutClass = "mainNavCellBg0";
var mOverLinksContainer = "mainNavCell";
var defaultStoreId = "10052";
var defaultCatId = "10051";

function highlight(td) {
  if (dom) {
//    var theDiv = a.parentNode;
//    var theTd = theDiv.parentNode;
//  alert(td.className);
    td.className = mOverClass;
    }
  }

function unhighlight(td) {
  if (dom) {
//    var theDiv = a.parentNode;
//    var theTd = theDiv.parentNode;
    td.className = mOutClass;
    }
  }


function init() {
  var allLinks = document.getElementsByTagName("A");

  for (var i=0; i<allLinks.length; i++) {
    var par = allLinks[i].parentNode;
    var grandPar = par.parentNode;
    if (grandPar.className.indexOf(mOverLinksContainer) != -1) {
      grandPar.setAttribute("onmouseover", "highlight(this)");
      grandPar.setAttribute("onmouseout", "unhighlight(this)");
//      alert(allLinks[i].getAttribute("href"));
      grandPar.setAttribute("onclick", "window.location.href='" + allLinks[i].getAttribute("href") + "'");
      allLinks[i].removeAttribute("href");
      }
    }
  }

// if (dom) window.onload = init;
// END -----------------------------------------------------------------




// BEGIN ###############################################################
// code to open help window

var helpWin;
function openHelp(section, poundDown) {
  var helpPages = new Array();
  var page = "http://"+ window.location.host +  "/content/stores/scholasticstore2/custserv/help/help.html";
  helpPages[0] = "searching-browsing.html";
  helpPages[1] = "ordering.html";
  helpPages[2] = "registration.html";
  helpPages[3] = "contact-us.html";
  helpPages[4] = "using-my-acct.html";
  helpPages[5] = "order-status.html";
  helpPages[6] = "faq.html";
  helpPages[7] = "about-the-experts.html";
  helpPages[8] = "returning-orders.html";
  
  
  
  // pass the help section and poundDown if we receive it from caller
  poundDown = (poundDown == null) ? "" : "#" + poundDown;
  page += (section == null) ? "" : "?" + helpPages[section] + poundDown;
  helpWin = window.open(page, "helpWindow", "width=550,height=400,toolbar=no");
  }


// END -----------------------------------------------------------------

// BEGIN ###############################################################
// code to open Need help window

var helpWin;
function openNeedHelp(section, poundDown, serverName) {
  // alert("serverName" +serverName);
  var helpPages = new Array();
  var page = "http://"+serverName+"/content/stores/scholasticstore2/custserv/help/help.html";

  helpPages[0] = "searching-browsing.html";
  helpPages[1] = "ordering.html";
  helpPages[2] = "registration.html";
  helpPages[3] = "using-my-acct.html";
  helpPages[4] = "order-status.html";
  helpPages[5] = "returning-orders.html";
  helpPages[6] = "faq.html";
  helpPages[7] = "about-the-experts.html";
  helpPages[8] = "contact-us.html";
  
  // pass the help section and poundDown if we receive it from caller
  poundDown = (poundDown == null) ? "" : "#" + poundDown;
  page += (section == null) ? "" : "?" + helpPages[section] + poundDown;
  helpWin = window.open(page, "helpWindow", "width=550,height=400,toolbar=no");
  }


// END -----------------------------------------------------------------

// BEGIN ###############################################################
// code to open help main window

var helpWin;
function openMainHelp() {
  
  var page = "/content/stores/scholasticstore2/custserv/help/help.html";
  helpWin = window.open(page, "helpWindow", "width=550,height=400,toolbar=no");
  }

// END -----------------------------------------------------------------



// BEGIN ###############################################################
// code to pop enlarged prod window

var prodWin;

function enlargeProd(url) {
  var height = 390;
  var width = 620;
  prodWin = window.open(url, "enlargedProdWin", "width=" + width+ ",height=" + height + ",toolbars=no");
  }

// END -----------------------------------------------------------------


// BEGIN ###############################################################
// code for help window

function closeAndGoto(page) {
//  alert(top.window.opener.location.href);
  top.window.opener.location.href = page;
  top.close();
  }

// END -----------------------------------------------------------------

// BEGIN ###############################################################
// code to make sure a user only presses a button once

var i=0;
function checkFirst() {
    if (i==0) {
      i++;
      return true;
    }
    else {
      return false;
    }
  }

// END -----------------------------------------------------------------

// BEGIN ###############################################################
// code to pop experts window

function expertsPop() { 
  var height = 400;
  var width = 570;
  var url = "/consumerstore/common/experts.html";
  expertsWin = window.open(url, "expertsPopWin", "width=" + width+ ",height=" + height + ",scrollbars=yes,toolbars=no");
}
// END -----------------------------------------------------------------


// BEGIN ###############################################################
/**
 * function is used to pop up "Email a link" form for user to email recommended URL to friends
 * the link to be sent is embedded in the form as hidden value with name=target
 */
 
function openTellFriend()
{
	popEmailForm(document.forms.emailSSForm);
	return false;
}
var newWin = null;
function popEmailForm(form) {
  var target = form.target.value;
  target = escape(target);

  // the email link form for user to fill out name, email address, and message
  var url = form.emailform.value;
  url += "?target=" + target;

  url = unescape(url);
  
  newWin = window.open(url,"MyWindow","toolbar=yes,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=454,height=560,left=20,top=20");
  return false;
}
// END -----------------------------------------------------------------

// BEGIN ###############################################################
// code to open collateral pop up window

var collatWin;
function popProductCollateral(url) {
  
  var page = url;
  collatWin = window.open(page, "productCollateral", "titlebar=no,toolbars=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=500,height=500");
  }

// END -----------------------------------------------------------------

function correctPrevURL(form)
{
   form.prevURL.value = window.location;
}

	var defaultSearchVal = "Enter Keyword or item #";
	
	function setDefaultValue()
	{
		defaultSearchVal = document.getElementById('query_').value;	
	}
	
   function clearGreeting(){	
		if(trimAll(document.getElementById('query_').value)==defaultSearchVal){
			 document.getElementById('query_').value='';
			 
		}		
	}

	function restoreGreeting(){	 
		if(trimAll(document.getElementById('query_').value)==''){
			 document.getElementById('query_').value=defaultSearchVal;
		}		
	}
	var defaultNewsletterVal = "Email address"
	function clearNewsletter(){	
		if(trimAll(document.getElementById('NewsletterField').value)==defaultNewsletterVal){
			 document.getElementById('NewsletterField').value='';
			 
		}		
	}

	function restoreNewsletter(){	 
		if(trimAll(document.getElementById('NewsletterField').value)==''){
			 document.getElementById('NewsletterField').value=defaultNewsletterVal;
		}		
	}
	
	function trimAll(sString) 
	{
		while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
		while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
		return sString;
	}
	
	function validateSearch(){
		if(trimAll(document.getElementById('query_').value)=='Item #, keyword, product name'){
			 document.getElementById('query_').value='';
		}	
	}
	function openPolicyWindow()
	{
	  mywindow = window.open ("http://www.scholastic.com/privacy.htm", 
  		"policyWindow","location=1,status=1,scrollbars=1, width=800,height=600");
	  mywindow.moveTo(0,0);
	  mywindow.focus();
	} 
	function resizeOutOfStock()
	{
		//resizeTo(620,390);	
	}
	
	function resizeOutOfStockThankYou()
	{
		 //resizeTo(620,320); 	
	}
	
	function showPrintableReceipt(receiptURL)
	{
		myWindow = 	window.open(receiptURL,"PrintableReceipt","width=700,height=700,menubar=1,toolbar=1,resizable=1,scrollbars=1");
		myWindow.moveTo(0,0);
		myWindow.focus();
	}
	function openStaticLink(page)
	{
		var newWin = "";
		var storeid = getParameter("storeId");
		var catid = getParameter("catalogId");
		// Set Default Values for QA environment if either is missing
		if(storeid=="")
		{
			storeid=defaultStoreId;
		}
		if(catid=="")
		{
			catid = defaultCatId;
		}

		page = page+"?storeId="+storeid+"&catalogId="+catid;									
		document.location.href=page;
	}
  	function openAffiliate(affilatePage)
	{
		var newWin = "";
		var storeid = getParameter("storeId");
		var catid = getParameter("catalogId");
		// Set Default Values for QA environment if either is missing
		if(storeid=="")
		{
			storeid=defaultStoreId;
		}
		if(catid=="")
		{
			catid = defaultCatId;
		}
		
		affilatePage = affilatePage+"?storeId="+storeid+"&catalogId="+catid;									
		newWin = window.open(affilatePage,"AffiliatePage","location=1,status=1,scrollbars=1,width=1100,height=800");
		newWin.focus();
	}
	
	function openCustomerService(csPage)
	{
		var newWin = "";
		var storeid = getParameter("storeId");
		var catid = getParameter("catalogId");
		// Set Default Values for QA environment if either is missing
		if(storeid=="")
		{
			storeid=defaultStoreId;
		}
		if(catid=="")
		{
			catid = defaultCatId;
		}

		csPage = csPage+"?storeId="+storeid+"&catalogId="+catid;									
		document.location.href=csPage;
	}
	
	function getParameter( name )
	{
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var tmpURL = window.location.href;
	  var results = regex.exec( tmpURL );
	  if( results == null )
    	return "";
	  else
    	return results[1];
	}		
	function openTrailor(url)
	{
		var newWin = "";							
		newWin = window.open(url,"Trailor","location=0,status=0,scrollbars=0,width=700,height=589");
		newWin.focus();	
	}