//specify page to pop-under
var popunder = "http://www.yellowfarmhousewines.com/playmusic.html";

//specify popunder window features
//set 1 to enable a particular feature, 0 to disable
var winfeatures = "width=20,height=20,scrollbars=0,resizable=0,toolbar=0,location=0,menubar=0,status=0,directories=0";

var W3CDOM = document.createElement && document.getElementsByTagName;
var ST_newWindow;
//alert(ST_newWindow);
//alert(readCookie('music'));
//alert(readCookie('ST_popup'));
if ( readCookie('music') !== 'off' && !ST_newWindow ){
	addEventSimple(window,"load",ST_init);
}
var ST_loaded = false;

function ST_init() {
	if (!W3CDOM) return;
/*	if (!ST_cookiesSupported) return;*/
	ST_loaded = true;
	addEventSimple(window,"unload",ST_exit);
	ST_openPopup();
//	addEventSimple(document,"load",ST_openPopup);
}

function ST_openPopup() {
	if (readCookie('ST_popup') && (!ST_newWindow || ST_newWindow.closed) ) return;
//	if ( !ST_newWindow || ST_newWindow.closed ){
		ST_newWindow = window.open(popunder,'ST_window',winfeatures);
		ST_newWindow.blur();
		//removeEventSimple(document,"click",ST_openPopup);
		createCookie('music','on',1);
//	}
}

function ST_exit() {
	ST_loaded = false;
	if (readCookie('ST_popup') == 'opened' && ST_newWindow){
		ST_newWindow.trackMain(location.href);
	}
}

function ST_closePopup() {
	if (readCookie('ST_popup')){
//		alert('detected');
		eraseCookie('ST_popup');
	}
	createCookie('music','off',1);
//	alert(ST_newWindow);
//	alert(!ST_newWindow.closed);
	if (ST_newWindow && !ST_newWindow.closed){
		ST_newWindow.close();
	}
}

/********************

      UTILITIES
      
********************/

function addEventSimple(obj,evt,fn) {
	if (obj.addEventListener)
		obj.addEventListener(evt,fn,false);
	else if (obj.attachEvent)
		obj.attachEvent('on'+evt,fn);
}

function removeEventSimple(obj,evt,fn) {
	if (obj.removeEventListener)
		obj.removeEventListener(evt,fn,false);
	else if (obj.detachEvent)
		obj.detachEvent('on'+evt,fn);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/;"+" domain=.yellowfarmhousewines.com;";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function newwin(page,X,Y){
	var settings = 'width='+X+',height='+Y+',scrollbars=yes,location=no,left=10,top=10,menubar=no,resizable=yes,toolbar=no,status=yes,alwaysRaised=yes';
	if (ressult && !ressult.closed) {
		ressult.focus();
	} else {
		var ressult = window.open(page,'ressult',settings);
		ressult.focus();
	}
	return false;
}

function isReady(form){
    var jpos = form.bsize.selectedIndex;
    var zpos = form.bsize.options[jpos].value;
      
    if(zpos==""){
      alert("Please choose a size.");
    return false;
    }
  return true;
}
