var ie4 = false; if(document.all) { ie4 = true; }
function getObject(id) {
	if (ie4) {
		return document.all[id];
	} else {
		return document.getElementById(id);
	}
}








Date.prototype.getDOY = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((this - onejan) / 86400000);
}

function compareDates (value1, value2) {
  var date1, date2;
  var month1, month2;
  var year1, year2;

  date1 = value1.substring (0, value1.indexOf ("-"));
  month1 = value1.substring (value1.indexOf ("-")+1, value1.lastIndexOf ("-"));
  year1 = value1.substring (value1.lastIndexOf ("-")+1, value1.length);

	
	
	
  date2 = value2.substring (0, value2.indexOf ("-"));
  month2 = value2.substring (value2.indexOf ("-")+1, value2.lastIndexOf ("-"));
  year2 = value2.substring (value2.lastIndexOf ("-")+1, value2.length);

	//alert(date1+'/'+month1+'/'+year1);
	//alert(date2+'/'+month2+'/'+year2);
	
  if (year1 > year2) {
  	return 1;
  }else if (year1 < year2) {
  	//alert('yyy');
  	return -1;
  }else if (month1 > month2) {
  	//alert(month1+'/'+month2);
  	//alert('aaa');
  	return 1;
  }else if (month1 < month2) {
  	//alert(month1+'/'+month2);
  	return -1;
  }else if (date1 > date2) {
  	//alert('bbb');
  	return 1;
  }else if (date1 < date2) {
  	//alert('xxx');
  	return -1;
  }else{
  	return 0;
  }
} 

function showCourseOptionDetails(sel, f) {
	setHiddenCourseDetails(f, sel);
	
	//alert(f.Preferred_Date.value);
	var myDays= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
	var aDate= f.Preferred_Date.value;
	var data1= aDate.split('-');
	var myDate=new Date();
	var pp= 0;
	myDate.setFullYear( parseInt(data1[2], 10), parseInt(data1[1], 10)-1, parseInt(data1[0], 10) );
	//alert(parseInt(data1[1], 10));
	
	//alert(parseInt(data1[1], 10)-1);
	
	thisDay=myDate.getDay()
	
	
	//thisDay=myDays[thisDay]
	//alert(thisDay);
	var str= '', i= sel.value;
	str= str+ 'Please select your time according to the times available.<br/>';
	if (thisDay== 0 || thisDay== 6) {	// weekend
		str= str+ 'Times (Sat/Sun): '+ courses[i]['times_weekend']+'<br>';
		f.Price_Per_Person.value= courses[i]['price_weekend'];
	}else{
		str= str+ 'Times (Mon-Fri): '+ courses[i]['times_week']+'<br>';
		f.Price_Per_Person.value= courses[i]['price_week'];
	}
	
	
	//alert(f.Price_Per_Person.value);
	if (isNaN(parseInt(f.Price_Per_Person.value)) ) {
		alert('The date and course option that you have selected are incongruous.\nIt\' a weekday/weekend problem.\nPlease select another course option or date.');
	}
	
	var dateRes1= compareDates(f.Preferred_Date.value, courses[i]['date_start']);
	var dateRes2= compareDates(f.Preferred_Date.value, courses[i]['date_end']);	
	if (dateRes1== -1 || dateRes2== 1) {
		alert('1. The date you have selected ('+f.Preferred_Date.value+') is not within the season for the option that you have selected.\nPlease pick a date between '+courses[i]['date_start']+' and '+courses[i]['date_end']);
		//f.Preferred_Date.value= '';
		f.Course_Option.value= '';
	}else{		
	  var target= getObject('course-option-details');
	  target.style.display= 'block';
	  target.style.background= '#8FC5FF';
	  target.innerHTML= str;
	  
	  f.Play_Type.value= courses[i]['play_type'];
		var cost;
		if (f.Play_Type.value=='round') {
			cost= 20;
		}else{
			cost= 130;
		}
		//f.Play_Type_Buggy_Cost.value= cost;
		//f.Play_Type_Caddy_Cost.value= cost;
	
	
	  setGolfersCost(f);
	  //setBuggiesCost(f);
	  //setCaddiesCost(f);
	  setTotal(f);
	}
}

function setGolfersCost(f) {
	var g=parseInt(f.Golfers.value);
	var p=parseInt(f.Price_Per_Person.value);
	if (isNaN(g)) g= 0;
	if (isNaN(p)) p= 0;
	//alert(g);
	f.Golfers_Cost.value= g * p;
	setTotal(f);
}

function setBuggiesCost(f) {
	var c=parseInt(f.Play_Type_Buggy_Cost.value);
	if (isNaN(c)) c= 0;
	
	f.Buggy_Cost.value= parseInt(f.Buggies.value) * c;
	setTotal(f);
}

function setCaddiesCost(f) {
	var c=parseInt(f.Play_Type_Caddy_Cost.value);
	if (isNaN(c)) c= 0;
	
	f.Caddy_Cost.value= parseInt(f.Caddies.value) * c;
	setTotal(f);
}

function setTotal(f) {
	//f.Total.value= parseInt(f.Golfers_Cost.value) + parseInt(f.Buggy_Cost.value) + parseInt(f.Caddy_Cost.value);
	f.Total.value= parseInt(f.Golfers_Cost.value);


	/*
	if (isNaN(f.Golfers.value) || isNaN(f.Price_Per_Person.value) ) {
		//
	}else{
		//alert('setTotal');
		f.Total.value= f.Price_Per_Person.value * f.Golfers.value;
		
		if (f.Play_Type.value=='round') {
			cost= 20;
		}else{
			cost= 130;
		}
		if (f.Buggies.value>0) {
			f.Total.value= parseInt(f.Golfers_Cost.value) + (cost* parseInt(f.Buggies.value));
		}
	}
	*/
}
	

function resetCourseOptionDetails() {
  var target= getObject('course-option-details');
  target.style.display= 'none';
  target.style.background= '#8FC5FF';
  target.innerHTML= '';
  //alert('test');
}

function resetCourseOption(f, elem) {
	//alert(f.name);
	var o = document.forms['myForm'].elements[elem];
	//alert(document.forms['myForm'].elements[elem].value);
	o.selectedIndex= 0;
	//sel.options[i].selected = false;
	resetCourseOptionDetails();
	
	
	document.forms['myForm'].Golfers.selectedIndex = 0
  document.forms['myForm'].Golfers_Cost.value=0;
  document.forms['myForm'].Price_Per_Person.value=0;
  document.forms['myForm'].Total.value=0;
  
}

function setDate(f, sel, prefix) {
	var str1= prefix+'Month';
	var str2= prefix+'Year';
	f.elements[prefix].value= f.elements[str1].value + '/' + f.elements[str2].value;
}

function bookingTime(f) {
	f.Preferred_Time.value= f.Hour.value+':'+f.Minute.value;
}

function setHiddenCourseDetails(f, e) {
	var s1= f.Course;
	var n = s1.selectedIndex;    // Which menu item is selected
	var val = s1[n].text;        // Return string value of menu item
	f.Course_Name.value= val;
	
	var s2= f.Course_Option;
	n = s2.selectedIndex;    // Which menu item is selected
	val = s2[n].text;        // Return string value of menu item
	f.Course_Option_Name.value= val;
	
}

/* Functions for old style rollovers */
function rollover(imgID) {
		
  // get the image object we're referring to
  var thisimg = document.getElementById(imgID);
  // and add "_on" to its src
  thisimg.src = thisimg.src.replace(/(\.[a-z0-9]+)$/i,'-h$1');
}
function rollout(imgID) {
  // get the image object we're referring to
  var thisimg = document.getElementById(imgID);
  // and remove "_on" from its src
  thisimg.src = thisimg.src.replace(/-h(\.[a-z0-9]+)$/i,'$1');
}

/* Functions for new style rollovers; these are actual event handlers,
   not just called from an event handler. */
function rollover_handler(e) {
	
  // get the source element in a cross-browser way
  if (window.event) { thisimg = window.event.srcElement;
  } else if (e.target) { thisimg = e.target;
  } else { return;
  }
  thisimg.src = thisimg.src.replace(/(\.[a-z0-9]+)$/i,'-h$1');
}
function rollout_handler(e) {
  // get the source element in a cross-browser way
  if (window.event) { thisimg = window.event.srcElement;
  } else if (e.target) { thisimg = e.target;
  } else { return;
  }
  thisimg.src = thisimg.src.replace(/-h(\.[a-z0-9]+)$/i,'$1');
}

// set the hidden action element in a form
function setProductOrderForm(f, a, i) {
	//alert (document.forms[f].name);
	f= document.forms[f];
	f.elements['action'].value= a;
	f.elements['data'].value= i;
	if (a=='removeItem') {
		if (!confirm('Remove this item?')) {
			return;
		}
	}
	f.submit();
	//alert(document.forms[f].elements['action'].value);
	//alert(document.forms[f].elements['product_id'].value);
}


function showVisitor(str) {
	var o= getObject('visitingParties');
	o.innerHTML= str;
}

function hideVisitor() {
	var o= getObject('visitingParties');
	o.innerHTML= '';
}

// Anti spam bot for email
//var lhs = "help";
//var rhs = "mspan.co.uk";
function email_me(lhs, rhs, aClass) {
	// class=\""+aClass+"\"
	document.write("<a href=\"mailto");
	document.write(":" + lhs + "@");
	document.write(rhs + "\" title=\"email: "+lhs+"@"+rhs+"\">" + lhs + "@" + rhs + "<\/a>");
}

function confirmPurchase() {
	if (!document.forms["myForm"].agreement.checked) {
		alert ("Please check the terms and conditions box, in order to proceed.");
		return;
	}
	document.forms["myForm"].submit();
}

function deliveryFormCheck(f, id) {
	var e = document.getElementById(id);
	e.style.display = 'block';
	
	e= f.First_Names;
	if (e.value=='') {
		e.value= 'Enter delivery details';
		setFocus(e);
		e.select()
	}
}

function deliveryFormCheckHide(f, id) {
	var things= new Array('First_Names', 'Last_Name', 'Street_Address_1', 'Street_Address_2', 'City', 'Postcode');
	for (var i=0; i< things.length; i++) {
		f.elements[things[i]].value='';
	} 
	var e = document.getElementById(id);
	e.style.display = 'none';
}

function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}


/* http://www.white-hat-web-design.co.uk/articles/js-fontsize.php */
var min=8;
var max=18;

var els = new Array (2)
els[0]='p';
els[1]='td';
els[2]='div';
els[3]='a';
  
function increaseFontSize() {
   var p;
	
  for(j=0;j<els.length;j++) {
  	p = document.getElementsByTagName(els[j]);
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
      	//alert(p[i].style.fontSize);
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 11;
      }
      if(s!=max) {
         s += 1;
      }
      
      
      p[i].style.fontSize = s+"px";
     }
   }
   //alert(s);
   store('fontSize', s);
}

function setFontSize(s) {
	//alert(s);
	var p;
	
  for(j=0;j<els.length;j++) {
    p = document.getElementsByTagName(els[j]);
    for(i=0;i<p.length;i++) {
      p[i].style.fontSize = s+"px"
    }
    store('fontSize', s);
    //alert(s);
  }
}

function decreaseFontSize() {
  var p;
	
  for(j=0;j<els.length;j++) {
  	p = document.getElementsByTagName(els[j]);
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 11;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }  
  }
   //alert(s);
   store('fontSize', s); 
}

function setFonts() {
	alert(getCookie('fontSize'));
}

function store(data,value) {
  var today = new Date();
  var expires = new Date();
  // set it for a short time
  expires.setTime(today.getTime() + 1000*60*60*24);
  setCookie(data, value, expires);
}

function setCookie(name, value, expire) {
  document.cookie = name + "=" + escape(value)  + ((expire == null) ? "" : ("; expires=" + expire.toGMTString())) + '; path=/';
}

function getCookie(Name) {
	var search = Name + "=";
	if (document.cookie.length > 0) { // if there are any cookies
  	offset = document.cookie.indexOf(search);
  	if (offset != -1) { // if cookie exists
    	offset += search.length;
    	// set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(offset, end))
  	}
	}
	return null;
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


// -----------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// Author: Stephen Poley
// wbsite: http://www.xs4all.nl/~sbpoley/webmatters/
// Update Jun 2005: discovered that reason IE wasn't setting focus was
// due to an IE timing bug. Added 0.1 sec delay to fix.
// -----------------------------------------
var glb_vfld;      // retain vfld for timer thread
function setFocusDelayed(vfld)
{
  glb_vfld.focus()
}
function setFocus(vfld)
{
	//alert(vfld.name);
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}

/****************************************************
     Author: Eric King
     Url: http://redrival.com/eak/index.shtml
     This script is free to use as long as this info is left in
     Featured on Dynamic Drive script library (http://www.dynamicdrive.com)
****************************************************/
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos){
if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=yes,toolbar=no,resizable=no';
win=window.open(mypage,myname,settings);}



/*
 * phplists
 */
function checkform() {
  for (i=0;i<fieldstocheck.length;i++) {
    if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].type") == "checkbox") {
      if (document.subscribeform.elements[fieldstocheck[i]].checked) {
      } else {
        alert("Please enter your "+fieldnames[i]);
        eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
        return false;
      }
    }
    else {
      if (eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].value") == "") {
        alert("Please enter your "+fieldnames[i]);
        eval("document.subscribeform.elements['"+fieldstocheck[i]+"'].focus()");
        return false;
      }
    }
  }
  for (i=0;i<groupstocheck.length;i++) {
    if (!checkGroup(groupstocheck[i],groupnames[i])) {
      return false;
    }
  }

  if(! compareEmail())
  {
    alert("Email Addresses you entered do not match");
    return false;
  }
  return true;
}

var fieldstocheck = new Array();
var fieldnames = new Array();
function addFieldToCheck(value,name) {
  fieldstocheck[fieldstocheck.length] = value;
  fieldnames[fieldnames.length] = name;
}
var groupstocheck = new Array();
var groupnames = new Array();
function addGroupToCheck(value,name) {
  groupstocheck[groupstocheck.length] = value;
  groupnames[groupnames.length] = name;
}

function compareEmail()
{
  return (document.subscribeform.elements["email"].value == document.subscribeform.elements["emailconfirm"].value);
}
function checkGroup(name,value) {
  option = -1;
  for (i=0;i<document.subscribeform.elements[name].length;i++) {
    if (document.subscribeform.elements[name][i].checked) {
      option = i;
    }
  }
  if (option == -1) {
    alert ("Please enter your "+value);
    return false;
  }
  return true;
}
