var imgWin = 0;
function popImg( imgfile, w, h )
{
  if(imgWin && !imgWin.closed){ imgWin.close(); }
  var popx, popy, strx, stry;
  popx = (screen.availWidth/2) - (w/2);
  popy = (screen.availHeight/2) - (h/2);

  if(document.all) { strx='left'; stry='top'; } else { strx='screenX'; stry='screenY'; };
  imgWin = window.open( 'displayimage.asp?img='+imgfile+'&width='+w+'&height='+h, '', 'toolbars=0,menubar=0,scrollbars=0,status=1,location=0,width='+w+',height='+h+','+strx+'='+popx+','+stry+'='+popy );
}

var editWin = 0;
function popDetails( sURL, w, h)
{
  if(editWin && !editWin.closed){ editWin.close(); }
  var popx, popy, strx, stry;
  popx = (screen.availWidth/2) - (w/2);
  popy = (screen.availHeight/2) - (h/2);

  if(document.all) { strx='left'; stry='top'; } else { strx='screenX'; stry='screenY'; };
  editWin = window.open( sURL,'', 'toolbars=0,menubar=0,scrollbars=1,status=1,location=0,width='+w+',height='+h+','+strx+'='+popx+','+stry+'='+popy ); 
}

//Help window
var helpWin = 0;
function showHelp(sURL, w, h)
{
  if(helpWin && !helpWin.closed){ helpWin.close(); }
  var popx, popy, strx, stry;
  popx = (screen.availWidth/2) - (w/2);
  popy = (screen.availHeight/2) - (h/2);

  if(document.all) { strx='left'; stry='top'; } else { strx='screenX'; stry='screenY'; };
  helpWin = window.open( sURL,'', 'toolbars=0,menubar=0,scrollbars=1,status=0,location=0,width='+w+',height='+h+','+strx+'='+popx+','+stry+'='+popy ); 
}


function validate(sPassword, sPassword2)
{
  var valid = false;
  if(document.forms[0].elements[sPassword].value != document.forms[0].elements[sPassword2].value)
  {
    alert('The passwords do not match');
//  } else if ( document.forms[0].elements[sEmail].value == '') {
//    alert('Please enter an email address');
  } else if ( document.forms[0].elements[sPassword].value == '') {
    alert('Please enter a password');
  } else {
    valid = true;
  }
  if ( !valid )
  {
    event.returnValue=false;
    return false;
  }
}

function checkIsNumber(sString, sField) {

	if (isNaN(Number(sString))) {
		document.forms[0].elements[sField].value = '0';
	}

}


/*********** MENUS *************/



var bLoaded;

function getObj(id){
  if (document.getElementById){
    this.obj = document.getElementById(id);
    this.style = document.getElementById(id).style;
  }
  else if (document.all){
    this.obj = document.all[id];
    this.style = document.all[id].style;
  }
  else if (document.layers){
    this.obj = getObjNN4(document,id);
    this.style = this.obj;
  }
}

function getObjNN4(obj,id){
  var lyrs = obj.layers;
  var rtn;
  for (var i=0;i<lyrs.length;i++){
    if (lyrs[i].id == id){
      return lyrs[i];
    }else if(lyrs[i].document.layers.length){
      return getObjNN4(lyrs[i].document, id);
    }
  }
  return 0;
}

function setElementStyle(id, styleProperty, styleValue)
{
  if(bLoaded){
    var obj = new getObj(id);
    if (obj.obj && obj.style){
      obj.style[styleProperty] = styleValue;
    }
  }
}

function showElement(id){
 closeAllPop();
 setElementStyle( id, 'display', 'block' );
}

function hideElement(id){
 setElementStyle( id, 'display', 'none' );
}


function bodyOnload()
{
  bLoaded=true;
  initialisePopups();
}

function validateQty(sFormName,sFieldName)
{
  var i = parseInt( document[sFormName][sFieldName].value );
  if( i>100 || i<0 ){
     alert( 'Please Enter a Quantity between 0 and 100' );
  }
}

function closeMe() {
	setElementStyle('refreshdiv', 'visibility', 'visible');
	window.opener.location.href = 'checkout.asp?p=3';
	window.close();
}

function upperCasePostCode()
{
	var jsPostCode;
	var jsFormPostCode;
	
	jsFormPostCode = document.getElementById('Postcode').value;
	jsPostCode = jsFormPostCode.toUpperCase();
	
	document.getElementById('Postcode').value = jsPostCode;
}

function Clear(id)
{
	document.getElementById(id).value = '';
}

function initialisePopups() {
	var oElements = document.getElementsByTagName('a');
	var iLoop, sDimensions;
	for(iLoop=0; iLoop < oElements.length; iLoop++) {
		if (oElements[iLoop].target.indexOf('_popup') >= 0) {
			oElements[iLoop].onclick = launchPopup;
			oElements[iLoop].onkeypress = launchPopup;
		}
	}
}

function launchPopup () {
	
	var iTop, iLeft, sTop, sLeft;
	var iWidth = this.getAttribute("popupwidth");
	var iHeight = this.getAttribute("popupheight");
	var sWidth = (iWidth != null) ? ',width=' + iWidth : '';
	var sHeight = (iHeight != null) ? ',height=' + iHeight : '';
	
	iLeft = (iWidth != null) ? (screen.availWidth / 2) - (iWidth / 2) : 0;
	iTop = (iHeight != null) ? (screen.availHeight / 2) - (iHeight / 2) : 0;

	if (document.all) { sLeft = 'left'; sTop = 'top'; } else { sLeft = 'screenX'; sTop = 'screenY'; }
		
	window.open(this.href, '', 'scrollbars=0,location=0,' + sLeft + '=' + iLeft + ',' + sTop + '=' + iTop + sWidth + sHeight);
	
	return false;
}

/* LB - 12/07/2007 - Deprecated
function selectUserType(s)
{
	if (s.options[s.selectedIndex].value == 'admin')
	{
		hide('tdStaffLabel');
		hide('tdStaffField');
		show('tdLabel');
		show('tdField');
		show('tdlogin');
	}
	else if(s.options[s.selectedIndex].value == 'staff')
	{
		show('tdStaffLabel');
		show('tdStaffField');		
		show('tdLabel');
		show('tdField');		
		show('tdlogin');
	}
}*/

function show(id)
{
	document.getElementById(id).style.display = 'block';
}

function hide(id)
{
	document.getElementById(id).style.display = 'none';
}

function reserveItem(id, qtyID, maxQty, reservedQty)
{
	var reserveQty = document.getElementById(qtyID).value;
	
	if (parseInt(reserveQty) > 0 || parseInt(reserveQty) < 0)
	{
		if (reserveQty > maxQty)
			alert('The reserve quantity specified (' + reserveQty + ') is higher than the remaining quantity for this item (' + maxQty + '), please select a quantity equal to or less than this amount.');
		else
			document.location = 'list.asp?action=reserve&id=' + id + '&qty=' + reserveQty
	}
	else
	{
		alert('Please enter a valid reserve quantity');
	}
		
}

function requestReserveItem(id, qtyID, maxQty, reservedQty)
{
	var reserveQty = document.getElementById(qtyID).value;
	
	if (parseInt(reserveQty) > 0 || parseInt(reserveQty) < 0)
	{
		if (reserveQty > maxQty)
			alert('The reserve quantity specified (' + reserveQty + ') is higher than the remaining quantity for this item (' + maxQty + '), please select a quantity equal to or less than this amount.');
		else
			document.location = 'list.asp?action=requestreserve&id=' + id + '&qty=' + reserveQty
	}
	else
	{
		alert('Please enter a valid reserve quantity');
	}
}

function requestReserveItems() {
	var iInstanceId, iQuantity, iRemaining, iReserved, sProduct, oElement, iChanges = 0;
	var bContinue = false;

	for (var iLoop = 0; iLoop < document.forms.listitems.elements.length; iLoop++) {
		var oElement = document.forms.listitems.elements[iLoop];
		if (oElement.id.indexOf('_reserve_qty') >= 0) {
			iInstanceId = oElement.id.replace(/_reserve_qty/gi, '').replace(/prod_/gi, '');
			iQuantity = +oElement.value;
			iRemaining = +document.getElementById('prod_' + iInstanceId + '_remaining').value;
			iReserved = +document.getElementById('prod_' + iInstanceId + '_reserved').value;
			sProduct = document.getElementById('prod_' + iInstanceId + '_product').value;
			//alert(sProduct + '\nQuantity: ' + iQuantity + '\nReserved: ' + iReserved + '\nRemaining: ' + iRemaining)
			if (oElement.value.length > 0 && isNaN(iQuantity)) {
				alert('The reserve quantity for ' + sProduct + ' is not numerical. Please check it and try again.');
				return;
			} else if (iQuantity > 0 && iQuantity > iRemaining) {
				alert('The reserve quantity for ' + sProduct + ' is invalid as there are not that many items still available to reserve.');
				return;
			} else if (iQuantity < 0 && iQuantity < -iReserved) {
				alert('The reserve quantity for ' + sProduct + ' is invalid as there are not that many items currently requested.');
				return;
			} else if (iQuantity > 0 || iQuantity < 0) {
				bContinue = true;
				iChanges = iChanges + 1;
			}
		}
	}
	
	if (bContinue) {
		document.forms.listitems.formaction.value = 'requestreserve';
		document.forms.listitems.submit();
	} else if (iChanges == 0) {
		alert('You have not specified any increases or decreases to the reserve quantities.');
	}
}

function fulfilReserveRequest() {
	if (confirm('Are you sure you wish to submit this request? Once it has been submitted it cannot be changed by anyone other than an administrator.')) {
		document.forms.listitems.formaction.value = 'fulfilreserverequest';
		document.forms.listitems.submit();
	}
}

function fulfilReservation(id, qty)
{
	document.forms.fFulfilReservation.prod.value = id;
	document.forms.fFulfilReservation.qtyPH.value = qty;
	document.forms.fFulfilReservation.qtyPH.name = 'prod_' + id + '_qty';
	document.forms.fFulfilReservation.submit();
}

/* Admin */

function checkAdminLogin() {
	if (!(document.getElementById('login').value.length > 0 && (document.getElementById('store').value.length > 0 || document.getElementById('login').options[document.getElementById('login').selectedIndex].className == 'a-1') && document.getElementById('txtPassword').value.length > 0)) {
		alert('You must enter all details in order to log in.');
		return false;
	} else {
		return true;
	}
}

function checkStoreField(oSelect) {
	if (oSelect.options[oSelect.selectedIndex].className == 'a-0') {
		document.getElementById('store-field').style.display = '';
		document.getElementById('store-field').disabled = false;
	} else {
		document.getElementById('store-field').style.display = 'none';
		document.getElementById('store-field').disabled = true;
	}
}