var image_num = "";
$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = -1;
  });
};
$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}
$(['ajax-loader.gif']).preload();
function clear_form_elements(ele) {	 
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });	 
}
function mycarousel_initCallback(carousel)
{
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}; 
$(function(){
    
    $('#mycarousel').jcarousel({
                auto: 2,
                wrap: 'circular',
                scroll:1,
                animation:"slow",
                initCallback: mycarousel_initCallback                        
    });
    
    $('#mycarousel_v').jcarousel({
                auto: 2,
                wrap: 'circular',
                scroll:1,
                vertical:true,
                animation:"slow",
                initCallback: mycarousel_initCallback                        
    });
       
    $("#quick_search_link").click(function(){         
         $("#show_banner_header").removeClass("none");
         $(".banner_wrapper").removeClass("bg_none");
         $(".menu").removeClass("new_menu");         
         $(".quick_search").toggle();
    });
    
    $("#quick_search_hide").click(function(){
        $("#show_banner_header").addClass("none");
        $(".banner_wrapper").addClass("bg_none");
        $(".menu").addClass("new_menu");        
        $(".quick_search").toggle();
    });
    
    $("a[rel='member_photos']").colorbox(); 
    $("a[rel='profile_photos']").colorbox();
        
    $(".profile_arrow").live("click",function(){        
        $(this).parent().next("div").slideToggle();
        var id = $(this).attr("id");
        $("#"+id).toggle();
    });
    
    $("body").keypress(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);     
        if(code == 27)
        {
            hideResponseText();
        } 
    });    
    $(".cancelmembership").bind("click",function(){
        if(confirm("Are you sure you want to cancel membership"))
        {
            showResponseMsg();    
            $.post("ajax.php",{action:"get_reason_box"},function(data){
                changeResponseText(data);    
            });            
        }            
        return false;    
    });
    
    $("#cmd_submit_reason").live("click",function(){
         if($.trim($("#msg_reason").val()) == "")
         {
              $("#msg_reason").focus();
              $(".errorbox").fadeIn(500).html("Please enter some reason detail.");
              $("#msg_reason").val("");
              return false;  
         }
         else
         {
             $(".infobox").show();
             $(".errorbox").hide();
             $.post("ajax.php",{action:"cancel_membership",data:base64_encode("reason_text="+$("#msg_reason").val())},function(data){
                 if(data == "done")
                 {
                      window.location.href = "account.php";  
                 }
                 else
                 {
                      $(".infobox").hide();
                      $(".errorbox").fadeIn(500).html(data); 
                 }
             });
         }            
    });
    
    $(".add_hot_list").live("click",function(){
        showResponseMsg();
        var to_id = $(this).attr("href");
        $.post("ajax.php",{action:"add_to_list",from_id:$("#from_member_id").html(),to_id:to_id},function(data)
        {
            if(data == "done")
                changeResponseText("<div class='success' id='info'>Your add request sent successfully</div>");
            else
                changeResponseText(data);
        });
        return false;
    });
        
    $(".open_poked_list").live("click",function(){
        showResponseMsg();
        var to_id = $(this).attr("id");
        $.post("ajax.php",{action:"get_preset_imlist"},function(data)
        {
            $("#to_member_id").html(to_id);
            changeResponseText(data);
        });
        return false;
    });
    $("#cmd_submit_poke").live("click",function(){
         var val = $('input:radio[name=poke_list]:checked').val();         
         if(val == undefined) 
             alert("Please select the IM's first");
         else{
             showResponseMsg();             
             $.post("ajax.php",{action:"submit_poke",poke_id:val,from_id:$("#from_member_id").html(),to_id:$("#to_member_id").html()},function(data){
                    if(data == "done")
                    {                        
                        changeResponseText("<div class='success' id='info'>You poked successfully</div>");
                        //refreshMemberContent("get_poke_list","poked_block")
                    }else
                    {                        
                        changeResponseText("<div class='failure' id='info'>Some error occur, please try later</div>");
                        //changeResponseText(data);
                    }    
             });             
         }             
         return false;
    });
    
    $(".open_message_box").live("click",function(){
        showResponseMsg();
        var to_id = $(this).attr("href");
        $("#to_member_id").html(to_id);
        var parent_id = $(this).attr("id");
        $("#message_parent_id").html(parent_id);
        var subject = $(this).attr("title");
        $.post("ajax.php",{action:"get_message_box",parent_id:parent_id,subject:subject},function(data)
        {
            changeResponseText(data);
        });
        return false;
    });            
    
    $("#cmd_submit_message").live("click",function(){
         var subject = $('#msg_subject').val();
         var message = $('#msg_message').val();         
         if(subject == "" || message == "") 
             alert("Subject or Message cannot leave empty");
         else{
             showResponseMsg();
             $.post("ajax.php",{action:"submit_message",message:message,subject:subject,parent_id:$("#message_parent_id").html(),from_id:$("#from_member_id").html(),to_id:$("#to_member_id").html()},function(data){
                    if(data == "done")
                        changeResponseText("<div class='success' id='info'>Your message sent successfully</div>");
                    else
                    {
                        changeResponseText("<div class='failure' id='info'>Some error occur, please try later</div>");
                        changeResponseText(data);
                    }    
             })
             
         }             
         return false;
    });
    
    $(".remove_friend").live("click",function(){         
         if(confirm("Are you sure you want to remove from hot list"))
         {
             showResponseMsg();
             var to_id = $(this).attr("id");
             $.post("ajax.php",{action:"remove_friend",to_id:to_id,from_id:$("#from_member_id").html()},function(data){
                   if(data == "done")
                   {                        
                        $("#friend_list" + to_id).hide();                        
                        hideResponseText();
                   }
                   else
                        changeResponseText("<div class='failure' id='info'>Some error occur, please try later</div>");
             });
         }   
         return false;
    }); 
    
    $("#accept_request").live("click",function(){
         if(confirm("Are you sure you want to add to hot list"))
         {
             showResponseMsg();
             var member_id = $(this).attr("name");
             $.post("ajax.php",{action:"accept_request",member_id:member_id,list_id:$("#from_member_id").html()},function(data){
                   if(data == "done")
                   {
                       $("#request_list" + member_id).hide();
                       hideResponseText();
                   }
                   else
                       changeResponseText("<div class='failure' id='info'>Some error occur, please try later</div>");
             });
         }   
         return false; 
    });
    
    $("#reject_request").live("click",function(){
          if(confirm("Are you sure you want to reject request"))
         {
             showResponseMsg();
             var member_id = $(this).attr("name");
             $.post("ajax.php",{action:"reject_request",member_id:member_id,list_id:$("#from_member_id").html()},function(data){
                   if(data == "done")
                   {
                        $("#request_list" + member_id).hide();
                        hideResponseText();
                   }
                   else
                        changeResponseText("<div class='failure' id='info'>Some error occur, please try later</div>");
             });
         }   
         return false;
    });
    
    /*$("#cmdUpgrade").bind("click",function()
    {
         showResponseMsg();
         $.post("ajax.php",{action:"validate_payment_information",data:base64_encode($("#paymant_form").serialize())},function(data){
              if(data == "done")
              {
                    $("#paymant_form").submit();
              }
              else
              {
                    changeResponseText("<div class='failure' id='info'>"+data+"</div>");
              }
         });
         return false;
    });
    */
       
    $("ul.tabs li").each(function()
    {
        $(this).click(function () {
            $("#" + $("li a.active").parent("li").attr("class")).hide();
			$("li a.active").removeClass("active");
            $(this).find("a").addClass("active");
            $("#" + $(this).attr("class")).show();
            $("#update_action").val($(this).attr("class"));
            return false;
	    });
    });
    
    refreshDropDown();        
    
    $("#rg_dob_year").bind("change",function(){
        calculate_age();        
    });        
    
    $("#rg_dob_day").bind("change",function(){
        calculate_age();        
    });
    
    $("#rg_dob_month").bind("change",function(){
        renderDay();   
    });   
    
    $("#cmdContact").bind("click",function(){
        showResponseMsg();
        $.post("ajax.php",{action:'send_contact_info',data:base64_encode($("#contact_form").serialize())},function(data){                    
            if(data=="done")
            {            
                changeResponseText("<div class='success' id='info'>Your feedback sent successfully. We will contact with you as soon as possible.</div>");
                clear_form_elements('#contact_form');
            }else
                changeResponseText("<div class='failure' id='info'>"+data+"</div>");                                                               
        });
        return false;
    });
    
    $("#cmdUpdate").bind("click",function(){
        showResponseMsg();
        $.post("ajax.php",{action:$("#update_action").val(),data:base64_encode($("#full_registration").serialize())},function(data){                    
            if(data=="done")            
                 changeResponseText("<div class='success' id='info'>Profile updated successfully</div>");
            else
            {
                 if(data=="done_refresh")
                 {
                     refreshMemberContent("get_member_images","photos");
                     hideResponseText();
                 }   
                 else
                     changeResponseText("<div class='failure' id='info'>"+data+"</div>");                                  
            }                    
        });
        return false;
    });
    
    $("#cmdRegister").bind("click",function(){
         showResponseMsg();
         $.post("ajax.php",{action:"user_available",username:$("#rg_username").val()},function(data){            
            if(data == "false")
            {
                changeResponseText("<div class='failure' id='info'>Username not available, please try another</div>");
            }
            if(data == "true")
            { 
                $.post("ajax.php",{action:"register",rg_image1:$("#rg_image").val(),data:base64_encode($("#full_registration").serialize())},function(data){
                    
                    if(data=="done")
                    {
                        // changeResponseText("<div class='success' id='info'>Congrats, your have successfully registered. <br />We have sent you a email with email confirmation link . <br/> Please confirm your email address before login</div>");
                       //  clear_form_elements('#full_registration');
                        // $("#change-image").trigger("click");
                        window.location.href = "register_payment.php";                       
                    }
                    else
                    {
                         changeResponseText("<div class='failure' id='info'>"+data+"</div>");
                    }
                });
            }
        });
        return false;
    });
 
    
    $(".close_thumb").live("click",function(){
        if(confirm("Are you sure you want to delete this image"))
        {
            showResponseMsg();
            $.post("ajax.php",{action:"delete_thumb",data:base64_encode($(this).attr("id"))},function(data){                                
                refreshMemberContent("get_member_images","photos");
                hideResponseText();  
            });
        }
    })
    
    $(".read_all").live("click",function(){
        var id = $(this).attr("id");
        $(this).hide();
        $("#small_message_"+id).hide();
        $("#full_message_"+id).fadeIn(500);
        $("#hide_message_"+id).fadeIn(500);
    });
    
    $(".hide_all").live("click",function(){
        var id = $(this).attr("id").replace("hide_message_","");
        $("#full_message_"+id).hide();
        $("#hide_message_"+id).hide();
        $("#"+id).fadeIn(500);
        $("#small_message_"+id).fadeIn(500);
    });
    
    $("#rg_button").bind("click",function(){      
        showResponseMsg();
        $.post("ajax.php",{action:"user_available",username:$("#rg_username").val()},function(data){            
            if(data == "false")
            {
                changeResponseText("<div class='failure' id='info'>Username not available, please try another</div>");
            }
            if(data == "true")
            { 
                $.post("ajax.php",{action:"quick_register",rg_image1:$("#rg_image").val(),data:base64_encode($("#quick_register").serialize())},function(data){
                    
                    if(data=="done")
                    {
                         changeResponseText("<div class='success' id='info'>Congrats, your have successfully registered. <br />We have sent you a email with email confirmation link . <br/> Please confirm your email address before login</div>");
                         clear_form_elements('#quick_register');
                         $("#change-image").trigger("click");                            
                    }
                    else
                    {
                         changeResponseText("<div class='failure' id='info'>"+data+"</div>");
                    }                    
                });    
            }             
        });
        return false;        
    });
    
    
    $("#cmd_login_button").bind("click",function(){
         showResponseMsg();
         var data = $("#login_form").serialize();
         $.post("ajax.php",{action:"login",data:data},function(data){
               if(data == "done")
               {                                        
                    window.location.href = 'account.php';
               }
               else if(data == "no-image")
               {
                    window.location.href = 'upload.php';
               }
               else
               {
                    changeResponseText("<div class='failure' id='info'>"+data+"</div>");
               } 
         });   
         return false;
    });
    
    $("#cmd_forgot_button").bind("click",function(){
         showResponseMsg();
         $.post("ajax.php",{action:"forgot_password",email:$("#forgot_email").val()},function(data){
               if(data == "done")
               {
                    changeResponseText("<div class='success' id='info'>Your password has been successfully sent to your email address.</div>");
               }
               else
               {
                    changeResponseText("<div class='failure' id='info'>"+data+"</div>");
               } 
         });   
         return false;
    });
    
    $("#rg_country").change(function(){
         renderRegionByCountry($(this).val());
    });
    $("#rg_region").change(function(){
         renderCityByRegion($(this).val());
    });
    
    
    /*$("#responseMsg").click(function(){
            $(this).hide();
            $("#responseMsg").html("");
            $("#bg").fadeOut();
    });*/  
    $("#bg").click(function(){
            $("#responseMsg").html("");
            $("#responseMsg").hide();
            $("#close_response_msg").hide();
            $(this).fadeOut();
    });
    $("#close_response_msg").click(function(){
        hideResponseText(); 
    });       
});

function refreshMemberContent(action,id)
{
    $.post("ajax.php",{action:action},function(data){
        $("#"+id).html(data); 
    });
}

function renderDay(day)
{
    if(typeof day == "undefined") {
        day = "";
    }
    if($("#rg_dob_year").val() == "")
    {
        alert("Please select Year.");
        return false;
    }                                           
    $('#rg_dob_day').next("span").text("Loading");
    $.getJSON('ajax.php', {action:"render_day",month:$("#rg_dob_month").val(),year:$("#rg_dob_year").val()}, function(data) {
          var len = data.length;
          var html = "<option value=''>Select Day</option>";
          var selected = "";          
          for (var i = 0; i<len; i++) {
               if(data[i].day == day) selected = " selected='selected' "; else selected = ""; 
               html += '<option '+ selected +' value="' + data[i].day + '">' + data[i].day + '</option>';
          }                                                                            
          $('#rg_dob_day').html(html);
          var title = $('option:selected',$('#rg_dob_day')).text();                    
          $('#rg_dob_day').next("span").text(title);	                                
    });           
    calculate_age();
}

function calculate_age()
{
    var year = $("#rg_dob_year").val();
    var month = $("#rg_dob_month").val();
    var day = $("#rg_dob_day").val();
    if(year != "" && month != "" && day != "")
    {        
        $.post("ajax.php",{action:"calculate_age",year:year,month:month,day:day},function(data){
            $("#calculated_age").html(data); 
        });
    }
    else
    {
        $("#calculated_age").html("");
    }
}

function renderRegionByCountry(country_id,region_id,forCity)
{    
     if (typeof region_id == "undefined") {
        region_id = "";
     }     
     if(typeof forCity == "undefined") {
        forCity = "true";
     }
     $('#rg_region').next("span").text("Loading");
     var first_id = "";
     $.getJSON('ajax.php', {action:"render_region",country:country_id}, function(data) {
           var len = data.length;
           var html = "";
           var selected = "";           
           for (var i = 0; i<len; i++) {
                $selected = "";
                if(first_id == "") first_id = data[i].region_id; 
                if(region_id == data[i].region_id) $selected = " selected='selected' ";        
                html += '<option ' + $selected + ' value="' + data[i].region_id + '">' + data[i].region + '</option>';                
           } 
           $('#rg_region').html(html);
           
           var title = $('option:selected',$('#rg_region')).text();
           $('#rg_region').next("span").text(title);
           
           if(forCity == "true") renderCityByRegion(first_id);
     });
}

function renderCityByRegion(region_id,city_id)
{    
     if (typeof city_id == "undefined") {
        city_id = "";
     }     
     $('#rg_city').next("span").text("Loading");
     $.getJSON('ajax.php', {action:"render_city",region:region_id}, function(data) {
           var len = data.length;
           var html = "";
           var selected = "";           
           for (var i = 0; i<len; i++) {
                $selected = "";
                if(city_id == data[i].city_id)
                    $selected = " selected='selected' ";    
                html += '<option ' + $selected + ' value="' + data[i].city_id + '">' + data[i].city + '</option>';
           }
           if(len == 0){
                html = '<option selected="selected" value="00000">Other</option>';
                $('#rg_city').html(html);
                var title = $('option:selected',$('#rg_city')).text();
                $('#rg_city').next("span").text(title);
           }else{                
                $('#rg_city').html(html);                
                var title = $('option:selected',$('#rg_city')).text();
                $('#rg_city').next("span").text(title);	      
           }           
     });
}

function quick_register()
{
    
}

function showResponseMsg()
{
    ajaxloader(true);
    changeResponseText("<img src='images/ajax-loader.gif' />",false);
    $("#responseMsg").css("padding-top","12px");    
}

function makeDivCenter(id,bShowClose)
{
    var top = ($(window).height() - $("#" + id).height() ) / 2+$(window).scrollTop();
    var left = ( $(window).width() - $("#" + id).width() ) / 2+$(window).scrollLeft();
    $("#" + id).css("top",top + "px");
    $("#" + id).css("left",left + "px");
    if(bShowClose)
    {        
        $("#close_response_msg").css("top",(top) + "px");
        $("#close_response_msg").css("left",($(window).width() - $("#" + id).width() ) / 2+$(window).scrollLeft() + $("#" + id).width() + "px");
        $("#close_response_msg").show();
    }        
}

function changeResponseText(sText,bShowClose)
{
    if(typeof bShowClose == "undefined")
    {
        bShowClose = true;
    }
    $("#ajaxloader").hide();
    $("#responseMsg").html("" + sText + "");
    $("#responseMsg").show();
    $("#responseMsg").css("padding-top","25px");
    makeDivCenter("responseMsg",bShowClose);    
}

function hideResponseText()
{
    $("#bg").fadeOut(500);
    $("#responseMsg").html("");
    $("#responseMsg").hide();
    $("#responseMsg").css("padding-top","12px");
    $("#close_response_msg").hide();
}

function ajaxloader(bShow)
{
    if(bShow)
    {
        $("#bg").fadeIn(500);
        $("#ajaxloader").show();            
    }    
    else
    {
        $("#bg").fadeOut(500);
        $("#ajaxloader").hide();            
    }
}

function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
}

function formReady(id,text)
{
    $("#"+id).val(text);
    $("#"+id).css("color","#a4a4a4");
    $("#"+id).bind('focus',function(){ if($(this).val() == text) {$(this).val("");$(this).css("color","#000000");}});
    $("#"+id).bind('blur',function(){ if($(this).val() == "") {$(this).val(text);$(this).css("color","#a4a4a4");}});
}
function formValid(id,text)
{
	var id = $("#"+id);
	if(id.val() == text || id.val() == "")
    {
        id.css("border-color","#ff0000"); 
        return text + "\n";       
    }
    else
    {
        id.css("border-color","#90BADE");
        return "";
    }
}

function showMsg(divID,sMsg)
{   
    $(".failure").fadeOut();
    $(".success").fadeOut();
    $(".warning").fadeOut();
    $(".information").fadeOut();
    $(divID).fadeIn();
    $(divID).html(sMsg);    
}

function makeMsgDivHide()
{
    $(".failure").fadeOut();
    $(".success").fadeOut();
    $(".warning").fadeOut();
    $(".information").fadeOut();
    $(".failure").bind("click",function()
    {        
        $(this).fadeOut();         
    }); 
    $(".success").bind("click",function()
    {        
        $(this).fadeOut();         
    }); 
    $(".warning").bind("click",function()
    {        
        $(this).fadeOut();         
    }); 
    $(".information").bind("click",function()
    {        
        $(this).fadeOut();         
    });             
}

function utf8_decode (str_data) {
    var tmp_arr = [],
        i = 0,
        ac = 0,
        c1 = 0,
        c2 = 0,
        c3 = 0;

    str_data += '';

    while (i < str_data.length) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if (c1 > 191 && c1 < 224) {
            c2 = str_data.charCodeAt(i + 1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i + 1);
            c3 = str_data.charCodeAt(i + 2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}
function utf8_encode (argString) { 
    if (argString === null || typeof argString === "undefined") {
        return "";
    }

    var string = (argString + ''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
    var utftext = "",
        start, end, stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.slice(start, end);
            }
            utftext += enc;
            start = end = n + 1;
        }
    }

    if (end > start) {
        utftext += string.slice(start, stringl);
    }

    return utftext;
}

function base64_encode (data) { 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        enc = "",
        tmp_arr = [];

    if (!data) {
        return data;
    }

    data = this.utf8_encode(data + '');

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1 << 16 | o2 << 8 | o3;

        h1 = bits >> 18 & 0x3f;
        h2 = bits >> 12 & 0x3f;
        h3 = bits >> 6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    enc = tmp_arr.join('');

    switch (data.length % 3) {
    case 1:
        enc = enc.slice(0, -2) + '==';
        break;
    case 2:
        enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}

function base64_decode (data) {    
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
        ac = 0,
        dec = "",
        tmp_arr = [];

    if (!data) {
        return data;
    }

    data += '';

    do { // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;

        o1 = bits >> 16 & 0xff;
        o2 = bits >> 8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);

    dec = tmp_arr.join('');
    dec = this.utf8_decode(dec);

    return dec;
}

function getFrameByName(name) {
  for (var i = 0; i < frames.length; i++)
    if (frames[i].name == name)
      return frames[i];

  return null;
}
function upload_start(obj,num)
{  
    $("#upload_id").val(num);    
    showResponseMsg();
    obj.submit();    
    if(num == "0") image_num = ""; else image_num = parseInt(num);        
}
function uploadDone(name) {
  
   var frame = getFrameByName(name);   
   if (frame) {
     ret = frame.document.getElementsByTagName("body")[0].innerHTML;     
     if(ret != "")
     {        
       var message = ret;
       if(message == "reload_page")
       {
            window.location.href = "account.php";
       }
       if(message == "Max file size")
       {
            changeResponseText("<div class='failure' id='info'>Maximum file size exceeded. File less than 1.5MB allowed.</div>");
            $("#rg_image" + image_num).val("");
            $(".uploaded_image" + image_num).html("");
       }
       else if(message == "Invalid file extension")
       {           
            changeResponseText("<div class='failure' id='info'>Only file with extensions \".jpg\",\".png\",\".gif\" allowed.</div>");
            $("#rg_image" + image_num).val("");
            $(".uploaded_image" + image_num).html("");
       }
       else
       {            
            $(".uploaded_image" + image_num).html("<img src='upload/tmp/" + message + "' />");        
            $("#rg_image" + image_num).val(message);
            hideResponseText();
       }       
     }
  }
}
function refreshDropDown(){
    $('.dropdown_large').each(function(){
        var title;
        title = $('option:selected',this).text();
        $(this)
            .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
            .after('<span class="select_large">' + title + '</span>')
            .change(function(){
                val = $('option:selected',this).text();
                $(this).next().text(val);
                })
    });
    $('.dropdown_small').each(function(){        
        var title;
        title = $('option:selected',this).text();
        $(this)
            .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
            .after('<span class="select_small">' + title + '</span>')
            .change(function(){
                val = $('option:selected',this).text();
                $(this).next().text(val);
                })
    });                
    
    $('.dropdown_medium').each(function(){
        var title;
        title = $('option:selected',this).text();
        $(this)
            .css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
            .after('<span class="select_medium">' + title + '</span>')
            .change(function(){
                val = $('option:selected',this).text();
                $(this).next().text(val);
                })
    });    
}
