(function(window){if(!ts.referjobpost)
ts.referjobpost={};ts.referjobpost={emailToTemplate:null,searchId:0,init:function(){var url='/CareerServices/ManageJobPost/EmailToTemplate';ts.ajax.send({url:url,successHandler:function(results){ts.referjobpost.emailToTemplate=results;},errorHandler:function(err){ts.msg.sendError('An error occurred while loading the EmailToTemplate.');}});this.registerEvents();this.validate();$('#EmailBody').cleditor({width:"100%",height:"100%"})[0];},validate:function(){$.validator.addMethod("recipientsRequired",function(val,e){var ret=$('.recipientTile').length>0;return ret;});$.validator.addMethod("htmlRequired",function(val,e){var value=$('#EmailBody').val();var ret=!(value===''||value==='
'||value==='
');return ret;});$('#jobpostrefer').validate({rules:{EmailSubject:{required:true,maxlength:255},hfRecipients:{recipientsRequired:true},hfBody:{htmlRequired:true}},messages:{EmailSubject:{required:'Email Subject is required.',maxlength:"Email Subject can not be more than 255 characters long."},hfRecipients:{recipientsRequired:'Please select recipients.'},hfBody:{htmlRequired:'Email Body is required.'}},onkeyup:false,onblur:true});$('#jobpostrefer').validate();},registerEvents:function(){$('#searchForm').submit(function(e){return false;});$("#removeRecipientCommand").live("click",function(e){ts.referjobpost.removeRecipient(e);return false;});},search:function(e,cb){var val=$(this).val();if(val!=$.data(this,"lastvalue")){if(val.length>1){ts.referjobpost.searchStudents(e,cb);}
else
$('#searchResult').html('');}
$.data(this,"lastvalue",val);},searchStudents:function(e,cb){e.stopImmediatePropagation();if(ts.referjobpost.searchId>100)
ts.referjobpost.searchId=0;ts.referjobpost.searchId+=1;var thisSearchId=ts.referjobpost.searchId;var data=$('#searchForm').serialize();var url='/CareerServices/ManageJobPost/StudentsReferSearch';ts.referjobpost.newsearch=false;ts.ajax.send({url:url,data:data,type:'POST',successHandler:function(r){if(thisSearchId==ts.referjobpost.searchId)
ts.utility.html('#searchResult',r);},errorHandler:function(r){ts.msg.sendError('An error occurred while searching students.');}});},addRecipient:function(e){e.stopImmediatePropagation();var id=$(this).attr('personid');var name=$(this).attr('personname');var emailaddress=$(this).attr('personemail');if($('#recipient'+id).length>0){ts.msg.sendError('Selected student is already in the recipients list.');}
else{var content=ts.referjobpost.emailToTemplate;content=content.replace(/{value}/g,name).replace(/{recipientid}/g,id).replace(/{emailaddress}/g,emailaddress).replace(/{recipienttype}/g,'Student');$('#toBox').append(content);$('#jobpostrefer').valid();}},removeRecipient:function(e){e.stopImmediatePropagation();var id=e.target.attributes['recipientid'].value;$('#recipient'+id).remove();$('#jobpostrefer').valid();},reorderRecipients:function(){$('.recipientTile').each(function(idx){$(this).find('.recipientId').attr('id','Recipients['+idx+'].RecipientId');$(this).find('.recipientId').attr('name','Recipients['+idx+'].RecipientId');$(this).find('.emailAddress').attr('id','Recipients['+idx+'].EmailAddress');$(this).find('.emailAddress').attr('name','Recipients['+idx+'].EmailAddress');});},save:function(){if($('#jobpostrefer').valid()){ts.referjobpost.reorderRecipients();ts.ajax.send({url:'/CareerServices/ManageJobPost/Refer',data:$('#jobpostrefer').serialize(),type:'POST',successHandler:function(result){if(result.State==0){ts.ajax.handleResult(result);window.location="/CareerServices/ManageJobPost/Search";}
else{ts.msg.sendError(result.Messages[0].Message);}},errorHandler:function(result){ts.msg.sendError('An error occurred while refering the job post.');ts.msg.sendMsg('sysWarning','unknown error ts.referjobpost.save ajax: '+result.responseText);}});};}};$(document).ready(function(){ts.referjobpost.init();});})(window);