var submitformnow = 0;

if ( ! window.$) {
     $ = function(id) { return document.getElementById(id); }
}
function showPrices(s) {
	// s is a select

	// hide master duration radio block (contains max 3 blocks, one per semester) first
	$('durationRadios').style.display = 'none';

	// hide payplan radio block
	$('payplanRadios').style.display = 'none';

	//hideContinue();

	// hide each duration radio block within the master
	for (var i=1; i<=3; i++) {
		$('durationGroup'+i).style.display = 'none';
	}

	if (s.selectedIndex != -1) {
		durGroupID = s.options[s.selectedIndex].getAttribute('semester');

		// clear last checked duration if it's not in the chosen semester's group.
		var inputs = document.getElementsByTagName('input');
		if ($('p_tpID').value) {
			for (var i=0; i<inputs.length; i++) {
				if (inputs[i].type == 'radio' && inputs[i].name == 'tpID') {
					if (inputs[i].value == $('p_tpID').value && inputs[i].getAttribute('groupID') != durGroupID) {
						$('p_tpID').value = "";
						break;
					}
				}
			}
		}

		so = s.options[s.selectedIndex];
		var f = document.theForm;

		// uncheck all duration radios except possibly one passed in
		for (var i=0; i<inputs.length; i++) {
			if (inputs[i].type == 'radio' && inputs[i].name == 'tpID') {
				inputs[i].checked = false;
				// pre-select first duration radio unless value passed in
				if (!$('p_tpID').value && (!$('p_durationID').value || inputs[i].getAttribute('durationID') == $('p_durationID').value) && inputs[i].getAttribute('groupID') == durGroupID && !inputs[i].disabled) {
					$('p_tpID').value = inputs[i].value;
				}
				//if (inputs[i].value == $('p_tpID').value && !so.getAttribute('notavailable')) {
				if (inputs[i].value == $('p_tpID').value && (!so.getAttribute('notavailable') || (f._admin_ && f._admin_.value))) {
					inputs[i].checked = true;
					showPayplans(inputs[i]);
				}
			}
		}

		if ($('endDate')) {
			$('endDate').innerHTML = '';
		}
		$('endDateP').style.display = 'block';
		$('notAvailable').style.display = 'none';
		if (so.getAttribute('notavailable') && !((f._admin_ && f._admin_.value) || (f._continue4_ && f._continue4_.value))) {
			hideContinue();

			// per samia 10/21/2008
			$('semName').innerHTML = so.getAttribute('startsemester');
			$('semYear').innerHTML = so.getAttribute('startyear');
			var nextAvailDate;
			var nextAvailMonth;
			for (var oi=s.selectedIndex+1; oi<s.options.length; oi++) {
				var thisso = s.options[oi];
				if (!thisso.getAttribute('notavailable')) {
					nextAvailDate = thisso.getAttribute('startmonth') + ', ' + thisso.getAttribute('startyear') + ' (' + thisso.getAttribute('startsemester') + ')';
					nextAvailMonth = thisso.getAttribute('startmonth');
					break;
				}
			}
			$('nextAvailDate').innerHTML = nextAvailDate;
			$('nextAvailMonth').innerHTML = nextAvailMonth;
			$('endDateP').style.display = 'none';
			$('notAvailable').style.display = 'block';
		}
		else {
			$('continueLink').style.display = '';
			if (so.getAttribute('semester')) {
				$('durationGroup'+so.getAttribute('semester')).style.display = 'block';
				$('durationRadios').style.display = 'block';
			}
			if (so.getAttribute('startYear')) {
				$('startYear').value = so.getAttribute('startYear');
			}
			if ($('endDate')) {
				$('endDate').innerHTML = so.getAttribute('endMonthDate');
			}
		}
	}
}
function showContinue(r)
{
	// r is a payplan radio
	document.theForm.monthlypayments.value = r.getAttribute('monthlypayments');
	document.theForm.payment.value = r.getAttribute('payment');
	document.theForm.lessonPrice.value = r.getAttribute('lessonPrice');
	document.theForm.justLessonPrice.value = r.getAttribute('justLessonPrice');

	$('continueLink').style.display = '';
}
function hideContinue()
{
	$('continueLink').style.display = 'none';
}
function showPayplans(r)
{
	// r is a duration radio

	document.theForm.durationID.value = r.getAttribute('durationID');

    if ($('selectMonth').style.display == 'block' && checkSupport()) {
      $('payplans').innerHTML = '<span style="font-style: italic;">Loading pay options...</span>';
      $('payplanRadios').style.display = 'block';
      url = 'getPayplans.cgi?l=' + $('l').value + '&tpID=' + r.value + '&locationID=' + $('locationID').value + '&lessons=' + $('startMonth').options[$('startMonth').selectedIndex].getAttribute('lessons') + '&s_h=' + $('s_h').value + '&payplanID=' + $('p_payplanID').value + '&durationID=' + r.getAttribute('durationID');
      loadDoc(url);
      return false;
    }
    else {
	//hideContinue();
	$('continueLink').style.display = 'none';
      return true;
    }
}

function checkSupport() 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
        }
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
            }
        }
    }
    return req;
}

function loadDoc(url) 
{
    req.onreadystatechange = processReqChange;
    req.open("GET", url, true);
    req.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT'); // for Safari!
    req.send(null);
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
		resp = req.responseText.split('^');
		$('payplans').innerHTML = resp[0];
		if ($('p_payplanID').value) {
			showContinue($('ppRadio'+$('p_payplanID').value));
			$('p_payplanID').value = "";
		}
		else if (resp[1]) {
			showContinue($('ppRadio'+resp[1]));
		}
		if (submitformnow) submitForm();
        } else {
		alert("There was a problem retrieving the data: " + req.status);
        }
    }
}

function submitForm() 
{
	f = document.theForm;

	// see if duration selected
	tpIDvalue = 0;
	n = f.tpID;
	if (n) {
		if (n.length) {
			// multiple duration radios exist
			for (i=0; i<n.length; i++) {
				if (n[i].checked) {
					tpIDvalue++;
					break;
				}
			}
		}
		// single duration radio exists
		else if (n.checked) {
			tpIDvalue++;
		}
	}

	// see if payplan selected
	payplanIDvalue = 0;
	n = f.payplanID;
	if (n) {
		// multiple payplan radios exist
		if (n.length) {
			for (i=0; i<n.length; i++) {
				if (n[i].checked) {
					payplanIDvalue++;
					break;
				}
			}
		}
		// single payplan radio exists
		else if (n.checked) {
			payplanIDvalue++;
		}
	}

	if (!tpIDvalue || !payplanIDvalue) {
		if (f.l.value == 'fr') {
			alert('Veuillez selectionner votre:\n\nDuree de cours\nPlan de paiement');
		}
		else {
			msg = 'Please choose a lesson duration and a pay plan';
			//msg += '(' + tpIDvalue + ' ' + payplanIDvalue + ')';
			alert(msg);
		}
		return;
	}

	f.lessons.value = $('startMonth').options[$('startMonth').selectedIndex].getAttribute('lessons');
	f.semesterID.value = $('startMonth').options[$('startMonth').selectedIndex].getAttribute('semester');
	f.submit();
	
}

function init() {
	showPrices($('startMonth'));
}

