/*
====================================================

  Copyright 2010 Internet Specialty Services, LLC.

  ANY UN-AUTHORIZED USE OR DISTRIBUTION OF THIS
  FILE OR IT'S CONTENTS IS STRICTLY PROHIBITED

====================================================
*/


var ActionFileUrl = 'http://'+window.location.host+'/iss/wheretobuy_response.php?lightbox=1&';


var d = new Date();
document.write('<scr'+'ipt type="text/javascript" src="http://'+window.location.host+'/iss/ProdFinder_ItemsAvailable.js?x="'+d.getDate()+''+d.getDay()+'.'+d.getHours()+'></scr'+'ipt>');


function issProdFinder(tObj) {
	var tLink = ActionFileUrl+'sku='+tObj.id;

	//window.open(tLink,"prodfinder","menubar=0,resizable=1,width=650,height=400");
	//Lightbox.showBoxByAJAX(tLink,650,400);

	tb_show('',tLink+'&height=400&width=650&modal=true','');
}


function issProdFinder_ShowBTN() {
	var tArr = PF_InStock.split('^');
	var tArrLen = tArr.length;
	var tID='';
	var tIDobj;
	var naObj;

	for (var i=0;i<=tArrLen;i++) {
		tID = tArr[i];
		if (tID!='') {
			tIDobj = document.getElementById(tID);
			if (tIDobj) {
				tIDobj.style.display='inline';
				naObj = document.getElementById('ISS_NotAvailable');
				if (naObj) {naObj.style.display='none';}
				}
			}
		}
}





/*
	Multifaceted Lightbox
	by Greg Neustaetter - http://www.gregphoto.net
	
	INSPIRED BY (AND CODE TAKEN FROM)
	==================================
	The Lightbox Effect without Lightbox
	PJ Hyett
	http://pjhyett.com/articles/2006/02/09/the-lightbox-effect-without-lightbox
	

	Lightbox JS: Fullsize Image Overlays 
	by Lokesh Dhakar - http://www.huddletogether.com

	For more information on this script, visit:
	http://huddletogether.com/projects/lightbox/

	Licensend under:
	Creative Commons Attribution 2.5 License - http://creativecommons.org/licenses/by/2.5/
	(basically, do anything you want, just leave my name and link)
	
*/

var Lightbox = {
	lightboxType : null,
	lightboxCurrentContentID : null,
	
	showBoxString : function(content, boxWidth, boxHeight){
		this.setLightboxDimensions(boxWidth, boxHeight);
		this.lightboxType = 'string';
		var contents = $('boxContents');
		contents.innerHTML = content;
		this.showBox();
		return false;
	},


	showBoxImage : function(href) {
		this.lightboxType = 'image';
		var contents = $('boxContents');
		var objImage = document.createElement("img");
		objImage.setAttribute('id','lightboxImage');
		contents.appendChild(objImage);
		imgPreload = new Image();
		imgPreload.onload=function(){
			objImage.src = href;
			Lightbox.showBox();
		}
		imgPreload.src = href;
		return false;
	},

	showBoxByID : function(id, boxWidth, boxHeight) {
		this.hideScrollbar(true);
		if (getBrowser() == "IE6"){
			this.hideElements(true);
		}
		
		this.lightboxType = 'id';
		this.lightboxCurrentContentID = id;
		this.setLightboxDimensions(boxWidth, boxHeight);
		var element = $(id);
		var contents = $('boxContents');
		contents.appendChild(element);
		Element.show(id);
		this.showBox();
		return false;
	},

	showBoxByAJAX : function(href, boxWidth, boxHeight) {
		// alert('hideScrollbar by zk');
		this.hideScrollbar(true);
		if (getBrowser() == "IE6"){
			this.hideElements(true);
		}
	
		this.lightboxType = 'ajax';
		this.setLightboxDimensions(boxWidth, boxHeight);
		var contents = $('boxContents');
		contents.innerHTML = '<h2 style="text-align:center;margin-top:75px;">Loading...<br><img src="iss/img/loading-bar.gif" width="126" height="22" border="0"></h2><br><br><a href=""javascript:void(0);"" onclick=""tb_remove();"">Close</a>';
		var myAjax = new Ajax.Updater(contents, href, {method: 'get'});
		this.showBox();
		return false;
	},
	
	hideScrollbar : function(bHidden){
		// alert('hideScrollbar by zk');
		var temp_h1 = document.body.clientHeight;
		var temp_h2 = document.documentElement.clientHeight;
		var isXhtml = (temp_h2 <= temp_h1 && temp_h2 != 0) ? true : false;
		var htmlbody = isXhtml ? document.documentElement : document.body;
		//alert(getBrowser());
		if (!bHidden){
			var sBrer = getBrowser();
			if (sBrer != "safari"){
				if (sBrer == "IE6" || sBrer == "IE7"){
					window.scrollTo(htmlbody.scrollLeft, htmlbody.scrollTop);
					if (sBrer == "IE6"){
						htmlbody.style.overflowY = "auto";
					}else{
						htmlbody.style.overflow = "auto";
					}
				}else{
					htmlbody.style.overflow = "auto";
				}
			}else{
				window.scrollTo(window.pageXOffset,window.pageYOffset);
				htmlbody.style.overflow = "scroll";
			}
		}else{
			htmlbody.style.overflow = "hidden";
		}
		return true;
	},
	
	hideElements : function(bHide) {
		if (bHide){
			if (!window.__temp__){
				window.__temp__ = new Array();
			}
			var o = ["select","iframe","applet","object"];
			for (var i = 0; i < o.length; i ++) {
				var elms = document.getElementsByTagName(o[i]);
				for (var j = 0; j < elms.length; j ++) {
		            window.__temp__[j] = elms[j];
		            elms[j].style.visibility = "hidden";
		        }
			}
		}else{
			if (window.__temp__){
				var t = window.__temp__;
				if (t){
					for (var i=0; i < t.length; i++){
						if (t[i]){
							t[i].style.visibility = "visible";
						}
					}
				}
			}
		}
	},
	
	setLightboxDimensions : function(width, height) {
		var windowSize = this.getPageDimensions();
		if(width) {
			if(width < windowSize[0]) {
				$('box').style.width = width + 'px';
			} else {
				$('box').style.width = (windowSize[0] - 50) + 'px';
			}
		}
		if(height) {
			if(height < windowSize[1]) {
				$('box').style.height = height + 'px';
			} else {
				$('box').style.height = (windowSize[1] - 50) + 'px';
			}
		}
	},


	showBox : function() {
		Element.show('overlay');
		this.center('box');
		return false;
	},
	
	
	hideBox : function(){
		var contents = $('boxContents');
		if(this.lightboxType == 'id') {
			var body = document.getElementsByTagName("body").item(0);
			Element.hide(this.lightboxCurrentContentID);
			body.appendChild($(this.lightboxCurrentContentID));
		}
		contents.innerHTML = '';
		$('box').style.width = null;
		$('box').style.height = null;
		Element.hide('box');
		Element.hide('overlay');
		this.hideScrollbar(false);
		if (getBrowser() == "IE6"){
			this.hideElements(false);
		}
		return false;
	},
	
	// taken from lightbox js, modified argument return order
	getPageDimensions : function(){
		var xScroll, yScroll;
	
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		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;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		arrayPageSize = new Array(windowWidth,windowHeight,pageWidth,pageHeight) 
		return arrayPageSize;
	},
	
	center : function(element){
		try{
			element = document.getElementById(element);
		}catch(e){
			return;
		}
		var windowSize = this.getPageDimensions();
		var window_width  = windowSize[0];
		var window_height = windowSize[1];
		
		$('overlay').style.height = windowSize[3] + 'px';
		/*if (document.getElementById("HSWrap")){
			alert(document.getElementById("HSWrap").style.zIndex);
		}*/
		//alert(document.documentElement.style.zIndex);
		element.style.position = 'absolute';
		element.style.zIndex   = 10000;
	
		var scrollY = 0;
	
		if ( document.documentElement && document.documentElement.scrollTop ){
			scrollY = document.documentElement.scrollTop;
		}else if ( document.body && document.body.scrollTop ){
			scrollY = document.body.scrollTop;
		}else if ( window.pageYOffset ){
			scrollY = window.pageYOffset;
		}else if ( window.scrollY ){
			scrollY = window.scrollY;
		}
	
		var elementDimensions = Element.getDimensions(element);
		var setX = ( window_width  - elementDimensions.width  ) / 2;
		var setY = ( window_height - elementDimensions.height ) / 2 + scrollY;
	
		setX = ( setX < 0 ) ? 0 : setX;
		setY = ( setY < 0 ) ? 0 : setY;
	
		element.style.left = setX + "px";
		element.style.top  = setY + "px";

		Element.show(element);
	},
	
	init : function() {				  
		var lightboxtext = '<div id="overlay" style="z-index:9999;display:none"></div>';
		lightboxtext += '<div id="box" style="z-index:9999;display:none">';
	//lightboxtext += '<img id="close" src="close.gif" onClick="Lightbox.hideBox()" alt="Close" title="Close this window" />';
		lightboxtext += '<div id="boxContents" style="z-index:10000;"></div>';
		lightboxtext += '</div>';
		var body = document.getElementsByTagName("body").item(0);
		new Insertion.Bottom(body, lightboxtext);
	}
}



/*
function getBrowser() {
	var sResult = "other";
	var s = navigator.userAgent.toLowerCase();
	var a = new Array("msie", "firefox", "safari", "opera", "netscape");
	for(var i = 0; i < a.length; i ++) {
		if(s.indexOf(a[i]) != -1) {
			sResult = a[i];
			}
		}

	if (sResult == "msie") {
		sResult = "IE6";
		if (window.XMLHttpRequest) {
			sResult = "IE7";
			}
		}

	TheBrowser = sResult;

	return sResult;
}*/




















var TheBrowser = '';



var ajAX = function(){
	this.xmlhttp = null;
	
	this.init();
	this.initXmlHttp();
}
ajAX.prototype.initData = function(){
	// set and get, obligatory
	this.requestFile = "";
	this.URLString = "";
	
	// set and get, optional
	this.failed = false;
	this.method = "GET";
	this.queryStringSeparator = "?";
	this.argumentSeparator = "&";
	this.bAsync = true;
	this.execute = false;
	this.elementID = "";
	this.callAfter = "";
		
	// just get
	this.responseText = "";
	this.oInterval = null;
}
	
ajAX.prototype.initEvent = function() {
	this.onLoading = function() {  };
	this.onLoaded = function() {  };
	this.onInteractive = function() {  };
	this.onCompletion = function() {  };
	this.onError = function() {  };
	this.onFail = function() {  };
}
	
ajAX.prototype.init = function(){
	this.initData();
	this.initEvent();
}



// CAN I USE THE MAIN GETBROWSER????	
ajAX.prototype.getBrowser = function() {
	var sResult = "other";
	var s = navigator.userAgent.toLowerCase();
	var a = new Array("msie", "firefox", "safari", "opera", "netscape");
	for(var i = 0; i < a.length; i ++) {
		if(s.indexOf(a[i]) != -1) {
			sResult = a[i];
		}
	}
	if (sResult == "msie") {
		sResult = "IE6";
		if (window.XMLHttpRequest) {
			sResult = "IE7";
		}
	}
	return sResult;
}
	
ajAX.prototype.initXmlHttp = function(){
	try {
		this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e1) {
		try {
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2) {
			this.xmlhttp = null;
		}
	}
	if (!this.xmlhttp) {
		if (typeof XMLHttpRequest != "undefined") {
			this.xmlhttp = new XMLHttpRequest();
		} else {
			this.failed = true;
		}
	}
}
	
ajAX.prototype.handleStateChange = function(){
	//alert(this.xmlhttp.responseText);
	switch(this.xmlhttp.readyState) {
		case 1:
			this.onLoading();
			break;
		case 2:
			this.onLoaded();
			break;
		case 3:
			this.onInteractive();
			break;
		case 4:
			//alert(this.xmlhttp.status);
			if (this.xmlhttp.status == 200) {
				if (this.oInterval){
					window.clearInterval(this.oInterval);
					this.oInterval = null;
					}
				this.responseText = this.xmlhttp.responseText;


			
				if (this.elementID){
					var oObj = document.getElementById(this.elementID);
					if (oObj) {
						var elemNodeName = oObj.nodeName;
						elemNodeName = elemNodeName.toLowerCase();
						if (elemNodeName == "input"
							|| elemNodeName == "select"
							|| elemNodeName == "option"
							|| elemNodeName == "textarea") {
							oObj.value = this.responseText;
							}
						else {
							oObj.innerHTML = this.responseText;
							}
						}
					}

//alert(oObj.innerHTML);
				if (this.execute) {
					if (this.callAfter!='') {
						var tcA = this.callAfter;
						tcA = tcA.replace(/{BACK}/,this.responseText);
						eval(tcA);
						}
					else {
						eval(this.responseText);
						}
					}



				this.onCompletion();
			}else{
				this.onError();
			}
			break;
	}
}
	
ajAX.prototype.doRequest = function(){
	if (this.failed == true){
		this.onFail();
	}else{
		if (this.xmlhttp) {
			var _this = this;
			if (this.method == "GET"){
				var totalurlstring = this.requestFile;
				if (this.URLString){
					totalurlstring += this.queryStringSeparator + this.URLString;
				}
				this.xmlhttp.open(this.method, totalurlstring, this.bAsync);
				this.xmlhttp.setRequestHeader("Content-Length", 0);
			}else if (this.method == "POST"){
				this.xmlhttp.open(this.method, this.requestFile, true);
				try {
					this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					this.xmlHTTP.setrequestheader("contentType", "text/html;charset=uft-8");
				} catch (e) { }
			}
			callback = function(){ _this.handleStateChange(); };
			//if (getBrowser() != "firefox") {
				this.xmlhttp.onreadystatechange = callback;
			//}else{
			//	this.oInterval = window.setInterval(callback, 10);
			//}
			this.xmlhttp.send(this.URLString);
		}
	}
}



/*
function SetOptOnChg() {
	var tOpt = document.getElementById('theOption');
	if (tOpt) {
		tOpt.onchange = function() {
			var aJ = new ajAX();
			aJ.bAsync = false;
			aJ.requestFile = ActionFileUrl + 'option='+this.value;
			//aJ.URLString = 'upc='+ts;
			aJ.elementID = 'StoreList';
			//aJ.execute = true;
			//aJ.callAfter = 'afterRateRev();';
			aJ.doRequest();



			}
		}
	else {
		document.getElementById('SelectASize').innerHTML = 'nope';
		}	
}
*/



function OptChanged() {
	hideID('StoreListRow');
	setHTML('StoreList','');

}



function OptSelected() {
	var tOpt = document.getElementById('theOption').value;
	if (tOpt=='') {
		setHTML('StoreList','Please select an option from the dropbox above');
		showID('StoreListRow');
		return;
		}

	setHTML('StoreList','Loading...');
	showID('StoreListRow');

	var aJ = new ajAX();
	aJ.bAsync = true;
	aJ.requestFile = ActionFileUrl + 'option='+tOpt;
//aJ.URLString = 'upc='+ts;
	aJ.elementID = 'StoreList';
//aJ.execute = true;
//aJ.callAfter = 'hideID('StoreListRow');';
	aJ.doRequest();
}



function FindByCityZip(ts) {
	var tZip = document.getElementById('CityZip').value;
	if (tZip=='Enter Your Zipcode' || tZip == '') {
		setHTML('RetailList','Please enter your zipcode in the field above<br>&nbsp;');
		showID('RetailListRow');
		return;
		}

	setHTML('RetailList','Loading...');
	showID('RetailListRow');

	var aJ = new ajAX();
	aJ.bAsync = true;
	aJ.requestFile = ActionFileUrl + 'sku='+ts+'&cityzip='+tZip;
//aJ.URLString = 'upc='+ts;
	aJ.elementID = 'RetailList';
//aJ.execute = true;
//aJ.callAfter = 'hideID('StoreListRow');';
	aJ.doRequest();
}



function LoadOptions(ts) {
	//alert(ts);

	var aJ = new ajAX();
	aJ.bAsync = true;
	aJ.requestFile = ActionFileUrl + 'sku='+ts;
	//aJ.URLString = 'upc='+ts;
	aJ.elementID = 'where2Buy';
	//aJ.execute = true;
	//aJ.callAfter = 'SetOptOnChg';
	aJ.doRequest();
}





function setHTML(tEid,tHTML) {
	var t = document.getElementById(tEid);
	if (t) {t.innerHTML = tHTML;}
}


function showID(tEid) {
	var tt;
	if (typeof(tEid)=='object') {
		tt = tEid;
		}
	else {
		tt = document.getElementById(tEid);
		}

	if (tt) {
		if (TheBrowser.length < 1) {TheBrowser = getBrowser();}

		if (Left(TheBrowser,2) != 'IE' && tt.tagName.toUpperCase() == 'TR') {
			tt.style.display = 'table-row';
			}
		else {
			tt.style.display = 'block';
			}
		}
}

function hideID(tEid) {
	var tt;
	if (typeof(tEid)=='object') {
		tt = tEid;
		}
	else {
		tt = document.getElementById(tEid);
		}

	if (tt) {tt.style.display = 'none';}
}


function GetElCoordinate(e) {
	var t = e.offsetTop;
	var l = e.offsetLeft;
	var w = e.offsetWidth;
	var h = e.offsetHeight;
	while (e = e.offsetParent) {
		t += e.offsetTop;
		l += e.offsetLeft;
		}

	return {
		top: t,
		left: l,
		width: w,
		height: h,
		bottom: t + h,
		right: l + w
		}
}



function getPosition(el){
	var e = el.offsetParent, x = el.offsetLeft, y = el.offsetTop;
	while(e){
		x += e.offsetLeft;
		y += e.offsetTop;
		e = e.offsetParent;
	}
	return{
		"left": x,
		"top": y,
		"width": el.offsetWidth,
		"height": el.offsetHeight
	}
}



function mouseCoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return{
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop - document.body.clientTop
	};
}





function getBrowser() {
	var sResult = "other";
	var s = navigator.userAgent.toLowerCase();
	var a = new Array("msie", "firefox", "safari", "opera", "netscape");
	for(var i = 0; i < a.length; i ++) {
		if(s.indexOf(a[i]) != -1) {
			sResult = a[i];
			}
		}

	if (sResult == "msie") {
		sResult = "IE6";
		if (window.XMLHttpRequest) {
			sResult = "IE7";
			}
		}

	TheBrowser = sResult;

	return sResult;
}




function Left(str,n) {
	if (n <= 0) {return "";}
	else if (n > String(str).length) {return str;}
	else {return String(str).substring(0,n);}
}

function Right(str,n) {
	if (n <= 0) {return "";}
	else if (n > String(str).length) {return str;}
	else {var iLen = String(str).length;return String(str).substring(iLen, iLen - n);}
}





function AddDbOpt(db,ts,tv) {
	if (!db) {return;}
	var tO = document.createElement("OPTION");
	tO.text = ts;
	tO.value = tv;
	db.options.add(tO);
}

function ClearSelDbOpt(db) {
	if (!db) {return;}
	var i;
	for(i=db.options.length-1;i>=0;i--) {
		if(db.options[i].selected) {
			db.remove(i);
			}
		}
}

function ClearDbOpt(db) {
	if (!db) {return;}
	var i;
	for(i=db.options.length-1;i>=0;i--) {
		db.remove(i);
		}
}




function trim(ts) {return ts.replace(/^\s+|\s+$/g,"");}

function ltrim(ts) {return ts.replace(/^\s+/,"");}

function rtrim(ts) {return ts.replace(/\s+$/,"");}

function Len(tFld) {if (tFld) {return tFld.value.length;} else {return 0;}}




function AddFFE(tID,tEvt,tFunc) {
	var tE = document.getElementById(tID);
	if (!tE) {return;}

	if (tE.addEventListener){
		tE.addEventListener(tEvt,function(){eval(tFunc)}, false);
		}
	else if (tE.attachEvent) {
		tE.attachEvent('on'+tEvt,function(){eval(tFunc)});
		}
}


function ReadyPage() {
	var fTxt = 'Enter Your Zipcode';
	AddFFE("CityZip","focus","if (document.getElementById('CityZip').value == '"+fTxt+"') {document.getElementById('CityZip').value = '';}")
	AddFFE("CityZip","blur","if (trim(document.getElementById('CityZip').value) == '') {document.getElementById('CityZip').value = '"+fTxt+"';}")
}


function ZipBox(tObj,tEvnt) {

//tObj.setAttribute('maxlength',5);

	var ztxt = trim(tObj.value);

	var fTxt = 'Enter Your Zipcode';
	if (tEvnt=='blur') {
		if (ztxt == '') {tObj.value = fTxt;}
		}
	else {
		if (ztxt == fTxt) {tObj.value = '';}
		}
}



if (window.addEventListener){
	window.addEventListener('load', ReadyPage, false);
	}
else if (window.attachEvent) {
	window.attachEvent('onload', ReadyPage);
}