$(function(){
	$('#errors').hide();
	var $write = 0;
		shift = false;
		capslock = false;
	
	$('#cancel').click(function(){
	    window.open('','_self');
	    window.close();
	});
	
	$('#close_button').click(function(){
	    window.open('','_self');
	    window.close();
	});
	
	$('#back').click(function(){
		parent.history.back();
        return false;
	});

	$('#next').click(function(){
		$('#errors').hide();
		if($('#name').attr("value") != "" && $('#name').attr("value") != "Required") {
			if($('#company').attr("value") != "" && $('#company').attr("value") != "Required") {
				$('#first_form').submit();
			}
			else {
				$('#errors').show();
				$('#errors_info').html(" PLEASE ENTER YOUR COMPANY NAME");
			}
		}
		else {
			$('#errors').show();
			$('#errors_info').html(" PLEASE ENTER YOUR NAME");
		}
	});
	
	
	$('#submit').click(function(){
		$('#errors').hide();
		if($('#phone').attr("value") != "" && $('#phone').attr("value") != "Required") {
			if($('#issue').attr("value") != "" && $('#issue').attr("value") != "Please be brief...") {
				$('#last_form').submit();
			}
			else {
				$('#errors').show();
				$('#errors_info').html(" PLEASE ENTER YOUR ISSUE");
			}
		}
		else {
			$('#errors').show();
			$('#errors_info').html(" PLEASE ENTER YOUR PHONE");
		}
	});
	
	$('.plus').click(function(){
		var formID = $(this).attr("title");
		var currentQuantity = parseInt($("#quantity_" + formID).html());
		$("#quantity_" + formID).html(currentQuantity + 1);
		$("#form_"+formID).val(currentQuantity + 1);
	});
	
	$('.minus').click(function(){
		var formID = $(this).attr("title");
		var currentQuantity = parseInt($("#quantity_" + formID).html());
		if(currentQuantity > 0) {
			$("#quantity_" + formID).html(currentQuantity - 1);
			$("#form_"+formID).val(currentQuantity - 1);
		}
	});
	
	$('#name').click(function(){
		$write = $('#name')
		if($('#name').attr("value") == "Required") { $('#name').attr("value",""); }
	});
	$('#name').blur(function(){ if($('#name').attr("value") == "") { $('#name').attr("value","Required"); }});
	
	$('#company').click(function(){ 
		$write = $('#company')
		if($('#company').attr("value") == "Required") { $('#company').attr("value",""); }
	});
	$('#company').blur(function(){ if($('#company').attr("value") == "") { $('#company').attr("value","Required"); }});
	
	$('#phone').click(function(){
		$write = $('#phone')
		if($('#phone').attr("value") == "Required") { $('#phone').attr("value",""); }
	});
	$('#phone').blur(function(){ if($('#phone').attr("value") == "") { $('#phone').attr("value","Required"); }});
	
	$('#issue').click(function(){
		$write = $('#issue')
		if($('#issue').attr("value") == "Please be brief...") { $('#issue').val(""); }
	});
	$('#issue').blur(function(){ if($('#issue').attr("value") == "") { $('#issue').attr("value","Please be brief..."); }});
	
	
	$('.keyboard li').click(function(){
		if($write == 0) {
			$write = $("#name");
		}
		if($write.attr("value") == "Required" || $write.attr("value") == "" || $write.attr("value") == "Please be brief...") {
			$write.attr("value","");
		}
		var $this = $(this),
			character = $this.html(); // If it's a lowercase letter, nothing happens to this variable
		
		// Shift keys
		if ($this.hasClass('left-shift') || $this.hasClass('right-shift')) {
			$('.letter').toggleClass('uppercase');
			$('.symbol span').toggle();
			
			shift = (shift === true) ? false : true;
			capslock = false;
			return false;
		}
		
		// Caps lock
		if ($this.hasClass('capslock')) {
			$('.letter').toggleClass('uppercase');
			capslock = true;
			return false;
		}
		
		// Delete
		if ($this.hasClass('delete')) {
			var html = $write.attr("value");
			
			$write.attr("value",(html.substr(0, html.length - 1)));
			return false;
		}
		
		// Special characters
		if ($this.hasClass('symbol')) character = $('span:visible', $this).html();
		if ($this.hasClass('space')) character = ' ';
		if ($this.hasClass('tab')) character = "\t";
		if ($this.hasClass('return')) character = "\n";
		
		// Uppercase letter
		if ($this.hasClass('uppercase')) character = character.toUpperCase();
		
		// Remove shift once a key is clicked.
		if (shift === true) {
			$('.symbol span').toggle();
			if (capslock === false) $('.letter').toggleClass('uppercase');
			
			shift = false;
		}
		
		// Add the character
		$write.attr("value",($write.attr("value") + character));
	});
});