$(function(){
		$("button:disabled").fadeTo(200,.5);

		$("li.colors label").click(function(){ 
			var color = $(this).find(":input").val();
			$(this).parents("li").find(".selected").removeClass("selected");
			$(this).addClass("selected");
			if ($("#quantity").val() !== "") $("#addtocart").removeAttr("disabled").fadeTo(200,1);
			var imagecontainer = $(".image-example", $(this).parents("fieldset"));
			imagecontainer.find("img").attr("src","/images/"+color.trim()+".jpg").attr("alt",color+" color example");
			imagecontainer.find("span.caption").text($(this).text().trim());
		});

		$(".quantity").change(function() {
			if (!isValid($(this).val(),"^[0-9]+$")) {
				addError("Quantity must be numeric.", $(this)[0]);
				$(this).val("");
			} else removeError($(this)[0]);		
		});
		$(".previousimg:first").attr("disabled","disabled").fadeTo(200,.5);
		$("#start_order #quantity").change(function() {
			if ($(this).val() == "") $("#addtocart").attr("disabled","disabled").fadeTo(200,.5);
			else if ($("[name='color']").val() !== "") $("#addtocart").removeAttr("disabled").fadeTo(200,1);
		});
		
	if ($('#upload').length) {
		$('#file').change(function() {
			if ($(this).val() != "") {
				$('#message').attr("disabled","disabled").parents("p").fadeTo(200,.5);	
			} else {
				$('#message').removeAttr("disabled").parents("p").fadeTo(200,1);
			}
		});
		$('#message').change(function() {
			if ($(this).val() != "") {
				$('#file').attr("disabled","disabled").parents("p").fadeTo(200,.5);	
			} else {
				$('#file').removeAttr("disabled").parents("p").fadeTo(200,1);
			}
		});
		$('#fwidth').change(function() {
			if (!isValid($(this).val(),"^[0-9.]+$")) {
				addError("Value must be numeric.", $(this)[0]);
				$(this).val("");
			}
			else if($(this).val() > 24) {
				addError("Value must be no greater than 24.", $(this)[0]);
				$(this).val("");
			} else removeError($(this)[0]);		
		});
		$('#fheight').change(function() {
			if (!isValid($(this).val(),"^[0-9.]+$") || $(this).val() > 31) {
				addError("Value must be numeric and no greater than 31.", $(this)[0]);
				$(this).val("");
			} else removeError($(this)[0]);
		});
		
		$('#upload').submit(function() {
			if (!isValid($('#fheight').val(),"^[0-9.]+$"))
			{
				addError("Please resolve all errors and make sure to complete all fields.1", $('#upload_img')[0]);
				return false;
			}
				
			else if($('#fheight').val() > 31)
			{
				addError("Please resolve all errors and make sure to complete all fields.2", $('#upload_img')[0]);
				return false;
			}
			else if(!isValid($('#fwidth').val(),"^[0-9.]+$"))
			{
				addError("Please resolve all errors and make sure to complete all fields.3", $('#upload_img')[0]);
				return false;
			} 
			else if($('#fwidth').val() > 24)
			{
				addError("Please resolve all errors and make sure to complete all fields.4", $('#upload_img')[0]);
				return false;
			}
			else if(($("#message").val() == "" && $("#file").val() == ""))
			{
				addError("Please resolve all errors and make sure to complete all fields.5", $('#upload_img')[0]);
				return false;
			}				
			else if($("[name='position']").val() == "")
			{
				addError("Please resolve all errors and make sure to complete all fields.6", $('#upload_img')[0]);
				return false;
			}
			return true;
		});
		
	}		
});

String.prototype.trim = function() {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function isValid(str,regex) {
  var format = new RegExp(regex); 
  var matches = format.exec(str);
  return matches;
}
function addError(text, element){
	if (element.parentNode.lastChild.tagName == "SPAN") return false;
	var span = document.createElement('span');
	span.className="error";
	if (document.createTextNode){
		var mytext=document.createTextNode(text);
		span.appendChild(mytext);
		element.parentNode.appendChild(span);
	}
	return true;
}
function removeError(element){
	$(element).parent().find(".error").remove();
}

