

//******************************************************
//
//	Check Login Form 
//
//******************************************************


function CheckLoginForm(){

if(document.getElementById('txtUsername').value == ""){

	fieldError('txtUsername')
	msg('Required Field: Office')
	return false;
}

if(document.getElementById('txtPwd').value == ""){

	fieldError('txtPwd')
	msg('Required Field: Password')
	return false
}
return true;
}






//******************************************************
//
//	Set Login Form
//
//******************************************************


function InitLoginForm(){
	document.getElementById('txtUsername').focus();
}


//******************************************************
//
//	Check IP Address Form 
//
//******************************************************


function CheckIPAddressForm(confirmMsg){

if(document.getElementById('txtIPAddress').value == ""){

	fieldError('txtIPAddress')
	msg('Required Field: IPAddress')
	return false;
}


if(document.getElementById('txtDescription').value == ""){

	fieldError('txtDescription')
	msg('Required Field: Description')
	return false;
}

if(!CheckIP()){
	fieldError('txtIPAddress')
	msg('Required Field: IPAddress')
	return false;	
}


return Confirmfn(confirmMsg);

return true;
}


function CheckIP() {
IPvalue = document.getElementById('txtIPAddress').value;

errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern); 

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
	return true;
else
alert (errorString);
return false;
}


//******************************************************
//
//	Check CMSFile Form 
//
//******************************************************


function CheckCMSFileForm(confirmMsg){

if(document.getElementById('txtDescription').value == ""){

	fieldError('txtDescription')
	msg('Required Field: Description')
	return false;
}



return Confirmfn(confirmMsg);

return true;
}




//******************************************************
//
//	Check User Form 
//
//******************************************************


function CheckUserForm(confirmMsg){

if(document.getElementById('txtForeName').value == ""){

	fieldError('txtForeName')
	msg('Required Field: ForeName')
	return false;
}

if(document.getElementById('txtSurname').value == ""){

	fieldError('txtSurname')
	msg('Required Field: Surname')
	return false
}

if(document.getElementById('txtDisplayname').value == ""){

	fieldError('txtDisplayname')
	msg('Required Field: Displayname')
	return false
}

if(document.getElementById('txtEmail').value == ""){

	fieldError('txtEmail')
	msg('Required Field: Email')
	return false
}

//check the validity of the email address
if(!(CheckEmail(document.getElementById('txtEmail').value))){
	fieldError('txtEmail')
	alert('Please enter a valid email address')
	return false;
}

if(document.getElementById('txtPassword').value == ""){

	fieldError('txtPassword')
	msg('Required Field: txtPassword')
	return false
}

if(!(document.getElementById('txtPassword').value == document.getElementById('txtConfirmPassword').value)){

	fieldError('txtPassword')
	fieldError('txtConfirmPassword')
	alert('Passwords do not match, please re-enter')
	return false
}

if(document.getElementById('txtUsername').value == ""){

	fieldError('txtUsername')
	msg('Required Field: txtUsername')
	return false
}

return Confirmfn(confirmMsg);


return true;
}


//******************************************************
//
//	Check Transaction Form 
//
//******************************************************


function CheckTransactionForm(confirmMsg){

if(document.getElementById('txtCompany').value == ""){

	fieldError('txtCompany')
	msg('Required Field: Company')
	return false;
}

if(document.getElementById('txtTransactionDate').value == ""){

	fieldError('txtTransactionDate')
	msg('Required Field: Transaction Date')
	return false;
}

//now check the validity of the date

if(!(CheckDate(document.getElementById('txtTransactionDate').value))){
	fieldError('txtTransactionDate')
	alert('Date must be in format DD/MM/YYYY')
	return false;
}


return Confirmfn(confirmMsg);

return true;
}


//******************************************************
//
//	Check CaseStudy Form 
//
//******************************************************


function CheckCaseStudyForm(confirmMsg){

if(document.getElementById('txtCompany').value == ""){

	fieldError('txtCompany')
	msg('Required Field: Company')
	return false;
}

if(document.getElementById('txtTitle').value == ""){

	fieldError('txtTitle')
	msg('Required Field: Title')
	return false;
}

if(document.getElementById('txtCaseStudyDate').value == ""){

	fieldError('txtCaseStudyDate')
	msg('Required Field: CaseStudyDate')
	return false;
}

//now check the validity of the date

if(!(CheckDate(document.getElementById('txtCaseStudyDate').value))){
	fieldError('txtCaseStudyDate')
	alert('Date must be in format DD/MM/YYYY')
	return false;
}



return Confirmfn(confirmMsg);

return true;
}


//******************************************************
//
//	Check SectorForm 
//
//******************************************************


function CheckSectorForm(confirmMsg){

if(document.getElementById('txtTitle').value == ""){

	fieldError('txtTitle')
	msg('Required Field: Title')
	return false;
}

return Confirmfn(confirmMsg);

return true;
}

//******************************************************
//
//	Check Portfolio Form 
//
//******************************************************


function CheckPortfolioForm(confirmMsg){

if(document.getElementById('txtName').value == ""){

	fieldError('txtName')
	msg('Required Field: Name')
	return false;
}

if(document.getElementById('txtDescription').value == ""){

	fieldError('txtDescription')
	msg('Required Field: Description')
	return false
}

if(document.getElementById('txtSector').value == ""){

	fieldError('txtSector')
	msg('Required Field: Sector')
	return false
}

if(document.getElementById('txtLocation').value == ""){

	fieldError('txtLocation')
	msg('Required Field: Location')
	return false
}
if(document.getElementById('txtDealDate').value == ""){

	fieldError('txtDealdate')
	msg('Required Field: DealDate')
	return false
}

return Confirmfn(confirmMsg);

return true;
}


//******************************************************
//
//	Check Culture Form 
//
//******************************************************


function CheckCultureForm(){

if(document.getElementById('txtCultureName').value == ""){

	fieldError('txtCultureName')
	msg('Required Field: Culture Name')
	return false;
}

if(document.getElementById('txtDisplayName').value == ""){

	fieldError('txtDisplayName')
	msg('Required Field: Display Name')
	return false
}

//fields are all ok now check syntax of culture & convert first
//2 letters to lowercase and last 2 to upper case

var toCheck = document.getElementById('txtCultureName').value

var CharAtPos1 = toCheck.charAt(0).toLowerCase();
var CharAtPos2 = toCheck.charAt(1).toLowerCase();
var CharAtPos3 = toCheck.charAt(2);
var CharAtPos4 = toCheck.charAt(3).toUpperCase();
var CharAtPos5 = toCheck.charAt(4).toUpperCase();

if(toCheck.length == '5'){
	if(CharAtPos3  != '-'){
		fieldError('txtCultureName')
		alert('Culture must be in the form: en-UK');
		return false;		
	}else{		
		//all ok now force upper  lower case syntax
		var newCulture = CharAtPos1 + CharAtPos2 + '-' + CharAtPos4 + CharAtPos5;	
		document.getElementById('txtCultureName').value = newCulture;
		return true;
	}
}
else{
	fieldError('txtCultureName')
	alert('Culture must be in the form: en-UK');
	return false
}

return false;
}



//******************************************************
//
//	Check Profile Form 
//
//******************************************************


function CheckProfileForm(confirmMsg){

if(document.getElementById('txtProfileName').value == ""){

	fieldError('txtProfileName')
	msg('Required Field: Profile Name')
	return false;
}

if(document.getElementById('txtCMSName').value == ""){

	fieldError('txtCMSName')
	msg('Required Field: CMS Name')
	return false;
}

//Get selections of Status options  (drop down lists)
//var index1  = document.getElementById('dlLockOutReportStatus').selectedIndex
//var index2  = document.getElementById('dlErrorReportStatus').selectedIndex
//var index3  = document.getElementById('dlPublishNotificationStatus').selectedIndex


if(!(document.getElementById('RadioLockOutReportStatus_1').checked)){
	if(document.getElementById('txtLockOutReportTo').value == ""){
		fieldError('txtLockOutReportTo')
		msg('Required Field: Lock Out Report to.')
		return false;
	}
	if(document.getElementById('txtSMTP').value == "" ){
		fieldError('txtSMTP')
		msg('Required Field: SMTP Address.')
		return false;
	}

	if(document.getElementById('txtFromName').value == ""){
		fieldError('txtFromName')
		msg('Required Field: Fromname.')
		return false;
	}

	if(document.getElementById('txtFromAddress').value == ""){
		fieldError('txtFromAddress')
		msg('Required Field: FromAddress.')
		return false;
	}


	//now check the validity of the email address
	if(!(CheckEmail(document.getElementById('txtLockOutReportTo').value))){
		fieldError('txtLockOutReportTo')
		alert('Plesae enter a valid email address')
		return false;
	}

	//now check the validity of the From address
	if(!(CheckEmail(document.getElementById('txtFromAddress').value))){
		fieldError('txtFromAddress')
		alert('Plesae enter a valid email address')
		return false;
	}
}

if(!(document.getElementById('RadioErrorReportStatus_1').checked)){
	if(document.getElementById('txtErrorReportTo').value == ""){
		fieldError('txtErrorReportTo')
		msg('Required Field: Error Report Status.')
		return false;
	}
	if(document.getElementById('txtSMTP').value == ""){
		fieldError('txtSMTP')
		msg('Required Field: SMTP Address.')
		return false;
	}

	if(document.getElementById('txtFromName').value == ""){
		fieldError('txtFromName')
		msg('Required Field: Fromname.')
		return false;
	}

	if(document.getElementById('txtFromAddress').value == ""){
		fieldError('txtFromAddress')
		msg('Required Field: FromAddress.')
		return false;
	}

	//now check the validity of the email address
	if(!(CheckEmail(document.getElementById('txtErrorReportTo').value))){
		fieldError('txtErrorReportTo')
		alert('Plesae enter a valid email address')
		return false;
	}

	//now check the validity of the From address
	if(!(CheckEmail(document.getElementById('txtFromAddress').value))){
		fieldError('txtFromAddress')
		alert('Plesae enter a valid email address')
		return false;
	}
}

if(!(document.getElementById('RadioPublishNotificationStatus_1').checked)){

	if(document.getElementById('txtPublishNotificationTo').value == ""){
		fieldError('txtPublishNotificationTo')
		msg('Required Field: Publish Notification.')
		return false;
	}
	if(document.getElementById('txtSMTP').value == ""){
		fieldError('txtSMTP')
		msg('Required Field: SMTP Address.')
		return false;
	}

	if(document.getElementById('txtFromName').value == ""){
		fieldError('txtFromName')
		msg('Required Field: Fromname.')
		return false;
	}

	if(document.getElementById('txtFromAddress').value == ""){
		fieldError('txtFromAddress')
		msg('Required Field: FromAddress.')
		return false;
	}	

	//now check the validity of the email address
	if(!(CheckEmail(document.getElementById('txtPublishNotificationTo').value))){
		fieldError('txtPublishNotificationTo')
		alert('Plesae enter a valid email address')
		return false;
	}

	//now check the validity of the From address
	if(!(CheckEmail(document.getElementById('txtFromAddress').value))){
		fieldError('txtFromAddress')
		alert('Plesae enter a valid email address')
		return false;
	}

	if(!(document.getElementById('txtPublishNotificationCC').value == "")){
		//now check the validity of the email address
		if(!(CheckEmail(document.getElementById('txtPublishNotificationCC').value))){
			fieldError('txtPublishNotificationCC')
			alert('Plesae enter a valid email address')
			return false;
		}	
	}
}




return Confirmfn(confirmMsg);

return true;
}


//******************************************************
//
//	Check News Form 
//
//******************************************************


function CheckNewsForm(confirmMsg){

//first check if user has selected this as a version of
//an existing item - if so ensure an existing item is //
//selected from the DD list presented.

//lstV = eval(document.getElementById('lstVersionOf'))

//var index  = lstV.selectedIndex
//var SelValue = lstV.options[index].value
//alert(SelValue)

if(document.getElementById('txtTitle').value == ""){

	fieldError('txtTitle')
	msg('Required Field: Title')
	return false;
}

if(document.getElementById('txtContent').value == ""){

	fieldError('txtContent')
	msg('Required Field: Content')
	return false
}

//now check the validity of the date

if(!(CheckDate(document.getElementById('txtDatePublished').value))){
	fieldError('txtDatePublished')
	alert('Date must be in format DD/MM/YYYY')
	return false;
}


return Confirmfn(confirmMsg);

return true;
}



//******************************************************
//
//	Check Team Form 
//
//******************************************************


function CheckTeamForm(confirmMsg){

//first check if user has selected this as a version of
//an existing item - if so ensure an existing item is //
//selected from the DD list presented.

//lstV = eval(document.getElementById('lstVersionOf'))

//var index  = lstV.selectedIndex
//var SelValue = lstV.options[index].value
//alert(SelValue)

if(document.getElementById('txtForename').value == ""){

	fieldError('txtForename')
	msg('Required Field: Forename')
	return false;
}

if(document.getElementById('txtSurname').value == ""){

	fieldError('txtSurname')
	msg('Required Field: Surname')
	return false
}

if(document.getElementById('txtRole').value == ""){

	//fieldError('txtRole')
	//msg('Required Field: Role')
	//return false
}

if(!(CheckForCategorySelection())){

	fieldError('chkCategories')
	document.all.chkCategories.focus()
	msg('Required Field: Executives')
	return false;
}

//check the validity of the email address
if(!(CheckEmail(document.getElementById('txtEmail').value))){
	fieldError('txtEmail')
	alert('Plesae enter a valid email address')
	return false;
}


return Confirmfn(confirmMsg);

return false;
}


function CheckForCategorySelection(){

	//loop through each slection and check relevant exec box
	for(i=0; i<11; ++i){
		var control = "chkCategories_" + i;			

		if(document.all[control].checked == true){
			return true;
		}
	}
	return false;			
}



//******************************************************
//
//	Check Page Form 
//
//******************************************************


function CheckPageForm(confirmMsg){

//first check if user has selected this as a version of
//an existing item - if so ensure an existing item is //
//selected from the DD list presented.

//lstV = eval(document.getElementById('lstVersionOf'))

//var index  = lstV.selectedIndex
//var SelValue = lstV.options[index].value
//alert(SelValue)

if(document.getElementById('txtTitle').value == ""){

	fieldError('txtTitle')
	msg('Required Field: Title')
	return false;
}

if(document.getElementById('txtContent').value == ""){

	fieldError('txtContent')
	msg('Required Field: Content')
	return false
}

if(document.getElementById('txtMetaData').value == ""){

	fieldError('txtMetaData')
	msg('Required Field: MetaData')
	return false
}

return Confirmfn(confirmMsg);

return true;
}



//******************************************************
//
//	Generic Form Methods
//
//******************************************************

function Confirmfn(confirmMsg){
	if(!(confirm(confirmMsg))){
		return false;
	}
}


function CheckEmail(who) {
	/* With RegExp */
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(who));
}

function CheckDate(dv){

	date=dv
	test1=(/^\d{2}[\-/]\d{2}[\-/]\d{4}$/.test(date))
	date=date.replace(/[\-/]/g,',')
	date=date.split(' ')
	d = new Date(date[2], date[1]-1, date[0])
	
	if(test1) {
		return true
	}else{
		return false
	}
}


function fieldError(control){
	document.getElementById(control).style.backgroundColor = '#F85757';
	document.getElementById(control).focus();
}

function fieldActive(control){
	document.getElementById(control).style.backgroundColor = '#7CD182';
}

function fieldOK(control){
	document.getElementById(control).style.backgroundColor = '#FFFFFF';
}
function msg(msg){
	//alert(msg)
}


