/*
* Author: Mark Hazlett @Pump Interactive
* Date: Nov 29th/2010
* Function: Grabs all the date picker information from the
* booking widget and modifies it so that it follows the procedure
*/

function dateCalculate()
{
	//create a date object from the parameters in the date picker widget
    var checkInDate = new Date(document.getElementById('date-sel').value, (document.getElementById('date-sel-mm').value-1), document.getElementById('date-sel-dd').value,23, 0, 0);
    
    //grab the current date to check against
    var currentDate = new Date();
	
	//Check if the check in date is before the current date and set the date going forward back to the current date
	if(checkInDate < currentDate)
	{
		//The check in date selected by the user was before the current date
		//so set the date back to the current date so the new booking widget doesn't get wrong data
		checkInDate = new Date();
		alert("Please select a date from today on.");
		return false;
	}
	
	//Get the seconds so that we can add the number of nights
    var tempSeconds = checkInDate.getTime();
	
	//Add the number of nights(turned into seconds) and add number of nights
    var numberOfDays = document.getElementById('nights').value * 86400000;

	var daysAdded = tempSeconds + numberOfDays;	

    var endDate = new Date(daysAdded);
	
	/*
	* If statement that checks for exeption cases that are not using the new booking engine
	* Once the booking engine goes live on bellstar.ca use resprocess.php instead and re-route that this way
	*/
    if (document.getElementById('hotelId').value == 1)
    {
    	//Bellasera site
		// creat the formid input field so eForm picks up the data on the other end
		var hidden_input = document.createElement('input');
		hidden_input.type = 'hidden';
		hidden_input.name = 'formid';
		hidden_input.value = 'reservation-form';
		document.getElementById('resform').appendChild(hidden_input);

		// convert the month number into text
		var month_array = new Array("", "January","February","March","April","May","June","July","August","September","October","November","December");
		var hidden_month = document.createElement('input');
		hidden_month.type = 'hidden';
		hidden_month.name = 'month';
		hidden_month.value = month_array[document.getElementById('date-sel-mm').value];
		document.getElementById('resform').appendChild(hidden_month);

        document.getElementById('resform').action = 'http://www.bellasera.ca/reservation.html';
		document.getElementById('resform').method = 'post';		
		document.getElementById('date-sel-dd').name = 'day';
		document.getElementById('date-sel').name = 'year';
		document.getElementById('numAdults').name = 'adults';
		document.getElementById('numChildren').name = 'children';
    }
    // HANDLED BY /resprocess.php NOW
	else if (document.getElementById('hotelId').value == 0) 
    {
    	//"Select a resort" option was selected
        alert("Please select a resort");
        return false;
    }
    else if (document.getElementById('hotelId').value == 3) 
    {
        document.getElementById('resform').action = 'http://www.secure-res.com/res/vn4/mp-3.aspx?mps=bell&mpg=1514';
    }
    else if (document.getElementById('hotelId').value == 2595)
    {
    	//Canmore Crossing
    	document.getElementById('resform').action = "http://www.secure-res.com/res/vn3/checka.aspx";
    }
    else 
    {
        document.getElementById('checkInDate').value = checkInDate.getFullYear() + "-" + (checkInDate.getMonth()+1) + "-" + checkInDate.getDate();
        document.getElementById('checkOutDate').value = endDate.getFullYear() + "-" + (endDate.getMonth()+1) + "-" + endDate.getDate();
    }

	return true;	
}

function isLeapYear(yrVal) {
    var leapYear = false;
    var year = parseInt(yrVal, 10);

    if (year % 4 == 0) {
        leapYear = true;
    }
    return leapYear;
}

function getDaysInMonth(monthVal, YrVal) {
    var maxDays = 31
    if (monthVal == 2) {
        if (isLeapYear(YrVal)) {
            maxDays = 29;
        }
        else {
            maxDays = 28;
        }
    }
    else if (monthVal == 3 || monthVal == 5 || monthVal == 8 || monthVal == 10) {
        maxDays = 30;
    }

    return maxDays;
}

function rewriteDays(selected_month) 
{
    var num_days = getDaysInMonth(selected_month, document.getElementById('date-sel').value);

    var day_select = document.getElementById('date-sel-dd');
    var selected_day = day_select.value;
    while (day_select.childNodes.length >= 1)
    day_select.removeChild(day_select.firstChild);

    for (var xi = 1; xi <= num_days; xi++) {
        var temp = document.createElement('option');
        temp.setAttribute('value', xi);

        // try to keep the same day selected
        if (xi == selected_day) temp.setAttribute('selected', 'selected');

        temp.innerHTML = xi;
        day_select.appendChild(temp);
    }
}

function setD(y, m, d) {
    document.checkaressrch.year.value = y;
    document.checkaressrch.month.selectedIndex = --m;
    for (var i = 0; i < document.getElementById('date-sel-dd').options.length; i++) {
        if (document.getElementById('date-sel-dd').options[i].value == d) {
            document.getElementById('date-sel-dd').selectedIndex = i;
        }
    }
}

function getDateString(y_obj, m_obj, d_obj) {
    var y = y_obj.options[y_obj.selectedIndex].value;
    var m = m_obj.options[m_obj.selectedIndex].value;
    var d = d_obj.options[d_obj.selectedIndex].value;
    if (y == "" || m == "") {
        return null;
    }
    if (d == "") {
        d = 1;
    }
    return str = y + '-' + m + '-' + d;
}

var advancedDay = 0;

function init() {
    var dt = new Date();

    var yr = dt.getFullYear();
    var curYr = dt.getFullYear();
    var mo = dt.getMonth();
    mo++;
    var da = dt.getDate();

    var daysInCurrent = getDaysInMonth(mo, yr);

    if (da + advancedDay > daysInCurrent) {
        da = ((da + advancedDay) % daysInCurrent);
        if (mo == 12) {
            mo = 1;
            yr++;
        }
        else {
            mo++;
        }
    }
    else {
        da = da + advancedDay;
    }
    da--;
    mo--;
    yr -= curYr;
    document.getElementById('date-sel-mm').selectedIndex = mo;
    document.getElementById('date-sel-dd').selectedIndex = da;
    document.getElementById('date-sel').selectedIndex = yr;
    return;
}

