﻿$(document).ready(function () {
    
    $('.lightbox').lightbox();
    $('.datepicker').datepicker();

});

function addItem(whatProduct) {

    $('#ifr').attr('width', 420).attr('height', 400);

    url = 'additem.asp?productId=' + whatProduct;

    new Boxy('#popup-ifr', { modal: true, title: 'Add Item to Order', closeable: true, closeText: '[CANCEL]', afterShow: function () { setTimeout("$('#ifr').attr('src', url)", 10); } });

}

function addBasic(whatProduct) {
    
    $('#ifr').attr('width', 420).attr('height', 120);

    url = 'addbasic.asp?productId=' + whatProduct;

    new Boxy('#popup-ifr', { modal: true, title: 'Add Item to Order', closeable: true, closeText: '[CANCEL]', afterShow: function () { setTimeout("$('#ifr').attr('src', url)", 10); } });

}
function addOthers(whatProduct) {

    $('#ifr').attr('width', 420).attr('height', 100);

    url = 'addothers.asp?productId=' + whatProduct;

    new Boxy('#popup-ifr', { modal: true, title: 'Add Item to Order', closeable: true, closeText: '[CANCEL]', afterShow: function () { setTimeout("$('#ifr').attr('src', url)", 10); } });
}

function addPlatter(whatPlatter) {

    $('#ifr').attr('width', 420).attr('height', 200);

    url = 'addplatter.asp?platterId=' + whatPlatter;

    new Boxy('#popup-ifr', { modal: true, title: 'Add Item to Order', closeable: true, closeText: '[CANCEL]', afterShow: function () { setTimeout("$('#ifr').attr('src', url)", 10); } });
}

function addItemText(item) {
    str = $('#orderitems').html();

    if (str.indexOf('x') > -1)
        str += ", " + item;
    else
        str = item;

    $('#orderitems').html(str);
}

function changeQuantity(iCount) {
  document.form1.action = "/view-order?change=" + iCount;
  document.form1.submit();
}
function validateForm(theHour, theMinute) {
  
  //-- Check the name field.
  if (document.form1.name.value=="") {
     alert("Please enter your name")
     document.form1.name.focus()
	 return false
  }
  
  //-- Check the address field.
  if (document.form1.address.value =="") {
     alert("Please enter your address")
     document.form1.address.focus()
	 return false
  }  
  
  //-- Check the phone field.
  if (document.form1.postcode.value =="") {
     alert("Please enter your postcode")
     document.form1.postcode.focus()
  	 return false
  }  
  //-- Check the phone field.
  if (document.form1.email.value =="") {
     alert("Please enter a valid email address")
     document.form1.email.focus()
  	 return false
  }    
  //-- Check the phone field.
  var okSoFar = 1
  var telephone= document.form1.telephone.value
  telephone = telephone.replace(" ", "")
  var numChars = telephone.length

  if (numChars == 8) {
    telephone = "028" + telephone  
  }
  else if (numChars != 11) {
    okSoFar = 0
  }


  if (!(telephone.substr(0,1) == '0')){ okSoFar = 0 }
  if (!((telephone.substr(1,1) > 0) || (telephone.substr(1,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(2,1) > 0) || (telephone.substr(2,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(3,1) > 0) || (telephone.substr(3,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(4,1) > 0) || (telephone.substr(4,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(5,1) > 0) || (telephone.substr(5,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(6,1) > 0) || (telephone.substr(6,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(7,1) > 0) || (telephone.substr(7,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(8,1) > 0) || (telephone.substr(8,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(9,1) > 0) || (telephone.substr(9,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(10,1) > 0) || (telephone.substr(10,1) < 10))){ okSoFar = 0 }
  if (!((telephone.substr(11,1) > 0) || (telephone.substr(11,1) < 10))){ okSoFar = 0 }

  if (okSoFar == 1) {
	document.form1.telephone.value = telephone
  }
  else {
    alert("Please enter a correct telephone number!")
	document.form1.telephone.focus()
    return false
  }
  if (document.form1.day.value == '0') {
    var orderHour = parseInt(document.form1.time.value.substr(0,2))
    var orderMinute = parseInt(document.form1.time.value.substr(3,2))
	var enoughTime = 1
	if (orderHour <= theHour) {
	  enoughTime = 0
	}
	else {
	  if ((orderHour - theHour) == 1) {
	    if (orderMinute < theMinute) {
		  enoughTime = 0
		}
	  }
	}
  }
  if (enoughTime == 0) {
    orderHour = theHour + 1
    if ((theHour > 14) || ((theHour == 14) && (theMinute > 30))) {
	  alert("Unfortunately home/office deliveries have ceased for the day. If you wish to order for tomorrow, please do so by choosing the option in the drop-down menu beside 'Delivery Time'.")
	}
	else {
	  if (theMinute < 10) { theMinute = "0" + theMinute; }
	  alert('You must give at least one hours notice for home/office deliveries. Please choose another time after ' + orderHour + '.' + theMinute)
	}
	return false
  }
}
