
function returnConfirm(confirmMessage, confirmLink){
	
	var confirmVar = confirm(confirmMessage);
	if(confirmVar){
		window.location = confirmLink;
	}else{
		return false;
	}
	
}

function generateOverlayLoader(){
	var editDiv = '';
	var editDivMask = '';
	var editDivContent = '';
	
	editDiv = jQuery('#editDetailsDiv');
	editDivMask = jQuery('#editDetailsDivMask');
	editDivContent = jQuery('#editDetailsDivContent');

	var winW = getWindowWidth();
	var leftOffset = (winW/2)-32;
	
	editDiv.attr('style', 'left: '+leftOffset+'px; width: 64px');
	
	editDivMask.show();
	editDivContent.html('<img src="/images/loading2.gif" alt="Loading..." style="margin: 16px 0px 10px 16px;"/>');
	setOverlayMiddle();
	
	showJQOverlay();
	
}

function populateOverlayContent(content, width){
	var editDiv = '';
	var editDivMask = '';
	var editDivContent = '';
	
	//Load the loading mask for the create form
	editDiv = jQuery('#editDetailsDiv');
	editDivMask = jQuery('#editDetailsDivMask');
	editDivContent = jQuery('#editDetailsDivContent');
	editDivMask.hide();
	editDivContent.html(content);
	
	var winW = getWindowWidth();
	var leftOffset = (winW/2)-(width/2);

	editDiv.animate({ left: leftOffset+'px', width: width }, 750);
	
	setTimeout(function(){editDivMask.show(); setOverlayMiddle();}, 700);
	
}

function setOverlayMiddle(){
	var winHeight = getWindowHeight();
	var divHeight = jQuery("#editDetailsDiv").height();
	var offsetHeight = winHeight - divHeight;
	jQuery("#editDetailsDiv").css('top', (offsetHeight/2)-20);
}

//For ticket #1854: Populate the overlay with content, set overlay's width and set whether overlayPosition is shown at the top or middle
//Similar to populateOverlayContent() above but we allow setting the overlay's position
function populateOverlayWithContent(content, width, overlayPosition)
{
	//Load the loading mask for the create form
	editDiv = $('#editDetailsDiv');
	editDivMask = $('#editDetailsDivMask');
	editDivContent = $('#editDetailsDivContent');
	editDivMask.hide();
	editDivContent.html(content);
	
	var winW = getWindowWidth();
	var leftOffset = (winW/2)-(width/2);

	//Note by Chris T: For now, if overlayPosition is set to "top", we'll assume that we need to use alot of real-estate for the overlay so we'll present it at the top-left
	if (overlayPosition == 'top'){
		leftOffset = 10;
	}
	
	editDiv.animate({ left: leftOffset+'px', width: width }, 750);
	
	if ($.trim(overlayPosition) != ''){
		if (overlayPosition == 'top'){
			setTimeout(function(){editDivMask.show(); setOverlayTop();}, 700);
		}
		else if (overlayPosition == 'middle') setTimeout(function(){editDivMask.show(); setOverlayMiddle();}, 700);
	}
}

function setOverlayTop()
{
	$("#editDetailsDiv").css('top', 5);
}

function getWindowWidth(){
	var winW = jQuery(window).width();
	return winW;
}

function getWindowHeight(){
	var winH = jQuery(window).height();
	return winH;
}

function showJQOverlay(){
	jQuery('#alphaOverlay').show();
	jQuery('#alphaOverlay').animate({ opacity: 0.8 }, 750);
	jQuery('#editDetailsDiv').show();
}

function hideJQOverlay(){
	jQuery('#alphaOverlay').hide();
	jQuery('#editDetailsDiv').hide();
	jQuery('#lightbox_close').hide();
}

function InviteRightParserSingle(textObj){
	
	//Loop through our answers and replace any bad characters
	var replaceText = textObj;
	var illegalChar = 0;
	var regExIllegal = /~|\\|\|/g;
	var notifyIllegal = false;
	//Check for Illegal Characters
	var illegalChars = new Array();
	var illegalChar1 = '';
	var illegalChar2 = '';
	var illegalChar3 = '';
		
	//check for illegal characters
	illegalChar = replaceText.search(regExIllegal);
	//Fix this later for AQs
	if(illegalChar > 0){
		//Check what illegal characters we are removing and tell the user
		if(replaceText.indexOf('~') > 0) 	illegalChar1 = '~';
		if(replaceText.indexOf('\\') > 0) 	illegalChar2 = '\\';
		if(replaceText.indexOf('|') > 0) 	illegalChar3 = '|';
		replaceText = replaceText.replace(regExIllegal, '');
		notifyIllegal = true;
	}
	
	replaceText = replaceText.replace(/&/g, '##inviteright-amp##');
	replaceText = htmlentities(replaceText);
	replaceText = replaceText.replace(/&amp;/g, '&').replace(/##inviteright-amp##/g, '&amp;'); //Fix for character encoding '&'
	//replaceText = replaceText.replace(/##inviteright-amp##/g, '&amp;');
	replaceText = replaceText.replace(/\u000a/g, "\\n").replace(/\r\n/g, "\\n"); //Newline replacement
	replaceText = replaceText.replace(/\\/g, '\\\\').replace(/\$/g, '&#36;');

	replaceText = jQuery.trim(replaceText);
	
	if(notifyIllegal){
		var illegalString = '';
		if(illegalChar1 != '') illegalChars.push(illegalChar1);
		if(illegalChar2 != '') illegalChars.push(illegalChar2);
		if(illegalChar3 != '') illegalChars.push(illegalChar3);
		illegalCharLength = illegalChars.length;
		for(var i=0; i < illegalCharLength; i++){
			illegalString += illegalChars[i];
			if(i != (illegalCharLength-1)) illegalString += ',';
		}
		alert('Unsupported characters have been removed from your forms ('+illegalString+'). Please review your forms.');
	}
	
	return replaceText;
	
}

function ShowActions(ActionLink, actionDiv){
				
	var actionsDiv = $('#'+actionDiv);
	var actionsBtn = $('#'+ActionLink);
	var boxwidth = actionsDiv.width();

	actionsDiv.show();
	
	var ay = actionsBtn.offset().top+16;
	var ax = actionsBtn.offset().left+10;
	
    actionsDiv.attr('style', 'left: '+ax+'px; top: '+ay+'px;');
}

function HideActions(ActionLink, actionDiv){

	var actionsDiv = $('#'+actionDiv);
	var actionsBtn = $('#'+ActionLink);
	
	actionsDiv.hide();
	
	actionsDiv.attr('style', 'left: -500px;');
}

function IsEmailAddress(data)
{
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	var isEmail = false;
	
	if (filter.test(data))
		isEmail = true;
	else
	{
		isEmail = false;
	}

	return isEmail;
}

$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function toggleGetSupport(supportButton){
	
	supportDiv = $('#get_support_div');
	
	anchor = getAnchorPosition('get_support');
	ax = anchor.x;
	ay = anchor.y;
	
	if(supportDiv.is(':hidden')){
		supportDiv.show(); 
	}else{
		supportDiv.hide();
	}

	boxwidth = supportDiv.attr('offsetWidth');
	padding = -20;
	
	offset = boxwidth+padding
	leftvar = ax-offset;
	topvar = ay+20;

    supportDiv.css('left', leftvar);
    supportDiv.css('top', topvar);
    
}

/**
type: first, last, all
*/
function strToUpper(value, type){
	var newval = '';
	switch(type){
		case 'first':
			newval = value.substr(0, 1).toUpperCase() + value.substr(1);
		break;
		case 'last':
			newval = value.substr(0, -1).toUpperCase() + value.substr(1);
		break;
		case 'all':
			newval = value.toUpperCase();
		break;
	}
	return newval;
}

function generateButton(linkObj, color, btnText, nextBtn, prevBtn, smallBtn){
	
	nextBtn = (nextBtn)? '_next' : '';
	prevBtn = (prevBtn)? '_prev' : '';
	smallBtn = (smallBtn)? '_small' : '';
	
	var tempDiv = $('<div>');
		tempDiv.addClass('button_default'+smallBtn)
			   .addClass('button'+smallBtn+'_'+color+'_leftcap'+prevBtn);
	linkObj.append(tempDiv);
	
	var tempDiv = $('<div>');
		tempDiv.addClass('button_default'+smallBtn)
			   .addClass('button'+smallBtn+'_'+color+'_center')
			   .append(btnText);
	linkObj.append(tempDiv);
	
	var tempDiv = $('<div>');
		tempDiv.addClass('button_default'+smallBtn)
			   .addClass('button'+smallBtn+'_'+color+'_rightcap'+nextBtn);
	linkObj.append(tempDiv);
		
	return linkObj;
		
}

function editGuestDetails(eventId, groupId){
	
	generateOverlayLoader();
	
	$.ajax({
	    url: '/organizer/viewdetails',
	    type: 'POST',
	    data: "eventId="+eventId+"&groupId="+groupId,
	    dataType: 'text',
	    error: function(){
	        alert('Error loading. Please try again.');
	    },
	    success: function(html){
	      populateOverlayContent(html, 600);
	    }
	});
	
}

function hideFeatureMessage(){
	
	$('#featureNotification').hide();
	
	$.ajax({
	    url: '/ajax/hidefeaturemessage',
	    type: 'POST',
	    data: "",
	    dataType: 'text',
	    error: function(){
	    },
	    success: function(){
	    }
	});
	
}

function IsNumeric(input)
{
   return (input - 0) == input && input.length > 0;
}

function sortNumber(a,b)
{
	return a - b;
}

//--start control of hideJQOverlay()
//- $('#alphaOverlay) by default has hideJQOverlay(). This allows organizer to click out from any lightbox
//		in the Organizer area. However, some lightbox such as Membership List doesn't allow organizers to click out.
//		Therefore, we need to use these two functions so that when Membership List for example removes the hideJQOverlay()
//		in $('#alphaOverlay), we can always restore it so that other lightboxes can have the click out capability
function allowHideJQOverlay()
{
	//Allow clicking on the background to close the overlay
	if ($('#alphaOverlay').length > 0 && $('#alphaOverlay').data('events') == null){
		$('#alphaOverlay').click(function(){ hideJQOverlay(); });
	}
}
function disallowHideJQOverlay()
{
	//Remove the onclick event handler from the alphaOverlay div to disable click out
	if ($('#alphaOverlay').length > 0) {
		$('#alphaOverlay').attr('onclick', '');
		$('#alphaOverlay').unbind('click');
	}
}
//--end control of hideJQOverlay

//Returns the keyboard equivalent of the given ascii code)
function getCharFromCharCode(code)
{
	return String.fromCharCode(code);
}
