// ------------------ main -----------------------

$(document).ready(function () {
//document ready:

	//fix tranparent PNGs in IE6
	$(".pngfix").supersleight();
	
	//wire up the form behaviors	
	$(".required").each(function() //copy the text from the hidden labels to the adjacent text box
	{
		if (!$(this).next("input").attr("value")  || $(this).next("input").attr("value") == "")
		{
		  val = $(this).html();
		  $(this).next("input").attr("value",val);
		}	
	});
	
	$(".textBox").focus(function() //clear the sample text box when it is entered
	{	   
	   if($(this).prev("label").html() == $(this).attr("value")) //if the text matches the text of the adjacent label this it is sample text
	   {
		  $(this).attr("value","");
	   }	
	});
	
	$(".textBox").blur(function() //when leaving the text boxes, restore the sample text if nothing was entered
	{
		val = $(this).attr("value")
		if(typeof(val) === "undefined" || val.match(/^\s*$/) )
		{
		  val = $(this).prev("label").html()
		  $(this).attr("value",val);
		}		
	});
	
	$("#haiku1").keyup(function() //check and report the total haiku length while each character is typed
	{
		reportHaikuLength();
	});
	$("#haiku2").keyup(function() //check and report the total haiku length while each character is typed
	{
		reportHaikuLength();
	});
	$("#haiku3").keyup(function() //check and report the total haiku length while each character is typed
	{
		reportHaikuLength();
	});
	
	reportHaikuLength(); //show the initial remaining characters
		
	//wire up the example haiku list behavior
	$("h3.example").toggle(
      function () {
        $(this).addClass("expanded");
		$("div.example").show();
      },
      function () {
        $(this).removeClass("expanded");
		$("div.example").hide();
      }
    );
	
//end docuemnt ready
});


// ----------------- functions ---------------------

function validate()
{
	
	mess = "";

	if( isHaikuToLong() ){ mess += "The haiku is to long\n" };	
	
	//no text boxes can have sample text
	$(".textBox").each(function()
	{
	  textBoxValue = $(this).attr("value");
	  if( $(this).prev("label").html() == textBoxValue )
	  {
		 
		 textBoxValue = textBoxValue.replace(/\*/,"") + " is a required field\n";
		 mess += textBoxValue;
		 $(this).addClass("error");
	  }
	});
	
	if (!isEmail($("#Email").attr("value")))
	{
		mess  = mess +  "Invalid E-mail address\n";
		$("#Email").addClass("error");
	}
	
	if (!document.getElementById("Agree").checked)
	{
		mess  = mess +  "You must confirm you read the rules\n";
		$("#Agree").addClass("error");
	}
	
	if (mess != "") 
	{
		alert(mess);
		return false;
	}
	
	return true;
}

function isEmail(s)
{
	re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
	return re.test(s);
}

var totalCharsAllowed = 80;
function reportHaikuLength(){	
	var charsRemaining = totalCharsAllowed - haikuLength();
	if(charsRemaining < 0){
		//charsRemaining = 0;	//don't display negative numbers;
		$("#charCountWarning").show(); //show the limit warning
	}else{
		$("#charCountWarning").hide(); //hide the limit warning
	}
	$("#charsRemaining").html(charsRemaining);
}
function isHaikuToLong(){
	var isToLong = false;
	if(haikuLength() > totalCharsAllowed){isToLong = true;}
	return isToLong;
}
function haikuLength(){
	//var totalHaikuLength = $("#haiku1").attr("value").length + $("#haiku2").attr("value").length + $("#haiku3").attr("value").length;	
	var totalHaikuLength = 0;
	
	var haiku1 = $("#haiku1");
	if( $(haiku1).prev("label").html() != $(haiku1).attr("value") ){ //if the text matches the adjacent label, it is sample text. Don't count the sample text
		totalHaikuLength += $(haiku1).attr("value").length;
	}
	
	var haiku2 = $("#haiku2");
	if( $(haiku2).prev("label").html() != $(haiku2).attr("value") ){ //if the text matches the adjacent label, it is sample text. Don't count the sample text
		totalHaikuLength += $(haiku2).attr("value").length;
	}
	
	var haiku3 = $("#haiku3");
	if( $(haiku3).prev("label").html() != $(haiku3).attr("value") ){ //if the text matches the adjacent label, it is sample text. Don't count the sample text
		totalHaikuLength += $(haiku3).attr("value").length;
	}
	
	return totalHaikuLength;
}
