jQuery(document).ready(function($) {
      $.noConflict();
      $('a[rel=tip]').tipsy({
            gravity: 's',
            html:'true',
            fade:true
      });
      $(".signin").click(function(e) {
            e.preventDefault();
            $("fieldset#signin_menu").toggle();
            $(".signin").toggleClass("menu-open");
            $('#username').val('');
            $('#password').val('');
            $('#log').hide();
      });
      $('#sendE').click(function(){
            var email = $('#email').val();
            var uid = $('#uid').val();
            var pid = $('#pid').val();
            var cat = $('#catids').val();
            if (IsValidEmail(email))
            {
                  $('#email').attr('disabled',true);
                  $.post('admin/ajax/?send=1',{
                        email:email,
                        id:uid,
                        pid:pid,
                        cat:cat
                  },function(d){
                        if(d=='OK'){
                              sendmail('cancel');
                              jQuery('div.messagebar').addClass('ok').show().html('Email send successfully to <b>'+email+'</b>').delay(3500).fadeOut(400);
                        ;
                        }
                        else{
                              sendmail('cancel');
                              jQuery('div.messagebar').addClass('error').show().html('A error occured when send mail to '+email).delay(3500).fadeOut(400);
                        }
                  })
            }
            else{
                  alert('Enter a Valid Email');
            }
      })
      $("fieldset#signin_menu").mouseup(function(){
            return false
      });
      $(document).mouseup(function(e){
            if($(e.target).parent("a.signin").length==0) {
                  $(".signin").removeClass("menu-open");
                  $("fieldset#signin_menu").hide();
            }
      }); 
      $('#signin_submit').click(function(){
            var username=$('#username').val();
            var password=$('#password').val();
	
            var logs=$('#log');
	
	
            if(username!=='' && password!==''){
		
                  var dataString='sublogin=1'+'&user='+username+'&pass='+password+'&level=1';
                  logs.show().removeClass('error').addClass('loading').html('Wait...');
                  $.ajax({
                        type: 'POST',
                        url: 'process.php',
                        data: dataString,
                        success: function(data) {
                              if(data=='OK'){
                                    window.location='myaccount.php';

                              }
                              else{
                    
                                    logs.show().removeClass('loading').addClass('error').html('Invalid Username/Password');
                              }
                        }//success
                  });//ajax

            }
            else{
                $('#er_log').hide();
                  logs.show().removeClass('loading').addClass('error').html('Fill all required fields');
            } 
      });  
      $('#signin_submit2').click(function(){
            var username=$('#username2').val();
            var password=$('#password2').val();
	
            var logs=$('#log2');
	
	
            if(username!=='' && password!==''){
		 $('#er_log').hide();
                  var dataString='sublogin=1'+'&user='+username+'&pass='+password+'&level=1';
                  logs.show().removeClass('error').addClass('loading').html('Wait...');
                  $.ajax({
                        type: 'POST',
                        url: 'process.php',
                        data: dataString,
                        success: function(data) {
                              if(data=='OK'){
                                    window.location='myaccount.php';
                              }
                              else{
                    
                                    logs.show().removeClass('loading').addClass('error').html('Invalid Username/Password');
                              }
                        }//success
                  });//ajax

            }
            else{
                 $('#er_log').hide();
                  logs.show().removeClass('loading').addClass('error').html('Fill all required fields');
            } 
      }); 

});   


function sendmail(action){
      if(action=='cancel'){
            jQuery('a#sendmaila').show();
            jQuery('div#sendmail').hide();
        
      }
      if(action=='show'){
            jQuery('div.messagebar').hide();
            jQuery('a#sendmaila').hide();
            jQuery('div#sendmail').show();
            jQuery('#email').val('').attr('disabled',false);
      }
      if(action=='alert'){
            alert('Please Login/Register!')
      }
}

function IsValidEmail(email)

{

      var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;

      return filter.test(email);

}
function showifa(){
      jQuery('.ifr').trigger('click');
      tipsy.hide();
}
//jQuery.fn.nl2br = function(){
//   return this.each(function(){
//     jQuery(this).val().replace("\r\n","<br>");
//   });
//};

function edit(state,action){
      var add = jQuery('#'+action+'_txt').html();
      var add_f = add.replace("You have not set a default billing address.", "");
      var oldText = add_f.replace(/(&nbsp;)/g, "").replace(/(<br>)/g, "\r").trim();
      
      if(state=='1'){
        
            jQuery('#'+action+'_txt').hide();
            jQuery('#'+action+'_t').show().html('<textarea id="'+action+'_area" style="width: 323px; height: 100px;"></textarea> <a href="javascript:void(0)" id="'+action+'_save">Save</a> |\n\
 <a href="javaScript:void(0);" id="'+action+'_c">Cancel</a>');
            jQuery('#'+action+'_area').html(oldText);
            jQuery('#'+action+'_c').attr("onclick","edit('0','"+action+"')");
            jQuery('#'+action+'_save').attr("onclick","edit('save','"+action+"')");
            jQuery('#edit_btn_'+action).hide();
        
        
      }
      if(state=='0'){
            jQuery('#'+action+'_txt').show();
            jQuery('#'+action+'_t').hide();
            jQuery('#edit_btn_'+action).show();
      //jQuery('#'+action+'_txt').html(add);
      //window.location.reload();
    
      }
      if(state=='save'){
        
            var v = jQuery('#'+action+'_area').val();
            var n = v.replace(/\n/g, "<br>").replace(/\r/g,"<br>");
            var id = jQuery('#uid_hid').val();
            jQuery('#'+action+'_t').hide(); 
            jQuery('#'+action+'_txt').show().html('<img src="images/load.gif"/>');
            jQuery.post('admin/ajax/?address=1',{
                  text:v,
                  id:id,
                  type:action
            },function(d){
                  if(d=='OK'){
                      if(n!==''){
                        jQuery('#'+action+'_txt').html(n);
                      }
                      else{
                         jQuery('#'+action+'_txt').html('You have not set a default billing address.'); 
                      }
                        jQuery('#edit_btn_'+action).show();
                  }
                  else{
                        alert(d);
                  }
            })
        
        
      }
}

function pass(action){
      if(action=='chk'){
            var p = jQuery('#oldpass').val();
            var id = jQuery('#uid_hid').val();
            if(p.length>=2){
                  jQuery('#peOld').hide();  
                  jQuery.post('admin/ajax/?old=1',{
                        p:p,
                        id:id
                  },function(d){
                        if(d=='OK'){
                              jQuery('#peOld').show().html('<img src="admin/img/icons/icon_approve.png"/>');
                              jQuery('#change').attr('disabled',false)
                        }
                        else{
                   
                              jQuery('#peOld').show().html('Invalid old password');
                              jQuery('#change').attr('disabled',true)
                        }
                  })
            }
            else{
                  jQuery('#peOld').show().html('Password at least 6 character long');
                  jQuery('#change').attr('disabled',true)
            }
      }
      if(action=='new'){
            var np = jQuery('#newpass').val();  
            if(np.length>=6){
                  jQuery('#penew').show().html('<img src="admin/img/icons/icon_approve.png"/>');
                  jQuery('#change').attr('disabled',false)
            }
            else{
                  jQuery('#penew').show().html('Password at least 6 character long');  
                  jQuery('#change').attr('disabled',true)
            }
      }
      if(action=='cf'){
            var newp = jQuery('#newpass').val();  
            var cfp = jQuery('#confpass').val();  
            if(cfp.length>=6){
                  if(newp == cfp){
                        jQuery('#pecf').show().html('<img src="admin/img/icons/icon_approve.png"/>'); 
                        jQuery('#change').attr('disabled',false)
                  }
                  else{
                        jQuery('#pecf').show().html('Password do not match');  
                        jQuery('#change').attr('disabled',true)
                  }
            }
            else{
                  jQuery('#pecf').show().html('Password at least 6 character long'); 
                  jQuery('#change').attr('disabled',true)
            }
        
      }
}




function whislist(id){
      var uid = jQuery('#uid').val();
      var pid = jQuery('#pid').val();
      jQuery('#w_love').html('<img src="images/load.gif"/>');
      jQuery.post('admin/ajax/?whis=1',{
            id:id,
            pid:pid,
            uid:uid
      },function(w){
            if(w=='OK')
                  jQuery('#w_love').html('<img title="This item already in your whislist" src="images/heart-active.png" border="0" style="margin-right: 8px;" alt="whislist"/>');
            else{
                  alert('Error');
            }
      });
   
}


function whis(action,id,uid){
      if(action=='del'){
            jQuery.post('admin/ajax/?whisdel=1',{
                  id:id,
                  uid:uid
            },function(d){
                  if(d=='OK'){
                        window.location.reload();
                  }
                  else{
                        alert('error');
                  }
            })
      }
}
function order(action,id,uid){
      if(action=='del'){
            if(confirm('Are you sure want to cancel this order?')){
                  jQuery.post('admin/ajax/?orddel=1',{
                        id:id,
                        uid:uid
                  },function(d){
                        if(d=='OK'){
                              window.location.reload();
                        }
                        else{
                              alert('error');
                        }
                  })
            }
      }
}
//function orders(uid,pid){
//      var qty = jQuery('select#qty option:selected').val();
//      jQuery.post('admin/ajax/?order=1', {
//            pid:pid,
//            uid:uid,
//            qty:qty
//      }, function(s){
//            if(s=='OK'){
//                  window.parent.location='myaccount.php?p=orders';
//            }
//            else{
//                  alert(s);
//            }
//      })
//}
