// JavaScript Document
/*
Create our object to hold all functions and vars
---------------------------------------------------------*/
var objSite = {};
/*
---------------------------------------------------------*/
objSite.site_url = "http://www.booktime.org.uk/";
objSite.reloadPage = function( data/*string*/)
{
window.location.reload();
}
/*--------------------------------------------------------
@description update the login data once a login has been submitted
---------------------------------------------------------*/
objSite.updateLogin = function( data/*string*/)
{
var data=data;
var isloggedIn ="false";
if(data=="" ||data==null){
isLoggedIn ="true";
}
//----------------------------------------------
var holder = document.getElementById("login");
var all_items = document.getElementById("loginError");
holder.removeChild(all_items);
//----------------------------------------------
//----------------------------------------------
var new_all_items = document.createElement("div");
new_all_items.setAttribute("id", "loginError");
new_all_items.innerHTML = data;
holder.appendChild(new_all_items);
if(isLoggedIn=="true"){
objSite.reloadPage(data);
}
}
/*--------------------------------------------------------
@description update the current div once and ajax response has been recieved
---------------------------------------------------------*/
objSite.updateAssignment = function( data/*string*/)
{
//----------------------------------------------
var holder = document.getElementById(objSite.holderDiv);
var all_items= document.getElementById(objSite.div);
holder.removeChild(all_items);
//----------------------------------------------
//----------------------------------------------
var new_all_items = document.createElement("div");
new_all_items.setAttribute("id", objSite.div);
new_all_items.innerHTML = data;
holder.appendChild(new_all_items);
setTimeout(function() {$('#success').fadeOut('slow')}, 2000);
}
/*--------------------------------------------------------
@description append to the current chat div once and ajax response has been recieved
---------------------------------------------------------*/
objSite.appendChat = function( data/*string*/)
{
if( data == 'ending chat' )
location.reload(true);
//check for any alerts
if( data.search(/warn::/)==0 ){
//show the warning and carry on
objSite.alertUser( "The moderator of this site has felt the need to give you a warning for your behaviour on this chat. Please be careful!" );
}
else if( data.search(/ban::/)==0 ){
var failText = "The moderator of this chat has banned you due to your innappropriate use of this chat. To reactivate your account please contact admin@booktrust.org.uk";
objSite.alertUser( failText, true );
}
else if( data.search(/commentDelete::/)==0 ){
new_data = data.replace("commentDelete::","");
$("#chatTranscript").empty();
$("#chatTranscript").append(new_data);
objSite.resetChatDelete();
}
else if( data!='fail'){
//append a new comment
$("#chatTranscript").append(data);
objSite.resetChatDelete();
}
var baseURL = objSite.site_url;
window.setTimeout(
function() {
var newURL = baseURL + "getComments";
$.ajax({
type: "POST",
url: newURL,
data: 'itemId=' + objSite.itemId + '&typeId=' + objSite.typeId,
success: objSite.appendChat
});
},1000);
}
objSite.resetChatDelete = function(){
//Needed to avoid adding multiple js calls onto an item
$('.modDisplay').unbind();
$('.modDisplay').hover(function(){
$(this).css('background-color','#f2f2f2');
objSite.deleteTool( $(this).attr('id'), 'Are you sure you want to delete this item?',objSite.itemId);
}, function(){
$("#delete").remove();
$("#tooltip").remove();
$(this).css('background-color','#ffffff');
});
}
objSite.alertUser = function( text, ban ){
var alertBoxHtml = '
Warning
';
alertBoxHtml += '
';
alertBoxHtml += text;
alertBoxHtml += '
';
$('#maincontainer').append(alertBoxHtml);
if (window.innerWidth || window.innerHeight)
var width = window.innerWidth;
else if (document.body.clientWidth || document.body.clientHeight)
var width = document.body.clientWidth;
var window_width = $('#userAlert').width();
var pos_left = (width/2)-(window_width/2);
if( typeof( window.pageYOffset ) == 'number' ) {
var pos_top = window.pageYOffset + 200;
}else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ){
var pos_top = document.documentElement.scrollTop + 150;
}
$('#userAlert').css({'left': pos_left,'top':pos_top});
if( !ban ){
window.setTimeout(
function() {
$("#userAlert").animate({
opacity: 0
}, 800, function(){ $("#userAlert").remove() } );
},5000);
}
else{
window.setTimeout(
function() {
$("#userAlert").animate({
opacity: 0
}, 800, function(){ objSite.logout(); } );
},5000);
}
}
/*--------------------------------------------------------
@description append to the current chat div once and ajax response has been recieved
---------------------------------------------------------*/
objSite.updateUsers = function( data/*string*/)
{
var all_users= document.getElementById('all_users');
if( all_users.innerHTML != data){
all_users.innerHTML = data;
$('.active_user').click(function(e){
var username = $(this).attr('id');
var username = username.replace('userid_','');
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
objSite.modTool( x, y, username );
});
}
var baseURL = objSite.site_url;
window.setTimeout(
function() {
var newURL = baseURL + "getUsers";
$.ajax({
type: "POST",
url: newURL,
data: 'itemId=' + objSite.itemId,
success: objSite.updateUsers
});
},4000);
}
/*--------------------------------------------------------
@description submit a comment for an item
@param textboxId id of text box containing comment data
@param itemId id of item for comment
@param typeId id of type
@param div div to replace
@param live whether the comment wil go live
@param userId id of user submitting comment
---------------------------------------------------------*/
objSite.submitComment=function(textboxId,itemId,typeId,type,div,live,userId){
var textbox=document.getElementById(textboxId);
var comment=textbox.value;
// ---------------------------------------------------------
var baseURL = objSite.site_url;
var newURL = baseURL + "addComment";
objSite.div=div;
objSite.holderDiv="commentsHolder";
chat=false;
$.ajax({
type: "POST",
url: newURL,
data: 'itemId=' + itemId + '&typeId=' + typeId+'&comment='+comment+'&live='+live+'&type='+type+'&userId='+userId,
success: objSite.updateAssignment
});
}
/*--------------------------------------------------------
@description submit a chat comment for a chat
@param textboxId id of text box containing comment data
@param itemId id of item for comment
@param typeId id of type
@param div div to replace
@param live whether the comment wil go live
@param userId id of user submitting comment
---------------------------------------------------------*/
objSite.submitChat=function(textboxId,itemId,typeId,type,div,live,userId){
var textbox=document.getElementById(textboxId);
var comment=textbox.value;
// ---------------------------------------------------------
var baseURL = objSite.site_url;
var newURL = baseURL + "addChat";
objSite.holderDiv="commentsHolder";
chat=false;
$.ajax({
type: "POST",
url: newURL,
data: 'itemId=' + itemId + '&typeId=' + typeId+'&comment='+comment+'&live='+live+'&type='+type+'&userId='+userId,
success: function(data){
$('#commentsText').attr('value','');}
});
}
objSite.updateChat = function( itemId, typeId, div ){
objSite.holderDiv="#chatTranscriptHolder";
objSite.itemId = itemId;
objSite.typeId = typeId;
var baseURL = objSite.site_url;
var newURL = baseURL + "getComments";
$.ajax({
type: "POST",
url: newURL,
data: 'itemId=' + itemId + '&typeId=' + typeId,
success: objSite.appendChat
});
var newURL = baseURL + "getUsers";
$.ajax({
type: "POST",
url: newURL,
data: 'itemId=' + itemId,
success: objSite.updateUsers
});
}
/*--------------------------------------------------------
@description submit n answer for a comp
@param radioname name of radio button
@param compId id of item for competition
@param userId id of user submitting comment
@param email email of user
---------------------------------------------------------*/
objSite.submitAnswer=function(radioName,compId,userId,email){
for(c=1;c<4;c++){
var radioBtn=document.getElementById(radioName+c);
if(radioBtn.checked){
var answer= radioBtn.value;
}
}
// ---------------------------------------------------------
var baseURL = objSite.site_url;
var newURL = baseURL + "submitAnswer";
objSite.div="compAnswers";
objSite.holderDiv="compHolder";
$.ajax({
type: "POST",
url: newURL,
data: 'compId='+compId+'&userId='+userId+'&email='+email+'&answer='+answer,
success: objSite.updateAssignment
});
}
/*--------------------------------------------------------
@description submit n answer for a text comp
@param radioname name input box
@param compId id of item for competition
@param userId id of user submitting comment
@param email email of user
---------------------------------------------------------*/
objSite.submitTextAnswer = function( inputName, textareaName, typeId, itemId, type ){
// get answer form radios -------------------------------------------------------------------------------------------
var answer1 = null;
var answer2 = null;
var input1=document.getElementById(inputName);
if(input1.value!=""){
var answer1= input1.value;
}
var input2=document.getElementById(textareaName);
if(input2.value!=""){
var answer2= input2.value;
}
/// get values -------------------------------------------------------------------------------------------
var first_name = $('#comp_name').val();
var surname = $('#comp_surname').val();
var age = $('#comp_age').val();
var add_1 = $('#comp_add1').val();
var add_2 = $('#comp_add2').val();
var add_3 = $('#comp_add3').val();
var phone = $('#comp_phone').val();
var email = $('#comp_email').val();
var postcode = $('#comp_postcode').val();
var base_str = 'itemId=' + itemId +'&typeId='+typeId;
var data_str = '&firstname='+first_name;
data_str += '&surname='+surname;
data_str += '&age='+age;
data_str += '&add_1='+add_1;
data_str += '&add_2='+add_2;
data_str += '&add_3='+add_3;
data_str += '&phone='+phone;
data_str += '&email='+email;
data_str += '&postcode='+postcode;
data_str += '&answer1='+answer1;
data_str += '&answer2='+answer2;
var rtn_str = base_str+data_str;
objSite.div="compAnswers";
objSite.holderDiv="compHolder";
// ---------------------------------------------------------
var baseURL = objSite.site_url;
var newURL = baseURL + "submitTextComp";
$.ajax({
type: "POST",
url: newURL,
data: rtn_str,
success: objSite.updateAssignment
});
}
/*--------------------------------------------------------
@description submit n answer for a comp
@param radioname name of radio button
@param compId id of item for competition
@param userId id of user submitting comment
@param email email of user
---------------------------------------------------------*/
objSite.submitVote=function(radioName,voteId,userIp){
var cookieName = 'booktrustVote_'+voteId;
var votedBefore = false;
if( objSite.readCookie( cookieName ) == null )
document.cookie = cookieName+'=true; expires=Thu, 2 Aug 2010 20:47:11 UTC; path=/';
else
votedBefore = true;
radioCount = $('.vote_answers').size() + 1;
for(c=1;c0) {
$('#prompts'+id).show();
$('#autoSuggestionsList'+id).html(data);
}
else{
$('#prompts'+id).hide();
}
}
});
}
}
/* ------------------------------------------------------
@description open up the chat moderator tools
@param userId the user id
@param chatId id of the chat
---------------------------------------------------------*/
objSite.modTool = function( x, y, username ){
var toolX = x+60;
var toolY = y-135;
var toolPosition = 'style="top: '+ toolY +'px; left: '+ toolX +'px"'
var toolHtml = '';
//attach to document
$('#column1').prepend(toolHtml);
//set actions
$('#closetools').click(function(){
$('#modtools').remove();
});
$('.warnuser').click(function(){
var newWidth = $("#modtools").width() + 125 + "px";
$("#modtools").animate({
width: newWidth,
height: "80px"
}, 800, function(){ objSite.confirmAlert('warn', username); } );
});
$('.banuser').click(function(){
var newWidth = $("#modtools").width() + 125 + "px";
$("#modtools").animate({
width: newWidth,
height: "80px"
}, 800, function(){ objSite.confirmAlert('ban', username); } );
});
}
objSite.confirmAlert = function( type, username ){
var confirmHtml = 'Are you sure you want to '+ type +' this user?
';
confirmHtml += '
Yes ';
$('.toolHolder').prepend(confirmHtml);
$(".alertConfirm").animate({
opacity: 1.0
}, 200 );
var baseURL = objSite.site_url;
var newURL = baseURL + "alertUser";
$('#confirmAlert').click(function(){
$.ajax({
type: "POST",
url: newURL,
data: 'username=' + username + '&alert='+type,
success: objSite.userAlerted
});
});
}
objSite.userAlerted = function(){
var confirmHtml = 'The users status has been updated
';
$("#modtools").empty();
$("#modtools").append(confirmHtml);
window.setTimeout(
function() {
$("#modtools").animate({
opacity: 0
}, 400, function(){ $("#modtools").remove() });
},2000);
}
objSite.deleteTool = function( chatCommentId, confirm, chatId ){
$("#" + chatCommentId).append('');
//create a tooltip
$('#delete').hover(function(e){
var x = 297;
var y = e.pageY - 377;
var toolPosition = 'style="top: '+ y +'px; left: '+ x +'px"';
$( "#commentsHolder" ).append('Delete this comment?
');
},function(){
$("#tooltip").remove();
});
$("#delete").mousedown(function(){
var baseURL = objSite.site_url;
var newURL = baseURL + "deleteChatComment";
newId = chatCommentId.replace("chatId_","");
$.ajax({
type: "POST",
url: newURL,
data: 'commentId=' + newId +'&chatId=' + chatId,
success:
function(data){
}
});
});
}
objSite.setupAdvancedOptions = function ( optionsList, optionsDiv ){
$('#'+optionsList).change( function(){
var type = $('#'+optionsList).val();
$('#'+optionsDiv+' span').empty();
$('#option-specific').empty();
$('#'+optionsDiv+' span').append( type+'s' );
switch (type){
case "day":
$('#month_advanced').hide();
$('#year_advanced').hide();
$('#week_advanced').hide();
break;
case "week":
$('#month_advanced').hide();
$('#year_advanced').hide();
$('#week_advanced').show();
break;
case "month":
$('#week_advanced').hide();
$('#year_advanced').hide();
$('#month_advanced').show();
break;
case "year":
$('#month_advanced').hide();
$('#week_advanced').hide();
$('#year_advanced').show();
break;
}
});
}
objSite.updateFormDates = function( dateVal ){
var dates = dateVal.split("/");
chosenDate = new Date( dates[2], dates[1]-1, dates[0] );
arrDays = new Array( 'Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
arrMonths = new Array( 'January','February','March','April','May','June','July','August','September','October','November','December');
arrOccur = new Array( 'First', 'Second','Third','Fourth','Last' );
intDay = chosenDate.getDay();
intMonth = chosenDate.getMonth();
intDate = chosenDate.getDate();
intDayOccur = Math.round( (intDate/7)+0.49 );
strDay = arrDays[ intDay ];
strMonth = arrMonths[ intMonth ];
strDayOccur = arrOccur[ intDayOccur-1 ];
//Find all the form values and select them - day inputs
$('input.advancedOpt').removeAttr('checked');
$('input.'+strDay).attr({checked: 'checked'});
//day options
$('option.advancedOpt').removeAttr('selected');
$('option.'+strDay).attr({selected: 'selected'});
$('input.'+strMonth).attr({checked: 'checked'});
$('input.day_'+intDate).attr({checked: 'checked'});
$('option.'+strDayOccur).attr({selected: 'selected'});
}
objSite.validateEvent = function( results ){
final_str = objSite.rtn_str + '&latitude='+results[0]+'&longitude='+results[1];
var baseURL = objSite.site_url;
var newURL = baseURL + "validate_new_event";
$.ajax({
type: "POST",
url: newURL,
data: final_str,
success: objSite.updateAssignment
});
}
objSite.submitEvent = function( formId, userId ){
/// get values -------------------------------------------------------------------------------------------
input_values = objSite.get_form_values( formId );
objSite.rtn_str = "userID=" + userId + input_values;
objSite.div=formId;
objSite.holderDiv = "event_holder";
//get postcode
postcode = $('#event_postcode').val();
postcode = postcode.replace(" ","");
objMapping.sePointFromPostcode( postcode );
}
/*-----------------------------------------------
@description get the values from a form
@param form_id string id of the form to submit
------------------------------------------------*/
objSite.get_form_values = function(form_id){
var inputs='';
$('#'+form_id+' :input').each(function(i,n){
switch (n.type){
case "checkbox":
if(n.checked){
inputs += '&'+n.name+'=y';
}
else{
inputs += '&'+n.name+'=n';
}
break;
case "radio":
if( n.checked )
inputs += '&'+n.name+'='+n.value;
break;
case "select-multiple":
//get selected values
var selected_values =new Array();
var selected_indexes =new Array();
while(n.selectedIndex!=-1){
selected_indexes.push(n.selectedIndex);
selected_values.push(n.options[n.selectedIndex].value);
n.options[n.selectedIndex].selected = false;
}
//reset indexes
for(var c=0;c