$.fn.copyTo = function(to) {
    var to = $(to);
    for ( var i = 1; i < arguments.length; i++ )
        to.set( arguments[i], this.get(0)[ arguments[i] ] );
    return this;
};

new function() {
    var show_messages= false; //show success, error messages
    var simple_validating= true;//stndard validating =true, advanced = false
        //$.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
        
          if(o.name == 'Email')         { this.email(o) }
          else if(o.name == 'Telephone'){ this.phone(o) }
          else if(o.type == 'text')     { this.text(o) }
/*
          if(o.name == 'username') { this.username(o) };
          if(o.name == 'password') { this.password(o) };
          if(o.name == 'email')    { this.email(o) };
          if(o.name == 'dob')      { this.dob(o) };
          if(o.name == 'phone')    { this.phone(o) };*/
          else if(o.name == 'pulldown') { this.pulldown(o) };
        },
        text: function(o) {
           if (!o.value=="") {
             doSuccess(o);
            } else {
             //doPass(o);
             doError(o,'Field is empty');
            };
        }, 
        pulldown: function(o) {
           if (!o.value=="") {
             doSuccess(o);
            } else {
             doError(o,'not selected');
            };
        },
        phone: function(o) {
          //var phone = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>\[a\-zA\-Z\])+]/;
          //var phone = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
        //working
          //var phone = /^([(0-9\-\(\)])+([0-9]){1}$/;
          //var phone = /^(([0-9\-]{0,2})(\([0-9]+\)[0-9\-\ ]+)|([0-9\-\ ]+))([0-9]){1}$/;
          var phone = /^(([a-zA-Z0-9][\-]?)?(\([a-zA-Z0-9]+\)[a-zA-Z0-9\-\ ]+)|([a-zA-Z0-9\-\ ]+))([0-9]){1}$/;
           if ((o.value.match(phone))&&(o.value!="Phone")) {
           //if ((o.value!="Phone")) {
             doSuccess(o);
            } else {
             doError(o,'no special characters allowed');
            };
        },
        username: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if ((!o.value.match(user))&&(o.value!="Full Name")) {
             doSuccess(o);
             //doValidate(o);
            } else {
             doError(o,'no special characters allowed');
            };
        },
        password: function(o) {
          var pass = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(pass)) {
             doSuccess(o);          
             //doValidate(o);
            } else {
             doError(o,'no special characters allowed');
            };
        },
        email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(email)) {
              doSuccess(o);
            } else {
              doError(o,'not a valid email');
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'not a valid date');
            };
        }
     };
	//if not error or success (entered nothing)
     function doPass(o) {
          if (simple_validating)
            {
            $('#' + o.id ).removeClass("success");
            $('#' + o.id ).removeClass("error");
	    }
	}
     function doSuccess(o) {
          if (simple_validating)
            {
            $('#' + o.id ).addClass("success");
            $('#' + o.id ).removeClass("error");
            }
          else
            {       
              $('#' + o.id + '_img').html('<img src="'+BASEURL+'images/validate/accept.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
             } 
     }

     function doError(o,m) {
          if (simple_validating)
            {           
            $('#' + o.id ).addClass("error");
            $('#' + o.id ).removeClass("success");
            }
          else
            {       
              $('#' + o.id + '_img').html('<img src="'+BASEURL+'images/validate/exclamation.gif" border="0" style="float:left;" />');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
             } 
     }
     //private helper, validates each type after check
     function doValidate(o) {
        	$('#' + o.id + '_img').html('<img src="'+BASEURL+'images/loading.gif" border="0" style="float:left;" />');
        	$.post('ajax.php', { id: o.id, value: o.value }, function(json) {
                  	eval("var args = " + json);
                        if (args.success == true)
                  	{
                  	  doSuccess(args);
                  	}
                  	else
                  	{
                          doError(args,args.msg);
                  	}
                  });
    };

};

$.fn.match = function(m) {
	$('#' + this.get(0).id + '_img').html('<img src="'+BASEURL+'images/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="'+BASEURL+'images/accept.gif" border="0" style="float:left;" />');
          $(m.error).removeClass("error");
          $(m.error).addClass("success");
          $('#' + this.get(0).id + '_msg').html("");
        } else {
          $('#' + this.get(0).id + '_img').html('<img src="'+BASEURL+'images/exclamation.gif" border="0" style="float:left;" />');
          $(m.error).addClass("error");
          $(m.error).removeClass("success");
          $('#' + this.get(0).id + '_msg').html("The passwords don't match, please try again");
        };
};
$(document).ready(function()
{

/*  $("div#shedule_top_box .validated").keyup(function(e) {
  	if(e.keyCode == 13) {
  		bookapp_submit_top();	
  	}
  });
  $("div#shedule_box .validated").keyup(function(e) {
  	if(e.keyCode == 13) {
  		submit();	
  	}
  });  */
//$(".validated").addClass("error");
/*if ($.fn.simple_validating)
{*/
  /*$("#i_top_email").blur(function() {
      $(this).validate.init(this);
  });
  */
  //add to components with class validated these

  $(".validated").blur(function() {
      $(this).validate.init(this);
  });
  //for pulldowns
  $(".validated").change(function() {
      $(this).validate.init(this);
  });
/*  jQuery("//[@class=validated]/input").blur(function() {
//  $("//[@class=validated]/input").blur(function() {
          $(this).validate.init(this);
  });
*/
/*  }
else  
{
  $("#confirmpass").blur(function() {
          $(this).match({match: '#password', error: '#confirmpass_li'});
  });


  $("#password").keyup(function() {
          $(this).copyTo("#password_copy","value");
  });

  // This Used To Be HOVER, But I.E. Didnt Like It
  //$(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  $(".form li").mouseover(function() {
          $(this).addClass("selected");
  });
  $(".form li").mouseout(function() {
          $(this).removeClass("selected");
  });

}*/ 
});

