$.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 == 'email') { 
		  	this.email(o) 
		  } else if (o.name == 'address') {
		  	this.street(o) 
		  } else if (o.name == 'dayphone') {
		  	this.phone(o) 
		  } else if (o.name == 'zipcode') {
		  	this.zipcode(o) 
          } else { 
		  	this.commonstring(o) 
		  };		
        },
        commonstring: function(o) {
          var user = /[(\*\(\)\[\]\+\.\,\/\?\:\;\'\"\`\~\\#\$\%\^\&\<\>)+]/;
           if (!o.value.match(user)) {
             doValidate(o);
            } else {
             doError(o,'Please, no special characters.');
            };
        },
        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,'Please enter a valid email address');
            };
        },
        zipcode: function(o) {
          var zipcode  = /^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/;
           if (o.value.match(zipcode)) {
              doSuccess(o);
            } else {
              doError(o,'Please enter a valid zip code');
            };
        },
        street: function(o) {
          var street  = /^[a-zA-Z\d]+(([\'\,\.\- #][a-zA-Z\d ])?[a-zA-Z\d]*[\.]*)*$/;
           if (o.value.match(street)) {
              doSuccess(o);
            } else {
              doError(o,'Please enter a valid street address');
            };
        },
		phone: function(o) {
		   var phone = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;	
           if (o.value.match(phone)) {
              doSuccess(o);
            } else {
              doError(o,'Please enter a valid phone number');
            };
		}
     };

     function doSuccess(o) {
              $('#' + o.id + '_img').html('<img src="http://images.studiokraft.com/images/icons/famfam/accept.png" border="0" style="float:left;" />');
              $('#' + o.id + '_li').removeClass("error");
              $('#' + o.id + '_msg').html("");
              $('#' + o.id + '_li').addClass("success");
     }

     function doError(o,m) {
              $('#' + o.id + '_img').html('<img src="http://images.studiokraft.com/images/icons/famfam/exclamation.png" border="0" style="float:left;" />');
              $('#' + o.id + '_li').addClass("error");
              $('#' + o.id + '_msg').html(m);
              $('#' + o.id + '_li').removeClass("success");
     }
     function doValidate(o) {
        	$('#' + o.id + '_img').html('<img src="http://images.studiokraft.com/images/tinyloading.gif" style="float:left;" />');
			var thisString = o.value;
			if (thisString.length<=0) {
               doError(o,"Please enter a value into the form.");
			} else {
        	$.post('http://www.studiokraft.com/index.php', { action: 'ajax_verify', 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="http://images.studiokraft.com/images/tinyloading.gif" border="0" style="float:left;" />');
	if ($(this).get(0).val() == $(m.match).val()) {
          $('#' + this.get(0).id + '_img').html('<img src="http://images.studiokraft.com/images/icons/famfam/accept.png" 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="http://images.studiokraft.com/images/icons/famfam/exclamation.png" 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(){
	$("input.validated").blur(function(){
		$(this).validate.init(this);
	});
	$("#contact_form").submit(function(){
		var skError = "";
		var sk_params = "action=contact-us-new&process=add_new&aj=1";
		var nameField = $("#contact_form #name").val();
		if (nameField.length<=4) {
			skError = skError + "* Please enter your full name into the form." + "\r\n";	
		} else {
			sk_params = sk_params + "&name=" + nameField;	
		}
		var emailField = $("#contact_form #email").val();
		if (emailField.length<=4) {
			skError = skError + "* Please enter your e-mail address into the form." + "\r\n";	
		} else {
			if (skValidateEmail(emailField)) {
				sk_params = sk_params + "&email=" + emailField;	
			} else {
				skError = skError + "* Please enter a valid e-mail address into the form." + "\r\n";			
			}
		}
		var zipcodeField = $("#contact_form #zipcode").val();
		if (zipcodeField.length<=4) {
			skError = skError + "* Please enter your zip code into the form." + "\r\n";	
		} else {
			sk_params = sk_params + "&zipcode=" + zipcodeField;	
		}
		var commentsField = $("#contact_form #comments").val();
		if (commentsField.length<=4) {
			skError = skError + "* Please enter your questions or comments into the form." + "\r\n";	
		} else {
			sk_params = sk_params + "&comments=" + commentsField;	
		}

		var captchaField = $("#contact_form #captcha").val();
		if (captchaField.length<=2) {
			skError = skError + "* Please enter the characters from the box into the form." + "\r\n";	
		} else {
			sk_params = sk_params + "&captcha=" + captchaField;	
		}

		var phoneField = $("#contact_form #dayphone").val();
		if (phoneField.length>=4) {
			sk_params = sk_params + "&dayphone=" + phoneField;	
		}
	
		var cityField = $("#contact_form #city").val();
		if (cityField.length>=4) {
			sk_params = sk_params + "&city=" + cityField;	
		}

		var stateField = $("#contact_form #state").val();
		if (stateField.length==2) {
			sk_params = sk_params + "&state=" + stateField;	
		}

		var addressField = $("#contact_form #address").val();
		if (addressField.length>=4) {
			sk_params = sk_params + "&address=" + addressField;	
		}


		if (skError.length>5) {
			alert(skError);
			$("#contact_form #submit").val("TRY AGAIN");
		} else {
			$("#contact_form #submit").val("VALIDATING...");
			$.ajax({
				type: "POST",
				url: "/index.php",
				dataType: "text",
				data: sk_params,
				success: function(msg){
					if (msg != "SUCCESS") {
						$("#contact_form #submit").val("TRY AGAIN");
						alert(msg);
					} else {
						$("#contact_form #submit").val("THANK YOU!");
						$("#contact_form #submit").attr('disabled', 'disabled');
						$("#contact_form_response").html("<span style=\"color:green;\">Thank you for contacting us - your message has been received and we will reply to you ASAP.</span>");
					}
				},
				error: function(msg){
					$("#contact_form #submitBut").val("TRY AGAIN");
					alert("Submission Failed: " + msg);			
					$("#contact_form_response").html(msg);
				}
			});
		}
		return false;
	});
});
