$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();
	
	$(".button").attr('disabled','disabled');
		
	  var name = $("input#name").val();
		if (name == "") {
      $("label#name_error").show();
      $("input#name").focus();
      return false;
    }
	
	  var age = $("input#age").val();
		if (age == "") {
      $("label#age_error").show();
      $("input#age").focus();
      return false;
    }
	
	  var gender = $("#gender").val();
		if (gender == "") {
      $("label#gender_error").show();
      $("#gender").focus();
      return false;
    }
	
	var email = $("input#email").val();
	var at = email.lastIndexOf("@");

	// Make sure the at (@) sybmol exists and  
	// it is not the first or last character
	if (at < 1 || (at + 1) === email.length) {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}
		

	// Make sure there aren't multiple periods together
	if (/(\.{2,})/.test(email)) {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}

	// Break up the local and domain portions
	var local = email.substring(0, at);
	var domain = email.substring(at + 1);

	// Check lengths
	if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255) {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}

	// Make sure local and domain don't start with or end with a period
	if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain)) {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}

	// Check for quoted-string addresses
	// Since almost anything is allowed in a quoted-string address,
	// we're just going to let them go through
	if (!/^"(.+)"$/.test(local)) {
		// It's a dot-string address...check for valid characters
		if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local)) {
			$("label#email_error").show();
			$("input#email").focus();
			return false;
		}
	}

	// Make sure domain contains only valid characters and at least one period
	if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1) {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}
	
	  var contact = $("input#contact").val();
		if (contact == "") {
      $("label#contact_error").show();
      $("input#contact").focus();
      return false;
    }
		
		var dataString = 'name='+ name + '&email=' + email + '&contact=' + contact + '&age=' + age + '&gender=' + gender;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "process/proc_girliciousGame.php",
      data: dataString,
      success: function() {
        $('#form').html("<object height=\"399\" width=\"714\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"><param value=\"gameShell_noTunein.swf\" name=\"movie\"/><param value=\"high\" name=\"quality\"/><embed height=\"399\" width=\"714\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" quality=\"high\" src=\"gameShell_noTunein.swf\"/></object>");
		$('#form').width(714);
		$('#form').height(410);
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#name").select().focus();
});
