
// If a field is set to a specific value, clear it (usually when it takes focus)
function fieldOnfocus(element, setval) {
	if (element.value == setval) {
		element.value = '';
	}
	return true;
}

// If a field is blank (usually when it loses focus) populate it with a specific value.
function fieldOnblur(element, setval) {
	if (element.value == '') {
		element.value = setval;	
	}
	return true;
}

var startupList = new Array();
function addStartupFunc(newFunc) {
	if (typeof newFunc == 'function') {
		startupList.push(newFunc);
		return true;
	}
	return false;
}

window.onload = function() {
	for (var i=0; i<startupList.length; i++) {
		startupList[i]();
	}
}

/**
 * Add onclick event on links that have href="#TODO"
 */
function TODOLinks() {
	var a = document.getElementsByTagName('a');
	for (var i=0; i<a.length; i++) {
		if (a[i].href.match(/\#TODO$/)) {
			a[i].onclick = function() {
				alert('This function is pending implementation.');
			}
		}
	}
}

addStartupFunc(TODOLinks);

/**
 * Track pdf/zip link clicks
 */
function TrackDownloads() {
	var links = document.getElementsByTagName('a');
	var parts = null;
	for (var i=0; i<links.length; i++) {
		if (links[i].href.match(/\/([^\/]+)\.(pdf|zip)$/i)) {
			links[i].onclick = function() {
				var parts = this.href.match(/\/([^\/]+)\.(pdf|zip)$/i);
				if (parts) {
					//alert(this.href);
					pageTracker._trackPageview(this.href);
					//alert('/downloads/'+parts[1]+'.'+parts[2]);
					//return false;
				}
			}
		}
	}
}

addStartupFunc(TrackDownloads);

/**
 * Print page
 */
function doPrint() {
	if (typeof window.print == 'function') {
		window.print();
	} else {
		alert('Please select print from your browser window.');
	}
	
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}


/*  this sets a cookie   */
function setCookiepick(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/* this gets a cookie    */
function getCookie(c_name){
	if (document.cookie.length>0){
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1){
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}




$(function() {

    if (!Array.prototype.indexOf)
    {
        Array.prototype.indexOf = function(elt /*, from*/)
        {
            var len = this.length >>> 0;

            var from = Number(arguments[1]) || 0;
            from = (from < 0)
            ? Math.ceil(from)
            : Math.floor(from);
            if (from < 0)
                from += len;

            for (; from < len; from++)
            {
                if (from in this &&
                    this[from] === elt)
                    return from;
            }
            return -1;
        };
    }
    
     if (getCookie('tescoschools')) {
         ticked = getCookie('tescoschools').split(',');
         for (a=0;a<ticked.length;a++) {

             $("#"+ticked[a]).attr('checked',true);
             number = ticked[a].charAt(8);
             $("tr#list"+number+" td p").addClass('strickthrough');

         }


     }
     ticks = new Array();
    if ($("#checkbox1").click(function(){
        if ($("#checkbox1").is(':checked')) {
            $("tr#list1 td p").addClass('strickthrough');
           
            ticks.push($("#checkbox1").val());
        } else {
            $("tr#list1 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox1'), 1);
            
        }
    }));

    if ($("#checkbox2").click(function(){
        if ($("#checkbox2").is(':checked')) {
            $("tr#list2 td p").addClass('strickthrough');
            ticks.push($("#checkbox2").val());
        } else {
            $("tr#list2 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox2'), 1);

        }
    }));

    if ($("#checkbox3").click(function(){
        if ($("#checkbox3").is(':checked')) {
            $("tr#list3 td p").addClass('strickthrough');
            ticks.push($("#checkbox3").val());
        } else {
            $("tr#list3 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox3'), 1);

        }
    }));

    if ($("#checkbox4").click(function(){
        if ($("#checkbox4").is(':checked')) {
            $("tr#list4 td p").addClass('strickthrough');
            ticks.push($("#checkbox4").val());
        } else {
            $("tr#list4 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox4'), 1);

        }
    }));

    if ($("#checkbox5").click(function(){
        if ($("#checkbox5").is(':checked')) {
            $("tr#list5 td p").addClass('strickthrough');
            ticks.push($("#checkbox5").val());
        } else {
            $("tr#list5 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox5'), 1);

        }
    }));

    if ($("#checkbox6").click(function(){
        if ($("#checkbox6").is(':checked')) {
            $("tr#list6 td p").addClass('strickthrough');
            ticks.push($("#checkbox6").val());
        } else {
            $("tr#list6 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox6'), 1);

        }
    }));

    if ($("#checkbox7").click(function(){
        if ($("#checkbox7").is(':checked')) {
            $("tr#list7 td p").addClass('strickthrough');
            ticks.push($("#checkbox7").val());
        } else {
            $("tr#list7 td p").removeClass('strickthrough');
            ticks.splice(ticks.indexOf('checkbox7'), 1);

        }
    }));



   if ($(":checkbox").click(function(){

       setCookiepick('tescoschools',ticks,60);


   }));

 $("#answer1").hide();
 $("#detailsyours").hide();
 $("#answer3").hide();

 if ($("#question1b").attr('checked')) {
     $("#answer1").show('slow');
 }

 if ($("#question3a").attr('checked')) {
     $("#answer3").show('slow');
 }

 if ($("#contactyes").attr('checked')) {
     $("#detailsyours").show('slow');
 }

 if ($("#question1b").click(function(){
     
        $("#answer1").show('slow');
 }));

 if ($("#question1a").click(function(){

        $("#answer1").hide('fast');
 }));

 if ($("#question3a").click(function(){

        $("#answer3").show('slow');
 }));

 if ($("#question3b").click(function(){

        $("#answer3").hide('fast');
 }));

 if ($("#contactyes").click(function(){

        $("#detailsyours").show('slow');
 }));

 if ($("#contactno").click(function(){

        $("#detailsyours").hide('fast');
 }));



});

function submitMessage () {
 if (document.getElementById('vouchers_collected').value != '' && document.getElementById('dmvouchers').value != '') {
     return window.confirm('Please confirm you have '+document.getElementById('vouchers_collected').value+' vouchers in your possession and the value of your Daily Mirror vouchers is '+document.getElementById('dmvouchers').value);
 } else if (document.getElementById('dmvouchers').value == '') {
  return window.confirm('Please confirm you have '+document.getElementById('vouchers_collected').value+' vouchers in your possession:');
 } else {
    return window.confirm('Please confirm you have '+document.getElementById('dmvouchers').value+' Daily Mirror vouchers in your possession:');
 }

}

function confirmMessage(day) {
  if (day < '4') {
    message = confirm ('Please note that you are booking a collection on a date that may be before some schools return from Summer Half Term. If this is okay, please confirm.');
    if (message == true) {
    return true;
  } else {
    return false;
  }
  } else if (day > '25') {
    message = confirm ('Please note that you are booking a collection on a date that may be after some Scottish Schools break up for Summer. If this is okay, please confirm.');
    if (message == true) {
    return true;
  } else {
    return false;
  }
  } else {
    return true;
  }
  
  
}

