$(function() {
	$('.error').hide();
	$("#submit-form").click(function() {
		$('.error').hide();
		var name = $("input#name").val();
		if (name == "") {
			$("label#name_error").fadeIn(1000);
			$("input#name").focus().addClass("input-error");
			return false;
		} else {
			$("input#name").removeClass("input-error");
		}
	
		var email = $("input#email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		if (email == "") {
			$("label#email_error").fadeIn(1000);
			$("input#email").focus().addClass("input-error");
			return false;
		} else {
			$("input#email").removeClass("input-error");
		}
		
		if(!filter.test(email)){
			$("label#email_error").fadeIn(1000);
			$("input#email").focus().addClass("input-error");
			return false;
		} else {
			$("input#email").removeClass("input-error");
		}

		var contact = $("select#contact").val();
		if (contact == "") {
			$("label#contact_error").fadeIn(1000);
			$("select#contact").focus().addClass("input-error");
			return false;
		} else {
			$("select#contact").removeClass("input-error");
		}

		var subject = $("select#subject").val();
		if (subject == "") {
			$("label#subject_error").fadeIn(1000);
			$("select#subject").focus().addClass("input-error");
			return false;
		} else {
			$("select#subject").removeClass("input-error");
		}

		var message = $("textarea#message").val();
		if (message == "") {
			$("label#message_error").fadeIn(1000);
			$("textarea#message").focus().addClass("input-error");
			return false;
		} else {
			$("textarea#message").removeClass("input-error");
		}

		var date = $("input#date").val();
		var uip = $("input#uip").val();
		var pip = $("input#pip").val();
		var proxy = $("input#proxy").val();
	
		$('#submit_btn').val("Processing... Please wait..."); 
	
		var dataString = 'name=' + name + '&email=' + email + '&contact=' + contact + '&subject=' + subject + '&message=' + message + '&date=' + date + '&uip=' + uip + '&proxy=' + proxy + '&pip=' + pip;
		
		$.ajax({
			type: "POST",
			url: "process.php",
			data: dataString,
			success: function() {
				$('#form').html("<div id='messages'></div>");
				$('#messages').html("<h2>Message Sent!</h2><p></p><p>Thank you for sending a message to '" + contact + "'... The message will be replied to,  if required.</p>")
				.hide()    
				.fadeIn(1500);
			}
		});
		return false;
	});
	
	$("select#contact").change( function () { 
		$("#subject_loading").show();
		var name = $('#contact').val();
		$.ajax({ 
			type: "POST",
			url: "update.php", 
			data: "contact="+name,
			success: function(html){
				$("#updater").empty();
				$("#updater").append(html);
				$("#subject_loading").hide();
			}
		});
	});
	
});

function tryToDownload(urlSetting){
	document.getElementById('downloadForce').src = urlSetting
}
function toggleTog(id){
	if ($("#tog-" + id + " .title").hasClass("selected")) {
		$("#tog-" + id + " .content").slideUp("fast",function(){
			$("#tog-" + id + " .title").removeClass("selected");
		});
	} else {
		$("#tog-" + id + " .content").slideDown("slow",function(){
			$("#tog-" + id + " .title").addClass("selected");
		});
	}
}
