function positionInfo(object) {

  var p_elm = object;

  this.getElementLeft = getElementLeft;
  function getElementLeft() {
    var x = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      x+= elm.offsetLeft;
      elm = elm.offsetParent;
    }
    return parseInt(x);
  }

  this.getElementWidth = getElementWidth;
  function getElementWidth(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetWidth);
  }

  this.getElementRight = getElementRight;
  function getElementRight(){
    return getElementLeft(p_elm) + getElementWidth(p_elm);
  }

  this.getElementTop = getElementTop;
  function getElementTop() {
    var y = 0;
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    while (elm != null) {
      y+= elm.offsetTop;
      elm = elm.offsetParent;
    }
    return parseInt(y);
  }

  this.getElementHeight = getElementHeight;
  function getElementHeight(){
    var elm;
    if(typeof(p_elm) == "object"){
      elm = p_elm;
    } else {
      elm = document.getElementById(p_elm);
    }
    return parseInt(elm.offsetHeight);
  }

  this.getElementBottom = getElementBottom;
  function getElementBottom(){
    return getElementTop(p_elm) + getElementHeight(p_elm);
  }
}

function trim(stringToTrim) 
{
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}


function setElementProperty(p_property, p_value, p_elmId)
{
  var p_elm = p_elmId;
  var elm = null;

  if(typeof(p_elm) == "object"){
    elm = p_elm;
  } else {
    elm = document.getElementById(p_elm);
  }
  if((elm != null) && (elm.style != null)){
    elm = elm.style;
    elm[ p_property ] = p_value;
  }
}
  

function createFormDiv(currentFieldId, html, width, height, xOffset, yOffset)
{
  //alert(currentFieldId + len + width);
  htmlInner = "<img src='/images/closePopup.gif' onclick='javascript:hidePopup();' style=\"position:absolute;left:"+(width-11)+"px; top:"+2+"px\">" + html;
  
  var formDivId = "PopupFormDiv";
  if(document.getElementById)
  {
    var formDiv = document.getElementById(formDivId);
    
    formDiv.innerHTML = htmlInner;
    //formDiv.style.display = '';
    setElementProperty('display', 'block', formDivId);

    var fieldPos = new positionInfo(currentFieldId);
  
    var x = (fieldPos.getElementLeft() + fieldPos.getElementRight() - width)/2 + xOffset;
    var y = fieldPos.getElementBottom() + yOffset;
    //alert(x+ " "+ y);
    setElementProperty('left', x + "px", formDivId);
    setElementProperty('top', y + "px", formDivId);
    if(width!=0)
    {
      setElementProperty('width', width + "px", formDivId);
    }
    if(height!=0)
    {
      setElementProperty('height', height + "px", formDivId);
    }  
  }
}

function blurrBackground()
{
  var fieldPos = new positionInfo(document.body);
  wrapperDiv = document.getElementById('PopupWrapperDiv');
  setElementProperty('display', 'block', wrapperDiv);
  setElementProperty('height', fieldPos.getElementHeight()+"px", wrapperDiv);
  setElementProperty('width', fieldPos.getElementWidth()+"px", wrapperDiv);  
  
  setElementProperty('left', '0', wrapperDiv);
  setElementProperty('top', '0', wrapperDiv);
}

function hidePopup()
{
  var formDiv = document.getElementById('PopupFormDiv');
  setElementProperty('display', 'none', formDiv);

  
  wrapperDiv = document.getElementById('PopupWrapperDiv');
  setElementProperty('display', 'none', wrapperDiv);
  setElementProperty('width', '0px', wrapperDiv); 
  setElementProperty('height', '0px', wrapperDiv); 
}

function callUser(currentFieldId, user, lets101No, callId)
{
  html = '\
  <table class=popupTable width=100%> \
    <tr><td colspan=2 class="popupTd bottomSpc">\
      To Call <b>' + user + '</b></td></tr>\
      <tr><td class="popupTd instrMsg">1)</td><td class="popupTd instrMsg">Dial <b>'+ lets101No + '</b>, from your registered phone</td></tr>\
      <tr><td class="popupTd instrMsg">2)</td><td class="popupTd instrMsg" >Press <b>1</b> on the Main menu</td></tr>\
      <tr><td class="popupTd instrMsg">3)</td><td class="popupTd instrMsg" >Enter <b>' + callId + '</b>, followed by the <b>#</b> key </td></tr>\
      <tr><td colspan=2 class="popupTd topSpc">  You will be instantly connected.</td></tr>\
      <tr><td colspan=2 class="popupTd topSpc comment instrMsg">(At the moment this feature is available only in US, UK and Canada)</td></tr>\
  </table>';

  createFormDiv(currentFieldId, html, 395, 0, 0, 2);
}

function emailUser(currentFieldId, to_user, to_user_seq, rand)
{
  html = '\
  <form name="emailUserForm" id="emailUserForm" ><table class=popupTable width=100%> \
    <tr><td class=popupTd>\
      To: </td><td class=popupTd>\
      <b>' + to_user + '</b>\
    </td></tr>\
    <tr><td class=popupTd >\
      Subject: </td><td class=popupTd>\
      <input type=text name=subject maxlength="120" style="width:400px;"/>\
    </td></tr>\
    <tr><td class=popupTd colspan=2>\
      <textarea name="msg" style="width:488px;height:120px"></textarea>\
      <input type=hidden name=sessid value="'+ rand +'"/>\
      <input type=hidden name=who value="'+ to_user +'"/>\
      <input type=hidden name=who_seq value="'+ to_user_seq +'"/>\
      <input type=hidden name=sendEmailMsg value="1"/>\
      <input type=hidden name=formSubmitted value="1"/>\
    </td></tr>\
    <tr><td class=popupTd colspan=2 align=center>\
      <input type=button class=cuteButton value="Send" name=send onclick="javascript:submitform(\'emailUserForm\',\'buddies/express_interest_page\',\'emailUserForm\');">\
    </td></tr>\
  </table></form>';
  
  createFormDiv(currentFieldId, html, 500, 0, 0, 2);
}

function referFriend(currentFieldId, isloggedIn, fname, profile_user, him_her)
{
  html = '\
  <form name="referFriendForm" id="referFriendForm" ><table class=popupTable width=100%>';
  if(isloggedIn != 1)
  {
    html = html + '    <tr><td class=popupTd>\
                Your Name\
              </td></tr>\
              <tr><td class="popupTd bottomSpc">\
                <input type=text name=sender_fname maxlength="50" style="width:200px;"/>\
              </td></tr>';
  }
  else
  {
    html = html + '<input type=hidden name=sender_fname value="'+fname+'"/>';
  }
  html = html + '\
    <tr><td class=popupTd>\
      Email ids of your friends\
    </td></tr>\
    <tr><td class=popupTd>\
      <input type=text name=email1 maxlength="50" style="width:200px;"/>\
    </td></tr>\
    <tr><td class=popupTd>\
      <input type=text name=email2 maxlength="50" style="width:200px;"/>\
    </td></tr>\
    <tr><td class=popupTd>\
      <input type=text name=email3 maxlength="50" style="width:200px;"/>\
    </td></tr>\
    <tr><td class=popupTd align=center>\
      <input type=hidden name=formSubmitted value="1"/>\
      <input type=hidden name=profile_user value="'+profile_user+'"/>\
      <input type=hidden name=him_her value="'+him_her+'"/>\
      <input type=hidden name=referToFriends value="1"/>\
      <input type=button class=cuteButton value="Send" name=send onclick="javascript:submitform(\'referFriendForm\',\'profile/referFriend\',\'referFriendForm\');">\
    </td></tr>\
  </table></form>';
  
  createFormDiv(currentFieldId, html, 212, 0, 0, 2);
}


function referLoginForm(addrBookType, currentFieldId, quizid)
{
  var emailTxt;
  if(addrBookType == 'gmail')
  {
    emailTxt = "Gmail";
  }
  else if(addrBookType == 'yahoo')
  {
    emailTxt = "Yahoo!";
  }
  else if(addrBookType == 'aol')
  {
    emailTxt = "Aol";
  }
  else if(addrBookType == 'msn')
  {
    emailTxt = "Hotmail or Msn";
  }    
  html = '\
  <form name=emailForm id=emailForm><table border=0  class=popupInviteTable width=100%>\
    <tr><td align=center class=popupInviteTd colspan=2>\
      <h3 class=addressBookMsg>Please enter the following information.</h3> We will fetch your address book and then you can select your friends\' email addresses to invite them. \
    </td></tr>\
    <tr><td colspan=2>&nbsp;</td></tr>\
    <tr><td align=center id=emailErr class="popupInviteTd commonErrMsg boldtxt" colspan=2></td></tr>\
    <tr><td align=center>\
        <img src="/images/invite/'+addrBookType+'_small.png" />\
      </td>\
      <td class=popupInviteTd>\
        '+emailTxt+' email address<br/>\
        <input type=\"text\" class=tdValInput id=\"username\" name=\"username\" maxlength=\"80\" />\
        <br/>'+emailTxt+' password<br/>\
        <input type=\"password\" class=tdValInput id=\"password\" name=\"password\" maxlength=\"80\" />\
        <br/><br/> \
        <input type=\"hidden\" name=\"emailType\" maxlength=\"80\" value=\"'+addrBookType+'\" />\
        <input type=\"hidden\" name=\"quizid\" maxlength=\"80\" value=\"'+quizid+'\" />\
        <input type=\"hidden\" name=\"emailFormFetch\" maxlength=\"80\" value=\"1\" />\
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type=button class=cuteInviteButton value="Continue" onclick="javascript:submitFormParseErr(\'emailForm\', \'emailErr\', \'/my/invites/fetchAddressBook\', \'emailForm\')" />\
    </td></tr>\
  </table></form><br/>\
  <table border=0  class=popupInviteTable width=100%>\
    <tr><td align=left class=popupInviteTd>\
     <ul>\
     <li>We hate spam as much as you do.\
     <li>We never store your email address or password and never share it with anyone. \
     <li>Also we never store your friends\' email addresses and never share them with anyone. </ul>\
    </td></tr>\
  </table><br/>';
  
  createFormDiv(currentFieldId, html, 412, 0, 0, 2);
}

function referFriendWithAddrBook(currentFieldId, isloggedIn, fname, quizid)
{
  html = '\
  <table border=0  class=popupInviteTable width=100%>\
    <tr><td colspan=2 align=center>\
       Select Your Friends\' Email Addresses From Address Book of:\
    </td></tr>     \
    <tr><td align=center class=inviteImgTd>\
       <img src="/images/invite/gmail_small.png" /> <br/> <input type=button class=cuteInviteButton value="Select" name=Select onclick="javascript:referLoginForm(\'gmail\',\''+currentFieldId+'\',\''+quizid+'\');"/>\
      </td>\
      <td align=center class=inviteImgTd>\
      <img src="/images/invite/yahoo_small.png" />  <br/> <input type=button class=cuteInviteButton value="Select" name=Select onclick="javascript:referLoginForm(\'yahoo\',\''+currentFieldId+'\',\''+quizid+'\');"/>\
    </td></tr>\
    <tr><td align=center class=inviteImgTd>\
       <img src="/images/invite/aol_small.png" />  <br/> <input type=button class=cuteInviteButton value="Select" name=Select onclick="javascript:referLoginForm(\'aol\',\''+currentFieldId+'\',\''+quizid+'\');"/>\
      </td>\
      <td align=center class=inviteImgTd>\
      <img src="/images/invite/msn_small.png" />  <br/> <input type=button class=cuteInviteButton value="Select" name=Select onclick="javascript:referLoginForm(\'msn\',\''+currentFieldId+'\',\''+quizid+'\');"/>\
    </td></tr>\
    <tr><td colspan=2 align=center>\
       <h3> OR </h3>\
    </td></tr>\
  </table>\
  <form name="referFriendForm" id="referFriendForm" ><table class=popupInviteTable width=100%>\
    <tr><td align=center>\
      Directly Enter Emails Of Your Friends:\
    </td></tr>\
  ';
  if(isloggedIn != 1)
  {
    html = html + '    <tr><td class=popupInviteTd align=center>\
                Your name\
              </td></tr>\
              <tr><td class="popupInviteTd bottomSpc" align=center>\
                <input type=text name=sender_fname maxlength="32" style="width:280px;"/>\
              </td></tr>';
  }
  else
  {
    html = html + '<input type=hidden name=sender_fname value="'+fname+'"/>';
  }
  html = html + '\
    <tr><td id=referFriendErr class="popupInviteTd commonErrMsg boldtxt" align=center></td></tr>\
    <tr><td class=popupInviteTd align=center>\
      Enter emails of your friends separated by comma\
    </td></tr>\
    <tr><td class=popupInviteTd align=center>\
      <textarea name=addresses style="width:280px;" /></textarea>\
    </td></tr>\
    <tr><td class=popupInviteTd align=center>\
      <input type=hidden name=formSubmitted value="1"/>\
      <input type=hidden name=directEmails value="1"/>\
      <input type=hidden name=quizid value="'+quizid+'"/>\
      <input type=hidden name=referToFriendsQuiz value="1"/>\
      <input type=button class=cuteInviteButton value="Send Invitation" name=send onclick="javascript:submitFormParseErr(\'referFriendForm\',\'referFriendErr\', \'/my/invites/sendInvitation\',\'referFriendForm\');">\
    </td></tr>\
  </table></form>\
  <table border=0  class=popupInviteTable width=100%>\
    <tr><td align=left class=popupInviteTd>\
     <ul>\
     <li>We hate spam as much as you do.\
     <li>We never store your email address or password and never share it with anyone. \
     <li>Also we never store your friends\' email addresses and never share them with anyone. </ul>\
   <!-- /td></tr><tr><td align=center>\
     - Nitin & Khiem (Lets101 founders) -->\
    </td></tr>\
  </table>';  
  
  createFormDiv(currentFieldId, html, 412, 0, 0, 2);
}

function signInMsg(currentFieldId, msg)
{
  html = '\
  <table class=popupTable width=100%> \
    <tr><td colspan=2 class="popupTd bottomSpc">\
    Please do a quick <a href="/sendMsg/register">Sign Up</a> to '+msg+'.<br/>Its all FREE!</td></tr>\
  </table>';

  createFormDiv(currentFieldId, html, 345, 0, 0, 2);
}

function signInMsgfp(currentFieldId, msg)
{
  html = '\
  <div class="popupTable popupTd bottomSpc"> \
    Please do a quick <a href="/sendMsg/register">Sign Up</a> to '+msg+'.<br/>Its all FREE!\
  </div>';

  createFormDiv(currentFieldId, html, 345, 0, -10, -3);
}

function introduceUs(currentFieldId, who, rand, him_her)
{
    var html = "\
    <form id=intro_request_form name=intro_request_form>\
    <table class=popupTable  width=100%> <tr><td class=popupTd>\
      <input type=hidden name=to_user value="+who+">\
      <input type=hidden name=sessid value="+rand+">\
      <input type=hidden name=formSubmitted value=1>\
        <table cellspacing=0 cellpadding=1 border=0 width=100% id=formdata>\
          <tr><td class=\"popupTd tdHead\">\
             Tell "+who+" three things that you liked most about "+him_her+" <br/>(We will send out an email introducing both of you.)\
          </td></tr>\
          <tr><td class=popupTd>\
            <b>1)</b> <input type=\"text\" class=tdValInput id=\"val1\" name=\"val1\" maxlength=\"80\" />\
          </td></tr>\
          <tr><td class=popupTd>\
            <b>2)</b> <input type=\"text\" class=tdValInput id=\"val2\" name=\"val2\" maxlength=\"80\"  />\
          </td></tr>\
          <tr><td class=popupTd>\
            <b>3)</b> <input type=\"text\" class=tdValInput id=\"val3\" name=\"val3\" maxlength=\"80\" />\
          </td></tr>\
          <tr><td class=popupTd align=center>\
            <input type=button class=cuteButton onclick=\"javascript:checkAndSubmitIntroForm('intro_request_form','introduce-us/intro-request','intro_request_form');\" name=\"send\" value=\"Introduce me\" class=\"cuteButton\" />\
          </td></tr>\
        </table>\
    </td></tr></table></form>";
    
    createFormDiv(currentFieldId, html, 450, 0, 60, 2);
}

function communicationDenied(currentFieldId, errStr, him_her)
{
  html = '\
  <div class="popupTable popupTd bottomSpc"> \
    Sorry user settings prevent you to communicate to ' + him_her + '. <br/>Reason: '+ errStr +' \
  </div>';

  createFormDiv(currentFieldId, html, 450, 0, -10, 0);
}

function communicationDeniedVerifyPhone(currentFieldId, doneurl)
{
  html = '\
  <div class="popupTable popupTd"> \
    <table ><tr><td align=left class=sendEmailMsgInstruction> \
              Sorry, but for your and other members\' safety and to prevent spamming, sending messages is allowed after you confirm your phone number. \
              <br/><br/><b>It takes just few seconds to confirm phone and after that you can freely send as many messages as you may like.</b> \
              <br/><br/><strong>To confirm your phone <a href="/my/calllogs?.done=' + doneurl + '">please click here</a></strong>. Please remember your phone number is always safe and kept confidential. \
              <br/><br/><span class=comment>Note: if you are from <b>country other than US, UK or Canada</b>, <a href="/my/contact?.done='+ doneurl +'">please drop us an email</a> and our customer service will verify your phone.</span> \
              </td></tr></table> \
  </div>';

  createFormDiv(currentFieldId, html, 450, 0, -10, 0);
}

function checkAndSubmitIntroForm(retDiv,url,form)
{
  if(trim(document.getElementById('val1').value) == ""  || 
      trim(document.getElementById('val2').value) == ""  || 
      trim(document.getElementById('val3').value) == "")
  {
    alert ("Please fill in all the three rows");
    return false;
  }
  submitform(retDiv, url, form);
  return true;
}

function togglechecked()
{
  for (var i = 0; i < document.emailForm.elements.length; i++) 
  {
    var e = document.emailForm.elements[i];
    if ((e.disabled == false) && (e.name != 'allbox') && (e.type == 'checkbox')) 
    {
      e.checked = document.emailForm.allbox.checked;
    }
  }
}

function toggleselect()
{
  document.emailForm.allbox.checked = !document.emailForm.allbox.checked;
    togglechecked();
}

document.write("<div id='PopupWrapperDiv' style='display:none; border:0px solid red; background:#CCFFFF; opacity:.40; position:absolute'></div>");
document.write("<div id='PopupFormDiv' style='display:none; padding:3px 3px 3px 3px; border:2px solid lightblue; background:white; position:absolute'></div>");
