// Rent button onclick method
g_loadTime = new Date().getTime();
g_platformsConfirmed = [];

/**
* imageType: 1 - Preorder
*            2 - Rent
*            3 - Queued
*            4 - Free trial
*            5 - Shipped
*/
function RentButton_onclick(dt_id, origImageType, logRec, confirm_platform)
{
	// 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 ( (g_loadTime + g_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) {

		// Grab image object...
		var rentButtonImgObject = document.getElementById('rentButtonImg_' + dt_id);

		var filename = '';


		// If the rent button object was successfully retrieved...
		if (rentButtonImgObject) {
			// Grab the current image src, and get the filename from it
			if (rentButtonImgObject.src.indexOf('/') != -1) {
				var pos = rentButtonImgObject.src.lastIndexOf('/');
				filename = rentButtonImgObject.src.substring(pos + 1);
			} else {
				filename = rentButtonImgObject.src;
			}
		}

		var platform_confirmed = '0';

		// If we need to confirm the platform (and we are attempting to rent the title)
		if (confirm_platform && (filename == 'button_rent.gif' || filename == 'button_preorder.gif')) {

			var dpid = confirm_platform;
			// have we already confirmed on this page ?
			if (g_platformsConfirmed[dpid]) {
				platform_confirmed = '1'
			} else {
				var confirm_msg = null;
				switch(confirm_platform) {
					case 2 : 
						confirm_msg = "This film is a PSP/UMD title and only playable on a Sony PlayStation Portable (PSP).\nClick OK to continue to rent this film?\nIf you click OK you will not be asked this question again.";
						break;
				}
				if (confirm_msg) {
					if (confirm(confirm_msg)) {
						platform_confirmed = '1';
						g_platformsConfirmed[dpid] = true;
					} else {
						return;
					}
				}
			}
		}


		// 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 rent script
		// In effect calls the rent script to register the queued/unqueued title
		var rentScriptImg = new Image();
		rentScriptImg.src = '/rent.php?ca_id=' + g_caid + '&dt_id=' + dt_id + '&rec=' + logRec + '&platform_confirmed=' + platform_confirmed + '&entropy=' + entropy;

		// If the rent button object was successfully retrieved...
		if (rentButtonImgObject) {

			// Determine the new filename based on the current filename
			var newFilename;
			var newAltText;

			if (filename == 'button_rent.gif' || filename == 'button_preorder.gif' || filename == 'button_reserve.gif') {
				newFilename = g_img_queued;
				newAltText  = 'Remove from Queue';

			} else if (filename == 'button_queued.gif') {

				if (origImageType == 10) {
					newFilename = g_img_reserve;
					newAltText  = 'Click to Reserve';
				} else if (origImageType == 1) {
					newFilename = g_img_preorder;
					newAltText  = 'Click to Pre-order';
				} else {
					newFilename = g_img_rent;
					newAltText  = 'Click to Rent';
				}
			}
			
            rentButtonImgObject.src = newFilename;
            
            if (!location.href.match(/ntltv/)) { // Something wierd happens with NTL new releases page...
                rentButtonImgObject.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.
		if (origImageType == 2) {
			updateNumItemsQueued(newFilename == g_img_queued ? 1 : -1);
		}

	// For browser not supporting document.getElementById call the old rentPopup functionality
	} else {
		rentPopup(dt_id);
	}
}

/**
* Updates the text 'You have x number of items queue'
*
* @param integer numToAdd		Number of items to add to the current num 
*/
function updateNumItemsQueued(numToAdd) {
	var dvdsQueuedDiv = document.getElementById('num_dvds_queued');

	if (dvdsQueuedDiv) {
		var currItems = dvdsQueuedDiv.innerHTML.match(/([0-9]+) items?/i);
		dvdsQueuedDiv.innerHTML = dvdsQueuedDiv.innerHTML.replace(dvdsQueuedDiv.innerHTML, (Number(currItems[1]) + numToAdd)  + ' items');	
	}
}

/**
* Old function here to handle situations when user has rating limit set
* and title is rated too high, and also older browsers.
*/
function rentPopup(dt_id) {
	var popup = window.open('/rent.php?ca_id=' + g_caid + '&dt_id=' + dt_id, 'rentPopup', 'width=250,height=130,left=437,top=364');
	popup.focus();
}

// Popup Window
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;

// With onkeypress event, this verifies 'Enter' key
function verifyKey(oElement,oEvent){
	if(oEvent.keyCode==13 && oElement.onclick) oElement.onclick();
}

// Verisign SiteSeal
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";
}

// Record a review as helpful is user is a member
function helpful(re_id,re_helpful,dt_id) {
	window.open('/helpful.php?re_id=' + re_id +'&re_helpful='+ re_helpful + '&dt_id=' + dt_id , 'helpful', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=250,height=20,left = 437,top = 364');
}

// Rate a review if user is a member
function ratereview(re_id,rra_rating,dt_id) {
	window.open('/ratereview.php?re_id=' + re_id +'&rra_rating='+ rra_rating + '&dt_id=' + dt_id , 'ratereview', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=250,height=20,left = 437,top = 364');
}

// Change page location to option selected from navigation drop-down
function goUrl(Selected) {
	var newURL = Selected.options[Selected.selectedIndex].value ;
	if (newURL!=" ") {
	document.location.href = newURL;
	}
}

// Change page location to option selected from navigation drop-down
function goGenre(obj) {
	var val = obj.options[obj.selectedIndex].value;
	if (isNaN(parseInt(val))) url = "/all-categories.php?type="+val;
	else url = "/category.php?ca_id="+val;
	window.location.href = url;
}

function lauchPostcodeFinder(ac_postcode) {
	if (!ac_postcode.value) {
		alert("Please enter a postcode");
		ac_postcode.focus();
	}
	else window.open('/postcodefinder.php?ac_postcode=' + ac_postcode.value, 'postcodefinder', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=490,height=230,left = 437,top = 364');
}

//function to selectAll checkboxes
function selectAll(formObj, element) 
{
   for (var i=0;i < formObj.length;i++) 
   {
      fldObj = formObj.elements[i];
      if (fldObj.type == 'checkbox')
      { 
		 fldObj.checked = element.checked;
      }
   }
}
