$(function() {
//debug_validation();
  $('#frm_enter .text-input').css({backgroundColor:"#3D0625"});
  $('#frm_enter .text-input').focus(function(){
    $(this).css({backgroundColor:"#000000"});
	$(this).css({background:"none"});
  });
  $('#frm_enter .text-input').blur(function(){
    $(this).css({backgroundColor:"#3D0625"});
	//error check on blur
	checkit(this);
  });
  
  $(".radio-input").click(function(e) {	//add hidden variable when a radio button is clicked
	var $this = $(this);
	var id = $this.attr("id");
	var ans = $("input#ans").val();
	$("<input type='hidden' name='ans' value='"+id+"' id='ans'>").insertAfter("#postcode");
	});

  //IE and FF handle change of selects differently
  if ($.browser.msie) {
	  $('#venue').change(function(){
				 var $this = $(this);
				 var id = $("#venue").children("[@selected]").text(); 
				 $("<input type='hidden' name='venue' value='"+id+"' id='hdn_venue'>").insertAfter("#postcode");
		});
  }
  else
  {
  $("#venue").change(onSelectChange);
  	 $("#venue option").click(function(e) {
		var $this = $(this);
		var id = $this.attr("id");
		
		$("<input type='hidden' name='venue' value='"+id+"' id='hdn_venue'>").insertAfter("#postcode");
	});
  }
	 $("#country option").click(function(e) {
		var $this = $(this);
		var id = $this.attr("id");
		$("<input type='hidden' name='country' value='"+id+"' id='hdn_country'>").insertAfter("#postcode");
	});
  
  $("#submit_enter").click(function() {
	var errors=0;
	
	if (check_submit("#first_name")) errors = errors+1;
	if (check_submit("#sur_name")) errors = errors+1;
	if (check_submit("#enter_email")) errors = errors+1;	
	if (check_submit("#phone")) errors = errors+1;
	if (check_submit("#firstline_address")) errors = errors+1;
	if (check_submit("#towncity")) errors = errors+1;
	if (check_submit("#county")) errors = errors+1;
	if (check_submit("#postcode")) errors = errors+1;
			
	var alias = $("input#alias").val();
	var ans = $("input#ans").val();
	var hdn_venue = $("input#hdn_venue").val();
	var name = $("input#first_name").val()+" "+$("input#sur_name").val();
	var email = $("input#enter_email").val();
	var phone = $("input#phone").val();
	var secondline_address = $("input#secondline_address");
	var country = $("input#hdn_country").val();
	
	if (!secondline_address) secondline_address = "";
	else secondline_address = ", "+$("input#secondline_address").val();
	if (!country) country = "";
	else country = ". "+$("input#hdn_country").val();
	
	var address = $("input#firstline_address").val()+secondline_address+", "+$("input#towncity").val()+". "+$("input#county").val()+". "+$("input#postcode").val()+country;
	
	if (ans){$("#multiple_choice_radios").css({background:"url(images/frm_tick_radio.gif) no-repeat #000000 right top"});}
	else {$("#multiple_choice_radios").css({background:"url(images/frm_radio_cross.gif) no-repeat #000000 right top"});
	 errors = errors+1;
	}
	
	if (hdn_venue) $("#venue-row").css({background:"url(images/frm_select_tick.gif) no-repeat #000000 top right"});
	else {
		$("#venue-row").css({background:"url(images/frm_select_cross.gif) no-repeat #000000 top right"});
		errors = errors+1;
	}
	
	var ext = $("input#ext").val();

	if (errors==0){

		var dataString = 'name='+ name + '&email=' + email + '&alias=' + alias + '&ans=' + ans + '&venue=' + hdn_venue + '&phone=' + phone + '&address=' + address + '&source='+ext;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "script/enter.php",
      data: dataString,
      success: function(html) {
		$('#main-content').html("<div id='post_enter'></div>");
        $('#post_enter').html("<h4 class='highlight'>Thank You</h3>")
		 .append("<P>"+html+"</p>")
        .hide()
        .fadeIn(1500, function() {
          $('#message').append("<img id='checkmark' src='images/check.png' />");
        });
		$("<img src='images/hdr_wintickets.gif'>").insertBefore("h4.highlight");
      }
     });
		}
    return false;
	});
});

function checkit(elem)
{
	var $this = $(elem);
	var id = "#"+$this.attr("id")
	var elem_val = $(id).val();

	if (id=="#alias" || id=="#secondline_address"){ var n = 0; }
	else {
		if (elem_val=="") {
			$(elem).css({background:"url(images/frm_cross.gif) no-repeat #3D0625 top right"});

		}
		else {
			$(elem).css({background:"url(images/frm_tick.gif) no-repeat #3D0625 top right"});

		}
	}
	
	//email address
	if (id=="#enter_email")
	{
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!elem_val.match(re)) {
			$(elem).css({background:"url(images/frm_cross.gif) no-repeat #3D0625 top right"});
		} 
		else {
			$(elem).css({background:"url(images/frm_tick.gif) no-repeat #3D0625 top right"});	
		}
	}
		
}

function debug_validation()
{
	$("input#multiple_a").attr("checked","true");
	$("input#first_name").attr("value","Jon");
	$("input#sur_name").attr("value","Whittlestone");
	$("input#alias").attr("value","Jonnyspoon");
	$("input#enter_email").attr("value","Jon.whittlestone@gmail.com");
	$("input#phone").attr("value","0118 9816510");
	$("option#Leeds").attr("selected","true");
	$("input#firstline_address").attr("value","16, Lilian Close");
	$("input#towncity").attr("value","London");
	$("input#county").attr("value","London");
	$("input#postcode").attr("value","N16 0SG");
	
	$("<input type='hidden' name='venue' value='Leeds' id='hdn_venue'>").insertAfter("#postcode");
	$("<input type='hidden' name='ans' value='multiple_a' id='ans'>").insertAfter("#postcode");
}





function check_submit(elem)
{
	var elem_val = $(elem).val();
	if (elem=="#enter_email")
	{
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!elem_val.match(re)) {
			$(elem).css({background:"url(images/frm_cross.gif) no-repeat #3D0625 top right"});
			return true;
		}
		else if (elem_val=="") {
			$(elem).css({background:"url(images/frm_cross.gif) no-repeat #3D0625 top right"});
			return true;
		}
		else{
			$(elem).css({background:"url(images/frm_tick.gif) no-repeat #3D0625 top right"});
			return false;
		}
	}
	else
	{
		if (elem_val==""){
			$(elem).css({background:"url(images/frm_cross.gif) no-repeat #3D0625 top right"});
			return true;
		}
		else
		{
			$(elem).css({background:"url(images/frm_tick.gif) no-repeat #3D0625 top right"});
			return false;
		}
	}
}

 function onSelectChange(){  
    var selected = $("#venue option:selected");       
     var output = "";  
    if(selected.val() != 0){$("#venue-row").css({background:"url(images/frm_select_tick.gif) no-repeat #000000 top right"});}
	else $("#venue-row").css({background:"url(images/frm_select_cross.gif) no-repeat #000000 top right"});
 }  
