function inviteFriends(){

   $.facebox({ajax:'profile/invitefriends'});
	
}

function sendInvitation(){
	
	var fromName = $('#fullName').val();
	var toFriends = $('#toFriends').val();
	var message = $('#message').val();
	
	var url = 'profile/sendinvitationtofriends';
	
	$.post(url,{
		from:fromName,
		to:toFriends,
		message:message
	},function(data){
	//hidePopup('inviteFriendsPopup');
        $(document).trigger('close.facebox');
	})
	
}

function howItWorks() {
    $.facebox({ajax:'home/howitworks'});
}

function addRemoveMessage(){
	
	var messageTdObj = document.getElementById('messageArea');
	var link = document.getElementById('addMessageOpt');
		
	if (messageTdObj.style.display == 'none') {
		messageTdObj.style.display = 'block';
		link.innerHTML='<<< Remove message'
                $('textarea#messageArea').autogrow({"width":"335px","fontSize":"11.5px","lineHeight":"normal"});

	}
	else {
		messageTdObj.style.display = 'none';
		link.innerHTML='Add message >>>'
		messageTdObj.value='';
                messageTdObj.style.height = "auto";
	}
		
}

function sendRequest(){
	
	var invitedUser = document.getElemenetById('invitedUserId');
	
	var messageObj = document.getElementById('messageArea');
	
	var parm = 'invite='+invitedUser.value+'&message='+messageObj.value;
	var url = 'ajax/invitation/addtofriends_ajax.php';
	
//Updater3(url,parm,closePopup);
	
}

function sendInvitationToMember(){
	
	var message = $('#messageArea').val();
	var invitedUser = $('#invitedUserId').val();
	
	 
	var url = 'profile/memberfriendship';
	
	$.post(url,{
		invitedmember:invitedUser,
		message:message,
		status:'invite'
	},function(data){
		hidePopup('addToFriends');
	})
	
//alert(parm);
//Updater3(url,parm,closePopup);
	
}

function addToFriends(id,username,image){
	//var popup = null;
	//popup = new pop('addToFriends','Add to Friends',340,360);
	//popup.setTheme('black');
	//popup.setUrl('profile/addtofriendspopup','post','inviteduser:'+id);
	//popup.show();
	//Ability to attach a message is disabled for the moment

	$.post('profile/checkfriendship',{
		invitedmember: id
	},function(data){
		if(data==1){
    		$.msgbox('<img class="list-picture-sub float-right " src="'+image+'" />You are about to send a friend request to <strong>'+username+'</strong>. Please note that if this request is accepted, your <b>full name will be discolosed</b> to this user. Click below to confirm?',{
                            type:'confirm',
                            buttons:[{
                                type:'submit',
                                value:'Confirm'
                            },{
                                type:'submit',
                                value:'Cancel'
                            }]
                        },
                        function(result){
                            if(result=='Confirm'){
                                $(this).addClass('processing');
                                inviteFriendsAction(id);
                            }
                        });
		}
		else if (data==2) $.msgbox('<img class="list-picture-sub float-right " src="'+image+'" />A previous friend requests is currently pending. Please wait while <strong>'+username+'</strong> responds.');
		else if (data==3) $.msgbox('<img class="list-picture-sub float-right " src="'+image+'" />Please note that you are already friends with <strong>'+username+'</strong>.');
	})


}

function removeFromFriends(id,username,image){
	//var popup = null;
	//popup = new pop('removeFromFriends','Remove from Friends',340,360);
	//popup.setTheme('black');
	//popup.setUrl('profile/removefromfriendspopup','post','friendid:'+id);
	//popup.show();
    // @kaveen, implementing Msgbox for remove friends

        $.msgbox('<img class="list-picture-sub float-right " src="'+image+'" />Are you sure you want to remove <strong>'+username+'</strong> from your friend list?',{
                            type:'confirm',
                            buttons:[{
                                type:'submit',
                                value:'Yes'
                            },{
                                type:'submit',
                                value:'No'
                            }]
                        },
                        function(result){
                            if(result=='Yes'){
                                $(this).addClass('processing');
                                removeFromFriendsAction(id);
                            }
                        });

}

function removeFromFriendsAction(id){

	$.post('profile/memberfriendship',{
		invitedmember: id,
		status:'remove'
	},function(data){
		$.msgbox('You successfully removed one of your friends');
		$('ul#nav li.friends').click();
	})
}

function inviteFriendsAction(id){
	
	$.post('profile/memberfriendship',{
		invitedmember: id,
		status:'invite'
	},function(data){

	})
}

function acceptFriendship(id, nid){
	
	$.post('profile/memberfriendship',{
		invitedmember: id,
		status:'accept'
	},function(data){
		$.msgbox("You have successfully accepted the friend request.",{type:'info'});
		Vetstoria.notifications.removeNotification(nid);
		//Vetstoria.contextualmenu.action_loadarea_notifications(Vetstoria.contextualmenu,$('ul#nav li.notifications'),true);
		//window.location = window.location;
		$('ul#nav li.friends').click();
		Vetstoria.contextualmenu.action_loadarea_notifications(Vetstoria.contextualmenu,$('ul#nav li.notifications'),true);		
	})
}

function declineFriendship(id, nid){
	
	//var popup = null;
	//popup = new pop('addToFriends','Decline friendship',340,360);
	//popup.setTheme('black');
	//popup.setUrl('profile/memberfriendship','post','invitedmember:'+id+",status:'decline'");
	//popup.show();

	$.post('profile/memberfriendship',{
		invitedmember: id,
		status:'decline'
	},function(data){
		$.msgbox("You have successfully declined the friend request.",{type:'info'});
		Vetstoria.notifications.removeNotification(nid);
		Vetstoria.contextualmenu.action_loadarea_notifications(Vetstoria.contextualmenu,$('ul#nav li.notifications'),true);
	})
	
}

$().ajaxStart(function(){

	$('#ajaxProcessing').show();
	$('#ajaxProcessing').html('Loading, please wait &hellip;');

});

$().ajaxError(function(){

	$('#ajaxProcessing').show();
	$('#ajaxProcessing').html('Connexion error !');

});

$().ajaxSuccess(function(){

	$('#ajaxProcessing').show();
	$('#ajaxProcessing').html('Done.');

});
  
$().ajaxStop(function(){
    
	$('#ajaxProcessing').hide();
});

function showContent(id){
	$('#'+id).fadeIn(500);
}

function hideContent(id){
	$('#'+id).fadeOut(300);
}

/**
 * Read a page's GET URL variables and return them as an associative array.
 * @example for URL http://www.example.com/index.html?hello=bonjour&goodevening=bonsoir
 * var hash = getUrlVars();
 * alert(hash['hello']); // prints 'bonjour'
 * alert(hash['goodevening']); // prints 'bonsoir'
 */
function get_url_vars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    return vars;
}

function SyntaxColored(js){
	if (!js || !js.replace) return "";
	js=js.replace(/\r/g,'\n');
	js=js.replace(/</g,'&lt;').replace(/>/g,'&gt;');

	var parens = /([\]\[(){}]+)/g;
	var comments = /(\/\/.+|\/\*(.|\n)+?\*\/)/g;		
	var keywords = /\b(function|var|if|isNaN|return|if|else|for|while|new|continue|switch|case|true|false|prototype|constructor|caller|Number|Date|Object|String|Function|Array|RegExp|Boolean|Math)\b/g;
	var strings = /((["'])(?:.*?(?:[^\\](?:\\\\)*|[^\\]))?\2)/g;
	var numbers = /\b(-?(?:\d+|\d*\.\d+)\b)/g;

	js=js.replace(strings,'<span class="string">$1<\/span>');
	js=js.replace(parens,'<span class="paren">$1<\/span>');
	js=js.replace(numbers,'<span class="number">$1<\/span>');
	js=js.replace(keywords,'<span class="keyword">$1<\/span>');
	js=js.replace(comments,'<span class="comment">$1<\/span>');
	if (document.all) js=js.replace(/\n/g,"<br>").replace(/  /g," &nbsp;").replace(/\t/g," &nbsp; &nbsp;"); //IE-specific hack
	js=js.replace(/<\/span>\n/g,"<\/span>&nbsp;\n");
	return js;
}


function isAlphaNumeric(val){
	var alphanum=/^[a-zA-Z0-9_-]*$/; //This contains _,-, A to Z , 0 to 9 and A to B
	if(val.match(alphanum)){
		return true;
	}else{
		return false;
	}
}

function forgotPassword(){
	var forgot_email = $('input#forgot_email').val();
	
	if(!/^([a-zA-Z0-9\._-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(forgot_email)){
		$.msgbox("Please note that the email provided is invalid. Enter the correct address and try again.",{type:"info"});
		return false;
	}

	var post_data = {};
	post_data["forgot_email"] = forgot_email;
	var result = Vetstoria.json_command('session/forgotpassword', post_data);

	if(result.state==0) {
		$.msgbox("An email has been send to <u>" + forgot_email + "</u> with your new password.",{type:"info"});
		$('input#forgot_email').val('');
	}
	else $.msgbox(result.data[0].Error,{type:"error"});

	return false;
}
