﻿/*************************************************************
 * Window Onload Manager (WOM) v1.0
 * Author: Justin Barlow - www.netlobo.com
 *
 * Description:
 * The WOM library of functions allows you to easily call
 * multiple javascript functions when your page loads.
 *
 * Usage:
 * Add functions to WOM using the womAdd() function. Pass the
 * name of your functions (with or without parameters) into
 * womAdd(). Then call womOn() like this:
 *     womAdd('hideDiv()');
 *     womAdd('changeBg("menuopts","#CCCCCC")');
 *     
 * WOM will run when your page loads and run all of the
 * functions you have added using womAdd()
 *************************************************************/

/*************************************************************
 * The womGo() function loops through the woms array and
 * runs each function in the array.
 *************************************************************/
window.onload = function() {
	for(var i = 0;i < woms.length;i++)
		eval(woms[i]);
}

/*************************************************************
 * The womAdd() function will add another function to the woms
 * array to be run when the page loads.
 *************************************************************/
function womAdd(func){
	woms[woms.length] = func;
}

/*************************************************************
 * The woms array holds all of the functions you wish to run
 * when the page loads.
 *************************************************************/
var woms = new Array();




var popupWidth;
var popupHeight;

function setPopupDimensions(popupurl) {
	popupWidth = null;
	popupHeight = null;
	if (popupurl.lastIndexOf("ab-1v") != -1) {popupWidth = 700; popupHeight = 900;}
	if (popupurl.lastIndexOf("ab-49r") != -1) {popupWidth = 630; popupHeight = 645;}
	if (popupurl.lastIndexOf("ab-57v") != -1) {popupWidth = 630; popupHeight = 730;}
	if (popupurl.lastIndexOf("ab-68v69r") != -1) {popupWidth = 630; popupHeight = 800;}
	if (popupurl.lastIndexOf("ab-77r") != -1) {popupWidth = 550; popupHeight = 680;}
	if (popupurl.lastIndexOf("ab-183v") != -1) {popupWidth = 590; popupHeight = 530;}
	if (popupurl.lastIndexOf("ab-89v") != -1) {popupWidth = 480; popupHeight = 580;}
	if (popupurl.lastIndexOf("ab-29v") != -1) {popupWidth = 700; popupHeight = 750;}
	if (popupurl.lastIndexOf("ab-224v") != -1) {popupWidth = 630; popupHeight = 520;}
	if (popupurl.lastIndexOf("ab-headandtail") != -1) {popupWidth = 440; popupHeight = 520;}
	if (popupurl.lastIndexOf("ab-blindtooling") != -1) {popupWidth = 620; popupHeight = 520;}
	if (popupurl.lastIndexOf("ab-clasp") != -1) {popupWidth = 560; popupHeight = 540;}

	if (popupurl.lastIndexOf("ds-scrolls1") != -1) {popupWidth = 460; popupHeight = 550;}
	if (popupurl.lastIndexOf("ds-trever") != -1) {popupWidth = 650; popupHeight = 690;}
	if (popupurl.lastIndexOf("ds-detail2") != -1) {popupWidth = 530; popupHeight = 400;}
	if (popupurl.lastIndexOf("ds-detail3") != -1) {popupWidth = 530; popupHeight = 400;}
	if (popupurl.lastIndexOf("ds-fragment1") != -1) {popupWidth = 515; popupHeight = 500;}

	if (popupurl.lastIndexOf("cccharged") != -1) {popupWidth = 500; popupHeight = 250;}

	if (popupurl.lastIndexOf("fe-logo") != -1) {popupWidth = 580; popupHeight = 430;}

}

// This variable will hold the window object.
// We only allow one pop-up at a time.
var popup = null;

var nokill = false;

/*/ FUNCTIONS THAT WILL WRAP THE POP-UP PROCESS /*/
/*/
/ / PURPOSE:
/ /		To create and center a pop-up window.
/ /
/ / COMMENTS:
/ /		It will replace old pop-up if called
/ / 	without calling DestroyWnd() first..
/*/
function openPopUp(fileUrl, width, height, normal) {

		nokill = false; //reset

	// close any other popups, thanks
	if (popup != null) { // || (popup.closed)) {
		DestroyWnd();
	}

	var attribs = '';
	var size = "no";

	// if no dimensions provided, apply default/percentage dimensions [and set the window to be resizable]
	if (height == null) {height = 0.6; size="yes";}
	if (width == null) {width = 0.6; size="yes";}

	// do percentage values
	if (height <= 1) height = (screen.height * height);
	if (width <= 1) width = (screen.width * width);

	// force minimum dimensions if desired
	// if (height < 375) height = 375;
	// if (width < 600) width = 600;

	// if the screen is smaller than the proposed window, make the window resizable and adjust the proposed window's dimensions accordingly...
	if(screen.width < width) {width = (screen.width * 0.9); size = "yes";}
	if(screen.height < height) {height = (screen.height * 0.75); size = "yes";}

	// if the screen is very small, ensure that the window resizable
	// if(screen.width <= 800 || screen.height <= 600) size = "yes";

	// center the window // THIS DOESN'T WORK RIGHT YET
	// find position
	WndTop  = Math.max(Math.round((screen.height - height) / 2),0);
	WndLeft = Math.max(Math.round((screen.width  - width)  / 2),0);

	// if normal variable is set, enable sizing override. STRONGLY RECOMMENDED - imagine if size was off on a normal window! Dumb.
	if (normal) {size = "yes";}
	// alert("size is " + size);
	
	//size and scrollbar override - strongly recommended for this kind of site for all popups.
	size = "yes"

	// collect the attributes
	attribs = "width=" + width + ",height=" + height + ",top=" + WndTop + ",left=" + WndLeft;

	if (normal) {attribs += ",resizable=" + size + ",scrollbars=" + size + ",status=yes,toolbar=yes,directories=yes,menubar=yes,location=yes"; nokill = true;} else {attribs += ",resizable=" + size + ",scrollbars=" + size + ",dependent=yes,status=no,toolbar=no,directories=no,menubar=no,location=no";}

	// UNUSED ATTRIBS: dependent, // not true, am trying it out again.

	popup = window.open(fileUrl,'',attribs); // optional window name prevents multiple windows and cause auto-recycles even when opening window is browsed elsewhere. Useful, but not here: popups are quickly killed off unless explicitly needed to stay open (nokill) - don't want to then recycle them. Recycling also stops sizing.
	// window.open(fileUrl,'fe',attribs); // window name prevents multiple windows and causes auto-recycles! Nice.
	popup.focus(); // brings popup to front whenever a new one is loaded.
}




/*/
/ / PURPOSE:
/ /		To destroy the pop-up window.
/ /
/ / COMMENTS:
/ /		This is available if wish to destroy
/ / 	the pop-up window manually.
/*/

function DestroyWnd ()
{
	// close the current window
	if(popup != null && nokill == false)
	{
		popup.close();
		popup = null;
	}
}

function nonpopupsOnload() {
	// check to see that the browser supports the getElementsByTagName method
	// if not, exit the loop
	if (!document.getElementsByTagName) {
		return false;
	}

	// create an array of objects of each link in the document
	var popuplinks = document.getElementsByTagName("a");
	// loop through each of these links (anchor tags)
	for (var i=0; i < popuplinks.length; i++) {
		// if the link has a class of "popupwin"...
		if (popuplinks[i].className.indexOf("popupwin") != -1) {
			// add an onclick event on the fly to pass the href attribute
			// of the link to our second function, openPopUp
			popuplinks[i].onclick = function() {
				setPopupDimensions(this.getAttribute("href"));
				openPopUp(this.getAttribute("href"),popupWidth,popupHeight,false);
				return false;
			}
		}
		if (popuplinks[i].className.indexOf("picture") != -1) {
			popuplinks[i].onclick = function() {
				setPopupDimensions(this.getAttribute("href"));
				openPopUp(this.getAttribute("href"),popupWidth,popupHeight,false);
				return false;
			}
		}
		if (popuplinks[i].className.indexOf("popupwinnormal") != -1) {
			popuplinks[i].onclick = function() {
				setPopupDimensions(this.getAttribute("href"));
				openPopUp(this.getAttribute("href"),popupWidth,popupHeight,true);
				return false;
			}
		}
	}
}
womAdd('nonpopupsOnload()');



















var windowpos_current = 0;
var ie567 = false;
var ie56 = false;
var content_min;
var content_max;
var contentdiv;
var backbutton;
var windowWidth,windowHeight;

var sessioncounter = 0;

function getWindowPos()
{
	if (window.innerHeight)
		{
			windowpos_current = window.pageYOffset;
		}
	else if (document.documentElement && document.documentElement.scrollTop)
		{
			windowpos_current = document.documentElement.scrollTop;
		}
	else if (document.body)
		{
			windowpos_current = document.body.scrollTop;
		}
}

function getWindowSize() // http://www.quirksmode.org/viewport/compatibility.html
{
	if (self.innerHeight) // all except Explorer
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
}

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

function setPopupContentWidth(elementname, content_width) {
	contentdiv = new getObj(elementname);
	if (!content_width) {
		getWindowSize();
/*		alert("content_min: " + content_min);
		alert("content_max: " + content_max);
		alert("width: " + windowWidth + "; height: " + windowHeight);
*/		
		if (content_min && (windowWidth - 30) < content_min) {
			contentdiv.style.width = content_min+"px";
		}
		else if (content_max && (windowWidth - 30) > content_max) {
			contentdiv.style.width = content_max+"px";
		}
		else if (content_max && content_min) {
			contentdiv.style.width = "100%";
		}
	}
	else contentdiv.style.width = content_width+"px";
}

function closePopup() {
	if (EnableSizeReport) {
		manualReport = true;
		PopupSizeReport();
	}
	else if (opener) opener.DestroyWnd();
}

var W3CDOM = (document.createElement && document.getElementsByTagName);
var mouseOvers = new Array();
var mouseOuts = new Array();

function doMouseovers()
{
	if (!W3CDOM) return;
	var nav = document.getElementById('mouseovers');
	var imgs = nav.getElementsByTagName('img');
	for (var i=0;i<imgs.length;i++)
	{
		imgs[i].onmouseover = mouseGoesOver;
		imgs[i].onmouseout = mouseGoesOut;
		var suffix = imgs[i].src.substring(imgs[i].src.lastIndexOf('.'));
		mouseOuts[i] = new Image();
		mouseOuts[i].src = imgs[i].src;
		mouseOvers[i] = new Image();
		mouseOvers[i].src = imgs[i].src.substring(0,imgs[i].src.lastIndexOf('.')) + "_f2" + suffix;
		imgs[i].number = i;
	}
}
function mouseGoesOver()
{
	this.src = mouseOvers[this.number].src;
}

function mouseGoesOut()
{
	this.src = mouseOuts[this.number].src;
}


function Currency(anynum,sign) {
        //returns number as string in $xxx,xxx.xx format.
        anynum = "" + eval(anynum)  //evaluate (in case an expression sent)
        intnum = parseInt(anynum)  //isolate integer portion
        intnum = Math.abs(intnum)
        intstr = ""+intnum
        //add comma in thousands place.
        if (intnum >= 1000) {
                intlen = intstr.length
                temp1=parseInt(""+(intnum/1000))
                temp2=intstr.substring(intlen-3,intlen)
                intstr = temp1+","+temp2

        }
        if (intnum >= 1000000) {
                intlen = intstr.length
                temp1=parseInt(""+(intnum/1000000))
                temp2=intstr.substring(intlen-7,intlen)
                intstr = temp1+","+temp2

        }

        decnum = Math.abs(parseFloat(anynum)-parseInt(anynum)) //isolate decimal portion
        decnum = decnum * 100 // multiply decimal portion by 100.
        decstr = "" + Math.abs(Math.round(decnum))
        if (decstr.length>2) {decstr=decstr.substring(0,2)}
        while (decstr.length < 2) {decstr="0"+decstr}
        retval = intstr + "." + decstr 
        if (anynum < 0) {
                retval="("+retval+")"
        }
		var printsign = "$";
		if (sign) printsign = sign;
        return printsign+retval
}
function DoTotal(inputname,price,sign) {
	var bookident = inputname.substr(0,2);
	var formid = new getObj('orderform');
	
	eval("formid.obj." + bookident + "_total.value = Currency(" + price + " * formid.obj." + bookident + "_quantity.value,sign);");
	if (bookident == "ps" && formid.obj.ps_dedication.checked) {
			eval("formid.obj.ps_total.value = Currency((" + price + " + 20) * formid.obj.ps_quantity.value,sign);");
	}
	
	var alltotals = (
					 parseFloat(formid.obj.ab_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.bh_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.kb_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.me_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.nf_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.pp_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.ps_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.rh_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.rm_total.value.replace('$','').replace(/,/g,'')) +
					 parseFloat(formid.obj.ts_total.value.replace('$','').replace(/,/g,''))
					 );
	formid.obj.order_total.value = Currency(alltotals);
	formid.obj.order_total2.value = Currency(alltotals);
}
function DoTotalPatron(inputname,price,sign) {
	var bookident = inputname.substr(0,2);
	var formid = new getObj('orderform');
	
	eval("formid.obj." + bookident + "_total.value = Currency(" + price + " * formid.obj." + bookident + "_quantity.value,sign);");
	
	var alltotals = (
					 parseFloat(formid.obj.me_total.value.replace('$','').replace(/,/g,''))
					 );
	formid.obj.order_total2.value = Currency(alltotals);
}

function setmessage(boxname,newtext) {
//	var mesgtext = new getObj('mesg');
//	eval(" var mesgtext = new getObj('mesg'); ");
	var mesgtext = new getObj(boxname);
//	eval(" var mesgtext = new getObj('mesg'); ");
	
	
	if (mesgtext.obj.value == '' || mesgtext.obj.value == 'enter search terms' || mesgtext.obj.value == 'הכנס חיפוש' || mesgtext.obj.value == '検索する語を入力' || mesgtext.obj.value == 'Suchbegriff eingeben' || mesgtext.obj.value == 'ввести термин' || mesgtext.obj.value == 'Please subscribe me to your newsletter.' || mesgtext.obj.value == 'Please update my contact details.' || mesgtext.obj.value == 'I am considering placing an order; please contact me.' || mesgtext.obj.value == 'Please call me back.' || mesgtext.obj.value == 'Please provide details here.')  mesgtext.obj.value = newtext;
}

function changecolor() {
	var box = new getObj('q');
	box.obj.style.color = "#444";
}

function showbannerdesc() {
	if (ie56) {
		bannerdiv = new getObj('bannerdesc');
		bannerdiv.style.display = 'block';
	}
}
function hidebannerdesc() {
	if (ie56) {
		bannerdiv = new getObj('bannerdesc');
		bannerdiv.style.display = 'none';
	}
}
