$(document).ready(function(){$(".subj-group p a").click(function(){var div=$(this).parent().siblings();if($(this).hasClass("expand")){$(this).attr("class","collapse");$(this).siblings().fadeIn();div.slideDown();}else if($(this).hasClass("collapse")){$(this).attr("class","expand");$(this).siblings().fadeOut();div.slideUp();}});$(".subj-group > p > input").click(function(){var group=$(this).attr("id").substr(-1);var checkboxes=$("#subjects"+group+" div input");if($(this).attr("checked")){checkboxes.attr("checked","checked");}else{checkboxes.removeAttr("checked");}});enableOthers(0);if($('[name="subject"] option:selected').val()=='Other (please specify below)'){$('[name="other-subj"]').show();}
if($('[name="study-uni"] option:selected').val()=='Other (please specify below)'){$('[name="other-uni"]').show();}});function getClassification(level,id){var levelVal=$(level).val();var classificationRow=$("#study"+id+" .classification");classificationRow.html('<label>Classification</label><select name="classification" disabled="disabled"><option value="">- Please Select -</option></select>');if(levelVal!=""){$.get("classification.php",{level:levelVal},function(data){classificationRow.html("<label>Classification</label>"+data);});}}
function save(id){var entry=("#study"+id);function getVal(name){return $(entry+' [name="'+name+'"]').val();}
var level=getVal("study-level");var subject=getVal("subject");var specifiedSubject=(subject=="Other (please specify below)");var otherSubj=getVal("other-subj");var classification=getVal("classification");var fromDate=getVal("from-date");var toDate=getVal("to-date");var uni=getVal("study-uni");var specifiedUni=(uni=="Other (please specify below)");var otherUni=getVal("other-uni");if(level==""||subject==""){$("#studied .err-msg").html("Please enter at least a level and subject.").show();}else if(specifiedSubject&&otherSubj==""){$("#studied .err-msg").html("Please specify a subject in the field provided.").show();}else if(specifiedUni&&otherUni==""){$("#studied .err-msg").html("Please specify a university in the field provided.").show();}else{subject=(specifiedSubject?otherSubj:subject);uni=(specifiedUni?otherUni:uni);addEntry(id,level,subject,classification,fromDate,toDate,uni);cacheAdd(level,subject,classification,fromDate,toDate,uni);enableAddAnother(id);$(entry).remove();$("#studied .err-msg").hide();}}
function cacheAdd(level,subject,classification,fromDate,toDate,uni){var cache=$('input[name="study-cache"]');var newVal=cache.val();if(newVal!="")newVal+=cacheDelimiter;newVal+=(level+fieldDelimiter+subject+fieldDelimiter+classification+fieldDelimiter+fromDate+fieldDelimiter+toDate+fieldDelimiter+uni);cache.val(newVal);}
function addEntry(id,level,subject,classification,fromDate,toDate,uni){if(classification=="")classification="&nbsp;";if(toDate=="")toDate="&nbsp;";if(fromDate==""){fromDate="&nbsp;";}else{toDate=(" - "+toDate);}
if(uni=="")uni="&nbsp;";var date=fromDate+toDate;var row='<tr id="entry'+id+'"><td width="130">'+level+'</td><td width="220">'+subject+'</td><td width="40" align="center">'+classification+'</td><td>'+date+'</td><td>'+uni+'</td>';row+='<td width="18"><a class="remove-entry" href="javascript:void(0)"><img src="'+removeImgSrc+'" alt="Remove" title="Remove" /></a></td></tr>';$("#entries").append(row).show();renumberEntries();}
function removeEntry(id){$("#entry"+id).remove();if($("#entries tr").length==0){$("#entries").hide();$("#studied .err-msg").after(getEntryForm(id,false));enableOthers(id);disableAddAnother();}
var cachedEntries=$('[name="study-cache"]').val();cachedEntries=cachedEntries.split(cacheDelimiter);cachedEntries.splice(id,1);cachedEntries=cachedEntries.join(cacheDelimiter);$('[name="study-cache"]').val(cachedEntries);renumberEntries();}
function renumberEntries(){var n=0;$("#entries tr").each(function(){$(this).attr("id","entry"+n);$(this).find(".remove-entry").removeAttr("onclick").click(function(){var id=$(this).parent().parent().attr("id").substr(5);removeEntry(id);});n++;});}
function addAnother(id){id++;$("#studied .err-msg").after(getEntryForm(id,true));enableOthers(id);disableAddAnother();}
function getEntryForm(id,heading){var form='<table id="study'+id+'" class="entry"';if(heading)form+=' style="margin-bottom:40px;"';form+='><tr>';if(heading)form+='<td colspan="3"><h5>New Entry</h5></td></tr><tr>';form+='<td><label>Level</label><select name="study-level" onchange="getClassification(this, '+id+')">'+dropDownOptions(levels,"- Please Select -")+'</select></td>';form+='<td class="classification"><label>Classification</label><select name="classification" disabled="disabled"><option value="">- Please select -</option></select></td>';form+='<td><label>University</label><select name="study-uni">'+dropDownOptions(universities,"- Please Select -")+'</select></td></tr>';form+='<tr><td><label>Subject</label><select name="subject">'+dropDownOptions(subjects,"- Please Select -")+'</select></td>';form+='<td class="dates"><label>Dates</label><select name="from-date">'+dropDownOptions(dates,"----")+'</select> to <select name="to-date">'+dropDownOptions(dates,"----")+'</select></td>';form+='<td><input type="text" name="other-uni" value="" maxlength="60" style="display:none;" /></td></tr>';form+='<tr><td><input type="text" name="other-subj" value="" maxlength="60" style="display:none;" /></td><td colspan="2"><input type="button" onclick="save('+id+')" value="Save" /></td></tr>';form+='</table>';return form;}
function enableOthers(id){$('#study'+id+' [name="subject"]').change(function(){if($(this).find("option:selected").val()=="Other (please specify below)"){$('#study'+id+' [name="other-subj"]').show();}else{$('#study'+id+' [name="other-subj"]').hide();}});$('#study'+id+' [name="study-uni"]').change(function(){if($(this).find("option:selected").val()=="Other (please specify below)"){$('#study'+id+' [name="other-uni"]').show();}else{$('#study'+id+' [name="other-uni"]').hide();}});}
function enableAddAnother(id){$("#studied h4 span").replaceWith('<a href="javascript:void(0)" onclick="addAnother('+id+')" class="expand">Add Another</a>');}
function disableAddAnother(){$("#studied h4 a").replaceWith('<span class="expand">Add Another</span>');}
function disable(input){input.attr("disabled","disabled");}
function enable(input){input.removeAttr("disabled");}
function dropDownOptions(optionsArray,blankText){var html='<option value="">'+blankText+'</option>';for(var i=0;i<optionsArray.length;i++){var val=optionsArray[i];html+='<option value="'+val+'">'+val+'</option>';}
return html;}