﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(itemId){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
		$("#newtext").html($('#'+itemId).html());
	}
	
}

function validatenominationsubmission() {
	// function no longer used
	// this was a stop gap until the proper central validation was completed
	if (!$('input[name=terms]').is(':checked')) {
		alert('You must agree to the terms and conditions');
		return false;
	}
	if ($('#email').val()=='') {
		alert('You must enter a valid email address.');
		return false
	}
}
//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		//show the navigation
		$("#adminnav").css({
			"display": "block"
		});
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	
	// see if the user has scrolled down a bit
	scr = getScrollXY();
	// display popup near the top of visible area
	topOffset=(windowHeight/8)+scr.scrOfY;
	leftOffset=windowWidth/2-popupWidth/2;
	
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": topOffset,
		"left": leftOffset
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function getScrollXY() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  	  
	  return {"scrOfX": scrOfX, "scrOfY": scrOfY}
	  
	}

function saveText() {
	// update the text in the database via ajax
	$.ajax({ 
		url: "ajax_process",
		type: 'POST',
		data: {
			'pagename' : 'home',
			'textkey' : $("#texteditid").val(),
			'newtext' : $("#newtext").val()
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			alert(request);
		}
	});
	
	// update the text on screen
	itemid=$("#texteditid").val();
	$('#' + itemid).html($("#newtext").val());
}

function loadRegionSelect(year) {
	$.ajax({ 
		url: "/site_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'regionlist',
			'year' : year
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// populate the region select box
			
			$('#region').html(request);
		}
	});			
}
function loadCategorySelect(year, region) {
	
	$.ajax({ 
		url: "/site_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'categorylist',
			'year' : year,
			'region' : region
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// populate the category select box
			$('#category').html(request);
			
		}
	});	
}

function showFilteredWinners(year, region, category) {
	$.ajax({ 
		url: "/site_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'winnersfiltered',
			'itemmodel' : 'nominations',
			'year' : year,
			'region' : region,
			'category' : category
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// display the winners
		
			$('#ukwinnerlist').html(request);
		}
	});	
}

function viewEmail(id) {
	
	centerPopup();
	
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		
		//hide the navigation
		$("#adminnav").css({
			"display": "none"
		});
		
		popupStatus = 1;
		$.ajax({ 
			url: "/member_ajax_process",
			type: 'POST',
			async: false,
			data: {
				'action' : 'viewemail',
				'id' : id
			}, 
			error: function(xhr,err){
			    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
			    alert("responseText: "+xhr.responseText);
			},
			success: function(request){
				// populate the email view
				//alert(request)
				
				$("#itemeditform").html(request);
			}
		});	
	}
}

function viewEndorsement(id) {
	
	centerPopup();
	
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		
		//hide the navigation
		$("#adminnav").css({
			"display": "none"
		});
		
		popupStatus = 1;
		$.ajax({ 
			url: "/member_ajax_process",
			type: 'POST',
			async: false,
			data: {
				'action' : 'endorsement-view',
				'itemmodel' : 'endorsements',
				'id' : id
			}, 
			error: function(xhr,err){
			    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
			    alert("responseText: "+xhr.responseText);
			},
			success: function(request){
				// populate the email view
				//alert(request)
				
				$("#itemeditform").html(request);
			}
		});	
	}
}

function viewInvite(id, subid) {
	
	centerPopup();
	
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		
		//hide the navigation
		$("#adminnav").css({
			"display": "none"
		});
		
		popupStatus = 1;
		$.ajax({ 
			url: "/member_ajax_process",
			type: 'POST',
			async: false,
			data: {
				'action' : 'invite-view',
				'itemmodel' : 'events',
				'id' : id,
				'subid' : subid
			}, 
			error: function(xhr,err){
			    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
			    alert("responseText: "+xhr.responseText);
			},
			success: function(request){
				// populate the email view
				//alert(request)
				
				$("#itemeditform").html(request);
			}
		});	
	}
}

function inviteSave(eventId, subsetId) {
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'invite-save',
			'itemmodel' : 'events',
			'eventid' : eventId,
			'subsetid' : subsetId
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			alert ("Success!");
		}
	});
}

function endorsementReject(id) {
	
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'endorsement-reject',
			'itemmodel' : 'endorsements',
			'id' : id
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			
			// populate the category select box
			$('#listitem_'+id).css({ "display": "none" });
			
			disablePopup();
		}
	});	
}

function endorsementCertificate(id) {
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'endorsement-certificate',
			'itemmodel' : 'endorsements',
			'id' : id
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			
			$('#listitem_'+id).css({ "display": "none" });
			alert('Thank you for ordering a certificate for this person/team. All certificates will be mailed to schools in June.');
			
			disablePopup();
		}
	});	
}

function endorsementAccept(id) {
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'endorsement-accept',
			'itemmodel' : 'endorsements',
			'id' : id
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// Hide the person type choosing section 
			$("#peopletype-select").fadeOut("fast");
			
			// Display the category choice section
			$("#award-select").slideDown();
			
		}
	});
}

function getAwardCategories(nominationsid) {
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'getawardcategories',
			'itemmodel' : 'categories',
			'nominationsid' : nominationsid
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// Populate the container with the radio button options (the HTML for which is contained within 'request')
			$("#categoryholder").html(request);
			// It's possible that we might not get any categories returned.  If this happens then we need to update the display text to reflect this.
			if (request=="") {
				$("#award-select").html("<p class='errors'>Unfortunately there is no available category for this person type as you have already endorsed another teacher into the category for which they are eligible.</p>");
			}
		}
	});
}

function endorsementPersonTypeSelect() {
	// Hide the status choosing section 
	$("#end-approval").fadeOut("fast");
	
	// Display the category choice section
	$("#peopletype-select").slideDown();
}

function personTypeSave(people_id, type_id) {
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'persontypesave',
			'itemmodel' : 'people',
			'peopleid' : people_id,
			'typeid' : type_id
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// We've updated the person type so now we need to get the available category types ready for display
			getAwardCategories($('#nominations_id').val());
			// Accept the endoresement and proceed as before
			endorsementAccept($('#endorsement_id').val());
		}
	});
}

function endorsementAwardSave(id, awardId) {
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'endorsement-awardsave',
			'itemmodel' : 'endorsements',
			'id' : id,
			'awardid' : awardId
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// Hide the category choosing section 
			$("#award-select").fadeOut("fast");
			
			// fill in the endorsement entry section
			$("#endorsement-entry").html(request);
			
			// Display the endorsement entry section
			$("#endorsement-entry").slideDown();
			
		}
	});
}

function endorsementSave(type, returntype) {
	
	
	$.ajax({ 
		url: "/member_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'endorsement-save',
			'itemmodel' : 'endorsements',
			'type' : type,
			'fields' : $('form').serialize()
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			if (returntype!='open') {
				navigateTo(request);
			} else {
				// this was a save and leave open
				$('#saveprogress').fadeIn().text('Saved').delay(1500).fadeOut('slow');
				
			}
		}
	});
	
	
}

function saveWinnerComment() {

	$.ajax({ 
		url: "/site_ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'winnercomment-save',
			'itemmodel' : 'nominations',
			'fields' : $('form').serialize()
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			if (returntype!='open') {
				navigateTo(request);
			} else {
				// this was a save and leave open
				
				
			}
		}
	});
	
	
}

function navigateTo(target) {
	self.location.href=target;
}

function toggleCatDetails(id) {
	
	if ($('#detailholder-'+id).is(':visible')) {
		$('#detailholder-'+id).hide('slow');
		$('#detailcontrol-'+id).text('Show details');
	} else {
		$('#detailcontrol-'+id).text('Hide details');
		$('#detailholder-'+id).show('slow');
	}
	
}

function expandSubmission(subId) {
	
	$('#subshort-'+subId).hide();
	$('#subfull-'+subId).show();
	
}

function reduceSubmission(subId) {
	
	$('#subfull-'+subId).hide();
	$('#subshort-'+subId).show();
	
}

function addPersonEmail(personid, email, desc) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'personemailadd',
			'itemmodel' : 'people',
			'itemtype' : 'people',
			'id' : personid,
			'email' : email,
			'desc' : desc
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// add the address to the list
			emailid = request;
			display='<input type="hidden" id="emailtype-'+desc+'" name="emailtype-'+desc+'" value="'+request+'" />';
			display+='<li id="e-'+emailid+'">';
			display+='<span style="clear:right;" class="right">';
			display+='<span title="Remove this email" class="icon e-remove" id="er-'+personid+'-'+emailid+'">&nbsp;</span>';
			display+='</span>';
			display+='<input type="radio" name="people-prefemail" value="'+emailid+'">';
			display+='<img style="display:none;" title="Preferred" class="pestar" id="pestar-'+request+'" src="/img/icons/favorite.png">';
			display+=email+' ('+$("#emailnewdesc option[value='"+desc+"']").text()+')';
			display+='</li>';
			$('#contactemails').append(display);
			$('#emailnew').val('');
			$('#emailnewdesc').val('');
			
		}
	});	
}

function addPersonTel(personid, tel, desc) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'personteladd',
			'itemmodel' : 'people',
			'itemtype' : 'people',
			'id' : personid,
			'tel' : tel,
			'desc' : desc
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// add the tel no to the list
			telid = request;
			display='<input type="hidden" id="teltype-'+desc+'" name="teltype-'+desc+'" value="'+request+'" />';
			display+='<li id="t-'+telid+'">';
			display+='<span style="clear:right;" class="right">';
			display+='<span title="Remove this telephone number" class="icon t-remove" id="tr-'+personid+'-'+telid+'-'+desc+'">&nbsp;</span>';
			display+='</span>';
			display+='<input type="radio" name="people-preftel" value="'+telid+'">';
			display+='<img style="display:none;" title="Preferred" class="ptstar" id="ptstar-'+request+'" src="/img/icons/favorite.png">';
			// desc is now an ID number so get the text from the option value that it relates to
			display+=''+tel+' ('+$("#telnewdesc option[value='"+desc+"']").text()+')';
			display+='</li>';
			$('#contacttels').append(display);
			$('#telnew').val('');
			$('#telnewdesc').val('');
		}
	});	
}

function removePeopleEmail(peopleId, emailId) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'removepeopleemail',
			'itemmodel' : 'people',
			'id' : peopleId,
			'emailid' : emailId
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// alert(request)
			// done
		}
	});	
}

function removePeopleTel(peopleId, telId) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'removepeopletel',
			'itemmodel' : 'people',
			'id' : peopleId,
			'telid' : telId
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			//alert(request)
			// done
		}
	});	
}

function addPersonAddress(personid, company, desc, address1, address2, address3, town, county, postcode) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'personaddressadd',
			'itemmodel' : 'people',
			'itemtype' : 'people',
			'id' : personid,
			'company' : company,
			'desc' : desc,
			'address1' : address1,
			'address2' : address2,
			'address3' : address3,
			'town' : town,
			'county' : county,
			'postcode' : postcode
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// add the address to the list
			display='<div class="formrow"><fieldset id="addholder-'+request+'" class="peopleaddress"><legend id="leg-'+request+'">ID: '+request+' </legend>';
			display+='<div class="formrow"><label>Description</label><p><input type="text" value="'+desc+'" id="add-'+request+'-description" name="add-'+request+'-description" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-description"></p></div>';
			display+='<div class="formrow"><label>Company</label><p><input type="text" value="'+company+'" id="add-'+request+'-company" name="add-'+request+'-company" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-company"></p></div>';
			display+='<div class="formrow"><label>Line 1</label><p><input type="text" value="'+address1+'" id="add-'+request+'-line1" name="add-'+request+'-line1" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-line1"></p></div>';
			display+='<div class="formrow"><label>&nbsp;</label><p><input type="text" value="'+address2+'" id="add-'+request+'-line2" name="add-'+request+'-line2" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-line2"></p></div>';
			display+='<div class="formrow"><label>&nbsp;</label><p><input type="text" value="'+address3+'" id="add-'+request+'-line3" name="add-'+request+'-line3" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-line3"></p></div>';
			display+='<div class="formrow"><label>Town</label><p><input type="text" value="'+town+'" id="add-'+request+'-town" name="add-'+request+'-town" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-town"></p></div>';
			display+='<div class="formrow"><label>County</label><p><input type="text" value="'+county+'" id="add-'+request+'-county" name="add-'+request+'-county" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-county"></p></div>';
			display+='<div class="formrow"><label>Postcode</label><p><input type="text" value="'+postcode+'" id="add-'+request+'-postcode" name="add-'+request+'-postcode" class=""></p><p style="display:none;" class="errors" id="errors-add-'+request+'-postcode"></p></div>';
			$('#addresses').append(display);
		}
	});	
}

function removePeopleAddress(peopleId, addressId) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'peopleaddress-remove',
			'itemmodel' : 'people',
			'peopleid' : peopleId,
			'addressid' : addressId
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			//alert(request)
		}
	});	
}

function updatePersonAddress(peopleId, addressId, company, desc, address1, address2, address3, town, county, postcode) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'personaddressupdate',
			'itemmodel' : 'people',
			'itemtype' : 'people',
			'peopleid' : peopleId,
			'addressid' : addressId,
			'company' : company,
			'desc' : desc,
			'address1' : address1,
			'address2' : address2,
			'address3' : address3,
			'town' : town,
			'county' : county,
			'postcode' : postcode
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			// add the address to the list
			alert ("Address has been updated");
		}
	});	
}

function setPreferredPeopleAddress(peopleId, addressId) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'peopleaddress-setpreferred',
			'itemmodel' : 'people',
			'peopleid' : peopleId,
			'addressid' : addressId
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			//alert(request)
		}
	});	
}

function updatePersonDetails(personid, title, firstname, lastname, honours) {
	$.ajax({ 
		url: "/ajax_process",
		type: 'POST',
		async: false,
		data: {
			'action' : 'updatepersondetails',
			'itemmodel' : 'people',
			'personid' : personid,
			'title' : title,
			'firstname' : firstname,
			'lastname' : lastname,
			'honours' : honours
		}, 
		error: function(xhr,err){
		    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
		    alert("responseText: "+xhr.responseText);
		},
		success: function(request){
			//alert(request)
		}
	});	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	
	
	 $("div#sitenav ul#sub-0").supersubs({ 
         minWidth:    12,   // minimum width of sub-menus in em units 
         maxWidth:    27,   // maximum width of sub-menus in em units 
         extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                            // due to slight rounding differences and font-family 
     }).superfish();  // call supersubs first, then superfish, so that subs are 
                      // not display:none when measuring. Call before initialising 
                      // containing tabs for same reason. 

	// hide everything that needs to be hidden
	$('.hideonready').hide();	
	
	//LOADING POPUP
	//Click the button event!
	$(".txt-edit").click(function(){
		$("#texteditid").val($(this).attr('id'));
		//centering with css
		centerPopup();
		//load popup
		loadPopup($(this).attr('id'));
	});
	
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#popupclose").click(function(){
		
		disablePopup();
	});
	
	//Click out event!
	$("#backgroundPopup").click(function(){
		// disbaled due to accidental outside clicking
	//	disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	// Press Update event!
	$("#updatetext").click(function(){
		saveText();
		disablePopup();
	});
	
	$(".emailview").click(function() {
		
		emailId=$(this).attr('id').replace('email-','');
		viewEmail(emailId)
	});
	
	$(".end-details").click(function() {
		
		id=$(this).attr('id').replace('end-','');
		viewEndorsement(id)
	});
	
	$(".inv-details").click(function() {
		var parts = $(this).attr('id').split('_');
		id = parts[0].replace('inv-','');
		subid = parts[1].replace('sub-','');
		viewInvite(id, subid)
	});
	
	$("#judgedetailsconfirm").click(function() {
		
		$("#judgedetails").slideUp();
		$("#judgecategories").slideDown();
		
	});
	
	$("#addmessage").click(function() {
		var errors='';
		if ($('#commentname').val()=='') {errors+='Please enter your name.\n';}
		if ($('#commentemail').val()=='') {errors+='Please enter your email address.\n';}
		if ($('#commentheadline').val()=='') {errors+='Please enter a headline for your comment.\n';}
		if ($('#commentmessage').val()=='') {errors+='Please enter your message.\n';}
		if (errors!='') {
			errors='The following errors were encountered:\n'+errors;
			alert(errors);
		} else {
			// all ok so send the comment
			saveWinnerComment();
			
			
			$('#commentform').html('<h4>Thank you</h4><p>Your message has been submitted for approval.</p><p>Please be aware it can take up to 48 hours for messages to appear on the site.</p><p><a href="#comments">View other messages</a></p>');
		}
	});
	
	$("#judgecategoryselect").click(function() {
		var retval=true;
		
		if($("#judgetandc").attr('checked')==false) {
			retval=false;
		} 
		if($("#judgecontract").attr('checked')==false) {
			retval=false;
		} 
		
		if (retval==false) {
			alert('You must agree to the Judging Terms & Conditions and Contract before you may accept the invitation');
		}
		return retval;
		
	});
	
	
	$(function() {
		//$("#sortable").sortable();
		
		$("#sortable").sortable({ opacity: 0.6, cursor: 'move', update: function() {
			
	//		var itemType=$('#itemtype').val();
			var awardorder=$(this).sortable("serialize");
			// remove the listitem fieldnames to give a clean series of category ids in order
			awardorder=awardorder.replace(/listitem\[\]=/g,'');
			
			$("#itemorder").val(awardorder);
	//		$.post("/member_ajax_process", order, function(theResponse){
	//			alert(theResponse);
	//		});
		}
		});
		$("#sortable").disableSelection();
		
	});
	
});


