﻿//Stir.com 2009 please do no use or distribute this code without permission from Stir.com

function rand(n)
{
    return (Math.floor(Math.random() * n + 1));
}

function genPassword()
{
    var a1 = new Array(); var animal = 'aardvark,addax,alligator,alpaca,anteater,antelope,armadillo,badger,bat,bear,bighorn,bison,buffalo,bull,bunny,burro,camel,canary,cat,chameleon,cheetah,chimpanzee,chinchilla,chipmunk,colt,cougar,cow,coyote,crocodile,crow,deer,dingo,doe,dog,donkey,duckbill,elephant,elk,fawn,ferret,finch,fish,fox,frog,gazelle,monster,giraffe,gnu,goat,gopher,gorilla,grizzly,bear,hog,guinea,hamster,hare,hedgehog,horse,hyena,ibex,iguana,impala,jackal,jaguar,kangaroo,kid,koala,koodoo,lamb,lemur,leopard,lion,lizard,llama,lovebird,lynx,mandrill,mare,marmoset,marten,mink,mole,mongoose,monkey,moose,mouse,mule,musk,deer,muskox,muskrat,mustang,bird,newt,ocelot,okapi,opossum,orangutan,oryx,otter,ox,panda,panther,parakeet,parrot,platypus,polar,bear,pony,porcupine,porpoise,prairie,dog,pronghorn,puma,puppy,quagga,rabbit,raccoon,ram,rat,reindeer,reptile,rhino,roebuck,salamander,seal,sheep,shrew,fox,sloth,snake,squirrel,stallion,steer,tapir,tiger,toad,turtle,walrus,waterbuck,weasel,whale,wildcat,wolf,wolverine,wombat,woodchuck,yak,zebra';
    a1 = animal.split(",");

    var a2 = new Array(); var color = 'red,green,blue,orange,brown,purple,pink,yellow,white,black,grey,silver,teal,lime,violet,rose,tan,gold';
    a2 = color.split(",");

    return a2[rand(18) - 1] + a1[rand(150) - 1];
}

function getDOB()
{
    var dt = new Date();
    dt.setFullYear(dt.getFullYear() - 99);
    return dt;
}

function retrieveUserFBData()
{
    //<![CDATA[ 	

    var api_key = '1aecf47a47944350d4f71e9125873402';
    var channel_path = 'xd_receiver.htm';
    FB_RequireFeatures(["Api"],
    function()
    {
        var api;
        var uid;
        FB.Facebook.init(api_key, channel_path);

        FB.ensureInit(
        function()
        {
            FB.Connect.get_status().waitUntilReady(
	        function(status)
	        {
	            switch (status)
	            {
	                case FB.ConnectState.connected:
	                    loggedIn = true;



	                    api = FB.Facebook.apiClient;
	                    uid = api.get_session().uid;
	                    api.users_hasAppPermission('email',
	                    function(result, ex)
	                    {
	                        if (!result)
	                        {
	                            FB.Connect.showPermissionDialog("email", function()
	                            {
	                                api.users_hasAppPermission('offline_access',
				        		    function(result, ex)
				        		    {
				        		        if (!result)
				        		        {
				        		            FB.Connect.showPermissionDialog("offline_access",
							                function()
							                {
							                    $('#divWaitingForFBReg').dialog('open');
							                    retrieveFBInfoAndPost();
							                });
				        		        }
				        		        else
				        		        {
				        		            $('#divWaitingForFBReg').dialog('open');
				        		            retrieveFBInfoAndPost();
				        		        }
				        		    });
	                            });
	                        }
	                        else
	                        {
	                            $('#divWaitingForFBReg').dialog('open');
	                            retrieveFBInfoAndPost();
	                        }
	                    });



	                    break;

	                case FB.ConnectState.appNotAuthorized:
	                case FB.ConnectState.userNotLoggedIn:
	                    FB.Connect.requireSession();
	                    loggedIn = false;
	                    $('#divWaitingForFBReg').dialog('close');
	                    break;
	            }
	        });
        });

        function retrieveFBInfoAndPost()
        {
            api.users_getInfo([uid], ['name', 'first_name', 'last_name', 'birthday', 'current_location', 'hometown_location', 'proxied_email', 'contact_email', 'relationship_status', 'sex', 'activities', 'movies', 'music', 'interests', 'books', 'tv', 'pic_square', 'pic', 'pic_big', 'meeting_sex', 'about_me'],
                function(result, ex)
                {
                    var userInfo = result[0];
                    var currentLocation = userInfo.current_location != undefined && userInfo.current_location != null ? userInfo.current_location : (userInfo.hometown_location != undefined && userInfo.hometown_location != null ? userInfo.hometown_location : new Array("nada", "nada", "nada"));

                    var fbAboutMe = (userInfo.about_me != undefined && userInfo.about_me != null && userInfo.about_me.length > 0 ? userInfo.about_me : "");
                    var fbActivities = (userInfo.activities != undefined && userInfo.activities != null && userInfo.activities.length > 0 ? userInfo.activities : "");
                    var fbInterests = (userInfo.interests != undefined && userInfo.interests != null && userInfo.interests.length > 0 ? userInfo.interests : "");
                    fbInterests = (fbInterests.length > 0 ? (fbInterests + ", " + fbActivities) : fbActivities);
                    var fbMusic = (userInfo.music != undefined && userInfo.music != null && userInfo.music.length > 0 ? userInfo.music : "");
                    var fbShows = (userInfo.tv != undefined && userInfo.tv != null && userInfo.tv.length > 0 ? userInfo.tv : "");
                    var fbMovies = (userInfo.movies != undefined && userInfo.movies != null && userInfo.movies.length > 0 ? userInfo.movies : "");
                    var fbBooks = (userInfo.books != undefined && userInfo.books != null && userInfo.books.length > 0 ? userInfo.books : "");

                    var fbPhotoPath = (userInfo.pic_big != undefined && userInfo.pic_big != null && userInfo.pic_big.length > 0 ? userInfo.pic_big : "");
                    var fbZip = (currentLocation.zip != undefined && currentLocation.zip != null ? currentLocation.zip : "");
                    var fbCity = (currentLocation.city != undefined && currentLocation.city != null ? currentLocation.city : "");
                    var fbState = (currentLocation.state != undefined && currentLocation.state != null ? currentLocation.state : "");
                    var fbCountry = (currentLocation.country != undefined && currentLocation.country != null ? currentLocation.country : "");
                    var fbFirstName = userInfo.first_name;
                    var fbLastName = userInfo.last_name;
                    var dateString = userInfo.birthday;
                    var fbBirthday = new Date(dateString);
                    if (fbBirthday == undefined || fbBirthday == null || fbBirthday == "Invalid Date")
                    {
                        fbBirthday = getDOB();
                    }

                    fbBirthday = (fbBirthday.getMonth() + 1) + "-" + fbBirthday.getDate() + "-" + fbBirthday.getFullYear();

                    var fbHandle = userInfo.first_name + userInfo.last_name.substring(0, 1);
                    var fbPassword = genPassword();
                    var Primary = userInfo.contact_email;
                    var Proxied = userInfo.proxied_email;
                    var fbEmail = Primary != null && Primary.length > 0 ? Primary : Proxied;
                    var fbGender = userInfo.sex;
                    var fbGenderPref;
                    if (fbGender != undefined && fbGender != null)
                    {
                        if (fbGender.toString().length == 0 || fbGender == "male")
                        {
                            fbGender = "1";
                            fbGenderPref = "2";
                        }
                        else if (fbGender == "female")
                        {
                            fbGender = "2";
                            fbGenderPref = "1";
                        }
                    }
                    else
                    {
                        fbGender = "1";
                        fbGenderPref = "2";
                    }

                    if (userInfo.meeting_sex != undefined && userInfo.meeting_sex != null && userInfo.meeting_sex.length > 0)
                    {
                        if (userInfo.meeting_sex.toString().indexOf(",") != -1)
                        {
                            fbGenderPref = (fbGender == "1" ? "2" : "1");
                        }
                        else
                        {
                            fbGenderPref = (userInfo.meeting_sex.toString() == "male" ? "1" : "2");
                        }
                    }

                    var fbRelationshipStatus = userInfo.relationship_status != undefined && userInfo.relationship_status != null && userInfo.relationship_status.length > 0 ? userInfo.relationship_status : "";
                    if (fbRelationshipStatus != undefined && fbRelationshipStatus != null && fbRelationshipStatus.length > 0)
                    {
                        if (fbRelationshipStatus == "Widowed")
                        {
                            fbRelationshipStatus = "Single";
                        }
                        if (fbRelationshipStatus == "Engaged" || fbRelationshipStatus == "It's Complicated" || fbRelationshipStatus == "In an Open Relationship")
                        {
                            fbRelationshipStatus = "In a Relationship";
                        }
                    }

                    if ((fbZip == null || fbZip == undefined || fbZip.length == 0) && (fbCity.length > 0 && fbState.length > 0 && fbCountry.length > 0))
                    {
                        fbZip = fbCountry + "," + fbState + "," + fbCity;
                    }

                    //		                alert("Activities: " + userInfo.activities + "\r\n"
                    //		                + "Movies: " + userInfo.movies + "\r\n"
                    //		                + "Music: " + userInfo.music + "\r\n"
                    //		                + "Interests: " + fbInterests + "\r\n"
                    //		                + "Books: " + userInfo.books + "\r\n"
                    //		                + "TV Shows: " + userInfo.tv + "\r\n"
                    //		                + "Gender preference: " + fbGenderPref + "\r\n"
                    //		                + "Relationship status: " + fbRelationshipStatus + "\r\n"
                    //		                + "200 by 600 profile pic url: " + userInfo.pic_big + "\r\n"
                    //		                + "Interested in: " + userInfo.meeting_sex + "\r\n"
                    //		                + "About me: " + userInfo.about_me + "\r\n"
                    //		                + "Gender: " + fbGender + "\r\n"
                    //		                + "Gender pref: " + fbGenderPref + "\r\n"
                    //		                + "Email: " + fbEmail);

                    asyncRegister('reg', fbHandle, fbEmail, fbPassword, fbGender, fbGenderPref, fbZip, fbBirthday, fbPhotoPath, fbRelationshipStatus, fbAboutMe, fbInterests, fbMusic, fbShows, fbMovies, fbBooks, uid);

                });
        }

    });
    //]]>
}

function asyncRegister(action, handle, email, password, gender, genderpref, postalcode, birthday, imagepath, relationshipstatus, fbaboutme, fbinterests, fbmusic, fbshows, fbmovies, fbbooks, fbuserid)
{
    $.post(window.location.protocol + "//" + window.location.host + "/asyncReg.aspx",
    {
        action: action,
        handle: handle,
        email: email,
        password: password,
        gender: gender,
        genderpref: genderpref,
        postalcode: postalcode,
        birthday: birthday,
        imagepath: imagepath,
        relationshipstatus: relationshipstatus,
        fbaboutme: fbaboutme,
        fbinterests: fbinterests,
        fbmusic: fbmusic,
        fbshows: fbshows,
        fbmovies: fbmovies,
        fbbooks: fbbooks,
        fbuserid: fbuserid,
        fbphotoalbumid: fbAlbumID,
        requestquerystring: window.location.href.toLowerCase()
    },
    function(data)
    {
        $('.requestMessage').text(data.toString());

        if (data.toString().substring(0, 5) == 'http:')
        {
            window.location = data.toString();
        }
        else
        {
            window.location = window.location;
        }

    });
}

var fbAlbumID = "";
function setProfilePhotoAlbumID(fbPhotoAlbumID)
{
    fbAlbumID = fbPhotoAlbumID;
}

