// JavaScript Document
// Copyright NISA Internet and Computer Solutions Limited 2009 all rights reserved

<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



 function GetDay(intDay){
    var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
                         "Thursday", "Friday", "Saturday")
    return DayArray[intDay]
    }

  function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December") 
    return MonthArray[intMonth] 	  	 
    }
  function getDateStrWithDOW(){
    var today = new Date()
    var year = today.getYear()
    if(year<1000) year+=1900
    var todayStr = GetDay(today.getDay()) + ", "
    todayStr += GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
    }
	
	

  //----------------------------------------------------------------------------
  // Name: field()
  // Role: Locate the document with the specified id
  //----------------------------------------------------------------------------
  function field( id ) {
    var ele = document.getElementById( id );
    if ( !ele ) {
      alert( 'Specified document element not found.  id="' + id + '"' );
    }
    return ele;
  }

  //----------------------------------------------------------------------------
  // Name: padout()
  // Role: prepend a leading '0' to values < 10 (e.g., 0..9)
  //----------------------------------------------------------------------------
  function padout( number ) {
    return ( number < 10 ) ? '0' + number : number;
  }
  //----------------------------------------------------------------------------
  // Name: showAllDates()
  // Role: Populate all listed combos
  //----------------------------------------------------------------------------
  // Inline in each page
  //function showAllDates( ) {
  //showDates('dateselector');
  //showDates('dateselector2');
  //showDates('dateselector3');
  //showDates('dateselector4');
  //showDates('dateselector5');
  //showDates('dateselector6');
  //showDates('dateselector7');
  //showDates('dateselector8');
  //showDates('dateselector9');
  //showDates('dateselector10');

  //}


  //----------------------------------------------------------------------------
  // Name: showDates()
  // Role: Generate all the dates in the specified range
  //----------------------------------------------------------------------------
  function showDates( id ) {
    var result = '';
    var combo	= field( id );
	var startDate = new Date();
	var endDate = new Date();
	//
	var today = new Date();
	//
	var shownfirst = false;
	var d = new Date();
	var weekday=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");

// Add '2-3 Working Days' to the first date dropdown option, this will be position [0] ** see below where it check the chosen delivery option
	var opt = document.createElement("option");
	opt.text = "2-3 Working Days";
	opt.text = "2-3 Working Days";
	combo.options.add ( opt );
	
    if (d.getHours() < 12)
    {
    	startDate= new Date();
		//
        startDate.setDate( d.getDate() + 1 );
		//
    }
    else
    {
    	startDate= new Date();
        //startDate.setDate( d.getDate() + 1 );
        startDate.setDate( d.getDate() + 2 );
    }

	endDate.setDate( d.getDate() + 14 );
    while ( startDate < endDate ) {
		if ((startDate.getDay() >0) && (startDate.getDay() <6)) { // If it's not Saturday or Sunday
        	if (shownfirst == false ) {
				
				// If today is Friday after 12pm (actually this won't kick in until 1pm) or today is Saturday or Sunday make the first available delivery day Tuesday not Monday
				if ((startDate.getDay() == 1) && ((weekday[today.getDay()] == 'Fri') && (d.getHours() > 11) || (weekday[today.getDay()] == 'Sat') || (weekday[today.getDay()] == 'Sun'))) {
        			startDate.setDate( startDate.getDate() + 1 );
				}
        	}
			
        	var opt = document.createElement("option");
        	opt.text = weekday[startDate.getDay()] + " "+ padout( startDate.getDate()) + '-' + padout( startDate.getMonth() + 1 ) + '-' + startDate.getFullYear();
        	opt.value = weekday[startDate.getDay()] + " "+ padout( startDate.getDate()) + '-' + padout( startDate.getMonth() + 1 ) + '-'+ startDate.getFullYear();

      		combo.options.add ( opt );
      		shownfirst=true;
		
		}

      startDate.setDate( startDate.getDate() + 1 );

    }


	//----
	// Add '2-3 Working Days' to the last date dropdown option, this will be position [9] ** see below where it check the chosen delivery option
	//var opt = document.createElement("option");
	//opt.text = "2-3 Working Days";
	//opt.text = "2-3 Working Days";
	//combo.options.add ( opt );
	//----
	

    return result.substring( 1 );
  }
  
  
  
// Comfy Gift Boxes Promotion Voucher Discount


var vouchval = "(blanket)"; // What user entered - not used
var apply_discount = false;

function ChkVouch (form_number) {
	

vouchval = document.form1.vouchcode.value; // Voucher code entered on website

promocode = "CGB8931" ; // Actual promotion code
voucher_discount = 2; // £2.00

    if (vouchval == promocode) {
		apply_discount = true;
		alert ("Valid promotion voucher code for a Comfy Gift Box, your discount is now in effect and will be applied at checkout.");
		return;
    }
  alert ("'" + vouchval + "'  is not a valid Comfy Gift Boxes promotion code.");
}



// Apply discount to the correct form and Buy Now button

function ReadForm (form_details) {

var form_fields = document.getElementById("form1")

//if (form_fields.custom1.value == "" || form_fields.custom2.value == "") {
//alert ("xxxx");
//}
//return false;


// Check fields have been entered

// Reset any coloured borders from last test
form_fields.custom1.style.borderColor = "#CCCCCC";
form_fields.custom2.style.borderColor = "#CCCCCC";
form_fields.custom3.style.borderColor = "#CCCCCC";
form_fields.custom4.style.borderColor = "#CCCCCC";
form_fields.custom6.style.borderColor = "#CCCCCC";
form_fields.custom7.style.borderColor = "#CCCCCC";
form_fields.custom8.style.borderColor = "#CCCCCC";
form_fields.os0.style.borderColor = "#CCCCCC";

// Name and email address
if (form_fields.custom1.value == "" || form_fields.custom2.value == "") {
	alert ("Please ensure both your name and email address have been entered.");
	if (form_fields.custom2.value == "") {
		form_fields.custom2.focus();	
		form_fields.custom2.style.borderColor = "#cc0000";
	}
	if (form_fields.custom1.value == "") {
		form_fields.custom1.focus();	
		form_fields.custom1.style.borderColor = "#cc0000";
	}
	return false;
}


// Recipient's name and delivery address line 1, Town, County and Postcode
if (form_fields.custom3.value == "" || form_fields.custom4.value == "" || form_fields.custom6.value == "" || form_fields.custom7.value == "" || form_fields.custom8.value == "") {
	alert ("Please ensure the recipient's name and delivery address have been entered.");
	if (form_fields.custom8.value == "") {
		form_fields.custom8.focus();	
		form_fields.custom8.style.borderColor = "#cc0000";
	}
	if (form_fields.custom7.value == "") {
		form_fields.custom7.focus();	
		form_fields.custom7.style.borderColor = "#cc0000";
	}
	if (form_fields.custom6.value == "") {
		form_fields.custom6.focus();	
		form_fields.custom6.style.borderColor = "#cc0000";
	}
	if (form_fields.custom4.value == "") {
		form_fields.custom4.focus();	
		form_fields.custom4.style.borderColor = "#cc0000";
	}
	if (form_fields.custom3.value == "") {
		form_fields.custom3.focus();	
		form_fields.custom3.style.borderColor = "#cc0000";
	}
	return false;
}


// Check a message has been entered
if (form_details.os0.value == "" && form_details.os1.value == "") {
	
	alert ("Please enter a message we can include on a gift card to go with your present.");
	form_details.os0.focus();
	form_details.os0.style.borderColor = "#cc0000";
	return false;
	
}


// Concatinate custom variables on the order page	
form_details.custom.value = 'Name: ' + form_fields.custom1.value + ' Email: ' + form_fields.custom2.value + ' Recipients Name: ' + form_fields.custom3.value + ' Address: ' + form_fields.custom4.value + ', ' + form_fields.custom5.value + ', ' + form_fields.custom6.value + ', ' + form_fields.custom7.value + ', ' + form_fields.custom8.value;


//


var amt,des;

// Get form number from the form name - format: form1, form_fields, form3 etc

form_no = form_details.name.match(/\d+$/)[0]; // Extract the number from the form name, e.f for form3 we want '3'

amt = form_details.baseamt.value*1.0; // Base amount from form
des = form_details.basedes.value; // Base description from form


// Apply the discount if discount is active in array relating to the form, i.e they entered a valid code in the same form as the Buy Now button they are pressing

  if (apply_discount == true) { 
    amt = Pound (amt - voucher_discount);
	des = des + " - with £2.00 " + vouchval + " promotion discount";
  }
  
  form_details.amount.value = Pound (amt);
  form_details.item_name.value = des;

//----
// If the Delivery Option chosen was Standard (os3[1]) then set the delivery date to '2-3 Working Days' rather than a date they may have chosen
// Also if Next Day was chosen (os3[1]) and '2-3 Working Days' was selected from the dropdown, set the Delivery Date to the first available date (os2[1])
// The check on the dropdown selection isn't really needed now we've got the check on the radio buttons
// os0 = To message
// os1 = From message
// os2 = Delivery Date dropdown
// os3 = Delivery Charge radio buttons - Standard [0] or Next Day [1])


var last_dropdown; // Could be [8] or [9]
last_dropdown = form_details.os2.length - 1;

// If the Standard Delivery option was selected, set the shipping cost and make sure the delivery date is set to '2-3 working days'
if (form_details.os3[0].checked == true) {
	document.form1.shipping.value = form_details.os3[0].value; // Set the shipping amount to the Delivery Charge chosen
	//form_details.os2[last_dropdown].selected = true; // Set the Delivery Date to '2-3 Working Days', last option in the dropdown
	form_details.os2[0].selected = true; // Set the Delivery Date to '2-3 Working Days', last option in the dropdown
}
else {
// If the Next Day Delivery was chosen set the shipping cost
	document.form1.shipping.value = form_details.os3[1].value; // Set the shipping amount to the Delivery Charge chosen
// if Next Day Delivery was chosen but the Delivery Date dropdown was set to '2-3 Working Days' set it to the first available delivery day instead
	//if (form_details.os2.selectedIndex == last_dropdown) {
	if (form_details.os2.selectedIndex == 0) {
		form_details.os2[1].selected = true; // Set the Delivery Date to the first available day, second option in the dropdown
	}
}
//----

  
}

function Pound (val) { // Force to valid pound amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1; // For old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}


//----

// Firefox needs to use the 'id=' property rather than the 'name=' property (document.getElementById(dateSelectorId)) so id= needs to be passed through which it is in dateSelectorId. This will be either dateselector, dateselector2 dateselector3 etc which is different for each form on the page
// <input name="os3" type="radio" value="3.95" onclick="showStandard('dateselector2')" />Standard Delivery £3.95
// <input name="os3" type="radio" value="6.95" onclick="showNext('dateselector2')" checked />Next Day Delivery £6.95


function showStandard(dateSelectorId) {
	var last_dropdown; // Should be [9]
	last_dropdown = document.getElementById(dateSelectorId).length - 1;

	//document.getElementById(dateSelectorId)[last_dropdown].selected = true; // Set the Delivery Date to '2-3 Working Days', last option in the dropdown
	document.getElementById(dateSelectorId)[0].selected = true; // Set the Delivery Date to '2-3 Working Days', last option in the dropdown
}

function showNext(dateSelectorId) {
	//document.getElementById(dateSelectorId)[0].selected = true; // Set the Delivery Date to the first day available, the first option in the dropdown
	document.getElementById(dateSelectorId)[1].selected = true; // Set the Delivery Date to the first day available, the second option in the dropdown
}




//-->