	  


function auth_using_fb() {
	//gets called ONLY after they have clicked the login via FB button
	//gets called EVERY page view after that.
	//Set Global Scope for all important variables that get passed in and out of functions
	  var myFBUID = '';
		var myQuery='';
 		var myName ='';
 		var myAvatar='';
 		var myFBLink='';


		update_user_box();
	
	//check if they are logged in through cuview with their facebook id
	//var fbuid = FB.Connect.get_loggedInUser();
	//if(readCookie('logged')=='-1') {  //NEED TO SET AND READ LOGGED IN COOKIES!
		//They are NOT logged in through CU View, so get their FB user id, display name, and avatar, and push them to CU View and force login to CU View
		
		myFBUID = FB.Connect.get_loggedInUser();
		myQuery='SELECT name, pic_square, profile_url FROM user WHERE uid=' + myFBUID;
 		myName ='';
 		myAvatar='';
 		myFBLink='';
 		
 		//temporary - makes sure login is current
 		//FB.Connect.forceSessionRefresh();
 		
 		
 		FB.Facebook.apiClient.fql_query(myQuery, function cuViewLogin (rows) {
					myName = rows[0].name;
  				myAvatar= rows[0].pic_square;
  				myFBLink= rows[0].profile_url;
			  
  
  					//alert("Hello, " + rows[0].name + ", your pic is here: " + rows[0].pic_square);
 
 
					  var postString = "{uid:'" + myFBUID + "',myname:'" + myName + "',avatar:'" + myAvatar + "',fblink:'" + myFBLink + "'}";
   					var loginRedirect = '/belvoirfcu/fb_login.php';
   					//alert(postString);
   					// postString value is:  {uid:'799071309',myname:'Jason Green',avatar:'http://profile.ak.fbcdn.net/v230/143/49/q799071309_9548.jpg',fblink:'http://www.facebook.com/profile.php?id=799071309'}
   					//postwith(loginRedirect,postString);
   					
   					//WORKS: postwith('posttest.php', {uid:'799071309',myname:'Jason Green',avatar:'http://profile.ak.fbcdn.net/v230/143/49/q799071309_9548.jpg',fblink:'http://www.facebook.com/profile.php?id=799071309'});
   					postwith(loginRedirect, {uid:myFBUID,myname:myName,avatar:myAvatar,fblink:myFBLink});
   					
   // postwith('posttest.php',{user:'peter',cc:'aus'});
   //postwith('posttest.php',{uid:myFBUID,myname:myName,avatar:myAvatar,fblink:myFBLink});
   //postwith(loginRedirect,postString);
   //alert(loginRedirect);
   					//alert(postString);
   // <a href="javascript:postwith('post.aspx',{user:'peter',cc:'aus'})">click</a>

   // self.location = "http://www.cuview.com/belvoirfcu"     ;
    //NEED TO SOMEHOW CALL AVATAR UPDATE AND PHP LOGIN FUNCTION and LAST LOGIN??
    //DO WE NEED TO SET CUVIEW USER ID SOMEHOW?
    //ONLY WAY I CAN FIGURE IS WITH JQUERY OR IMG SRCING A PHP FUNCTION

  
		}
 		
 		
 		
 		
 		
 		
 		);
    
  }//end auth_using_fb function
    
 
function update_user_box(){


 		var user_box = document.getElementById("user"); 
	
 		if((user_box != 'undefined') && !(null === user_box)){
		user_box.innerHTML = "<span>" + 
											 "<fb:profile-pic uid='loggedinuser' facebook-logo='true' size='square'></fb:profile-pic>" + 
											 "Welcome, <fb:name uid='loggedinuser' useyou='false'></fb:name>. We are now logging you in with your Facebook account. <b>If this message persists, you may need to reload this page!</b>" + 
											 "</span>"; 
											 // because this is XFBML, we need to tell Facebook to re-process the document 
											 FB.XFBML.Host.parseDomTree(); 
		} //end if user_box exists
 		
}



 
 
 function postwith (to,p) {
 	//from http://mentaljetsam.wordpress.com/2008/06/02/using-javascript-to-post-data-between-pages/
  var myForm = document.createElement("form");
  myForm.method="post" ;
  myForm.action = to ;
  myForm.target = "_self";
  for (var k in p) {
    var myInput = document.createElement("input") ;
    myInput.setAttribute("name", k) ;
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput) ;
  }
  document.body.appendChild(myForm) ;
  myForm.submit() ;
  //document.body.removeChild(myForm) ;
}

// To insert a link into a page just use a normal anchor tag in the HTML and call the function.
// <a href="javascript:postwith('post.aspx',{user:'peter',cc:'aus'})">click</a>
 
 
 
 function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
