$(document).ready(init);
function init()
{
	formReset();
}//end init


function formReset(){
    var x=document.forms.bookingform;
	if(x){
	    x.reset();
	}
}//end function


function checkPeople(){

var total=Number($('select#selectAdults').val())+Number($('select#selectChildren').val()) +Number($('select#selectInfants').val());

//check to see what holiday type is selected
var ctype = $('select#holidayType').val();
if (ctype == "Touring"){
	return true;
}else{

if( Number($('select#accomType').val())==6){
//total can be 6

	if (total >6){
		alert('Total number of people ('+total+') exceeds accommmodation (6)');
		return false;
	}else{
		return true;
	}//end if

}else{

	// total can be 4
	if (total >8){
	        alert('Total number of people ('+total+') exceeds accommmodation (8)');
	        return false;
	}else{
		return true;
	}

}//end if
}//end holiday check

}//end func



function selectPeople(people){
    
    var options = "<option value='1'>1 </option>";
    options += "<option value='2'>2 </option>";
    options += "<option value='3'>3 </option>";
    options += "<option value='4'>4 </option>";
    options += "<option value='5'>5 </option>";
    options += "<option value='6'>6 </option>";

	if(people==8){
	    options += "<option value='7'>7 </option>";
	    options += "<option value='8'>8 </option>";
	}

    options += "<option value='' selected='selected'>Adults</option>";

    $('select#selectAdults').html(options);

}//end func


function selectNumberNights(nights){

    var ctype = $('select#holidayType').val();
    var options="";

    if(ctype=='Touring'){
	    $('div#accomWrapper').hide();
	    
	    var options = "<option value='1'>1 Night</option>";
	    options += "<option value='2'>2 Nights</option>";
	    options += "<option value='3'>3 Nights</option>";
	    options += "<option value='4'>4 Nights</option>";
	    options += "<option value='5'>5 Nights</option>";
	    options += "<option value='6'>6 Nights</option>";
	    options += "<option value='7'>7 Nights</option>";
	    options += "<option value='8'>8 Nights</option>";
	    options += "<option value='9'>9 Nights</option>";
	    options += "<option value='10'>10 Nights</option>";
	    options += "<option value='11'>11 Nights</option>";
	    options += "<option value='12'>12 Nights</option>";
	    options += "<option value='13'>13 Nights</option>";
	    options += "<option value='14'>14 Nights</option>";
	    options += "<option value='15'>15 Nights</option>";
	    options += "<option value='16'>16 Nights</option>";
	    options += "<option value='17'>17 Nights</option>";
	    options += "<option value='18'>18 Nights</option>";
	    options += "<option value='19'>19 Nights</option>";
	    options += "<option value='20'>20 Nights</option>";
	    options += "<option value='21'>21 Nights</option>";
	    options += "<option value=''>Please choose...</option>";
	
	    $('select#numberNights').html(options);
	
	    var options = "<option value='6' selected='selected'>Touring selected</option>";
	    $('select#accomType').html(options);
	
	    var options = "<option value='1'>1 </option>";
	    options += "<option value='2'>2 </option>";
	    options += "<option value='3'>3 </option>";
	    options += "<option value='4'>4 </option>";	
	    options += "<option value='5'>5 (additional cost)</option>";
	    options += "<option value='6'>6 (additional cost)</option>";
	    options += "<option value='7'>7 (additional cost)</option>";
	    options += "<option value=\"null\">Please choose...</option>";
	
	    $('select#selectAdults').html(options);

    }else{

	    $('div#accomWrapper').show();
	    
	    var options = "<option value='3'>3 Night</option>";
	    options += "<option value='4'>4 Nights</option>";
	    options += "<option value='7'>7 Nights</option>";
	    options += "<option value='14'>14 Nights</option>";
	    options += "<option value=\"null\" selected='selected'>Please choose...</option>";
	
	    $('select#numberNights').html(options);
	
	    var options = "<option value='Budget'>Budget Accommodation</option>";
	    options += "<option value='Bronze'>Bronze Accommodation</option>";
		options += "<option value='Silver'>Silver Accommodation</option>";
		options += "<option value='Gold'>Gold Accommodation</option>";
	    options += "<option value=\"null\" selected='selected'>Please choose Accommodation...</option>"; 
	
	    $('select#accomType').html(options);

    }

}//end func

function selectHolidayType(park){

if(park==1){

    var options = "<option value='Self Catering'>Self Catering</option>";
    options += "<option value='Touring'>Touring</option>";
    options += "<option value='' selected='selected'>Please choose</option>";

    $('select#holidayType').html(options);
}else{
    var options = "<option value='Self Catering'>Self Catering</option>";
    options += "<option value='' selected='selected'>Please choose...</option>";

    $('select#holidayType').html(options);
}

}//end func


function addDays(myDate,days) {
    day=myDate.substring(8,10);

    month=myDate.substring(5,7);

    year=myDate.substring(0,4);

    d1= new Date(year,month-1,day);
    var d=new Date(d1.getTime() + days*24*60*60*1000);
    return d.formatDate('Y-m-d');
}//end func


function selectStartDate(Month){

   Month=Month-1;
   var options= "";
   //errors here
   if($('select#holidayType').val() != 'Touring'){
       
    	if ($('select#numberNights').val()==3){
	
    		var days = WeekStart(Month,2010,5);
        
            /*for (x in days){*/
            for(x=0;x<days.length;x++){
                options += "<option value='" + days[x].formatDate('d-m-Y') + "'>" + days[x].formatDate('D, dS M Y') + "</option>";
    		}//end for
	
    	}else if($('select#numberNights').val()==4){
	
    		var days = WeekStart(Month,2010,1);
                
    		/*for (x in days){*/
            for(x=0;x<days.length;x++){
    			options += "<option value='" + days[x].formatDate('d-m-Y') + "'>" + days[x].formatDate('D, dS M Y') + "</option>";
    		}//end for
	
    	}else{
	
    	    var d1 = new Array();
    	    var d4 = WeekStart(Month,2010,6);
	
    	    var days = d1.concat(d4);
	 
    	    /*for (x in days){*/
            for(x=0;x<days.length;x++){
                options += "<option value='" + days[x].formatDate('d-m-Y') + "'>" + days[x].formatDate('D, dS M Y') + "</option>";
    	    }//end for
	
    	}
    } else {
	
	    var d1 = new Array();
	    var d2 = WeekStart(Month,2010,1);
	    var d3 = WeekStart(Month,2010,2);
	    var d4 = WeekStart(Month,2010,3);
	    var d5 = WeekStart(Month,2010,4);
	    var d6 = WeekStart(Month,2010,5);
	    var d7 = WeekStart(Month,2010,6);
	    var d8 = WeekStart(Month,2010,7);
	
	    var days = d1.concat(d2,d3,d4,d5,d6,d7,d8);
	    options="";
	
	    /*for (x in days){*/
        for(x=0;x<days.length;x++){
	    	options += "<option value='" + days[x].formatDate('d-m-Y') + "'>" + days[x].formatDate('D, dS M Y') + "</option>";
	    }//end for
	    
	}//end if

	options += "<option value='' selected='selected'>Please choose...</option>";
	
	$('select#arrivalDate').html(options);

}//end func


function WeekStart(Month,Year,StartOfWeekDay) {
  _Date = new Date(Year, Month, 1);
  _LastDate = new Date();
  _LastDate = new Date(_Date);
  _LastDate.setMonth(_Date.getMonth() + 1);
  _LastDate.setHours(_LastDate.getHours() - 24);

  _Returns = new Array();

  if (StartOfWeekDay === null) {
    StartOfWeekDay = 0;
  }
  var I = 1;
  while (I <= _LastDate.getDate()) {
    _Date.setDate(I);
    if (_Date.getDay() == StartOfWeekDay) {
      var d = new Date(_Date);
      _Returns[_Returns.length] = d;
      I += 6;
    }
    I++;
  }

  return(_Returns);
}

// get URL params func
function getUrlVars(){

    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++){
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }//end for

    return vars;

}//end func
