function NewWindowA(a) {
	return NewWindow(a.href,a.target,'740','650','yes');
}
function NewWindow(mypage, myname, w, h, scroll) {	
	mypage = mypage.replace(/\#/,'%23');
	mypage = mypage.replace(/\*/,'%2A');
	if (h>screen.height-60) {
		h = screen.height-60;
	}
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2-60;
	if (wint<0) {
		wint = 0;
	}
	var scrollbars = 'scrollbars';
	if (scroll == 'no') {
		scrollbars = '';
	}
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable,'+scrollbars;
	var win;
	if(myname=='opener' && window.opener){
		win = window.opener;
	}else{
		if(myname=='opener'){ myname='_blank'; }
		win = window.open('about:blank', myname, winprops);
		try{
			win.document.open();
			win.document.writeln("\n<html><head><style>body { color: gray; } div#rotate { position:absolute; top:0px; left:0px;} div#rotate span { position:absolute; } h1 { text-align:center; position:relative; top:0; left:0; font-family:Times;}</style>\n<scr"+"ipt>//<!--\nvar c = 0;\nfunction rotate() {\nvar t = document.getElementById('rotate').getElementsByTagName('span');\nfor(var i=0; i < t.length; i++) {\nif ((i % 4) == c) {\nt[i].style.visibility = 'visible';\n} else {\nt[i].style.visibility = 'hidden';\n}\n}\nc = (c + 1) % 4;\nsetTimeout('rotate()', 300);\n}\nfunction draw() {\nvar t = document.getElementById('rotate');\nvar a = Math.PI / 24;\nvar r = 50;\nvar s = '';\nvar w = (document.body.clientWidth / 2);\nvar h = (document.body.clientHeight / 2);\nfor(i=0;i<=48;i++) {\nvar b = document.createElement('span');\nb.style.top = Math.round(Math.sin(i * a) * r) + h;\nb.style.left = Math.round(Math.cos(i * a) * r) + w;\nb.innerHTML = '&middot;';\nt.appendChild(b);\n}\nt = document.getElementsByTagName('h1');\nt[0].style.top = ((document.body.clientHeight / 2) - (2 * r));\n}\n//-->\n</scr"+"ipt>\n</head>\n<body>\n<h1>Loading ....</h1>\n<div id='rotate'>\n</div>\n</body>\n</html>\n");
			win.draw();
			win.rotate();
			win.document.close();
		}catch(e){}
	}
	if (mypage != '' && mypage != 'about:blank') win.location = mypage;
	win.focus();
}

function NewWindowMaximized(mypage, myname) {
	var winl = screen.width-7;
	var wint = screen.height-75;
	winprops = 'height='+wint+',width='+winl+',top=0,left=0,resizable,directories,location,menubar,toolbar,scrollbars';
	win = window.open(mypage, myname, winprops);
	win.focus();
}

function NewWindowStatus(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2-60;
	if (wint<0) {
		wint = 0;
	}
	var scrollbars = 'scrollbars';
	if (scroll == 'no') {
		scrollbars = '';
	}
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable,status,'+scrollbars;
	win = window.open(mypage, myname, winprops);
	win.focus();
}


function NewWindow2(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2-30;
	var scrollbars = 'scrollbars';
	if (scroll == 'no') {
		scrollbars = '';
	}
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable,'+scrollbars;
	win = window.open(mypage, myname, winprops);
	win.focus();
}

function displayURL(url,x,y) {
	props = "resizable,scrollbars,width=" + x + ",height=" + y;
	imagepreview = window.open("","preview_window", props);
	imagepreview.focus();
	imagepreview.location=url;
	return false;
}
function NewSlowWindow(mypage, myname, w, h, scroll, text) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2-30;
	var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable';
	var win = window.open('', myname, winprops);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
	win.document.body.innerHTML = '';;
	win.document.writeln('<center><br><font color="darkblue">' + text + '</font></center>'); // win.close();
	win.location = mypage;
	return win;
}


function GoSiteMap(sel) {
	var goref = sel.options[sel.selectedIndex].value;
	// See if we create a new window
	sel.selectedIndex = 0;
	sel.blur(); // Ensure focus doesn't stay on dropdown
	if (goref=='') {
		// Empty string. Do nothing
		return false;
	}
	if (goref.search(/NW\|/)==0) {
		// Open new window, so find coords
		// NW:640:480:href
		var NewWArr = goref.split(/\|/);
		NewWindowStatus(NewWArr[3],'_blank',NewWArr[1],NewWArr[2],'yes');
		return false;
	}
	// Standard link. Change target and run
	parent.dvd_main.location.href=goref;
	return false;
}

// Cart Functions.
function getSizes(a,pid){
	var ret = new Array();
	var size=document.getElementById('sizecolor_id_'+pid);
	if(!size) return ret;
	if(size.type == 'hidden'){
			ret.push(size.value);
			return ret;
	}
	var pd = size.getElementsByTagName('input');
	var sizeStr = '';
	for(var i=0; i<pd.length; i++) {
		if(pd[i] && (pd[i].checked || pd[i].type == 'hidden') && pd[i].value>=0){
			ret.push(pd[i].value);
		}
	}
	return ret;
}
function setCartUrl(a,pid){
	if(!pid || pid<=0) { return true; }
	var qty=document.getElementById('qty_'+pid);
	if(qty){ a.href=a.href.replace(/qty_\d+/,('qty_'+qty.value)); }	
	var sizes=getSizes(a,pid);
	if(sizes==false) return false;
	var sizeStr = '';
	for(var i=0; i<sizes.length; i++) {
			sizeStr += '/size_'+sizes[i];
	}	
	if(sizeStr != '') {
		a.href = a.href.replace(/(\/size_[-\d]+)+/,sizeStr);
		return true;
	}
	return false;
}
function showCart(a,pid){
	var ret = setCartUrl(a,pid);
	if(ret){
		var cookieText = Get_Cookie('CartProds');
		if(cookieText) {
			if(a.href.match(/cookie=Start/)) {
				a.href = a.href.replace(/cookie=Start.*/,'cookie='+cookieText);
			} else {
				a.href += '?cookie='+cookieText;
			}
		}
		// alert(a.href);
		clearCartCookie();
		NewWindow(a.href,a.target,'650','1600','yes');
		return false;
	}else{
		alert('Please select a Size / Colour to purchase this product');
		return false;
	}
}


var switchLargeImage;
function switchLargeImage(on,off){
	if(!(on && off)){
		if(document.getElementById('largeimage_href_front').style.display=='inline'){
			off = 'back';
			on = 'front';
		}else{
			off = 'front';
			on = 'back';
		}
	}
	try {
		document.getElementById('largeimage_'+off).style.display='none';
		document.getElementById('largeimage_'+on).style.display='block';

		document.getElementById('largeimage_href_'+off).style.display='inline';
		document.getElementById('largeimage_href_'+on).style.display='none';
	}catch(e){}
}

//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com
function clickIE4(){
	if (event.button==2){
		alert('Function Disabled!');
		return false;
	}
}
function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			alert('Function Disabled!');
			return false;
		}
	}
}
function disableRightMouse(){
   var node = document.getElementById("largeimage_click_div");
   if(node){
	   if (node.layers){
		   node.captureEvents(Event.MOUSEDOWN);
		   node.onmousedown=clickNS4;
	   }
	   else if (node.all&&!node.getElementById){
		   node.onmousedown=clickIE4;
	   }
	   node.oncontextmenu=new Function("alert('Function Disabled!');return false");
   }
}

// Set links to local to show spinning load first.
function getScrollingPosition() {
	var position = [0, 0];
	if (typeof window.pageYOffset != 'undefined') {
		position = [ window.pageXOffset, window.pageYOffset ];
	} else if (typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0) {
		position = [ document.documentElement.scrollLeft, document.documentElement.scrollTop ];
	} else if (typeof document.body.scrollTop != 'undefined') {
		position = [ document.body.scrollLeft, document.body.scrollTop ];
	}
	return position[1];
}

function rotate(c) {
	if(!c){ c=0; }
	var loading = document.getElementById('loading');
	if(loading){
		var vpHeight = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight);
		if(document.body.clientHeight && vpHeight){
			loading.style.top = (getScrollingPosition() + (vpHeight / 2) - 100) + 'px';
		}
		var t = loading.getElementsByTagName('span');
		for(var i=0; i < t.length; i++) {
			if ((i % 4) == c) {
				t[i].style.visibility = 'visible';
			} else {
				t[i].style.visibility = 'hidden';
			}
		}
		c = (c + 1) % 4;
	}
	setTimeout('rotate('+c+')', 300);
}

function draw() {
	try{
		var loading = document.getElementById('loading');
		var t = document.getElementById('rotate');
		if(!loading){
			//<div id='loading'><h1>Loading ....</h1><div id='rotate'></div></div>
			loading = document.createElement('div');
			loading.id='loading';
			var h1 = document.createElement('h1');
			h1.appendChild(document.createTextNode('Loading ....'));
			loading.appendChild(h1);
			t = document.createElement('div');
			t.id='rotate';
			loading.appendChild(t);
			document.body.appendChild(loading);
		}
		var a = Math.PI / 24;
		var r = 50;
		var s = '';
		for(i=0;i<=48;i++) {
			var b = document.createElement('span');
			b.appendChild(document.createTextNode('.'));
			b.style.top = (Math.round(Math.sin(i * a) * r) + r + 40)+'px';
			b.style.left = (Math.round(Math.cos(i * a) * r) + r + 30)+'px';
			b.style.color = 'gray';
			t.appendChild(b);
		}
		// alert(t.innerHTML);
		loading.style.top = '100px'
		loading.style.left = (document.body.clientWidth ? ((document.body.clientWidth/2)-90)+'px' : '500px');

		// Set in middle
		var vpHeight = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight);
		if(document.body.clientHeight && vpHeight && loading && loading.style){
			loading.style.top = (getScrollingPosition() + (vpHeight / 2) - 100) + 'px';
		}
	}catch(err){ alert(err); }
}

function hideSpinner(doc,loading,main) {
	// alert(doc + ' : ' + loading + ' : ' + main);
	try{
		if(!doc) doc=document;
		if(!loading) loading = doc.getElementById('loading');
		if(loading && loading.display=='block' || loading.style.display=='block'){
			if(!main) content = doc.getElementById('main');
			loading.display='none';
			loading.style.display='none';
			if(main){
				main.visibility='';
				main.style.visibility='';
			}
		}
	}catch(err){}
}
function showSpinner(loading,main) {
	try{
		window.scrollTo(0,0);
		if(!loading) loading = document.getElementById('loading');
		if(!loading) draw();
		if(!loading) loading = document.getElementById('loading');
		if(loading){
			if(!main) main = document.getElementById('tdbody');
			loading.display='block';
			if(loading.style) loading.style.display='block';
			if(main){
				main.visibility='hidden';
				if(main.style) main.style.visibility='hidden';
			}
		}
		rotate();
	}catch(err){alert(err);}
	return true;
}

function setSpinner() {
	var divbody = document.getElementById('tdbody');
	if(!divbody) divbody = document.getElementById('main');
	if(!divbody) divbody = document.getElementById('content');
	if(!divbody) return;
	var a = document.getElementsByTagName('A');
	var hasCookie = (Get_Cookie('VSession')=='' ? 0 : 1);
	if(window.location && window.location.toString().match(/^https/)) { hasCookie = false; }
	var match = location.href.match('/(adult-(gay-)?dvds)/');
	for(var i=0; i<a.length; i++){
		if(a[i].href && a[i].href!='' && a[i].href.toString().indexOf('#')==-1 && a[i].href.toString().substr(0,7)!='mailto:' && a[i].href.toString().substr(0,11)!='javascript:'){
			if(!a[i].target && !a[i].onclick) {
				var aNode = a[i];
				if(match && match[1] && a[i].href.match(match[1])) {
					aNode.onclick = getPageMiddle;
				} else {
					aNode.onclick = showSpinner;
				}

				// Removed Session_id from string.
				var hrefStr = hasCookie ? a[i].href.toString() : '';
				if(hasCookie) {
					var hrefStr = a[i].href.toString();
					if(!hrefStr.match(/^http/) && hrefStr.match(/\/S[a-fA-F0-9]{16}\//)) {
						hrefStr = hrefStr.replace(/\/S[a-fA-F0-9]{16}\//,'/');
						a[i].href = hrefStr;
					}
					if(hrefStr.match(/sess_id=S[a-fA-F0-9]{16}/)) {
						hrefStr = hrefStr.replace(/sess_id=S[a-fA-F0-9]{16}&?/g,'');
						a[i].href = hrefStr;
					}
					a[i].href = hrefStr = hrefStr.replace(/\&$/,'');
					a[i].href = hrefStr = hrefStr.replace(/\?$/,'');
				}
			}
		}
	}
}


//if(window.addEventListener){window.addEventListener('load',setSpinner,false);}
//else if(window.attachEvent){window.attachEvent('onload',setSpinner);}

if(window.addEventListener){window.addEventListener('unload',hideSpinner,false);}
else if(window.attachEvent){window.attachEvent('onunload',hideSpinner);}

// Make checkboxes pretend to be radio boxes ; uses a surrounding div to make worl.
function pretentRadio(r) {
	var div = r.parentNode;
	while(div && div.nodeName != 'DIV') { div = div.parentNode; }
	if(div.nodeName == 'DIV') {
		var radios = div.getElementsByTagName('input');
		for(var i=0; i<radios.length; i++) {
			if(radios[i].type=='checkbox' && radios[i] != r){
				radios[i].checked = false;
			}
		}
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
function Get_Cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return ''; }
	if ( start == -1 ) return '';
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
					obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
function hideMsgBox() { var box=document.getElementById('msgBox'); if(box){box.style.display='none';} }
function msgBox(txt,top2) {
	var box=document.getElementById('msgBox');
	if(!box){
		box = document.createElement('div');
		box.id = 'msgBox';
		document.body.appendChild(box);
	}
	while(box.hasChildNodes && box.firstChild) { box.removeChild(box.firstChild); }
	var box2 = document.createElement('div');
	box.appendChild(box2);
	box2.appendChild(document.createTextNode(txt));
	box.style.display='block';
	box.style.left = (document.body.clientWidth ? ((document.body.clientWidth/2)-100)+'px' : '500px');
	var vpHeight = (window.innerHeight ? window.innerHeight : document.documentElement.clientHeight);
	if(top2 && top2>0){
		box.style.top = (top2)+'px';
	} else if(document.body.clientHeight && vpHeight){
		box.style.top = ((vpHeight / 2) - 200) + 'px';
	} else {
		box.style.top = '300px';
	}

	if(box.addEventListener){box.addEventListener('click',hideMsgBox,false);}
	else if(box.attachEvent){box.attachEvent('onclick',hideMsgBox);}
	// alert('here i am: '+box.style.top);
	setTimeout('hideMsgBox()',750);
}

// Cart Functions.
function clearCartCookie() {
	Set_Cookie('CartProds','Start',4,'/');
	var cookieText = Get_Cookie('CartProds');
	//alert('clearCartCookie: New = '+cookieText);
}
function addToCart(a,pid,tid){	
	var ret = setCartUrl(a,pid);
	if(!ret){
		alert('Please select a Size / Colour to purchase this product');
		return false;
	}

	if(document.getElementById) {
		var cookieText = Get_Cookie('CartProds');
		if(!cookieText){Set_Cookie('CartProds','Start',4,'/');}
		cookieText = Get_Cookie('CartProds');
		if(cookieText && tid>0){
			var tmp = '::' + 't'+tid+',p'+pid;
			var qty=document.getElementById('qty_'+pid);
			if(qty && qty.value>0){ cookieText += ',q'+qty.value; }
			// Set Sizes.
			var sizes=getSizes(a,pid);
			if(sizes.length>0){
				for(var i=0;i<sizes.length;i++){
					cookieText += tmp + ',s' + sizes[i];
				}
			} else {
				cookieText += tmp;
			}

			Set_Cookie('CartProds',cookieText,4,'/');
			msgBox('Product Added to Cart',findPosY(a));
			// alert(cookieText);
		}
		var iframe=document.getElementById('addtocartframe');
		if(!iframe){
			//<iframe id="addtocartframe" width="1" height="1" src="about:blank"></iframe>
			iframe = document.createElement('iframe');
			iframe.id = 'addtocartframe';
			iframe.width = 1;
			iframe.height = 1;
			iframe.src = 'about:blank';
			document.body.appendChild(iframe);
		}
		if(iframe){
			iframe.src=a.href;
			return false;
		}
	}
	return true;
}

function setFormSessID(inp) {
	if(inp && inp.value=='') {
		var m = document.location.pathname.match(/^\/S([0-9a-fA-F]{16})\//);
		if(m && m[1]) {
			inp.value = 'S'+m[1];
		} else {
			m = document.location.search.match(/sess_id=S([0-9a-fA-F]{16})/);
			if(m && m[1]) {
				inp.value = 'S'+m[1];
			}
		}
	}
	// alert(inp.value);
}
