﻿
var PDS_Child = new Array(0, 0, 0, 74, 96, 116, 137, 154, 170, 186, 199, 211, 222, 234, 244);
var PDS_Adult = new Array(0, 0, 0, 111, 143, 173, 205, 230, 254, 277, 297, 315, 332, 249, 364);
var PDS_Senior = new Array(0, 0, 0, 89, 114, 138, 164, 184, 203, 222, 238, 252, 266, 279, 291);

var MLG_Child = new Array(0, 0, 0, 60.70, 78.20, 95.00, 112.80, 124.90, 134.50, 148.90, 164.20, 176.90, 187.70, 198.90, 211.50);
var MLG_Adult = new Array(0, 0, 0, 80.70, 104.20, 127.30, 149.70, 165.20, 180.70, 198.90, 218.10, 235.20, 250.90, 265.40, 281.90);
var MLG_Senior = new Array(0, 0, 0, 64.30, 83.00, 101.90, 119.90, 133.40, 144.90, 159.40, 174.60, 187.70, 199.90, 211.40, 224.90);


function GetSkiPassPrice() {

//alert("test");
  var childPrice = 0;
  var adultPrice = 0;
  var seniorPrice = 0;
     
  if($('.dropdownskiarea').val() == 'pds') {
    childPrice = $('.dropdownskichildpasses').val() * PDS_Child[$('.dropdownskidays').val()];
    adultPrice = $('.dropdownskiadultpasses').val() * PDS_Adult[$('.dropdownskidays').val()];
    seniorPrice = $('.dropdownskiseniorpasses').val() * PDS_Senior[$('.dropdownskidays').val()];
  }
  else {
    childPrice = $('.dropdownskichildpasses').val() * MLG_Child[$('.dropdownskidays').val()];
    adultPrice = $('.dropdownskiadultpasses').val() * MLG_Adult[$('.dropdownskidays').val()];
    seniorPrice = $('.dropdownskiseniorpasses').val() * MLG_Senior[$('.dropdownskidays').val()];
  }


  var totalPrice = (childPrice + adultPrice + seniorPrice);

  $('#ChildPrice').text(roundNumber(childPrice, 2));
  $('#AdultPrice').text(roundNumber(adultPrice, 2));
  $('#SeniorPrice').text(roundNumber(seniorPrice, 2));
  $('.finalprice').text(roundNumber(totalPrice, 2));  
  $('.hiddentextbox').val(roundNumber(totalPrice, 2));  
}


function ValidateStartDate() {

  var startDate = new Date();
  startDate.setFullYear(2009, 12, 19);

  var endDate = new Date();
  endDate.setFullYear(2010, 4, 3); 

  var BookingDate = $('.textboxski').val();
  //alert(startDate);
  if(BookingDate != "") {
    if(isDate(BookingDate) == true) {
      
      BookingDate = getDate(BookingDate);
      
      if(BookingDate >= startDate && BookingDate <= endDate) {
        // Validate Date  
        $('#DateError').attr('style','display: none'); 
      }
      else {
        // Invalid Date     
        $('#DateError').attr('style','display: block'); 
      }
    }
    else {
      // Invalid Date     
      $('#DateError').attr('style','display: block'); 
    }
  }
  else {
    // Blank date
    $('#DateError').attr('style','display: none'); 
  }
}



function roundNumber(number,decimals) {
	var newString;// The new rounded number
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		if (d1 == 10) {
			numString = numString.substring(0, numString.lastIndexOf("."));
			var roundedNum = Number(numString) + 1;
			newString = roundedNum.toString() + '.';
		} else {
			newString = numString.substring(0,cutoff) + d1.toString();
		}
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	//var newNumber = Number(newString);// make it a number if you like
  //	document.roundform.roundedfield.value = newString; // Output the result to the form field (change for your purposes)
  return newString;
}



/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function getDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
	var returnDate = new Date();
	returnDate.setFullYear(year, month, day);
  return returnDate;
}

