/*
#################################################################################################### 
# File Name: facebook.js
# File Version: v 1.0
# Created By: Milind Bhide
# Created On: 22 Mar 2010
# Last Modified By:
# Last modified On:
# This file carries the javascript for facebook connect
#====================================================================================================
*/

/*
#====================================================================================================
#  Function Name    :    ajaxGetLinkingForm
#  Created By: Milind Bhide
#  Created On: 22 Mar 2010
#  Last Modified By: 
#  Last modified On: 
#  Purpose : To render the linking form for surveyhead and facebook accounts
#----------------------------------------------------------------------------------------------------
*/
function ajaxGetLinkingForm() 
{
	objFbLinkingFormSettings = GetXmlHttpObject();
	if(objFbLinkingFormSettings == null){
		alert(translate('browser_not_support_ajax'));
	} else {
		url = 'ajaxFacebook.php';
		url += '?action=getPage';
		objFbLinkingFormSettings.onreadystatechange = function(){
			if(objFbLinkingFormSettings.readyState==4 || objFbLinkingFormSettings.readyState=='complete')
			{
				 $('#fbLightBox').html('');
				lightbox = document.getElementById('fbLightBox');	
				var a = document.createElement('div');
				a.innerHTML = objFbLinkingFormSettings.responseText;
				linkingForm = lightbox.getElementsByTagName('div')[0];
				if(linkingForm)
					lightbox.removeChild(linkingForm);
				lightbox.appendChild(a);
				lightbox.style.display = 'block';
			}
		}
		objFbLinkingFormSettings.open("GET",url,true);
		objFbLinkingFormSettings.send(null);
	}
}


/*
#====================================================================================================
#  Function Name    :    ajaxConnectAndLogin
#  Created By: Milind Bhide
#  Created On: 22 Mar 2010
#  Last Modified By: 
#  Last modified On: 
#  Purpose : To map the surveyhead and facebook accounts and log the user in
#----------------------------------------------------------------------------------------------------
*/
function ajaxConnectAndLogin() 
{
	objFbLinkingFormSettings = GetXmlHttpObject();
	if(objFbLinkingFormSettings == null){
		alert(translate('browser_not_support_ajax'));
	} else {
		url = 'ajaxFacebook.php';
		url += '?action=connect';
		url += '&uname='+encodeURIComponent(document.getElementById('username').value);
		url += '&pwd='+document.getElementById('pwd').value;
		objFbLinkingFormSettings.onreadystatechange = function(){
			if(objFbLinkingFormSettings.readyState==4 || objFbLinkingFormSettings.readyState=='complete')
			{
				if(objFbLinkingFormSettings.responseText == 1) {
					window.location = 'login_fb_user.php';
				}else {
					lightbox = document.getElementById('fbLightBox');	
					var a = document.createElement('div');
					a.innerHTML = objFbLinkingFormSettings.responseText;
					linkingForm = lightbox.getElementsByTagName('div')[0];
					if(linkingForm)
						lightbox.removeChild(linkingForm);
					lightbox.appendChild(a);
					lightbox.style.display = 'block';
				}
			}
		}
		objFbLinkingFormSettings.open("GET",url,true);
		objFbLinkingFormSettings.send(null);
	}
}

/*
#====================================================================================================
#  Function Name    :    ajaxCheckIfConnected
#  Created By: Milind Bhide
#  Created On: 22 Mar 2010
#  Last Modified By: 
#  Last modified On: 
#  Purpose : To ask for user permissions & 
			  check if the user's account is already mapped with facebook account
#----------------------------------------------------------------------------------------------------
*/
function ajaxCheckIfConnected() 
{
 FB.Connect.showPermissionDialog
 (
	"user_about_me,user_birthday,user_education_history,email,user_hometown,user_relationship_details,user_location,user_religion_politics,user_relationships,user_website,user_work_history,user_activities,user_likes,user_checkins,user_events,user_interests",	
	function(perms) { ajaxFacebookCheckConnectOnLogin(); }
 )

}
/*
#====================================================================================================
#  Function Name    :    ajaxFacebookCheckConnectOnLogin
#  Created By: Gauri Sawaikar
#  Created On: 1 April 2011
#  Last Modified By: 
#  Last modified On: 
#  Purpose : To check if the user's account is already mapped with facebook account
#----------------------------------------------------------------------------------------------------*/
function ajaxFacebookCheckConnectOnLogin()
{
	$('#fbLightBox').html('');
	$('#fbLightBox').append('<img src="./images/load.gif" style="border:0 none;left:45%;position: absolute;top:45%;">');
    $('#fbLightBox').css('display','block');
	objFbLinkingFormSettings = GetXmlHttpObject();
	if(objFbLinkingFormSettings == null){
		alert(translate('browser_not_support_ajax'));
	} else {
		url = 'ajaxFacebook.php';
		url += '?action=checkconnected';
		objFbLinkingFormSettings.onreadystatechange = function(){
			if(objFbLinkingFormSettings.readyState==4 || objFbLinkingFormSettings.readyState=='complete')
			{
				if($.trim(objFbLinkingFormSettings.responseText) == 'UserNotConnected') {
					ajaxGetLinkingForm();
				} else {
					window.location = 'login_fb_user.php';
				}
			}
		}
		objFbLinkingFormSettings.open("GET",url,true);
		objFbLinkingFormSettings.send(null);
	}
}


/*
#====================================================================================================
#  Function Name    :    validateConnectForm
#  Created By: Milind Bhide
#  Created On: 22 Mar 2010
#  Last Modified By: 
#  Last modified On: 
#  Purpose : validation of the form used to map the surveyhead and facebook accounts
#----------------------------------------------------------------------------------------------------
*/
function validateConnectForm(frm) 
{
	with(frm)
    {
        if(!IsEmpty(username, translate('enter_email_address')))
        {
            return false;
        }
		
        if(!IsEmail(username, translate('enter_valid_email_address')))
        {
            return false;
        }
		
		if(pwd.value=="")
		{
		  alert(translate('enter_the_password'));
		  return false;
		}		
    }
	ajaxConnectAndLogin();	
}

function showLinkingForm()
{
	document.getElementById('light').style.display='block';
}

function hideLinkingForm()
{
	document.getElementById('fbLightBox').style.display='none';
}

