//if you pick a state
 function resetZipAndOnlineOnlyAndSubmit(formToSubmit) {
	 formToSubmit.online.checked = false;
	 formToSubmit.zip.value = '';
	 formToSubmit.radius.value = '';
	 //document.topSearchForm.radius.selectedIndex = 0;

	 formToSubmit.submit();
 }

function clearbottomlevelandSubmit(formToSubmit, level, dropDownPrefix) {
	var total_level;
	var drop_down_option;
	for (total_level = level + 1; total_level <= 3; total_level = total_level + 1) {
		drop_down_option = document.getElementById(dropDownPrefix + "C" + total_level);

		if (drop_down_option != null && typeof(drop_down_option) != "undefined") {
			drop_down_option.selectedIndex = 0;
			drop_down_option.options[0].value = 0;
		}
	}
	formToSubmit.submit();
}

//if you click online only checkbox
function resetStateAndZipAndSubmit(formToSubmit) {
	formToSubmit.state.value = 'ALL';
	formToSubmit.state.selectedIndex = 0;
	formToSubmit.zip.value = '';
	formToSubmit.radius.value = '';
	//document.topSearchForm.radius.selectedIndex = 0;

	formToSubmit.submit();
}

//if you enter a zip and click submit
function prepareForSubmit(formToSubmit, submitButton) {
	if (formToSubmit.zip.value != '' && formToSubmit.zip.value != 'zip') {
		formToSubmit.state.value = 'ALL';
		formToSubmit.state.selectedIndex = 0;
		formToSubmit.online.checked = false;
	}

	submitButton.disabled = true;
}

function fieldOnFocus(formField, formDefault) {
	if (formField.value == formDefault) {
		formField.value = '';
	}
	return false;
}

function fieldOnBlur(formField, formDefault) {
	if (formField.value == '') {
		formField.value = formDefault;
	}
	return false;
}

function disableButtonAndSubmit(formToSubmit, submitButton) {
	submitButton.disabled = true;
	formToSubmit.submit();
}

function enableButtons() {
	enableButton(document.getElementById("sfSearchButton"));
	enableButton(document.getElementById("locSearchButton"));
	enableButton(document.getElementById("submitText"));
}

function enableButton(button) {
	if (button) {
		button.disabled = false;
	}
}

window.onfocus = enableButtons;

// set up jquery stuff
function setUpStateLocationSlider() {
	$("span.stateLocationSlider").click(function() {
		var locations = $(this).parent().children(".stateLocations");

		// seems backwards but remember, the toggle hasn't happened yet.  If you put this after the toggle, you'll need
		// to wait for it to finish
		if (locations.css("display") == "none") {
			$(this).html("Hide All");
		}
		else {
			$(this).html("Show All");
		}

		locations.slideToggle("fast");
	}) ;
}

function setUpCampusLocationSlider() {
	$("span.campusLocationSlider").click(function() {
		var locations = $(this).parent().parent().find(".campusLocations");

		if (locations.css("display") == "none") {
			$(this).html("Hide All");
		}
		else {
			$(this).html("Show All");
		}

		locations.slideToggle("fast");
	});
}

function setUpSearchResultsSlider() {
	$("a.searchSlider").click(function() {
		var allLocations = $(this).parent().children(".searchResultCampuses");
		allLocations.slideToggle("slow");
	});
}

function setUpDirectoryTableSlider() {
	$("a.directorySlider").click(function() {
		var allLocations = $(this).parent().children(".directorySliderTable");
		allLocations.slideToggle("slow");
	});
}

function setUpSchoolNameSlider() {
	$("h2.schoolNameLink").click(function() {
		var locations = $(this).parent().children(".upperLocations");
		locations.slideToggle("fast");
	});
}

function setUpSchoolImgSlider() {
	$("h2.schoolImgLink").click(function() {
		var locations = $(this).parent().parent().children(".upperLocations");
		locations.slideToggle("fast");
	});
}

function newForm(searchString, fullString) {
	var ndx = fullString.indexOf(searchString);

	if (ndx >= 0) {
		window.CT_C_OrderTotal = fullString.substring(ndx + searchString.length, fullString.indexOf(" ", ndx));
		//alert(window.CT_C_OrderTotal);
	}
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$(document).ready(function() {
	setUpStateLocationSlider();
	setUpCampusLocationSlider();
	setUpSearchResultsSlider();
	setUpDirectoryTableSlider();
	setUpSchoolNameSlider();
	setUpSchoolImgSlider();
	$('input#textSearch').hint();
	$('input#tSHome').hint();
});

// for the forms
$(document).ready(function() {
	// preload the spinner
	var spin = document.createElement('img');
	spin.src = "/images/spinner.gif";
	
	//$('.schoolFormImage').click(function() {
	$('#leadForm .submitButton input.submitLast').click(function() {
		var schoolName = $("#leadForm").attr("title");		
		$.blockUI({
			message: '<div class="blockUI"><h3><img src="/images/spinner.gif"/>Processing...</h3><p>Your request is being submitted to ' + schoolName + '.</p></div>',
			timeout: 32000
		});
	});

	if ($.browser.msie) {
		$("#leadForm select")
		 .mousedown(function() {
			if ($(this).css("width") != "auto") {
				$(this)
				 .data("origWidth", $(this).css("width"))
				 .css("width", "auto");
			}
		}).change(function() {
			$(this).css("width", $(this).data("origWidth"));
		});
	}
});