function checkForm(form) {

	var errorMsg = "";
	var friend0 = 0;
	var friend1 = 0;
	var friend2 = 0;
	var friend3 = 0;
	var friend4 = 0;
	var noFriend = true;

	errorMsg += isFieldValid(form.fromName.value,"From name",1,25,"text");
	errorMsg += isFieldValid(form.fromEmail.value,"From Email Address",1,50,"email");


	if (form.toName0.value.length >= 0)
	{
		friend0 = 1;
		$(".mandatoryOptional0").show();
		noFriend = false;
	}
	if (form.toName1.value.length > 0)
	{
		friend1 = 1;
		$(".mandatoryOptional1").show();
		noFriend = false;
	}
	if (form.toName2.value.length > 0)
	{
		friend2 = 1;
		$(".mandatoryOptional2").show();
		noFriend = false;
	}
	if (form.toName3.value.length > 0)
	{
		friend3 = 1;
		$(".mandatoryOptional3").show();
		noFriend = false;
	}
	if (form.toName4.value.length > 0)
	{
		friend4 = 1;
		$(".mandatoryOptional4").show();
		noFriend = false;
	}

	if (noFriend == true)
	{
		errorMsg += "<p>You must enter <strong>at least one</strong> friend</p>"
	}
	errorMsg += isFieldValid(form.toName0.value,"First friend name",friend0,25,"text");
	errorMsg += isFieldValid(form.toEmail0.value,"First friend email Address",friend0,50,"email");
	errorMsg += isFieldValid(form.toName1.value,"Second friend name",friend1,25,"text");
	errorMsg += isFieldValid(form.toEmail1.value,"Second friend email Address",friend1,50,"email");
	errorMsg += isFieldValid(form.toName2.value,"Third friend name",friend2,25,"text");
	errorMsg += isFieldValid(form.toEmail2.value,"Third friend email Address",friend2,50,"email");
	errorMsg += isFieldValid(form.toName3.value,"Fourth friend name",friend3,25,"text");
	errorMsg += isFieldValid(form.toEmail3.value,"Fourth friend email Address",friend3,50,"email");
	errorMsg += isFieldValid(form.toName4.value,"Fifth friend name",friend4,25,"text");
	errorMsg += isFieldValid(form.toEmail4.value,"Fifth friend email Address",friend4,50,"email");

	if (form.cms.value == "cma")
	{
		errorMsg += isFieldValid(form.thankYouPage.value,"Thank You Page is missing from page",0,6,"num"); 
	}

	return errorMsg;
}


function submitForm(form) {
	var errorMsg = checkForm(form);

	var reEnterMsg = " <p> Please click the 'Close' button and reenter it.</p>";
	if (isEmpty(errorMsg)) {
		form.submit();
	} else {
		popup = window.open("","tell_a_friend","width=400,height=400,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
		popup.document.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head>");
		popup.document.write("<link rel='stylesheet' href='/css/National/GlobalStyle.css' type='text/css'></head>");
		popup.document.write("<title>NAB - Tell a Friend</title>");
		popup.document.write("<body>");		
		popup.document.write("<strong>NAB - Tell a Friend</strong><br><br>");
		popup.document.write(errorMsg);
		popup.document.write("<br>");
		popup.document.write(reEnterMsg);
		popup.document.write("<center><form><input type=button value=Close onClick=javascript:window.close();></center></form>");
		popup.document.write("</body>");
		popup.document.write("</html>");
		popup.document.close();
	}
}

var toNameMin = new Array(4);

function mandatoryCheck(form) {
	var friendForm = form;
	for (j=1;j<=4 ;j++ )
	{
		$(".mandatoryOptional"+[j]).hide();
	}
	if (friendForm.toName1.value.length > 0)
	{
		$(".mandatoryOptional1").show();
	}
	if (friendForm.toName2.value.length > 0)
	{
		$(".mandatoryOptional2").show();
	}
	if (friendForm.toName3.value.length > 0)
	{
		$(".mandatoryOptional3").show();
	}
	if (friendForm.toName4.value.length > 0)
	{
		$(".mandatoryOptional4").show();
	}
}

function messageChange(form){
	var ff = form;
	var fromSender = ff.fromName.value;

	var newMessage = "<div style='background-color:#777;font-weight:bold;color:#fff;border:1px solid #777'>Message preview:</div><p><strong>" + fromSender + "</strong> thought you might be interested in the National Australia bank Graduate Program.</p>";
	newMessage += "<p>So, if you dream of a challenging career in wealth management, want to establish yourself in banking or can see yourself advance through the capital markets, we have opportunities that will stimulate your mind and get you on track to fulfil your aspirations.</p>";
	newMessage += "<p>Banking isn't your thing? Stay with us we also have some amazing graduate roles within the specialist areas of our business.</p>";
	newMessage += "<p>To find out more visit nab.com.au/graduatecareers</p>";



	$('#friendMessage').empty().append(newMessage);
}


//ON PAGE LOAD - DO THIS...

//CHECK FOR CAMPAIGN ID.
$(document).ready(function() {
var refID="";
var strURL = document.URL;
var urlPARAM = strURL.split("errorID=");
	if (urlPARAM[1]) {
		var refID = urlPARAM[1].split("&");
	}
	if (refID == "err")
	{
		$("#ErrorMessage").show();
	}

})




function submitCookieValue() {
	var exp = new Date();
	var currDate = exp.getTime();
	var newExpDate = currDate + (182 * 24 * 60 * 60 * 1000);
	exp.setTime(newExpDate);
	document.cookie = "lastLogin= ; expires=" + exp.toGMTString();
}


