$.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() {
       // $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'username') { this.username(o) };
          if(o.name == 'firstname') { this.namecheck(o) };
          if(o.name == 'name') { this.namecheck(o) };
          if(o.name == 'address') { this.address(o) };
          if(o.name == 'address2') { this.address2(o) };
          if(o.name == 'company') { this.companycheck(o) };
          if(o.name == 'phone') { this.phone(o) };
          if(o.name == 'vatnumber') { this.vat(o) };
          if(o.name == 'captcha') { this.captcha(o) };
          if(o.name == 'workphone') { this.workphone(o) };
          if(o.name == 'fax') { this.workphone(o) };
          if(o.name == 'city') { this.city(o) };
          if(o.name == 'postcode') { this.postcode(o) };
          if(o.name == 'newpassword') { this.password(o) };
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'dob') { this.dob(o) };
        },
        username: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
			o.value = $.trim(o.value);
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'invalid characters');
            };
        },
        namecheck: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
		   o.value = $.trim(o.value);
           if (!o.value.match(user)) {
				if (o.value.length > 20) {
             		doError(o,'name too long');
				} else if (o.value.length < 1) {
             		doError(o,'name is empty');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'invalid characters');
            };
        },
        companycheck: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
		   o.value = $.trim(o.value);
           if (!o.value.match(user)) {
				if (o.value.length > 35) {
             		doError(o,'name bigger than 35 characters');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'invalid characters');
            };
        },
        address: function(o) {
          var user = /[(\*\(\)\[\]\+\?\:\;\'\"\`\~\$\^\&\<\>)+]/;
		   o.value = $.trim(o.value);
           if (!o.value.match(user)) {
				if (o.value.length > 35) {
             		doError(o,'address too long');
				} else if (o.value.length < 1) {
             		doError(o,'address is empty');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'invalid characters');
            };
        },
        address2: function(o) {
          var user = /[(\*\(\)\[\]\+\?\:\;\'\"\`\~\$\^\&\<\>)+]/;
		   o.value = $.trim(o.value);
           if (!o.value.match(user)) {
				if (o.value.length > 35) {
             		doError(o,'address too long');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'invalid characters');
            };
        },
        city: function(o) {
          var user = /[(\*\(\)\[\]\+\?\:\;\'\"\`\~\$\^\&\<\>)+]/;
		   o.value = $.trim(o.value);
           if (!o.value.match(user)) {
				if (o.value.length > 90) {
             		doError(o,'entry too long');
				} else if (o.value.length < 1) {
             		doError(o,'city is empty');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'invalid characters');
            };
        },
        phone: function(o) {
          var phonecheck = /^([\s\+\-0-9])*$/;
		   o.value = o.value.replace(/\s/g,"");
           if (o.value.match(phonecheck)) {
				if (o.value.length > 15) {
             		doError(o,'number too long');
				} else if (o.value.length < 10) {
             		doError(o,'number too short');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'not a number');
            };
        },
        workphone: function(o) {
          var phonecheck = /^([\s\+\-0-9])*$/;
		   o.value = o.value.replace(/\s/g,"");
           if (o.value.match(phonecheck)) {
				if (o.value.length > 15) {
             		doError(o,'number too long');
				} else if ((o.value.length > 0 ) && (o.value.length < 10)) {
             		doError(o,'number too short');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'not a number');
            };
        },
        vat: function(o) {
          var vatcheck = /^[a-zA-Z][a-zA-Z].*$/;
		   o.value = o.value.replace(/\s/g,"");
			if (o.value.length > 15) {
				doError(o,'vat number too long');
			} else if ((o.value.length > 0 ) && (o.value.length < 6)) {
				doError(o,'vat number too short');
			} else if (o.value.length > 0) {
			   if (o.value.match(vatcheck)) {
              	  doSuccess(o);
				} else {
				  doError(o,'incorrect vat number');
				};
			} else {
              doSuccess(o);
            }
        },
        captcha: function(o) {
          var vatcheck = /^[a-zA-Z0-9][a-zA-Z0-9].*$/;
		   o.value = o.value.replace(/\s/g,"");
			if (o.value.length > 6) {
				doError(o,'captcha too long');
			} else if ((o.value.length >= 0 ) && (o.value.length < 6)) {
				doError(o,'captcha too short');
			} else if (o.value.length > 0) {
			   if (o.value.match(vatcheck)) {
              	  doSuccess(o);
				} else {
				  doError(o,'incorrect captcha');
				};
			} else {
              doSuccess(o);
            }
        },
        postcode: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
		   o.value = $.trim(o.value);
           if (!o.value.match(user)) {
				if (o.value.length > 10) {
             		doError(o,'zipcode too long');
				} else if (o.value.length < 1) {
             		doError(o,'zipcode is empty');
				} else {
              	  doSuccess(o);
				}
            } else {
             doError(o,'invalid characters');
            };
        },
        email: function(o) {
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		   o.value = $.trim(o.value);
           if (o.value.match(email)) {
		  	    if (o.value.length > 50) {
             		doError(o,'e-mail too long');
				} else {
             	 	doValidate(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/;
		   o.value = $.trim(o.value);
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'not a valid date');
            };
        }
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:right;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:right;" />');
              $('#' + 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="images/loading.gif" border="0" style="float:left;" />');
            var currentid = "";
            if ($('#accountid').size()) {
                currentid = $('#accountid').val();
            }
        	$.post('checkemail.php', { id: o.id, value: o.value, recordid : currentid }, 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="images/loading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="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="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()
{
  $('#javascriptcheck').hide();
  $('#javascriptcheck').html("");

  $("//[@class=validate]/input").blur(function() {
          $(this).validate.init(this);
  });
  $("//[@class=validate]/input").keyup(function() {
    $('#action').val("verify");
  });


  if ($('form.valcheck').size()) {
    $("//[@class=validate]/input").blur();
  }
  

  $("#confirmpass").blur(function() {
          $(this).match({match: '#password', error: '#confirmpass_li'});
  });


  $("#password").keyup(function() {
          $(this).copyTo("#password_copy","value");
  });


  $('#register').submit(function() {
//        $("//[@class=validate]/input").blur();

//	    checkupsaddress(); 
		if ($('#action').val() == 'verify') {
	        $("//[@class=validate]/input").blur();
			if ($("//[@class*=error]/input").size() > 0) {
			   $('#upserror').html('Please fix the values that have been marked as incorrect.');
				return false;
			} else {
				checkupsaddress();
				return false;
			}
		}
//		if ($('#upserror').html() == "") {
//			alert("true");
//			return false;
//		} else {
//			alert("false");
//			return false;
//		}
   });

  // This Used To Be HOVER, But I.E. Didnt Like It
  //$(".form li").hover(function(){$(this).addClass("selected");},function(){$(this).removeClass("selected");});
  //$("form#register li").mouseover(function() {
  //        $(this).addClass("selected");
  //});
  //$("form#register li").mouseout(function() {
  //        $(this).removeClass("selected");
  //});
});

function checkupsaddress() {
	var company = $('#company').val();
	var name = $('#name').val();
	var firstname = $('#firstname').val();
	var city = $('#city').val();
	var address = $('#address').val();
	var postcode = $('#postcode').val();
	var phone = $('#phone').val();
	var countryid = $('#countryid').val();

    $('#action').val("...please wait...");

	$.post('addresscheck.php', { 
		company: company, 
		name: name, 
		firstname: firstname, 
		phone: phone, 
		address: address, 
		city: city, 
		countryid: countryid, 
		postcode: postcode 
	}, function(json) {
			eval("var args = " + json);
			if (args.response_status_code == 1)
			{
			  $('#action').val("submit");
			  $('#register').submit();
			  return true;
			} else {
			  $('#upserror').html("UPS address check failed: " + args.error_description);
			  $('#action').val("verify");
			  return false;
			}
		  });
}


