  $(document).ready(function() { 
      $("#refUserOtherLang").click(function(){
        $("#trUserOtherLang").toggle();
      }
      );
      
     $("#users_height").change(function(){
         calculateCMToInch(); 
     });
     calculateCMToInch(); 
     
     
     $("#users_weight").change(function(){
         calculateKGToPond(); 
     });
     calculateKGToPond(); 
  
     $("#users_country_code").change(function(){
        loadStateList();
     });             
     loadStateList();
     
     $("#btnFormSubmit").click(function(){
       if( $("#users_headimg").val().length>0 && document.getElementById("users_chkDeleteCurrentImg")){
           document.getElementById("users_chkDeleteCurrentImg").checked = true; 
       } 
     });
     
     $("#hrefMoreIntro").tooltip();
     
 });
 
 
 function calculateCMToInch(){           
         var value = $("#users_height").val();
         var cm=parseInt(value)+139;
         var inch=cm*0.39;  
         var feet = inch/12;
         var feet_whole = parseInt(feet);
         var feet_dec=    feet - feet_whole;
         inch =   parseInt(feet_dec*12);
         $("#userHeightInc").html("("+ feet_whole+"'"+inch+"\")");
 } 
 
 function calculateKGToPond(){
        value =  $("#users_weight").val();
        var pond = (parseInt(value)+39) *2.2;
        $("#userWeightPond").html(
        "("+ pond.toFixed(1) +" lbs)"
        );
 }
 function loadStateList(){
               $("#loader").show();
               $("#div_user_state").load(
                    "/getStates",    
                    { countryCode: $("#users_country_code").val()},
                    function(){ $("#loader").hide();   }
                );
         }
 
 var bSendingAJAX=false;
         
 function addFavorite(favorite){
       
      if(bSendingAJAX==false){
          bSendingAJAX=true; 
      }else{
          return;
      }
     
       if(document.getElementById("imgIndicator")){
           $("#imgIndicator").show();           
       }    
       if(document.getElementById("memberIndicator_"+favorite)){
           document.getElementById("memberIndicator_"+favorite).style.display="";
       }
          
       
       $.post(
        "/addtofavorite/"+favorite,
        function(data){
           if(document.getElementById("imgIndicator")){
           $("#imgIndicator").hide();           
           }    
           if(document.getElementById("memberIndicator_"+favorite)){
               document.getElementById("memberIndicator_"+favorite).style.display="none";
           } 
            
            if(data=="0"){
               alert ("User "+favorite+" is already in your favorite list.");
            }else
            if(data=="1"){
               alert ("User "+favorite+" has been succesfully added as your favorite list."); 
            }else{
                alert("You don't need to add yourself to your favorite list.");
            }
         
            bSendingAJAX=false;
        }
        
       ); 
 
 }
 
 function addInterest(interest){
     
      if(bSendingAJAX==false){
          bSendingAJAX=true; 
      }else{
          return;
      }
     
       if(document.getElementById("imgIndicator")){
           $("#imgIndicator").show();           
       }    
       if(document.getElementById("memberIndicator_"+interest)){
           document.getElementById("memberIndicator_"+interest).style.display="";
       }
          
     
      $("#imgIndicator").show();  
       $.post(
        "/addtointerest/"+interest,
        function(data){
            switch(data){
                case "0": str = "You don't need to add yourself to your interest list.";break;
                case "1": str = "Please wait patiently for your interest expression to be accepted by user "+interest ;break; 
                case "2": str = "User "+interest+" is already in your buddy list, no need to send interest expression again.";break;
                case "3":
                case "4": str = "Relation between you can user "+interest +" has been blocked, the interest expression can not be sent now.";break;
                case "8": str = "Your interest expression has been succesfully sent to user "+interest+" again, please wait for the reply.";break;  
                case "9": str = "Your interest expression has been succesfully sent to user "+interest+", please wait for the reply.";
            }
        
           if(document.getElementById("imgIndicator")){
           $("#imgIndicator").hide();           
           }    
           if(document.getElementById("memberIndicator_"+interest)){
               document.getElementById("memberIndicator_"+interest).style.display="none";
           }
           alert (str);
           bSendingAJAX=false;
        }               
       );               
 }
 
 function sendGift(){
      $("#divSendGift").slideDown('slow');
 }
 
 function hideSendGift(){
      $("#divSendGift").slideUp('slow');
 }
 /*
 function showGiftInCategory(id,ids){
     var idArray=ids.split(",");
     for(var i=0;i<idArray.length;i++){
         $("#divGiftsInType_"+idArray[i]).slideUp('slow');
         if(idArray[i]!=id){             
             $("#giftTypeLnk_"+idArray[i]).css("background-color","#f0f0f0");
             $("#giftTypeLnk_"+idArray[i]).css("color","#000058");
         }else{
             $("#giftTypeLnk_"+idArray[i]).css("background-color","#8080FF");
             $("#giftTypeLnk_"+idArray[i]).css("color","#ffffff"); 
         }
     }    
     $("#divGiftsInType_"+id).slideDown('slow');
     
     $("#spanTotalGiftNum").html($("#divGiftsNumInType_"+id).html());
 }
 */
 
 function showGiftInCategoryPaginator(id,ids,culture){
  
     var idArray=ids.split(",");
     for(var i=0;i<idArray.length;i++){
        $("#divGiftPageControllerForType_"+idArray[i]).slideUp('slow');
         if(idArray[i]!=id){             
             $("#giftTypeLnk_"+idArray[i]).css("background-color","#f0f0f0");
             $("#giftTypeLnk_"+idArray[i]).css("color","#000058");
         }else{
             $("#giftTypeLnk_"+idArray[i]).css("background-color","#8080FF");
             $("#giftTypeLnk_"+idArray[i]).css("color","#ffffff"); 
         }
     }
     $("#divGiftPageControllerForType_"+id).slideDown('slow');
     $("#spanTotalGiftNum").html($("#divGiftsNumInType_"+id).html());          
     loadGifts(id,0,culture);
 }
 
 function loadGifts(catID,page,culture){
        // alert ("page " + page +" in cat "+catID)
       
        var pages = $("#divTotalPagesInCategory_"+catID).html();
        for(var i=0;i<pages;i++){
         if(i!=page){             
             $("#giftPagerLnk_"+catID+"_"+i).css("background-color","#f0f0f0");
             $("#giftPagerLnk_"+catID+"_"+i).css("color","#000058");
         }else{
             $("#giftPagerLnk_"+catID+"_"+i).css("background-color","#8080FF");
             $("#giftPagerLnk_"+catID+"_"+i).css("color","#ffffff"); 
         }
        }
         
        $("#divGiftsInCategory").hide("fast");
        $("#divLoadGiftsIndicator").show();
        $("#divGiftsInCategory").load(
                    "/"+culture+"/getgifts/"+catID+"/"+page,{},
                    function(){ $("#divLoadGiftsIndicator").hide();$("#divGiftsInCategory").show("slow"); }
        );
        
 }
 
 function showRegReminder(){
     
 }
 
 var gift_id="";
 
 var flash_gift_url;
 
 function selectGift(id,title, url,cost,culture,type,urlFlash){
    
      gift_id = id;
      var reminder;
      if(culture=="zh"){
        reminder="您选择了以下礼物，"+title+"， <img src='"+url+"'/>，送给您心仪的朋友，这个礼物将花费您"+cost+"元爱币。";
      }else{
         reminder="You have select this gift, " + title +", <img src='"+url+"'/>, for your desired friend, this gift will cost you "+ cost +" LoveDollars.";  
      }
      $("#divGiftReminder").html(reminder);
      $("#divGiftReminder").show();  
      
      //alert (id+"/"+title+"/"+url+"/"+cost+"/"+type+"/"+url);
      
         
      if(type=="Flash"){
         showFlashGiftMovie(urlFlash);   
      }
      
 }
 
 
 
 
 function showGiftDesc(id){
     
     var curImgSrc = $("#vg_gift_desc_show_"+id).attr("src");
     if(curImgSrc.indexOf("down")>0){
        $("#vg_gift_desc_show_"+id).attr("src","/images/arrow_up.gif");
        $("#divGiftDescription_"+id).show('slow');   
     }else{
        $("#vg_gift_desc_show_"+id).attr("src","/images/arrow_down.gif")
        $("#divGiftDescription_"+id).hide('slow');  
     }
    
     
 }
 
 function SendGiftNow(recName,culture){
 
     if(gift_id==""){
          if(culture=="zh"){
            alert ("请先选择您喜欢的礼物再发送给对方。");
          }else{
            alert ("Please select your desired gift first and then send to this member.");  
          } 
          
     }else{
         
         $.post(
           "/sendgift", { receiver: recName, giftId: gift_id, msg: $("#txtGiftMsg").val()},
            function(data){ 
                var response;
                if(culture=="zh"){
                    switch(data){
                        case "0": response="恭喜您，您的礼物已经成功送出。请耐心等待对方的答复。";break;
                        case "1": response="非常抱歉，只有黄金会员或白金会员才可以向其他会员送出礼物。请尽快升级您的会员资格。当您升级会员资格后，我们会向您的帐户添加免费爱币的。";break;
                        case "2": response="非常抱歉，您已经没有任何爱币可以使用，请购买爱币以送出您的礼物。";break;
                        case "3": response="非常抱歉，您目前的爱币金额不足以购买此项礼物，请购买爱币以送出您的礼物。";break;
                    }
                }else{
                    switch(data){
                        case "0": response="Congratulations! Your gift has alerady been sent to your desired member. Please wait patiently for the reply.";break;
                        case "1": response="We apologize that only gold member or platinum member can send gift to others. Please upgrade your member level as soon as possible. After you upgrade your account, we will apply some free Love Dollars to your account!";break;
                        case "2": response="We apologize that you no longer have any love dollars in your account. Please purchase love dollars in order to send gift.";break;
                        case "3": response="We apologize that the remaining love dollars in your account is not enough to pruchase this gift. Please purchase more love dollars in order to send the gift.";break;
                    }
                }
                alert ( response); 
            }               
           );                
           $("#divSendGift").slideUp('slow');  
     }   
        
 }
 
 
  function acceptFriend(friend){
       
      
       if(bSendingAJAX==false){
          bSendingAJAX=true; 
      }else{
          return;
      }
     
       if(document.getElementById("imgIndicator")){
           $("#imgIndicator").show();           
       }    
       if(document.getElementById("memberIndicator_"+friend)){
           document.getElementById("memberIndicator_"+friend).style.display="";
       }
      
      
       $.post(
        "/addtofriend/"+friend,
        function(data){
            switch(data){
                case "0": str = "You can not add "+friend+" as your friend directly, please show interest first, and wait for "+friend+" to accept you as friend.";break;
                case "1": str = friend +" is already your friend, you don't need to accept it again.";break; 
                case "2": str = "Your relationship with "+friend+" has been blocked by either of you, you can no longer add "+ friend +" as your friend.";break; 
                case "3": str = "You have succesfully accpeted  "+friend+" as your friend";break; 
                }
           if(document.getElementById("imgIndicator")){
               $("#imgIndicator").hide();           
           }    
           if(document.getElementById("memberIndicator_"+friend)){
               document.getElementById("memberIndicator_"+friend).style.display="";
           }
           alert (str);
           bSendingAJAX=false;
        }               
       );               
 }
 
 function blockFriend(friend){
      
     
       if(bSendingAJAX==false){
          bSendingAJAX=true; 
      }else{
          return;
      }
     
       if(document.getElementById("imgIndicator")){
           $("#imgIndicator").show();           
       }    
       if(document.getElementById("memberIndicator_"+friend)){
           document.getElementById("memberIndicator_"+friend).style.display="";
       }
      
      $.post(
        "/blockfriend/"+friend,
        function(data){
            switch(data){
                case "0": str = "You can not block "+friend+", only related friend can be blocked.";break;
                case "1": str = friend +" is already blocked, you don't need to block it again.";break; 
                case "2": str = "Your relationship with "+friend+" has not been accepted yet, you don't need to block it.";break; 
                case "3": str = "You have succesfully blocked  "+friend+" from your friend list.";break; 
                }
           if(document.getElementById("imgIndicator")){
               $("#imgIndicator").hide();           
           }    
           if(document.getElementById("memberIndicator_"+friend)){
               document.getElementById("memberIndicator_"+friend).style.display="";
           }
           alert (str);
           bSendingAJAX=false;
        }               
       );
 }
 
 function unBlockFriend(friend){
      
       if(bSendingAJAX==false){
          bSendingAJAX=true; 
      }else{
          return;
      }
     
       if(document.getElementById("imgIndicator")){
           $("#imgIndicator").show();           
       }    
       if(document.getElementById("memberIndicator_"+friend)){
           document.getElementById("memberIndicator_"+friend).style.display="";
       }
      $.post(
        "/unblockfriend/"+friend,
        function(data){
            switch(data){
                case "0": str = "You can not unblock "+friend+", please contact us for help";break;
                case "1": str = "You already have active relationship with"+friend +", you don't need to unblock it again.";break; 
                case "2": str = "Your relationship with "+friend+" has not been accepted yet, you don't need to unblock it.";break; 
                case "3": str = "You have succesfully unblocked  "+friend+" from your blocked friend list.";break; 
                }
            if(document.getElementById("imgIndicator")){
               $("#imgIndicator").hide();           
           }    
           if(document.getElementById("memberIndicator_"+friend)){
               document.getElementById("memberIndicator_"+friend).style.display="";
           }
           alert (str);
           bSendingAJAX=false;
        }               
       );
 }
 
 function removeFavorite(friend){
         if(bSendingAJAX==false){
          bSendingAJAX=true; 
      }else{
          return;
      }
     
       if(document.getElementById("imgIndicator")){
           $("#imgIndicator").show();           
       }    
       if(document.getElementById("memberIndicator_"+friend)){
           document.getElementById("memberIndicator_"+friend).style.display="";
       }
        $.post(
        "/removefavorite/"+friend,
        function(data){
            switch(data){
                case "0": str = friend + " is no longer in your favorite list, you don't need to remove it.";break;
                case "1": str = "You have succesfully removed "+friend+" from your favorite list.";break;
                }
              if(document.getElementById("imgIndicator")){
                   $("#imgIndicator").hide();           
               }    
               if(document.getElementById("memberIndicator_"+friend)){
                   document.getElementById("memberIndicator_"+friend).style.display="";
               }
               alert (str);
               bSendingAJAX=false;
        }               
       );
 }
 
  function chkValidity(culture){
      
       if(document.getElementById("chkAgree")){
           if(document.getElementById("chkAgree").checked!=true){
               if(culture=="zh"){
                  alert ("请先阅读并接受我们的有关使用规定再注册您新的账户。");
               }else{
                 alert ("Please  read and accept our terms first before registering new account.");
               }
               document.getElementById("chkAgree").focus();
               return false;
           }
       }
      
      
        
        var countryCode = $("#users_country_code").val();
        var postCode = $("#users_zip").val();
    
        if(postCode != ""){
            var newpost=checkPostCodeValidity(postCode,countryCode);   
            if(!newpost ){
              alert ("Your zip code format is invalid, please enter your zip/postcode in accurate format.")
               return false;    
            }else{
                $("#users_zip").val(newpost);
                
                //now first popup the waiting div
                //in popup.js
                showWaitingReminder();  
                
                return true;  
            }
        }else{
            //if no postcode here, then return and submit the form
            
            //now first popup the waiting div
            //in popup.js
            showWaitingReminder();  
            return true;
        }
  }
  
  
  
  function getGeocode(postcode,countryCode){
     var geocoder = null;  
     geocoder = new GClientGeocoder(); 
     geocoder.getLocations(postcode+","+countryCode, addGeoCodeToForm);       
  }
  
  
   function addGeoCodeToForm(response) {
          if (!response || response.Status.code != 200) {
          } else {
            place = response.Placemark[0];
            latitude =  place.Point.coordinates[1];
            longitude=  place.Point.coordinates[0];
        
            $("#users_latitude").val(latitude);
            $("#users_longitude").val(longitude);     
            // alert ( $("#users_latitude").val() + " "+ $("#users_longitude").val());       
            $("#frmUser").submit();    
          }
        }

 
  
