/* jQuery form functions to show/hide fields in Room/Commute selections */

enableRoomer = function() {
	// Hide the commuter options and clear them out. 
	$('#commuterRequest').hide('100');
	$("input[type='radio']#meal_preference").attr("checked", false);
	
	// Show the room options. 
	$('#roomRequest').show('200');
	return false;
};

enableCommuter = function() {
	$('#roomRequest').hide('100');
	$("input[type='radio']#lodging_preference").attr("checked", false);
	$("input[type='radio']#room_type").attr("checked", false);
	$("input[type='text']#roommate").attr("value", '');
	
	$('#commuterRequest').show('200');
	return false;

};

checkRoomAndCommute = function() {
	if ($("input[type='radio']#roomAtKanuga").attr("checked") == false)
		$('#roomRequest').hide();
	
	if ($("input[type='radio']#commuteToKanuga").attr("checked") == false)
		$('#commuterRequest').hide();
}

$(document).ready(function(){
	checkRoomAndCommute(); 
	
});

/* Generic functions */ 

var shippingRates = new Array(); 
shippingRates["us_first_class"] = "5";
shippingRates["us_priority"] = "8";
shippingRates["international_first_class"] = "9";
shippingRates["international_priority"] = "18";

function calculateTotals(form) {
	var numDVDid = returnObjById("dvd_quantity");
	var numDVDs = numDVDid.value;
	
	// Grab the selected shipping method: 
	var shipping = 0; 
	if ($("input[name='shipping']:checked").val())
		shipping = shippingRates[$("input[name='shipping']:checked").val()];
	
	/*
	for (var i=0; i< form.elements["radiobutton"].length; i++) {
		
		var chk = form.elements["radiobutton"][i];
		if (chk.checked) {
			shipping = shippingRates[i];
			break;
		}
	} */
	
	if (numDVDs) {
		var total = numDVDs * 49; 
		total += shipping * numDVDs;
			
	} else
		total = 0;
	

	var result = returnObjById("total_cost");
	result.value = "$" + total + ".00";
}

/* Timer functions */ 

var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = 5
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        
        submitForm(); 
    }
    else
    {
        self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

/* End Timer functionality. */

/* Begin form functionality. */ 

function returnObjById( id ) {
	if (document.getElementById)
		var returnVar = document.getElementById(id);
	else if (document.all)
		var returnVar = document.all[id];
	else if (document.layers)
		var returnVar = document.layers[id];
	return returnVar;
}

function submitForm() {
	var ppForm = returnObjById('payPalForm');
	
	if (ppForm)
		document.forms[0].submit();
}
