// Place ID's of all required fields here.
rrequired = ["rname", "remail", "company", "rphone", "plate_type", "base_metal", "specifications", "dimensions", "rspamcheck"];
// If using an ID other than #email or #error then replace it here
remail = $("#remail");
errornotice = $("#error");
// The text to show up within a field when it is incorrect
emptyerror = "Please fill out this field.";
emailerror = "Please enter a valid e-mail.";

// Clears any fields in the form when the user clicks on them
$(":input, :textarea").focus(function(){		
   if ($(this).hasClass("needsfilled") ) {
		$(this).val("");
		$(this).removeClass("needsfilled");
   }
});

$(document).ready(function(){
	var button = $('#upload'), interval;
	var d=new Date();
    var directory = d.getTime();
	
	var uploader = new AjaxUpload(button, {
		action: '/upload-handler.php', 
		onSubmit : function(file, ext){
			// change button text, when user selects file			
			$('#uploading').show();
							
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
		},
		onComplete: function(file, response){
						
			window.clearInterval(interval);
						
			// enable upload button
			this.enable();
			$('#uploading').hide()
			// add file to the list
			$('<li></li>').appendTo('#files').text(file+' uploaded');
			$('#uploaddir').val('http://www.silvexinc.com/uploads/'+directory+'/');					
		}
	});
	uploader.setData({'directory':directory});
});

$('#rfqform').submit( function(){
	//Validate required fields
	for (i=0;i<rrequired.length;i++) {
		var input = $('#'+rrequired[i]);
		if ((input.val() == "") || (input.val() == emptyerror)) {
			input.addClass("needsfilled");
			input.val(emptyerror);
			errornotice.slideDown(750);
		} else if (input.hasClass('spam')) {
			var spamcheck = input.val().toLowerCase();
			spamcheck != 'hot'?input.addClass("needsfilled").val("Please type: hot"):input.val(spamcheck);
		} else {
			input.removeClass("needsfilled");
		}
	}
	// Validate the e-mail.
	if (!/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(remail.val())) {
		remail.addClass("needsfilled");
		remail.val(emailerror);
	}

	//if any inputs on the page have the class 'needsfilled' the form will not submit
	if ($(":input").hasClass("needsfilled")) {
		return false;
	} else {
		errornotice.slideUp(400);
		$('#rfqform').hide();
		$('#bar2').fadeIn(400);
		$.post('/sendform.php',{
			subject:$('#rsubject').val(),
			recipient:$('#rrecipient').val(),
			name:$('#rname').val(),
			company:$('#company').val(),
			email:$('#remail').val(),
			phone:$('#rphone').val(),
			fax:$('#fax').val(),
			plate_type:$('#plate_type').val(),
			base_metal:$('#base_metal').val(),
			other_base:$('#other_base').val(),
			specifications:$('#specifications').val(),
			dimensions:$('#dimensions').val(),
			qty:$('#qty').val(),
			method:$('#method').val(),
			packaging:$('#packaging').val(),
			certifications:$('input[name=certifications]').val(),
			files_uploaded_to:$('#uploaddir').val(),
			comments:$('#notes').val(),
			spam:$('#rspamcheck').val()},
		  
			function(data){
			  $('#bar2').hide();
			  $('#loader2').append(data).fadeIn(700);
		});
		return false;
	}

});

$('#base_metal').change(function() {
	if ($(this).val() == 'Other') {
		$('#otherHidden').fadeIn(700);
	} else {
		$('#otherHidden').hide();
		$('#otherHidden input').val(" ");
	}
});
