// JavaScript Document

<!--


function validateReferFriend(theForm) {

  var reason = "";

  reason += validateName(theForm.Friend1Name);
  reason += validateEmail(theForm.Friend1Email);
  reason += validateFriendEmail(theForm.Friend2Email);
  reason += validateFriendEmail(theForm.Friend3Email);
  
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  
  step3();
}




function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateName(theForm.Name);
  reason += validatePhone(theForm.Phone);
  reason += validateEmail(theForm.Email);
  reason += validateMessage(theForm.Message);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#FFFFCC'; 
        error = "You did not enter a name.\n"
    } else {
        fld.style.background = '#F0F8FA';
    }
    return error;  
}
function validateMessage(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#FFFFCC'; 
        error = "You did not enter a comment/query.\n"
    } else {
        fld.style.background = '#F0F8FA';
    }
    return error;  
}

function validateEmail(fld) {
    var error="";
    var tfld = jQuery.trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#FFFFCC';
        error = "You did not enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFFFCC';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFFFCC';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = '#F0F8FA';
    }
    return error;
}

function validateFriendEmail(fld) {
	if (fld.val() != "")
	{
		var tfld = jQuery.trim(fld.val());                        // value of field with whitespace trimmed off
		var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
		
		if (!emailFilter.test(tfld)) {      
			fld.css("background","#FFFFCC");
			return false;
		} else if (fld.val().match(illegalChars)) {
			fld.css("background","#FFFFCC");
			return false;
		} else {
			
			fld.css("background","#F0F8FA");
			return true;
		}
	}
	return false;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You did not enter a phone number.\n";
        fld.style.background = '#FFFFCC';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#FFFFCC';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = '#FFFFCC';
    }
	else {
        fld.style.background = '#F0F8FA';
    }
    return error;
}


// resize Prototype
(function($){

    //Reposition the container on ready or resize
    $.fn.repos = function() {  
        //Invoke the reposnow() function on document ready
        $(document).ready(function() {
            $('#outerContainer').reposnow();
        });
        //Invoke the reposnow() function on browser resize
        $(window).bind("resize", function() {
            $('#outerContainer').reposnow();
        });
	
    };
	
    //Adjust container position
    $.fn.reposnow = function() {
        var browserheight = $(window).height();	
		var browserwidth = $(window).width();
        $('#outerContainer').css('top', ((browserheight - $(this).height())/2)+20);
		$('#outerContainer').css('left', ((browserwidth - $(this).width())/2));
    };
	
})(jQuery);



//Back to Intro
function backIntro() {
	$('#step1').fadeOut("normal", function(){
		$('#intro').fadeIn("normal");
		$('.scrollbox', '#intro').jScrollPane();								 
	});
	return false;
};
//Back to Step1
function backStep1() {
	$('#step2').fadeOut("normal", function(){
		$('#step1').fadeIn("normal");
		$('.scrollbox', '#step1').jScrollPane();								 
	});
	return false;
};
//Back to Step2
function backStep2() {
	$('#step3').fadeOut("normal", function(){
		$('#step2').fadeIn("normal");
		$('.scrollbox', '#step2').jScrollPane();								 
	});
	return false;
};





//Go to step 1
function step1() {
	$('#intro').fadeOut("normal", function(){
		$('#step1').fadeIn("normal");
		$('.scrollbox', '#step1').jScrollPane();								 
	});
	
	return false;
};

//Go to step 2
function step2() {
	
	// check that at least one activity input item has been selected before moving to next step
	if ($('.activityItems :checked').length > 0) {
		$('#step1').fadeOut("normal", function(){
			$('#step2').fadeIn("normal");
			$('.scrollbox', '#step2').jScrollPane();								 
		});
	} else {
		//alert("Please select at least one activity");
		// if entry not found then display error message		
		$('#errorMsg').html("Please select at least one activity.");
		showAlertBox();
	}
	
	return false;	
};


		//$('#errorMsg').html("");
		//showAlertBox();

//Go to step 3
function step3() {

	// Check if we have at least one value filled in
	if (($('#Friend1Name').val() == '' || $('#Friend1Email').val() == '') && ($('#Friend2Name').val() == '' || $('#Friend2Email').val() == '')) {
		//alert("Please enter at least one friend's details");
		$('#errorMsg').html("Please complete at least one friend's details.");
		showAlertBox();
		return false;
	}
	
	
	if (($('#Friend1Name').val() == '' && $('#Friend1Email').val() != '')) {
		$('#errorMsg').html("Please provide a Name for your first friend.");
		showAlertBox();
		return false;
	}
	
	if (($('#Friend1Name').val() != '' && $('#Friend1Email').val() == '')) {
		$('#errorMsg').html("Please provide an Email Address for your first friend.");
		showAlertBox();
		return false;
	}
	
	if (($('#Friend2Name').val() == '' && $('#Friend2Email').val() != '')) {
		$('#errorMsg').html("Please provide a Name for your second friend.");
		showAlertBox();
		return false;
	}
	
	if (($('#Friend2Name').val() != '' && $('#Friend2Email').val() == '')) {
		$('#errorMsg').html("Please provide an Email Address for your second friend.");
		showAlertBox();
		return false;
	}
	
	
	
	// if email is specified check that its valid
	if ($('#Friend1Email').val() != '' && !validateFriendEmail($('#Friend1Email'))) {
		//alert("Please ensure that your first friend's Email Address is correct");
		$('#errorMsg').html("Please ensure that your first friend's Email Address is correct.");
		showAlertBox();
		return false;
	}
	
	// if email is specified check that its valid
	if ($('#Friend2Email').val() != '' && !validateFriendEmail($('#Friend2Email'))) {
		//alert("Please ensure that your second friend's Email Address is correct");
		$('#errorMsg').html("Please ensure that your second friend's Email Address is correct.");
		showAlertBox();
		return false;
	}
	
	
	
	
	// by now it should have passed all checks so ok to move on
	$('#step2').fadeOut("normal", function(){
		$('#step3').fadeIn("normal");
		$('.scrollbox', '#step3').jScrollPane();								 
	});

	return false;
};

//Go to step 4
function step4() {
	
	
	// check if Daily Newsletter is checked
	if (!$("#Daily_Newsletter").is(":checked")) {
		//alert("Please ensure that you have opted in for the Daily Newsletter");
		$('#errorMsg').html("You need to subscribe to the daily newsletter in order to be eligible for the prize draw.");
		showAlertBox();
		return false;
	}
	
	// Check for Name
	if ($('#firstname').val() == '') {
		//alert("Please enter your Name");
		$('#errorMsg').html("Please enter your Name.");
		showAlertBox();
		return false;
	}
	
	// Check for Surname
	if ($('#surname').val() == '') {
		$('#errorMsg').html("Please enter your Surname.");
		showAlertBox();
		return false;
	}
	

	// check if email is valid
	if (!validateFriendEmail($('#emailaddress'))) {
		//alert("Please enter a valid Email Address");
		$('#errorMsg').html("Please enter a valid Email Address.");
		showAlertBox();
		return false;
	} 
	
	
	// Check for Telephone
	if ($('#contactnumber').val() == '') {
		$('#errorMsg').html("Please enter your Contact Number.");
		showAlertBox();
		return false;
	}
	
	
	// check if language is selected
	if ($("#home_language").val() == '0') {
		$('#errorMsg').html("Please indicate your Home Language.");
		showAlertBox();
		return false;
	}
	
	// check if gender is selected
	if ($("#gender").val() == '0') {
		$('#errorMsg').html("Please indicate your Gender.");
		showAlertBox();
		return false;
	}
	
	
	// check if DOB is selected
	if ($("#dob_day").val() == '0' || $("#dob_month").val() == '0' || $("#dob_year").val() == '0'){
		$('#errorMsg').html("Please indicate your Date Of Birth.");
		showAlertBox();
		return false;
	}
	
	
	// check if Country is selected
	if ($("#country").val() == '0') {
		$('#errorMsg').html("Please indicate which Country you live in.");
		showAlertBox();
		return false;
	}
	
	
	// check if City is selected
	if ($("#city").val() == '0') {
		$('#errorMsg').html("Please indicate your closest City.");
		showAlertBox();
		return false;
	}
	

	// check if terms are valid
	if (!$("#Terms_Conditions").is(":checked")) {
		//alert("You have not agreed to the Terms and Conditions");
		$('#errorMsg').html("You need to agree to the Terms and Conditions in order to enter the competition.");
		showAlertBox();
		return false;
	}

		
	// do post (prolly via ajax)
	
	
	//$('#step3').fadeOut("normal", function(){
		//$('#step4').fadeIn("normal");
		//$('.scrollbox', '#step4').jScrollPane();								 
	//});
	
	return true;	
};

//Close
function kill() {
	$('#outerContainer').css("display", "none");
};

//Toggle Activity Categories
function toggle_cat (_id)
{
	$("#"+_id).slideToggle("fast", function(){
		$('.scrollbox', "#step1").jScrollPane();	
	});
}








// ===== ERROR BOX ======= //
// Create a page overlay for messaging
function showAlertBox() 
{
	var BH = $(window).height();	
	var BW = $(window).width();	
	$('#errorBox').css('top', (BH - $('#errorBox').height())/2);
	$('#errorBox').css('left', (BW - $('#errorBox').width())/2);
	
	$('#overlay').fadeIn("normal");
	$("#errorBox").fadeIn("normal");
}

function hideAlertBox()
{
	$('#overlay').fadeOut("normal");
	$("#errorBox").fadeOut("normal");
}
// ======================= //






function giveFocus(){
	$('#'+this.id).css('border', '1px solid #ffff00');
}
function looseFocus(){
	$('#'+this.id).css('border', 'none	');
}



// DOM loaded
$(document).ready(function() {
						   			   
	// initialise the repositioning function	   
	$("div#outerContainer").repos();
	
	
	// img rollover 
	$(".continueBtn img").hover(function() {
		$(this).attr("src", $(this).attr("src").split(".").join("_o."));
	}, function() {
		$(this).attr("src", $(this).attr("src").split("_o.").join("."));
	});
	
	// Bind step buttons to actions
	$(".btnStep1").click(step1);
	$(".btnStep2").click(step2);
	$(".btnStep3").click(step3);
	$(".btnStep4").click(step4);
	
	$(".btnBackIntro").click(backIntro);
	$(".btnBackStep1").click(backStep1);
	$(".btnBackStep2").click(backStep2);
	
	
	/*
	$(".friendField").onfocus(giveFocus);
	$(".friendField").onblur(looseFocus);
	
	$(".signupField").onfocus(giveFocus);
	$(".signupField").onblur(looseFocus);
	*/
	
	
	$("input.friendField").focus(function () {
         $(this).css('background','#ffff00');
    });
	
	$("input.friendField").blur(function () {
         $(this).css('background','#ffffff');
    });
	
	$("input.signupField").focus(function () {
         $(this).css('background','#ffff00');
    });
	
	$("input.signupField").blur(function () {
         $(this).css('background','#ffffff');
    });

	
	

	
	
});
//-->