﻿// JScript File

loadTime = new Date().getTime();

function favButton_onclick(i_id, origImageType, t_id,comp_id)
	{
		var timeout = 1200 * 1000;
		// If a shipped button, the link is to the queue page, so don't do any of this
		// malarky.
		if (origImageType == 5) {                      // Shipped
			//location.href = '/view_queue.php?';
			return;
		}
		// Check that session timeout hasn't passed
		if ( (loadTime + timeout) < (new Date().getTime()) ) {
			alert('Alert! Your session may have timed out. Please refresh the page, and try again.');
			return;
		}

		if (!document.getElementById && document.all) {
			document.getElementById = document.all;
		}

		if (document.getElementById) {
			// A little entropy to ensure no caching takes place
			var date    = new Date();
			var entropy = String(date.getHours()) + String(date.getMinutes()) + String(date.getSeconds()) + String(date.getMilliseconds());

			// Create a new Image object, and set the source to the fav script
			// In effect calls the fav script to register the queued/unqueued title
			var favScriptImg = new Image();
			favScriptImg.src = 'addtoFavs.aspx?comp_id=' + comp_id + '&i_id=' + i_id + '&t_id=' + t_id + '&entropy=' + entropy + '&';

			// Grab image object...
			var favButtonImgObject = document.getElementById('favButtonImg_' + i_id);

			// If the fav button object was successfully retrieved...
			if (favButtonImgObject) {
				// Grab the curfav image src, and get the filename from it
				if (favButtonImgObject.src.indexOf('/') != -1) {
					var pos = favButtonImgObject.src.lastIndexOf('/');
					var filename = favButtonImgObject.src.substring(pos + 1);
				} else {
					var filename = favButtonImgObject.src;
				}

				// Determine the new filename based on the curfav filename
				var newFilename;
				var newAltText;

				if (filename == 'btn_addToFav.gif' || filename == 'btn_prop.gif') {
					newFilename = 'images/btn_FavDone.gif';
					newAltText  = 'Remove from My Enquiry';

				} else if (filename == 'btn_FavDone.gif') {

					if (origImageType == 2) {
						newFilename = 'images/btn_prop.gif';
                        newAltText  = 'Click to Pre-order';
					} else {
						newFilename = 'images/btn_addToFav.gif';
                        newAltText  = 'Add to Favourites';
					}
				}

				favButtonImgObject.src = newFilename;
				favButtonImgObject.alt = newAltText;
			}

			// Update the "You have x items Queued" text at the topright of the
			// page if possible and only if this is not a preorder.
//		var updateTotal   = (origImageType == 2);
//		var dvdsQueuedDiv = document.getElementById('num_dvds_queued');

//			if (dvdsQueuedDiv && updateTotal) {
//				var numToAdd  = (newFilename == '/images/btn_FavDone.gif' ? 1 : -1);

//               var currItems = dvdsQueuedDiv.innerHTML.match(/([0-9]+) items?/i);
//              dvdsQueuedDiv.innerHTML = dvdsQueuedDiv.innerHTML.replace(dvdsQueuedDiv.innerHTML, (Number(currItems[1]) + numToAdd)  + ' items');
//	}

		// For browser not supporting document.getElementById call the old favPopup functionality
		} else {
			favPopup(i_id);
		}
	}

	/**
	* Old function here to handle situations when user has rating limit set
	* and title is rated too high, and also older browsers.
	*/
	function favPopup(i_id) {
		var popup = window.open('/fav.aspx?ca_id=5&i_id=' + i_id + '&', 'favPopup', 'width=250,height=130,left=437,top=364');
		popup.focus();
	}

	var win=null;
	function NewWindow(mypage,myname,w,h,scroll,pos)
	{
		if(pos=="center"){LeftPosition=(screen.availWidth)?(screen.availWidth-w)/2:50;TopPosition=(screen.availHeight)?(screen.availHeight-h)/2:50;}
		settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
		win=window.open(mypage,myname,settings);
		if(win.focus){win.focus();}
	}
	function CloseNewWin(){if(win!=null && win.open)win.close()}
	window.onfocus=CloseNewWin;

	function popUp(url) {
		sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0, status=1,menubar=1,scrollbars=1,resizable=1,width=510,height=450');
		self.name = "mainWin";
	}




