/*********************************************
	VALIDATE SEARCH FIELD
*********************************************/
function checkSearch(theForm) {
	var fOK = true;
	var strMsg = "You have not filled in all the required fields:\n";
	if (theForm.searchbox.value.length == 0) {
		fOK = false;
		strMsg = strMsg + "\n" + "- Enter search keywords.";
	}
	if(!fOK) {
		alert(strMsg);
	}
	return fOK;
}
function isValidEmail(str)
{   
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        return false;
    } else {
        return true;
    }

}
/*********************************************
	VALIDATE CONTACT FORM
*********************************************/
function checkContact(theForm) {
	var fOK = true;
	var strMsg = "You have not filled in all the required fields:\n";
	if (theForm.fullname.value.length == 0) {
		fOK = false;
		strMsg = strMsg + "\n" + "- Enter your full name.";
	}
	if (theForm.email.value.length == 0 || (theForm.email.value.length != 0 && !isValidEmail(theForm.email.value))) {
		fOK = false;
		strMsg = strMsg + "\n" + "- Enter a valid email address.";
	}
	if (theForm.enquiry.value.length == 0) {
		fOK = false;
		strMsg = strMsg + "\n" + "- Enter your enquiry.";
	}
	if(!fOK) {
		alert(strMsg);
    } else {		
		document.contactForm.submit();
	}
}
/*********************************************
	MULTIPLE ONLOADS
*********************************************/
var onloads = new Array();
function bodyOnLoad() {
  for ( var i = 0 ; i < onloads.length ; i++ )
	 onloads[i]();
}  
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
function goViewSite(theURL,winName,features) { //v2.0
	if (confirm('Have you saved the page your working on?\n\nClick OK to continue.')) {
		window.location.href='/challenge/diaries/preview.cfm?frame='+theURL;
	}
}
loadParent = function(weburl) {
	top.window.location.href=weburl;
}
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
} 
function testIsValidObject(objToTest) {
	if (null == objToTest) {
		return false;
	}
	if ("undefined" == typeof(objToTest) ) {
		return false;
	}
	return true;
}
function addInputSubmitEvent(form, input) {
    input.onkeydown = function(e) {
        e = e || window.event;
        if (e.keyCode == 13) {
			  if (form.name == "loginform") {
	            gncLogin.checkLogin(document.loginform);
			  } else {
					checkSearch(document.searchForm);  
			  }
        }
    };
}
checkLoginBox = function() {
	if (testIsValidObject(document.getElementById('username'))) {
		document.getElementById('username').focus();
		addInputSubmitEvent(document.loginform,document.getElementById('username'));	
		addInputSubmitEvent(document.loginform,document.getElementById('password'));	
	}
}

function enews_validate(frm) {
	var isError = false;
	var errorMessage = "";
	var emailAddress = frm.Email.value;
	

	
	if (frm.FirstName.value == "") {
		errorMessage += "The first name field is required\n";
		isError = true;

	}
	if (emailAddress == "" || emailAddress.indexOf("@") == -1) {
		errorMessage += "The email address field is required\n";
		isError = true;
	}
	if (isError) {
		alert(errorMessage);
		return false;
	} else {
		return true;
	}	
}