(function($) { 
var regs = {email:/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,password:/[^\s]{6,15}/i,zip:/\d{5}/,phone:/\d{10}/i};
var def = {
    alert:false, // show alert message
    h:false, // show hint icon
    r: false, // regular express 
    msg:'', // error message
    hint:''// error icon
};
$.fn.hint = function(options){  
    this.options = $.extend({}, def, options || {});  
    if( this.options.alert && this.options.msg.length > 0) alert(this.options.msg);
    if(!this.options.h || this.options.hint.length == 0 ) return this;
    var s = '/images/wizard/';
     
    if($(this).next().length>0 && $(this).next().hasClass("hint")){
	  if(this.options.hint == "load")
	    $(this).next().attr('src',s+'load.png');
	  else if(this.options.hint == "good")
	    $(this).next().attr('src',s+'good.png');
	  else if(this.options.hint == 'error')
	    $(this).next().attr('src',s+'bad.png');
	}else{
	if(this.options.hint == "load")
	    $(this).after('<img src="'+s+'load.png" class="hint" />');
	  else if(this.options.hint == "good")
	     $(this).after('<img src="'+s+'good.png" class="hint" />');
	  else if(this.options.hint == 'error')
	     $(this).after('<img src="'+s+'bad.png" class="hint" />');
	}
	
	return this;
};

$.fn.valreg = function(options){
   var self = $(this);
   if(!options.r.test(self.val().trim())){
        options.hint = 'error';
        self.hint(options);
        self.focus();
      return false;
   }
   options.msg = '';
   options.hint = 'good';
   self.hint(options);
   return true;
};

$.fn.valreq = function(options){
options.r = /\w+/i;
return $(this).valreg(options);
}
$.fn.valemail = function(options){
if(typeof options == 'undefined') options = {};
options.r = regs.email;
options.msg = 'please correct your email.';
return $(this).valreg(options);
};
$.fn.valpwd = function(options){  
options.r = regs.password;
options.msg = 'Password length must between 6-15';
return $(this).valreg(options);
};
$.fn.valzip = function(options){ 
options.r = regs.zip;
options.msg = 'Zip code length not equal to 5';
return $(this).valreg(options);
};
$.fn.valphone = function(options){
   options.r = regs.phone;
   options.msg = 'phone number length not equal to 10.';
 return $(this).valreg(options);};
$.fn.checkvemail=function(options){
     var self = $(this);
     if(!regs.email.test(self.val())){    self.hint({hint:'error'});     return this;}
     
     $(this).hint({h:true,hint:'load'});
     // register:1,login:2,forgot:3
	 $.post('/service/account.ashx',{email:$(this).val(),checkable:options.l}, function(res){
            if(!res.success){
                options.msg = res.message;
                options.hint = 'error';
                self.hint(options);
            }else{
                options.msg = '';
                options.hint = 'good';
                self.hint(options);
            } 
      }, 'json');
      return self;	
};

$.fn.confirmpass = function(options){
	if($(this).val() !=$(options.e).val() ){
	   options.hint='error';
	   options.msg ='Passwords mismatch!';
	   $(this).hint(options);
			return false;
	 }
	 options.hint='good';
	 $(this).hint(options);
	 return true;
};

$.fn.chkCCDate = function(options) {
      options.msgBox.html(" ");

      if(options.year.get(0).selectedIndex <= 0){
           options.msgBox.html("Please select year!");
          return false;
      }
 
      if(options.month.get(0).selectedIndex <= 0){
          options.msgBox.html("Please select month!");
          return false;
      }

       var today = new Date();
       var myDate = new Date();
       myDate.setYear(year.val());
       myDate.setMonth(month.val());
       
       if(myDate > today){return true; }
      
       options.msgBox.html("Credit card expired!");
       return false;
};

$.fn.validateLogin= function(options)	{
	var ret = true;
	var self = $(this);
	if(!self.find(".email").valemail(options)) ret = false;
    if(!self.find(".password").valpwd(options)) ret = false;
	return ret;
};

$.fn.caps = function(cb){
    return this.keypress(function(e){
        var w = e.which ? e.which : (e.keyCode ? e.keyCode : -1);
        var s = e.shiftKey ? e.shiftKey : (e.modifiers ? !!(e.modifiers & 4) : false);
        var c = ((w >= 65 && w <= 90) && !s) || ((w >= 97 && w <= 122) && s);
        cb.call(this, c);
    });
};
	
$.fn.validateRegister=function(options){
     if($(this).length == 0) return false;

	var self = $(this);
	if(!self.find(".email").valemail(options)) return false;
    if(!self.find(".password").valpwd(options)) return false;
	if(!self.find(".password2").valpwd(options))return false;
	if(!self.find(".password").confirmpass({e:self.find(".password2")})) return false;
	if(!self.find(".fname").valreq(options) )return false;
	if(!self.find(".lname").valreq(options) ) return false;
	if(self.find(".state").val() == 0){self.find(".state").focus(); return false;}
	if(!self.find(".zip").valzip(options))return false;
    if(!self.find(".phone").valphone(options)) return false;
	
	return true;
};
})(jQuery);	


   