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

	$('#cancel').click(function(){
		parent.history.back();
        return false;
	});
	
	$('#close_button').click(function(){
	    window.open('','_self');
	    window.close();
	});
	
	$('#back').click(function(){
		parent.history.back();
        return false;
	});

	$('#order').click(function(){
		$('#errors').hide();
		if($('#name').attr("value") != "" && $('#name').attr("value") != "Required") {
			if($('#company').attr("value") != "" && $('#company').attr("value") != "Required") {
				$('#last_form').submit();
			}
			else {
				$('#errors').show();
				$('#errors_info').html(" PLEASE ENTER YOUR COMPANY NAME");
			}
		}
		else {
			$('#errors').show();
			$('#errors_info').html(" PLEASE ENTER YOUR NAME");
		}
	});
	
	$('#checkbox_confirm').click(function(){
		if($("#checkbox").val() == 1) {
			$("#checkbox").val(0)
			$("#checkbox_confirm").removeClass("checkbox_confirm_ON");
			$("#checkbox_confirm").addClass("checkbox_confirm_OFF");
		}
		else {
			$("#checkbox").val(1)
			$("#checkbox_confirm").removeClass("checkbox_confirm_OFF");
			$("#checkbox_confirm").addClass("checkbox_confirm_ON");
		}
		
	});
	
	$('#next').click(function(){
		$("#products_form").submit();
	});
	
	$('.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"); }});
	
	$('.keyboard li').click(function(){
		if($write == 0) {
			$write = $("#name");
		}
		if($write.attr("value") == "Required" || $write.attr("value") == "") {
			$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));
	});
});