//-------------------------------------------------- // General Notes //-------------------------------------------------- var classNum = 0; var sectionsOfClass = new Array(); //-------------------------------------------------- // Validation Functions //-------------------------------------------------- //-------------------------------------------------- // Default Error Message //-------------------------------------------------- /* on IE, jQuery.validator doesn't exist */ if (jQuery.validator) { jQuery.each(jQuery.validator.messages, function(i) { jQuery.validator.messages[i] = "

Please fill the field

"; }); //-------------------------------------------------- // Time Selection Validation //-------------------------------------------------- jQuery.validator.addMethod( "selectNone", function(value, element) { if (element.value == "none") { return false; } else return true; }, "

Please select a time

" ); //-------------------------------------------------- // Days of Week validation //-------------------------------------------------- jQuery.validator.addMethod( "daysRequired", function(value, element) { var checkedCount = 0; jQuery(element).parent().parent().children().children('.daysRequired:checked').each( function() { checkedCount++; }); if (checkedCount == 0) { return false; } else return true; }, "

Select a day!

" ); //-------------------------------------------------- // Add validation rules //-------------------------------------------------- jQuery.validator.addClassRules("selectRequired", { selectNone: true }); jQuery.validator.addClassRules("daysRequired", { daysRequired: true }); } //-------------------------------------------------- // General Input Functions //-------------------------------------------------- /** * \brief * Returns the common inputs for each new section. */ function genSectionHtml(cnum) { genSectionHtml_n(cnum, '', '', '', '', '', '', ''); } /* @TODO: This should select & set items based on args, if the args != '' */ function genSectionHtml_n(cnum, name, synonym, stime, etime, days, prof, location) { var snum = sectionsOfClass[cnum]; var result = ''; result = result + ''; result = result + ''; result = result + '\ \ \ \ \ \ '; result = result + '
' + ''+ ''; return result; } /** * Outputs an \n"; } /** * \brief * Add a section to a class. */ function add_section_n(cnum, name, synonym, stime, etime, days, prof, location) { jQuery('.pclass'+cnum).after(genSectionHtml_n(cnum, name, synonym, stime, etime, days, prof, location)); sectionsOfClass[cnum] ++; } function add_section(cnum) { return add_section_n(cnum, '', '', '', '', {'m':false, 't':false, 'w':false, 'h':false, 'f':false}, '', ''); } /** * Add a list of sections gotten via an AJAX call. */ function add_sections(cnum, data) { var i; if (!data.sections) return; for (i = data.sections.length - 1; i >= 0; i --) { section = data.sections[i]; add_section_n(cnum, section.section, section.synonym, section.time_start, section.time_end, section.days, section.prof, section.location); } } //-------------------------------------------------- // Adds a new class to the input. //-------------------------------------------------- function add_class_n(name) { sectionsOfClass[classNum] = 0; // Initialize at 0 jQuery('#jsrows').append('
'); if (jQuery('.className' + classNum).autocomplete) jQuery('.className' + classNum).autocomplete({ source: "auto.php" }); jQuery('.className' + classNum).bind('autocompleteselect', {'class_num': classNum}, function(event, ui) { if (ui.item && ui.item.value.indexOf('-')) { jQuery.ajax( { url: 'auto.php', data: {'getsections': 1, 'term': ui.item.value}, context: {'class_num': event.data.class_num}, success: function(data, textStatus, reqobj) { add_sections(this.class_num, data); } } ); } }); classNum++; return (classNum - 1); }; function add_class() { return add_class_n(''); } /** * \brief * Render a slate_permutate-encoded time-of-day. * * \param time_str * A four-character representation of a time of day based on a * time's 24-hour representation. * \return * A string representing the specified time. */ function prettyTime(time_str) { var i_hour; var hour_str; var m; i_hour = time_str.substr(0, 2) * 1; if (i_hour <= 12) { m = 'a'; } else { m = 'p'; i_hour -= 12; } hour_str = new String(i_hour); /* uncomment to have 08:01 instead of 8:01 */ /* while (hour_str.length < 2) hour_str = '0' + hour_str; */ return hour_str + ':' + time_str.substr(2) + ' ' + m + 'm'; } //-------------------------------------------------- // Items bound to pageload/events //-------------------------------------------------- jQuery(document).ready(function() { //-------------------------------------------------- // Validates the form (pre-submission check) //-------------------------------------------------- /* don't call if IE doesn't think the function exists */ if (jQuery('#scheduleForm').validate) jQuery('#scheduleForm').validate({ debug: false, }); //-------------------------------------------------- // Bind the class-adding method //-------------------------------------------------- jQuery('#addclass').click(function() { add_class(); }); //-------------------------------------------------- // Deletes the selected class from input //-------------------------------------------------- jQuery('.deleteClass').live('click', function() { if(confirm('Delete class and all sections of this class?')) { jQuery('.class'+ jQuery(this).parent().parent().attr("title")).remove(); } }); //-------------------------------------------------- // Deletes the selected section from the input //-------------------------------------------------- jQuery('.deleteSection').live('click', function() { sectionsOfClass[jQuery(this).parent().parent().attr("title")]--; // Decreases the number of classes jQuery(this).parent().parent().remove(); }); //-------------------------------------------------- // Bind the section-adding method //-------------------------------------------------- jQuery('.addSection').live('click', function() { add_section(jQuery(this).parent().parent().attr("title"), sectionsOfClass[jQuery(this).parent().parent().attr("title")]); }); //-------------------------------------------------- // Default text //-------------------------------------------------- jQuery(".defText").focus(function(srcc) { if (jQuery(this).val() == jQuery(this)[0].title) { jQuery(this).removeClass("defaultTextActive"); jQuery(this).val(""); } }); jQuery(".defText").blur(function() { if (jQuery(this).val() == "") { jQuery(this).addClass("defaultTextActive"); jQuery(this).val($(this)[0].title); } }); jQuery(".defText").blur(); //-------------------------------------------------- // Show/Hide advanced items //-------------------------------------------------- jQuery('.advanced').hide(); jQuery('#showadvanced').click( function() { jQuery('#showadvanced').hide(); jQuery('.advanced').slideToggle(); }); });