var flashWidth;
function flashHide()
{
    var tdiv = document.getElementById('flashcontent');
    flashWidth = tdiv.style["width"];
    tdiv.style["width"] = 0 + 'px';
}

function flashShow()
{
    if (flashWidth == undefined)
        return;
    var tdiv = document.getElementById('flashcontent');
    tdiv.style["width"] = flashWidth;
}

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);
}

function OpenInviteWindow(personId,profileId)
{
  Modalbox.show('', $('lightbox_home') + 'invite/custom/'+profileId+'/0?fromFlash=1', {width: 720, height:50, slideDownDuration: 0, onShow: flashHide, afterHide: flashShow});
  ActionScriptInvoke("SetPersonFlags");
  //ActionScriptInvoke("SetPersonFlags", personId, true, true, false); // args : personId, invited, personEmail, personDoNotHaveEmail
  //ActionScriptInvoke("SetPersonFlags");  //does nothing but just hides the loading progress...
}

function SetInviteResult(personId, result)
{
  if(result==1)
  {
    ActionScriptInvoke("SetPersonFlags", personId, true, true, false, true); // args : personId, invited, personEmail, personDoNotHaveEmail, personInvitePopup
  }
  else
  {
    ActionScriptInvoke("SetPersonFlags");  //does nothing but just hides the loading progress...
  }
}

function SetNoEmail(personId,profileId)
{
    parameters = $H({profileId: profileId});

    new Ajax.Request($('lightbox_home') + 'set_show_invite', {
        asynchronous : false,
        onSuccess : function(transport) {
          ActionScriptInvoke("SetPersonFlags", personId, false, false, false, false); // args : personId, invited, personEmail, personDoNotHaveEmail, personInvitePopup
        },
        onFailure :  proceedError,
        parameters : parameters
    });

}

function AddPhoto(profileId)
{

    $('lightbox_add_photo').href = $('lightbox_add_photo').href.sub(/[0-9]+$/, profileId);
    Modalbox.show('', $('lightbox_add_photo').href,
                  {width: 600, height:50, slideDownDuration: 0, onShow: flashHide, afterHide: flashShow});
    ActionScriptInvoke("SetPersonFlags");
//    lightBox = new lightbox($('lightbox_add_photo'));
//    flashHide();
    ActionScriptInvoke("SetTag", "skipCenterPerson", "true");
    lightBox.activate();

}

function CreateTree(first_name, last_name, email, isMale)
{

    if (isMale.toString() == "true") {
        gender = "m";
    } else {
        gender = "f";
    }

    parameters = $H({first_name: first_name, last_name: last_name, email: email, gender: gender});

    new Ajax.Request($('lightbox_home') + 'create', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/create');
            window.location.href = "/";
//            ViewTree(transport.responseText, transport.responseText);
//            ActionScriptInvoke("ShowHints");
//            new Ajax.Updater($('pageHeader'), $('lightbox_home') + 'getHeader');
//            new Ajax.Updater($('conversion_tracking'), $('lightbox_home') + 'trackCreate', {asynchronous: false, evalScripts: true});
        },
        onFailure :  proceedError,
        parameters : parameters
    });

}

function ViewProfile(profileId, toProfilePage)
{
  if(window.viewPublicTree == true)
  {
    new Ajax.Request($('lightbox_home') + '/tree/checkProfileAccess/' +profileId, {
          asynchronous : false,
          onSuccess : function(){
                track('/tree/checkProfileAccess');
                if(toProfilePage)
                  window.location = $('lightbox_home') + 'profileView/' + profileId;
                else
                  window.location = $('lightbox_home') + 'profileViewSubTab/'+ profileId + '/4/2';
          },
          onFailure:  function(){ ActionScriptInvoke("ShowMsg","", window.profileAccessError); }
    });
  }
  else
  {
    if(toProfilePage)
      window.location = $('lightbox_home') + 'profileView/' + profileId;
    else
      window.location = $('lightbox_home') + 'profileViewSubTab/'+ profileId + '/4/2';
  }
}

function CheckEmail(email,delegateName)
{
    //ActionScriptInvoke(delegateName,'true');

    parameters = $H({email: email});
    async = delegateName != ""; // if delegateName is set then the checkEmail must be asynchronous,
                                // otherwise must return the result

    new Ajax.Request($('lightbox_home') + 'checkEmail', {
        asynchronous : async,
        method : 'get',
        onSuccess : function(transport) {
            track('/checkEmail');
            toReturn = transport.responseText;
            if (async)
              ActionScriptInvoke(delegateName,toReturn);
        },
        onFailure : proceedError,
        parameters: parameters
    });

    return toReturn;

}

function DeletePerson(personId, profileId,confirmed)
{
    parameters = $H({profile: profileId, person: personId, deleteConfirmed: confirmed});


    new Ajax.Request($('lightbox_home') + 'tree/delPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
           if (isNaN(transport.responseText) == false)
           {
              track('/tree/delPerson');
              ActionScriptInvoke("Reload");
           }
           else
           {
             ActionScriptInvoke("ShowMsg","",transport.responseText);
           }
        },
        onFailure: proceedError,
        parameters : parameters
    });
}

function SendInvite(profileId, personId)
{

    parameters = $H({profile: profileId});

    new Ajax.Request($('lightbox_home') + 'flashInvite', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/flashInvite');
            if (isNaN(personId) == false) {
                ActionScriptInvoke("OnLoadCenterPerson", personId);
            }
            ActionScriptInvoke("Reload");
        },
        onFailure: proceedError,
        parameters : parameters
    });
}

function SaveEmail(profileId, email, invite, personId, formText)
{

    parameters = $H({profile: profileId, email: email, invite: invite});

    new Ajax.Request($('lightbox_home') + 'addEmail', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/addEmail');
            if (isNaN(personId) == false) {
                ActionScriptInvoke("OnLoadCenterPerson", personId);
            }
            ActionScriptInvoke("Reload");
        },
        onFailure: proceedError,
        parameters : parameters
    });
}


function EditProfile(profileId, personId)
{
  window.location = '/profileEditInfo/'+profileId;
}

function ViewTree(personid, profileId)
{
  var url;
  if(window.viewPublicTree == true)
    url = $('lightbox_home') + "tree/getPublicXMLTree/" + profileId+"/notime";
  else
    url = $('lightbox_home') + "tree/getXMLTree/" + profileId+"/notime";

  ActionScriptInvoke("Reload", profileId, url);
}

var reloadTimeout = 2000;
function AddAsChild(partnershipId, first_name, maiden_name, last_name, gender, is_live, email, invite, senderPersonID,
                      bdate,bmoth,byear,bcirca,ddate,dmonth,dyear,dcirca,viewHidden,senderProfileID)
{

  parameters = $H({partnership_id: partnershipId, first_name: first_name, maiden_name: maiden_name, last_name: last_name, gender: gender,
  is_live: is_live, email: email, invite: invite, add_as: 'child', birth_day: bdate, birth_month: bmoth, birth_year: byear,
      birth_circa:  bcirca,death_day: ddate, death_month: dmonth, death_year: dyear, death_circa: dcirca});

    new Ajax.Request($('lightbox_home') + 'tree/addPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/tree/addPerson');

            if (isNaN(transport.responseText) == false) {
                if(viewHidden != "true")
                {
                	setTimeout('reloadAfterAdd("'+transport.responseText+'", "'+senderPersonID+'")', reloadTimeout);
                }
                else
                {
                    ActionScriptInvoke("OnLoadCenterPerson", transport.responseText);
                    ActionScriptInvoke("OnLoadCenterPerson2", senderPersonID);
                    ViewTree(senderPersonID, senderProfileID);
                }
            }
            else
              ActionScriptInvoke("ShowMsg","",transport.responseText,true);
        },
        onFailure: proceedError,
        parameters : parameters
    });

}

function AddAsChildEx(personId, profileId, first_name, maiden_name, last_name, gender, is_live, email, invite,bdate,bmoth,byear,bcirca,ddate,dmonth,dyear,dcirca,
                        viewHidden)
{

  parameters = $H({person_id: personId, profile_id: profileId, first_name: first_name, maiden_name: maiden_name, last_name: last_name,
  gender: gender, is_live: is_live, email: email, invite: invite, add_as: 'child_only',
  birth_day: bdate, birth_month: bmoth, birth_year: byear, birth_circa:  bcirca,death_day: ddate, death_month: dmonth, death_year: dyear, death_circa: dcirca});

    new Ajax.Request($('lightbox_home') + 'tree/addPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/tree/addPerson');
            if (isNaN(transport.responseText) == false) {
                if(viewHidden != "true")
                {
                  setTimeout('reloadAfterAdd("'+transport.responseText+'", "'+personId+'")', reloadTimeout);
                }
                else
                {
                    ActionScriptInvoke("OnLoadCenterPerson", transport.responseText);
                    ActionScriptInvoke("OnLoadCenterPerson2", personId);
                    ViewTree(personId,profileId);
                }
            }
            else
              ActionScriptInvoke("ShowMsg","",transport.responseText,true);
        },
        onFailure: proceedError,
        parameters : parameters
    });

}

function AddAsSpouse(personId, profileId, first_name, maiden_name, last_name, gender, is_live, email, invite, partnership_type,
                      bdate,bmoth,byear,bcirca,ddate,dmonth,dyear,dcirca,mdate,mmonth,myear,mcirca)
{

  parameters = $H({person_id: personId, profile_id: profileId, first_name: first_name, maiden_name: maiden_name,last_name: last_name, gender: gender,
      is_live: is_live, email: email, invite: invite, partnership_type: partnership_type, add_as: 'partner',
      birth_day: bdate, birth_month: bmoth, birth_year: byear, birth_circa: bcirca, death_day: ddate, death_month: dmonth, death_year: dyear, death_circa: dcirca,
      marr_day: mdate, marr_month: mmonth, marr_year: myear, marr_circa: mcirca});

    new Ajax.Request($('lightbox_home') + 'tree/addPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/tree/addPerson');
            if (isNaN(transport.responseText) == false) {
                  setTimeout('reloadAfterAdd("'+transport.responseText+'", "'+personId+'")', reloadTimeout);
            }
            else
              ActionScriptInvoke("ShowMsg","",transport.responseText,true);
        },
        onFailure: proceedError,
        parameters : parameters
    });

}

function AddAsParent(personId, profileId, first_name, maiden_name, last_name, gender, is_live, email, invite,bdate,bmoth,byear,bcirca,ddate,dmonth,dyear,dcirca)
{

  parameters = $H({person_id: personId, profile_id: profileId, first_name: first_name, maiden_name: maiden_name, last_name: last_name, gender: gender,
      is_live: is_live, email: email, invite: invite, add_as: 'parent', birth_day: bdate, birth_month: bmoth, birth_year: byear,
      birth_circa:  bcirca, death_day: ddate, death_month: dmonth, death_year: dyear, death_circa: dcirca});

    new Ajax.Request($('lightbox_home') + 'tree/addPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/tree/addPerson');
            if (isNaN(transport.responseText) == false) {
                  setTimeout('reloadAfterAdd("'+transport.responseText+'", "'+personId+'")', reloadTimeout);
            }
            else
              ActionScriptInvoke("ShowMsg","",transport.responseText,true);
        },
        onFailure: proceedError,
        parameters : parameters
    });

    //	ActionScriptInvoke("Reload");
}


function AddAsProfile(personId, first_name, maiden_name, last_name, gender, is_live, email, invite,bdate,bmoth,byear,bcirca,ddate,dmonth,dyear,dcirca)
{

  parameters = $H({person_id: personId, first_name: first_name, maiden_name: maiden_name, last_name: last_name, gender: gender, is_live: is_live,
      email: email, invite: invite, add_as: 'profile', birth_day: bdate, birth_month: bmoth, birth_year: byear,
      birth_circa:  bcirca,death_day: ddate, death_month: dmonth, death_year: dyear, death_circa: dcirca});

    new Ajax.Request($('lightbox_home') + 'tree/addPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/tree/addPerson');
            if (isNaN(transport.responseText) == false) {
                setTimeout('reloadAfterAdd("'+transport.responseText+'", "'+personId+'")', reloadTimeout);
            }
            else
              ActionScriptInvoke("ShowMsg","",transport.responseText,true);
        },
        onFailure: proceedError,
        parameters : parameters
    });

}

function AddAsSibling(personId, profileId, first_name, maiden_name, last_name, gender, is_live, email, invite,bdate,bmoth,byear,bcirca,ddate,dmonth,dyear,dcirca)
{

  parameters = $H({person_id: personId, profile_id: profileId, first_name: first_name, maiden_name: maiden_name, last_name: last_name, gender:
          gender, is_live: is_live, email: email, invite: invite, add_as: 'sibling',
          birth_day: bdate, birth_month: bmoth, birth_year:  byear, birth_circa:  bcirca, death_day: ddate,
          death_month: dmonth, death_year: dyear, death_circa: dcirca});

    new Ajax.Request($('lightbox_home') + 'tree/addPerson', {
        asynchronous : false,
        onSuccess : function(transport) {
            track('/tree/addPerson');
            if (isNaN(transport.responseText) == false) {
                  setTimeout('reloadAfterAdd("'+transport.responseText+'", "'+personId+'")', reloadTimeout);
            }
            else
              ActionScriptInvoke("ShowMsg","",transport.responseText,true);
        },
        onFailure: proceedError,
        parameters : parameters
    });

    //	ActionScriptInvoke("Reload");
}

function reloadAfterAdd(newId, personId)
{
	ActionScriptInvoke("OnLoadCenterPerson", newId);
  // bellow is set when the added parent is hidden ( added to a spouse with no blood relation to the base person )
  ActionScriptInvoke("OnLoadCenterPerson2", personId);
  ActionScriptInvoke("Reload");
}

function redirect(url)
{
  if (url.indexOf('/') == 0)
    url = url.substring(1);

  window.location = $('lightbox_home')+url;
}

function showHowItWorks()
{
  lightBox = new lightbox($('lightbox_how_works'));
  flashHide();
  lightBox.activate();
}
