jQuery(document).ready(function($) {
	var objCompany = jQuery('input[name="signupCompany"]');
	objCompany.val('KATIECO');
	
	// Setup the go-pro links
	$('a[rel="hd-video"]').colorbox({transition:"fade", iframe:true, innerWidth:425, innerHeight:344});
});

// Global variables
var m_nStudentCount = 1;
var m_nTableStudentRows = 1;
var m_nLessonCount = 1;
var m_bIsPackage = false;
var m_bUpdatingPage = false; // indicates if getLessonInfo is running, so it doesn't run multiple times
// Checks to make sure the elements on the form have entries
var a_RequiredFields = new Array('lessonMonth', 'lessonDay', 'lessonHour', 'lessonLocation', 'phone', 'email', 'emergencyContactName', 'emergencyContactPhone', 'billingFirstName', 'billingLastName', 'billingAddress1', 'billingCity', 'billingState', 'billingZipCode', 'billingCountry', 'ccNumber', 'ccCVV', 'ccMonth', 'ccYear');

// Adds another student signup input during signup process
function addStudent() {
    // Update the HTML of the object
    m_nStudentCount = m_nStudentCount + 1;
    m_nTableStudentRows = m_nStudentCount;

	// Make sure the objects exist
	var objTable = getElement('tblstudent');
	if(!objTable || !objTable.rows) { return; }
	
	// Get the last row
	var lastRow = objTable.rows.length - 1;
	// Insert rows (these must be done in reverse to get things to line up correctly)
	// Insert the row for the student Weight
	var rWeight = objTable.insertRow(lastRow);
	rWeight.id = 'studentRow' + m_nStudentCount + 'Weight';
	// Insert the row for the student Height
	var rHeight = objTable.insertRow(lastRow);
	rHeight.id = 'studentRow' + m_nStudentCount + 'Height';
	// Insert the row for the student Age
	var rAge = objTable.insertRow(lastRow); // leave the add studnet link as the last row
	rAge.id = 'studentRow' + m_nStudentCount + 'Age';
	// Insert the row for equipment sizing header information
	var rEquipHeader = objTable.insertRow(lastRow); // leave the add student link as the last row
	rEquipHeader.id = 'studentRow' + m_nStudentCount + 'equipHeader';	
	// Insert the row for student information
	var rStudentInfo = objTable.insertRow(lastRow); // leave the add student link as the last row
	rStudentInfo.id = 'studentRow' + m_nStudentCount;	
	
	// Insert the left cell for the student row
	var leftCell = rStudentInfo.insertCell(0);
	leftCell.className = 'alt';
	leftCell.innerHTML = '<strong id="studentCount" name="studentCount">Student Name</strong> <small>(<a href="#" onclick="if(confirm(&quot;Are you sure you want to remove this student?&quot;)) { removeStudent(' + m_nStudentCount + '); } return false;">remove</a>)</small>';
	// Insert the right cell for the student row
	var rightCell = rStudentInfo.insertCell(1);
	rightCell.className = 'alt';
	rightCell.innerHTML = '<input type="text" id="studentname[]" name="studentname[]" maxlength="200" />';
	
	// Insert the cell for the student equipment row
	leftCell = rEquipHeader.insertCell(0);
	leftCell.colSpan = 2;
	leftCell.innerHTML = '<strong name="studentCount2">Student Equipment Sizing:</strong>';
	
	// Insert the left cell for the age row
	leftCell = rAge.insertCell(0);
	leftCell.setAttribute("align", "right");
	leftCell.innerHTML = 'Student Age';
	// Insert the right cell for the age row
	rightCell = rAge.insertCell(1);
	rightCell.innerHTML = '&nbsp;<input type="text" id="studentage[]" name="studentage[]" maxlength="20" value="" class="medinfo" /> yrs';
	
	// Insert the left cell for the height row
	leftCell = rHeight.insertCell(0);
	leftCell.setAttribute("align", "right");
	leftCell.innerHTML = 'Student Height';
	// Insert the right cell for the height row
	rightCell = rHeight.insertCell(1);
	rightCell.innerHTML = '&nbsp;<input type="text" id="studentheight[]" name="studentheight[]" maxlength="20" value="" class="smallinfo" /> ft <input type="text" id="studentheight2[]" name="studentheight2[]" maxlength="20" value="" class="smallinfo" /> in';
	
	// Insert the left cell for the height row
	leftCell = rWeight.insertCell(0);
	leftCell.className = 'alt';
	leftCell.setAttribute("align", "right");
	leftCell.innerHTML = 'Student Weight';
	// Insert the right cell for the height row
	rightCell = rWeight.insertCell(1);
	rightCell.className = 'alt';
	rightCell.innerHTML = '&nbsp;<input type="text" id="studentweight[]" name="studentweight[]" maxlength="20" class="medinfo" /> lbs';
	
	// Update the pricing and description
	getLessonInfo();
}

function removeStudent(_studentIndex) //_studentNameID, _studentEquipHeaderID, _studentAgeID, _studentHeightID, _studentWeightID)
{
	// Parameters:
	//		_studentIndex = the index of the student to remove
	//		_studentNameID = the ID of the <tr> tag containing the student name information
	//		_studentEquipheaderID = the ID of the <tr> tag containing the student equip header
	//		_studentAgeID = the ID of the <tr> tag containing the student age
	//		_studentHeightID = the ID of the <tr> tag containing the student height
    //		_studentWeightID = the ID of the <tr> tag containing the student weight

    // Update the global variables
    m_nStudentCount = m_nStudentCount - 1;
    m_nTableStudentRows = m_nStudentCount;
	
	// Make sure the parameters are valid
	if(!_studentIndex) { return; }
	var nStudentIndex = parseInt(_studentIndex, 10);
	if(!nStudentIndex) { return; } // even if it's 0, we don't remove the initial student row
	
	// Get the objects
	var objTable = getElement('tblstudent');
	var objStudentName = getElement('studentRow' + nStudentIndex);
	var objStudentEquipHeader = getElement('studentRow' + nStudentIndex + 'equipHeader');
	var objStudentAge = getElement('studentRow' + nStudentIndex + 'Age');
	var objStudentHeight = getElement('studentRow' + nStudentIndex + 'Height');
	var objStudentWeight = getElement('studentRow' + nStudentIndex + 'Weight');
	
	if(!objTable || !objTable.rows || !objStudentName || !objStudentEquipHeader || !objStudentAge || !objStudentHeight || !objStudentWeight || !objStudentName.rowIndex || !objStudentEquipHeader.rowIndex || !objStudentAge.rowIndex || !objStudentHeight.rowIndex || !objStudentWeight.rowIndex) { return; }
	
	// Remove the rows
	if(objTable.rows.length > objStudentName.rowIndex) { objTable.deleteRow(objStudentName.rowIndex); }
	if(objTable.rows.length > objStudentEquipHeader.rowIndex) {  objTable.deleteRow(objStudentEquipHeader.rowIndex); }
	if(objTable.rows.length > objStudentAge.rowIndex) { objTable.deleteRow(objStudentAge.rowIndex); }
	if(objTable.rows.length > objStudentHeight.rowIndex) { objTable.deleteRow(objStudentHeight.rowIndex); }
	if(objTable.rows.length > objStudentWeight.rowIndex) { objTable.deleteRow(objStudentWeight.rowIndex); }
	
	// Update the pricing and description
	getLessonInfo();
}

function lessonTypeChanged(objDropdown)
{
	// Make sure we have an object
	if(!objDropdown) { return; }
	
	// Reset member variables
	m_nStudentCount = 1;
	m_nLessonCount = 1;
	m_bIsPackage = false;
	
	// Update the member variables
	switch(objDropdown.selectedIndex)
	{
		case 1:
		case 2:
		case 3:
			m_nLessonCount = 3 * objDropdown.selectedIndex;
			m_bIsPackage = true;
			break;
		case 4:
		case 5:
		case 6:
		case 7:
		case 8:
			m_nStudentCount = objDropdown.selectedIndex - 2;
			break;
		default:
			break;
	}
	
	// Update the page
	getLessonInfo();
}

// Updates the pricing and description on the page
function getLessonInfo()
{
	// Make sure this isn't already running
	if(m_bUpdatingPage) { return; }
	
	// Update the member variable
	m_bUpdatingPage = true;
	
	// Create the price and description arrays
	// Indices 0-3 = prices per lesson for packages
	// Indices 4-8 = prices per person for group
	//						  0    1   2   3   4   5    6   7   8
	var a_Prices = new Array(109, 99, 95, 90, 99, 89, 79, 69, 65);
	var a_Descriptions = new Array("A two hour private lesson that includes personal attention and all surf equipment--including a wet suit and surf board.", "Three two-hour private lessons. In addition to the basics, you'll learn proper paddling technique, surfboard form, safety, and save on the regular surf lesson price.", "Six two-hour private lessons. In addition to the basics, you'll learn how to turn, how to judge the power of a wave, wave timing, and save on the regular surf lesson price.", "Nine two-hour private lessons. In addition to the basics, you'll learn advanced wave catching, advanced paddling techniques, how to scout for surf locations and surfing trips, and save on the regular lesson price. If you're new to surfing and want to become a full fledged surfer, this is the package for you!", "A shared two-hour private group lesson for two people that includes all equipment--including a wet suits and surf boards. You'll both receive personal attention and save on the regular surf lesson price.", "A shared two-hour private group lesson for three people that includes all equipment--including wet suits and surf boards. You'll all receive personal attention and save on the regular surf lesson price.", "A shared two-hour private group lesson for four people that includes all equipment--including wet suits and surf boards. You'll all receive personal attention and save on the regular surf lesson price.", "A shared two-hour private group lesson for five people that includes all equipment--including wet suits and surf boards. You'll all receive personal attention and save on the regular surf lesson price.", "A shared two-hour private group lesson for six or more people that includes all equipment--including wet suits and surf boards. You'll all receive personal attention and save on the regular surf lesson price.");
	var dPhotographyPrice = 59, dPhotographyAdded = 0;
	
	// Get the array index based on the number of students
	var nIndex = 0;
	var nPriceCount = 1;
	
	if(m_bIsPackage)
	{
		nPriceCount = m_nLessonCount;
		switch(m_nLessonCount)
		{
			case 3:
			case 6:
			case 9:
				nIndex = m_nLessonCount / 3;
				break;
			default: // this will also handle case 3
				nIndex = 1;
				nPriceCount = m_nLessonCount = 3;
				break;
		}
	}
	else
	{
		nPriceCount = m_nStudentCount;
		switch(m_nStudentCount)
		{
			case 1:
				nIndex = 0;
				break;
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
				dPhotographyPrice = dPhotographyPrice + ((m_nStudentCount - 1) * 10);
				nIndex = m_nStudentCount + 2;
				break;
			default:
				dPhotographyPrice = dPhotographyPrice + ((m_nStudentCount - 1) * 10);
				nIndex = 8;
				break;
		}
	}
	
	// Update the page
	var objLessonType = getElement('ddlLessonType');
	var objPrice = getElement('lblLessonPrice');
	var objPrice2 = getElement('lblSignupTotal');
	var objPrice3 = getElement('lblSignupTotal_Bottom');
	var objDescription = getElement('lblDescription');
	var objPhotographyPrice = getElement('lblPhotoPrice');
	var objPhotography = getElement('chkPhotography');
	if(objPhotography && objPhotography.checked) { dPhotographyAdded = dPhotographyPrice; }
	objPhotographyPrice.innerHTML = '$' + dPhotographyPrice;
	if(m_nStudentCount > 1) { objPhotographyPrice.innerHTML = objPhotographyPrice.innerHTML + " <small style='font-size:.7em;'>(only $59 plus $10 per additional person)</small> "; }

	if (objPrice && objPrice2) {
		if(nIndex != 8)
		{
			objPrice.innerHTML = objPrice2.innerHTML = objPrice3.innerHTML = '$' + ((a_Prices[nIndex] * nPriceCount) + dPhotographyAdded);
			if (nPriceCount > 1) {
				var szFooter = "person";
				if (m_bIsPackage) { szFooter = "lesson"; }
				objPrice.innerHTML = objPrice2.innerHTML = objPrice.innerHTML + ' <small class="specialNote">(only $' + a_Prices[nIndex] + ' per ' + szFooter + ')</small>';
			}
		} else {
			objPrice.innerHTML = objPrice2.innerHTML = '<small class="specialNote">Only $' + a_Prices[nIndex] + ' per person</small>';
		}
	}
	if (objDescription) {
	    objDescription.innerHTML = a_Descriptions[nIndex];
	}
	if (objLessonType) {
	    objLessonType.selectedIndex = nIndex;
	}
	
	// Make sure there are enough student name inputs for the number of students
	var nStudentCount = m_nStudentCount;
	m_nStudentCount = m_nTableStudentRows;
	while(m_nStudentCount != nStudentCount)
	{
		if(m_nStudentCount > nStudentCount)
		{
			removeStudent(m_nStudentCount - 1);
		}
		else
		{
			addStudent();
		}
	}
	
	// Update the student information
	var a_StudentCounts = document.getElementsByName("studentCount");
	if(a_StudentCounts && a_StudentCounts.length > 0)
	{
		var nLoopCount = a_StudentCounts.length;
		for(var i = 0; i < nLoopCount; i++)
		{
			var szSuffix = "th";
			var nStudentNumber = i + 2;
			
			if(nStudentNumber == 21 || nStudentNumber == 31)
			{
				szSuffix = "st";
			} else if(nStudentNumber == 2 || nStudentNumber == 22 || nStudentNumber == 32)
			{
				szSuffix = "nd";
			}
			else if(nStudentNumber == 3 || nStudentNumber == 23 || nStudentNumber == 33)
			{
				szSuffix = "rd";
			}
			
			// Update the tagget
			a_StudentCounts[i].innerHTML = nStudentNumber + szSuffix + " Student Name:";
		}
	}
	a_StudentCounts = document.getElementsByName("studentCount2");
	if(a_StudentCounts && a_StudentCounts.length > 0)
	{
		var nLoopCount = a_StudentCounts.length;
		for(var i = 0; i < nLoopCount; i++)
		{
			var szSuffix = "th";
			var nStudentNumber = i + 2;
			
			if(nStudentNumber == 21 || nStudentNumber == 31)
			{
				szSuffix = "st";
			} else if(nStudentNumber == 2 || nStudentNumber == 22 || nStudentNumber == 32)
			{
				szSuffix = "nd";
			}
			else if(nStudentNumber == 3 || nStudentNumber == 23 || nStudentNumber == 33)
			{
				szSuffix = "rd";
			}
			
			// Update the tag
			a_StudentCounts[i].innerHTML = nStudentNumber + szSuffix + " Student Equipment Sizing:";
		}
	}
	
	// Update the member variable
	m_bUpdatingPage = false;
}

// Processes the signup form and saves the result to a cookie
function processForm()
{
	if(!checkForm())
	{
		return;
	}
	
	// Submit the form
	var objForm = getElement('frmMain');
	if(objForm) { objForm.submit(); }
}

// Checks a form to see if it is populated
function checkForm()
{
	// Return variable
	var bReturn = true;
	
	// Validate in reverse order so the focus is set
	a_RequiredFields.reverse();
	
	// Validate each element
	var arLen = a_RequiredFields.length;
	for(var i = 0; i < arLen; i++)
	{
		if(!checkElement(a_RequiredFields[i], true))
		{
			bReturn = false;
		}
	}
	
	// Return the result
	return bReturn;
}

// Checks an element to see if it is populated
function checkElement(elementID, setFocus)
{
	// Parameters:
	//		elementID = the ID of the <input> to validate
	//		setFocus = indicates if we should set the focus on the element as well
	
	// Make sure the parameter is valid
	if(!elementID) { return; }
	var objElement = getElement(elementID);
	var objValidator = getElement(elementID + '-rfv');
	if(!objElement || !objValidator) { return true; }
	
	// Check for input
	if((objElement.options && objElement.selectedIndex == 0) || (!objElement.value && !objElement.innerHTML))
	{
		// This isn't valid
		objValidator.className = '';
		// Set focus
		if(setFocus) { objElement.focus(); }
		return false;
	}
	
	// This is valid
	objValidator.className = 'invis';
	return true;
}
