$(document).ready(function(){
	
	
 $("input[name='submit']").attr("disabled", true); 
 
 
 
 if($("#text > h1:first").text() == "Requests"){
	requestsForm();
} else if($("#text > h1:first").text() == "Shopping Cart"){
 	checkoutForm();
 } else {
	commentsForm();
}



});


// Request form
	
function requestsForm(){
 var errors = new Array(4);
 
 $("input[name='name']").blur(function(){
	if($("input[name='name']").val() == "") {
		$("span#name").html("<i>Please enter your name.</i>");
		$("input[name='submit']").attr("disabled", true);
		 errors[0] = "Name";
	 } else {
		$("span#name").html("");
		errors[0] = "";
	}
 });
 
 $("input[name='email']").blur(function(){
	var emailRegex = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/i;
	if($("input[name='email']").val() == "@" || $("input[name='email']").val() == "") {
		$("span#email").html("<i>Please enter your email.</i>");
  		$("input[name='submit']").attr("disabled", true);
		errors[1] = "Email";
	} else if($("input[name='email']").val().search(emailRegex)==-1) {
 		$("span#email").html("<i>Please enter a valid email.</i>");
  		$("input[name='submit']").attr("disabled", true);
		errors[1] = "Email";
	} else {
		$("span#email").html("");
		errors[1] = "";
	}
 });
 
 $("input[name='title']").blur(function(){
	if($("input[name='title']").val() == "") {
		$("span#title").html("<i>Please enter a title.</i>");
		$("input[name='submit']").attr("disabled", true);
		 errors[2] = "Title";
	 } else {
		$("span#title").html("");
		errors[2] = "";
	}
 });
 
 $("input[name='artist']").blur(function(){
	if($("input[name='artist']").val() == "") {
		$("span#artist").html("<i>Please enter the artist.</i>");
		$("input[name='submit']").attr("disabled", true);
		 errors[3] = "Artist";
	 } else {
		$("span#artist").html("");
		errors[3] = "";
	}
 });
 
 
 $("input").keydown(function(){

 	if((errors[0]=="" && errors[1]=="" && errors[2]=="") || (errors[0]=="" && errors[1]=="" && errors[3]=="") || (errors[0]=="" && errors[2]=="" && errors[3]=="") || (errors[1]=="" && errors[2]=="" && errors[3]=="")){
		$("input[name='submit']").attr("disabled", false);
 	}
	
	
 });

 

 
 $("input[name='submit']").click(function(e) {
  	e.preventDefault();
		var name = $("input[name='name']").val();
		var email = $("input[name='email']").val();
		var title = $("input[name='title']").val();
		var artist = $("input[name='artist']").val();
		var comments = $("textarea").val();
 		$.ajax({
			type: "POST",
			url: "/requests/send/#contact",
			data: "name="+name+"&email="+email+"&title="+title+"&artist="+artist+"&comments="+comments,
			success: function(sentmsg){
				$("#contactbox").animate({opacity: "hide"}, "normal");
				$("#sentmsg").html(sentmsg);
			}
		});
 
 });
	
}
 
 
//Comments and contact forms
 
function commentsForm() {

 var errors = new Array(3);

 $("input[name='name']").blur(function(){
	if($("input[name='name']").val() == "") {
		$("span#name").html("<i>Please enter your name.</i>");
		$("input[name='submit']").attr("disabled", true);
		 errors[0] = "Name";
	 } else {
		$("span#name").html("");
		errors[0] = "";
	}
 });
 
 $("input[name='email']").blur(function(){
	var emailRegex = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/i;
	if($("input[name='email']").val() == "@" || $("input[name='email']").val() == "") {
		$("span#email").html("<i>Please enter your email.</i>");
  		$("input[name='submit']").attr("disabled", true);
		errors[1] = "Email";
	} else if($("input[name='email']").val().search(emailRegex)==-1) {
 		$("span#email").html("<i>Please enter a valid email.</i>");
  		$("input[name='submit']").attr("disabled", true);
		errors[1] = "Email";
	} else {
		$("span#email").html("");
		errors[1] = "";
	}
 });
 
 $("textarea[name='comments']").blur(function(){
	if($("textarea[name='comments']").val() == "") {
		$("span#comments").html("<i>Please enter your comments.</i>");
  		$("input[name='submit']").attr("disabled", true);
		errors[2] = "Comments";
	} else {
		$("span#comments").html("");
		errors[2] = "";
	}
 });
 
 $("input").keydown(function(){
	if((errors[1]=="" && errors[2]=="") || (errors[0]=="" && errors[2]=="")){
		$("input[name='submit']").attr("disabled", false);
	}
 });

 $("textarea").keydown(function(){
	if(errors[0]=="" && errors[1]==""){
		$("input[name='submit']").attr("disabled", false);
 	}
 });

 
 $("input[name='submit']").click(function(e) {
  	if($(this).attr("id") == "addcomments"){
		e.preventDefault();
		var name = $("input[name='name']").val();
		var email = $("input[name='email']").val();
		var url = $("input[name='url']").val();
		var comments = $("textarea").val();
		var art_id = $("input[name='art_id']").val();
 		$.ajax({
			type: "POST",
			url: "comments.php?art_id="+art_id+"&itemtype=gallery&action=add",
			data: "name="+name+"&email="+email+"&url="+url+"&comments="+comments,
			success: function(newcomment){
				$("#commentform").animate({opacity: "hide"}, "normal");
				$("#newcomment").html(newcomment);
				$("#nocomments").hide();
			}
		});
	}else if($(this).attr("id") == "sendemail"){
		e.preventDefault();
		var name = $("input[name='name']").val();
		var email = $("input[name='email']").val();
		var subject = $("input[name='subject']").val();
		var comments = $("textarea").val();
 		$.ajax({
			type: "POST",
			url: "/contact/send/#contact",
			data: "name="+name+"&email="+email+"&subject="+subject+"&comments="+comments,
			success: function(sentmsg){
				$("#contactbox").animate({opacity: "hide"}, "normal");
				$("#sentmsg").html(sentmsg);
			}
		});
	}
 
 });
 
	
}


// Shopping cart checkout form
 
function checkoutForm() {
 
 
 $("#total").hide();
 $("#orderform").hide();
 
 $("select").change(function(){
 //	$("option").click(function(){
  		//if($(this).text() != "") {
 			$("#orderform").slideDown("normal");
 			var country = $(this).attr("value");
			var quantity = $("#total_qty").text();
			var subtotal = $("#total_price").text();
			var type = $("#total_qty").attr("class");
  			$.ajax({
				type: "POST",
				url: "/cart/gettotal/",
				data: "country="+country+"&quantity="+quantity+"&subtotal="+subtotal+"&type="+type,
				success: function(total){
 					$("#total").slideDown("normal");
					$("#total").html(total);
				}
		//	});
	//	}
 	});
 });

 var errors = new Array();
 var errorsCount = 4;
 var emailRegex = /^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/i;
 
 $("input[name='name']").focus(function(){
  	if($("input[name='name']").val() == "") { // Field initially empty
		errors[0] = false;
	} else { // Field initially filled
  		errors[0] = true;
	}
 });

 $("input[name='name']").blur(function(){
	if($("input[name='name']").val() == "" && errors[0] == false) { // Field still empty; no change
		$("span#name").html("<i>Please enter your name.</i>");
		$("input[name='submit']").attr("disabled", true);
	} else if ($("input[name='name']").val() != "" && errors[0] == false) { // Field filled; errors--
		$("span#name").html("");
		errorsCount--;
	} else if($("input[name='name']").val() == "" && errors[0] == true) { // Field emptied; errors++
		$("span#name").html("<i>Please enter your name.</i>");
		$("input[name='submit']").attr("disabled", true);
		errorsCount++;
	} else if ($("input[name='name']").val() != "" && errors[0] == true) { // Field still filled; no change
		$("span#name").html("");
	}
 });
 
 
 
 $("input[name='email']").focus(function(){
  	if($("input[name='email']").val().search(emailRegex)==-1 || $("input[name='email']").val() == "") { // Field initially empty
		errors[1] = false;
	} else { // Field initially filled
  		errors[1] = true;
	}
 });
 
  $("input[name='email']").blur(function(){
	if(($("input[name='email']").val() == "" || $("input[name='email']").val().search(emailRegex)==-1) && errors[1] == false) { // Field still empty; no change
		$("span#email").html("<i>Please enter a valid email address.</i>");
		$("input[name='submit']").attr("disabled", true);
	} else if (($("input[name='email']").val() != "" || $("input[name='email']").val().search(emailRegex)==1) && errors[1] == false) { // Field filled; errors--
		$("span#email").html("");
		errorsCount--;
	} else if(($("input[name='email']").val() == "" || $("input[name='email']").val().search(emailRegex)==-1) && errors[1] == true) { // Field emptied; errors++
		$("span#email").html("<i>Please enter a valid email address.</i>");
		$("input[name='submit']").attr("disabled", true);
		errorsCount++;
	} else if (($("input[name='email']").val() != "" || $("input[name='email']").val().search(emailRegex)==1) && errors[1] == true) { // Field still filled; no change
		$("span#email").html("");
	}
 });
 
 
 
 $("input[name='address']").focus(function(){
  	if($("input[name='address']").val() == "") { // Field initially empty
		errors[2] = false;
	} else { // Field initially filled
		errors[2] = true;
	}
 });
 
 $("input[name='address']").blur(function(){
	if($("input[name='address']").val() == "" && errors[2] == false) { // Field still empty; no change
		$("span#address").html("<i>Please enter your address.</i>");
		$("input[name='submit']").attr("disabled", true);
	 } else if($("input[name='address']").val() != "" && errors[2] == false) { // Field filled; errors--
		$("span#address").html("");
		errorsCount--;
	} else if($("input[name='address']").val() == "" && errors[2] == true) { // Field emptied; errors++
		$("span#address").html("<i>Please enter your address.</i>");
		$("input[name='submit']").attr("disabled", true);
		errorsCount++;
	} else if($("input[name='address']").val() != "" && errors[2] == true) { // Field still filled; no change
		$("span#address").html("");
	 }
 });
 
 
 
  $("input[name='city']").focus(function(){
	if($("input[name='city']").val() == "") {
		 errors[3] = false;
	 } else {
		 errors[3] = true;
	}
 });
 
 
  $("input[name='city']").blur(function(){
	if($("input[name='city']").val() == "" && errors[3] == false) { // Field still empty; no change
		$("span#city").html("<i>Please enter the city.</i>");
		$("input[name='submit']").attr("disabled", true);
	 } else if($("input[name='city']").val() != "" && errors[3] == false) { // Field filled; errors--
		$("span#city").html("");
		errorsCount--;
	} else if($("input[name='city']").val() == "" && errors[3] == true) { // Field emptied; errors++
		$("span#city").html("<i>Please enter your city.</i>");
		$("input[name='submit']").attr("disabled", true);
		errorsCount++;
	} else if($("input[name='city']").val() != "" && errors[3] == true) { // Field still filled; no change
		$("span#city").html("");
	 }
 });
 
 
 
 
 $("input").keydown(function(){
 	if(errorsCount==1){
		$("input[name='submit']").attr("disabled", false);
 	}
 });
 

 $("input[name='reset']").click(function(e) {
	errorsCount = 4
 });

 
 $("input[name='submit']").click(function(e) {
	e.preventDefault();
	var name = $("input[name='name']").val();
	var email = $("input[name='email']").val();
	var country = $("option").val();
	var address = $("input[name='address']").val();
	var city = $("input[name='city']").val();
	var state = $("input[name='state']").val();
	var zipcode = $("input[name='zipcode']").val();
 	$.ajax({
		type: "POST",
		url: "/cart/checkout/",
		data: "name="+name+"&email="+email+"&country="+country+"&address="+address+"&city="+city+"&state="+state+"&zipcode="+zipcode,
		success: function(orderinfo){
			$("#orderform").animate({opacity: "hide"}, "normal");
			$("#orderinfo").html(orderinfo);
		}
	});
 });
 
 
}
	