/* ******************************************************************************
*this is external javaScript file attached to form in 
*in www.smytham.co.uk aplication
*scripted by Richard Wiseman 
*created on 30/07/2010
*********************************************************************************/

//arrary to contain month lengths
var monthLength = new Array (31,29,31,30,31,30,31,31,30,31,30,31);
var arrivalDayNumber=0;
var departDayNumber=0;
var strCost=0;


// get price data and put in array for latter use
var allDataArray = new Array();		

function writePrice(){//populate the multidimentional allDataArray by spliting priceArrayCamp when page is loaded
	for (i=60; i<[priceArrayCamp.length]; i++){
		allDataArray[i] = new Array;
				
		allDataArray[i] = priceArrayCamp[i].split(',');
	}
	//bit to write price table to page populate using allDataArray
	strTd1='<td align="center" style="font-size:12px;">'
	strTd2='</td><td align="center" style="font-size:12px;">'
	strTd3='<tr><td align="center" style="font-size:12px;">'
	strStyle='Season<br /><span style="font-size: 10px;">'
//first row
	document.write('<table id="campPriceTable" border="0" cellpadding="0"><tr><td width="175"></td>'+strTd1+'Caravan'+strTd2+'Camper'+strTd2+'Tent'+strTd2+'Two Man Tent    '+strTd2+'Awning'+strTd2+'Extra Person</td>'+strTd2+'Pet</td>'+strTd2+'Extra Car</td></tr>');
	
	//low season price [85] mid season [170] high season[210]
//second row
	document.write(strTd3+'Low '+strStyle+'23rd March-3rd April<br />8th May-31st May<br />2nd Sept-31st Oct</span></td>');
	fillInForm(85,0);//caravan and camper price are from the same array cell so this one first then cycle through
	document.write(strTd1+'&#163;'+strCost+'</td>');
		for (i=0; i<5; i++){//cycle cells in 1st row
			fillInForm(85,i);
			document.write(strTd1+'&#163;'+strCost+'</td>');
		}	
	//last two cells in row
	fillInForm2();
//third row
	document.write(strTd3+'Mid '+strStyle+'4th May-7th May<br />10th June-18th July</span></td>');
	fillInForm(170,0);
	document.write(strTd1+'&#163;'+strCost+'</td>');
		for (i=0; i<5; i++){//cycle cells in 2nd row
			fillInForm(170,i);
			document.write(strTd1+'&#163;'+strCost+'</td>');
		}	
	//last two cells in row
	fillInForm2();
//forth row
	document.write(strTd3+'High '+strStyle+'1st June-9th June<br />19th July-1st Sept</span></td>');
	fillInForm(210,0);
	document.write(strTd1+'&#163;'+strCost+'</td>');
		for (i=0; i<5; i++){//cycle cells in 3rd row
			fillInForm(210,i);
			document.write(strTd1+'&#163;'+strCost+'</td>');
		}	
	//last two cells in row
	fillInForm2();
	
	//finish the table
	document.write('</tr>'+strTd3+'Late stay until 16:00 on the day of your departure may be availible, check at reception </td></tr></table>');
}

//attached to writePrice()
function fillInForm(num1,num2){
	strCost=parseFloat(allDataArray[num1][num2]);
	formatCurrency(strCost)
}

//attached to writePrice()
function fillInForm2(){
	strCost=parseFloat(carCost);
	formatCurrency(strCost);
	
	document.write(strTd1+ '&#163;'+strCost+'</td>')
	
	strCost=parseFloat(petCost);
	formatCurrency(strCost);
	
	document.write(strTd1+ '&#163;'+strCost+'</td>')
}
//**************************************

function numberDays(choise){
var arriveMonth = document.getElementById("arrivalMonth").value;
var departMonth = document.getElementById("departureMonth").value;
}

	//function to remove all options form the arrivelDay field
function removeOptionLast(day){
	for (i = 0; i <= 30; i++){
		var elSel = document.getElementById(day);
	if (elSel.length > 0)			{
		elSel.remove(elSel.length - 1);
	}
	}
}
	
function setDays(number,place){//function to create day options
	for (i = 1; i <=number; i++){
		var elOptNew = document.createElement('option');
		elOptNew.text = i;
		elOptNew.value = i;
		var elSel = document.getElementById(place);
		try {
		elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
		elSel.add(elOptNew); // IE only
		}
	}
}


//function to add a number ofoptions to the arrivelDay field if certain month is selected
function addDay(monthId,dayId){
	removeOptionLast(dayId);
	var strMonthValue = document.getElementById(monthId);
	strMonthValue=parseInt(strMonthValue.value,10);
	setDays(monthLength[strMonthValue],dayId);
}

function arriveAddDay(){
	addDay('arrivalMonth','arrivalDay');
}

function departAddDay(){
	addDay('departureMonth','departureDay');
}

//now including 7 for the price of 6 at end of function
function calculate(){
	arrivalDayNumber=0;
	departDayNumber=0;
	strCost=0;

	
		
	
	
	
	//errorText(" ","errorMessage");//reset error msg

	//arrival date get value and convert to day number
	var arrivalMonth = document.getElementById('arrivalMonth');
	var departureMonth = document.getElementById('departureMonth');
	arrivalMonth=parseInt(arrivalMonth.value,10);
	departureMonth=parseInt(departureMonth.value,10);
	//departure date get their value and convert to day number
	var arrivalDay = document.getElementById('arrivalDay');
	var departureDay = document.getElementById('departureDay');
	arrivalDay=parseInt(arrivalDay.value,10);
	departureDay=parseInt(departureDay.value,10);

	//turn day and month in to a day number for arrival and departure
	for (i=0;i<=arrivalMonth-1;i++){
		arrivalDayNumber=arrivalDayNumber+monthLength[i];
	}
	arrivalDayNumber=arrivalDayNumber+arrivalDay
	for (i=0;i<=departureMonth-1;i++){
		departDayNumber=departDayNumber+monthLength[i];
	}
	departDayNumber=departDayNumber+departureDay;

	checkDays(arrivalDayNumber,departDayNumber);

	//get unit type
	var strUnitType = document.getElementById('unitType');
	strUnitType=parseInt(strUnitType.value);
//load field for tent size
	if (strUnitType==3){
		loadPayment('tentSize','')
	}//end if 
	
	var strFindCost='';

	if(strUnitType!=0){
		if(strUnitType==1){//caravan
			strFindCost=0
		}
		if(strUnitType==2){//camper same as caravan
			strFindCost=0
		}
		if(strUnitType==3){//tent
		
		
		
			strFindCost=1
		}
		if(strUnitType==4){//walker tent
			strFindCost=2
		}
	}
	else{
		formatCurrency(0);//this resets holiday cost to zero

		errorText('please select type of camping!',"errorMessage2");
		return false;
	}

	//get number of people
	var strExtraPerson = document.getElementById('numberPeople');
	strExtraPerson=parseInt(strExtraPerson.value,10);	
	//get number of pets
	var strPets = document.getElementById('numberPets');
	strPets=parseInt(strPets.value,10);	
	
	for (i=arrivalDayNumber;i<=departDayNumber-1;i++){
		var dayCost=allDataArray[i][strFindCost];
		dayCost=parseFloat(dayCost);	
		strCost=strCost+dayCost;

		if(document.getElementById("awning").checked){
			var awningCost=allDataArray[i][3];
			awningCost=parseFloat(awningCost);
			strCost=strCost+awningCost;
		}
		if (strExtraPerson>=3){
			var extraPersonCost=allDataArray[i][4];
			extraPersonCost=parseFloat(extraPersonCost);
			strCost=strCost+(extraPersonCost*(strExtraPerson-2));
		}
		if (strPets>=1){
			strCost=strCost+(petCost*strPets);//pet cost stays same not in allDataArray

		}
		if (document.getElementById("extraCar").checked){//extra car cost stays same not in allDataArray
			strCost=strCost+carCost;
		}		
	}

	//now include 7 for the price of 6
	var tmpCost=0;//to reset
	var tmpCostArray=new Array();
	
	if(departDayNumber-arrivalDayNumber>6){//more than 6 nights
		if(allDataArray[arrivalDayNumber][0]!=allDataArray[213][0]){
			if(allDataArray[departDayNumber][0]!=allDataArray[213][0]){//start and finish date are not high season, check against aug 1st
				//subtract lowest cost night put prices in array first then compare using  array.min()   subtract this from total
				c=0
				for(i=arrivalDayNumber;i<=departDayNumber-1;i++){
					tmpCostArray[c]=allDataArray[i][1];
					tmpCost=tmpCost+parseFloat(allDataArray[i][1]);
					c=c+1
				}
				//this bit finds the minimum value in the array
				var strMinimum;
				var firstNumber = tmpCostArray[0];
				var minPlace=0;
				for (i = 0; i<tmpCostArray.length; i++){
					if (tmpCostArray[i] <= firstNumber){
						minPlace=i+arrivalDayNumber;//need this to find extra costs in tmpCostArray
						strMinimum = parseFloat(tmpCostArray[i]);
					}
				}//end of this bit
				//also add extras to var minimum
				if(document.getElementById("awning").checked){//is also an awning?
					var strTemp=parseFloat(allDataArray[minPlace][3]);
					strMinimum=strMinimum+strTemp;
				}
				if (strExtraPerson>=3){//is extra person more than 2
					var strTemp=parseFloat(allDataArray[minPlace][4]);
					strMinimum=strMinimum+(strTemp*(strExtraPerson-2));
				}
				if (strPets>=1){//is pet 1 or more
					strMinimum=strMinimum+(petCost*strPets);
				}
				if (document.getElementById("extraCar").checked){//is extra car
					strMinimum=strMinimum+carCost;
				}

				errorText('This price includes 7 for the price of 6. You are saving '+strMinimum+' pounds',"errorMessage2");//error msg 'includes 7 for the price of 6
				strCost=strCost-strMinimum;
				
				if(departDayNumber-arrivalDayNumber>13){//more than 13 nights subtract one more night do subtract strMinimum again
					strCost=strCost-strMinimum;//subtract minimum again
					errorText('This price includes 7 for the price of 6. You are saving '+strMinimum*2+' pounds',"errorMessage2");
				}
			}
		}	
	}//end of 7 for the price of 6	


	formatCurrency(strCost);
	document.campForm.priceMsg.value = strCost;
	checkAvailable(arrivalDayNumber,departDayNumber);//need to check if dates are availible	
	

	payNow(strCost);
	
}//end  calculate()

//function called from calculate()
function payNow(strCost){
	if(strWishToBook==1){
		strCost=strCost+"";
		//get todays year day number
		var currentTime = new Date()	
		var toDayNumber = currentTime.getDate();
		var toDayMonth = currentTime.getMonth();

		for (i=0;i<=toDayMonth-1;i++){
			toDayNumber=toDayNumber+monthLength[i];
		}
		//clear the priceMessage field
		
		loadPayment('payment1','none');
		loadPayment('payment2','none');
		loadPayment('payment3','none');
		loadPayment('payment4','none');

		
		//compare to start of holiday day of year number if difference less than 21, print to page message "pay in full now"
		if(toDayNumber+21>arrivalDayNumber){
			formatCurrency(strCost);	
			document.getElementById("payInFull").checked = true
			loadPayment('payment1','');
			errorText(strCost,"priceMessage1");
		}
		else{
			if(strCost<20){		
				loadPayment('payment2','');
				errorText(strCost,"priceMessage2");
			}
			else{
				strCost=strCost-20
			
				formatCurrency(strCost);
				
				loadPayment('payment3','');
				loadPayment('payment4','');
				errorText(strCost,"priceMessage3");
			}
		}
	}
	if(strWishToBook==0){
		loadPayment('payment1','none');
		loadPayment('payment2','none');
		loadPayment('payment3','none');
		loadPayment('payment4','none');	
	}
}

//show or hide payment row
function loadPayment(rowToFind,displayType){
	var strStuff=document.getElementsByTagName('*');//get all tags and put in strStuff
	for(var i=0; i<strStuff.length; i++) {
		if(strStuff[i].className==rowToFind) {
			strStuff[i].style.display=displayType;
			
		}
	}
}

// bit to check availability for all days selected and call for error masg
function checkAvailable(arrivalDayNumber,departDayNumber){
var strMonth;
var strDay;
	for (i=arrivalDayNumber;i<=departDayNumber-1;i++){
	var strBookCheck=allDataArray[i][5];
	strBookCheck=parseFloat(strBookCheck);
		if(strBookCheck==2){
			switch (true){//name the month
				case (i<=90)://march
					strMonth="March";
					strDay=i-59;//take of total of days before month start
					bookedMsg(strMonth,strDay)
					break;
				case (i<=120)://april
					strMonth="April";
					strDay=i-90;
					bookedMsg(strMonth,strDay)
					//return false;
					break;
				case (i<=152)://may
					strMonth="May";
					strDay=i-120;
					bookedMsg(strMonth,strDay)
					break;
				case (i<=182)://june
					strMonth="June";
					strDay=i-151;
					bookedMsg(strMonth,strDay)
					break;
				case (i<=213)://july
					strMonth="July";
					strDay=i-181;
					bookedMsg(strMonth,strDay)
					break;	
				case (i<=244)://aug
					strMonth="Aug";
					strDay=i-212;
					bookedMsg(strMonth,strDay)
					break;
				case (i<=274)://sept
					strMonth="Sept";
					strDay=i-243;
					bookedMsg(strMonth,strDay)
					break;		
				default://oct
					strMonth="Oct";
					strDay=i-273;
					bookedMsg(strMonth,strDay)
					break;	
				formatCurrency(0)
				return false;
			}
		}
	}
}	

function bookedMsg(strMonth,strDay){
	errorText("sorry "+strMonth+" "+strDay+" is booked","errorMessage2");
	formatCurrency(0);
	document.campForm.priceMsg.value = strCost;
	return false;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))num = "0";
	num = Math.floor(num*100+0.50000000001);
	pence = num%100;
	num = Math.floor(num/100).toString();
	if(pence<10)
	pence = "0" + pence;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+num.substring(num.length-(4*i+3));
	strCost=(num + '.' + pence);
	strCost=strCost+" ";
	return strCost;
}

//function to compare the dayNumbers of arrival and departure and return error message if arrival is latter than departure
function checkDays(arrival,departure){
	if(arrival<=83){//compare arrival date to opening date
		errorText("Sorry we do not open until the 25th of March","errorMessage2")	
	}
	else{
		if(departure<=arrival){
			errorText("please select a departure date that is later than your arrival","errorMessage2")
		}
		else{
			errorText(" ","errorMessage2")
		}
	}
}





