/*
	Please amend the functions carefully
	Most have interdependencies
	You can safely amend text within quotes that is normally displayed in alert or confirm boxes
*/

// Opens up an information window
var myWin;
function infoWindow(URL) {
var winHeight = window.screenHeight;
/*var parms = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no,height='+ winHeight + ',width=800,left=0,top=0'; Done by Ish due to window height issue with IE*/
var parms = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,height=600,width=800,left=0,top=0';
 	if (!myWin || myWin.closed) {
		myWin = window.open(URL, 'info', parms);
 	} else {
 		myWin.location = URL;
 	}
	myWin.focus();
}

// Initialises help popup window and resizes main page so that both are visible
var myHelpWin;
function getHelp(bookMark) {
	typeof bookMark == 'undefined' ? bookMark = '' : '';
	var winWidth = 330; // Width of help window
	var winHeight = screen.availHeight-50;
	var winPos = screen.availWidth-winWidth-10;
	window.moveTo(0,0);
	window.resizeTo(winPos, screen.availHeight);
	var parms = 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,height=' + winHeight + ',width=' + winWidth + ',left=' + winPos + ',top=0';
    myHelpWin = window.open("/admin/help.htm#" + bookMark, 'helpWina', parms);
}

// Initialilses a timeout to the function below
function checkForHelp() {
	setTimeout('expandMainWindow()', 1000);
}

// Expands the main window only if the help window has been shut
function expandMainWindow() {
	if (!myHelpWin || myHelpWin.closed) {
		window.resizeTo(screen.availWidth, screen.availHeight)
	}
}

// Verifies the username and password. Only checks the username if it is present
function checkAdminFrm(frm) {

	Msg = '';
	// This checks for complete usernames and the first part of an email address
	if (frm.username) {
		if (frm.username.value == '') {
			Msg += 'Please enter a username\n';
		} else if (frm.username.value.search(/[\w\-_]+\@[\w\-_]+/) == -1) {
			Msg += 'Please enter a valid username\n';
		}
	} else if (frm.user_part) { // We are only entering the first part of the email address
		if (frm.user_part.value == '') {
			Msg += 'Please enter the first part of your email address\n';
		} else if (frm.user_part.value.search(/[^\w\-_]+/) >= 0) {
			Msg += 'Please enter a valid first part of your email address (only letters and numbers, dashes and underscores)\n';
		}
	}

	// Checks and compares passwords
	if (frm.change_password) {
		if (frm.change_password.value == '') {
			Msg += 'Please enter a password\n';
		}
		if (frm.verify_password.value == '') {
			Msg += 'Please re-enter your password in the second box\n';
		}
		if (frm.change_password.value.length < 6) {
			Msg += 'Please enter a password of at least 6 characters\n';
		}
		if (frm.change_password.value != frm.verify_password.value) {
			Msg += 'Your entered pasword and repeated password do not match\n';
		}
	}

	// Check anything associated with email account - delete, alias addition and password change
	if (frm.login) {
		if (frm.mode.value == 'add_alias_forward') {
			if (frm.login.value == '') {
				Msg += 'Please choose the email account that you wish to add a' + (frm.sub_mode.value == 'forward' ? ' ' :  'n ') + frm.sub_mode.value + ' to\n';
			}
		} else if (frm.mode.value == 'change_account_password') {
			if (frm.login.value == '') {
				Msg += 'Please choose an account whose password you wish to change\n';
			}
		} else if (frm.mode.value == 'delete_account') {
			if (frm.login.value == '') {
				Msg += 'Please choose an account to delete\n';
			} else if (!confirm('Are you sure that you want to delete the account ' + frm.login.value + ' and all associated aliases and forwards?')) {
				return false; // Do not submit the form if the user has changed their mind
			}
		}
	}
	
	// Forward addition. Check for correct email address
	if (frm.forward) {
		if (frm.mode.value == 'add_alias_forward') {
			if (!checkForEmail(frm.forward)) {
				Msg += 'Please enter a valid forward. This must be a valid email address\n';
			}			
		} else if (frm.mode.value = 'delete_alias_forward') {
			if (frm.forward.value == '') {
				Msg += 'Please choose a forward to delete\n';
			} else if (!confirm('Are you sure that you want to delete the forward ' + frm.forward.value + '?')) {
				return false; // Do not submit the form if the user has changed their mind
			}
		}
	}

	// Alias deletion
	if (frm.alias) {
		if (frm.alias.value == '') {
			Msg += 'Please choose an alias to delete\n';
		} else if (!confirm('Are you sure that you want to delete the alias ' + frm.alias.value + '?')) {
			return false; // Do not submit the form if the user has changed their mind about deleting the alias
		}
	}

	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}

// Validates the initial signup page for ADSL
function validate(frm) {
	Msg = '';
	if (frm.firstname.value == '') {
		Msg += 'Please enter your first name\n';
	}
	if (frm.lastname.value == '') {
		Msg += 'Please enter your last name\n';
	}
	if (frm.postcode.value == '') {
		Msg += 'Please enter your postcode\n';
	}
	if (frm.question.value == '') {
		Msg += 'Please select a security question\n';
	}
	if (frm.answer.value == '') {
		Msg += 'Please select an answer to the security question\n';
	}
	frm.user_part.value = frm.user_part.value.toLowerCase();
	if (frm.user_part.value == '') {
		Msg += 'Please enter your email address\n';
	} else if (frm.user_part.value.length < 3) {
		Msg += 'Please enter an email address of at least 3 characters\n';
	} else if (frm.user_part.value.search(/[^a-zA-Z0-9- _]/) > 0) {
		Msg += 'Please enter only letters, numbers, dashes or underscores for your desired email address\n';
	}

	// Verify that the password and password confirm fields are entered correctly
	if (frm.password.value == '') {
		Msg += 'Please enter a password\n';
	}
	if (frm.password2.value == '') {
		Msg += 'Please re-enter your password in the second box\n';
	}
	if (frm.password.value.length < 6) {
		Msg += 'Please enter a password of at least 6 characters\n';
	}
	if (frm.password.value != frm.password2.value) {
		Msg += 'Your entered pasword and repeated password do not match\n';
	}
	//if (frm.terms && frm.terms.checked == false) {
	//	Msg += 'You must agree to the terms and conditions\n';
	//}

	frm.postcode.value = frm.postcode.value.toUpperCase(); // Upper case postcode

	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}

// Displays the aliases and forwards that are going to be deleted for a given email account
// The arrays aliasMenu and forwardMenu are constructed dynamically at page creation time
// They are displayed in JavaScript towards the top of the page
function showAliasesForwards(fldVal) {
	cnt = 0;
	document.getElementById('associatedAliases').innerHTML = ''
	for (var i in aliasMenu) {
		if (aliasMenu[i]['id'] == fldVal) {
			document.getElementById('associatedAliases').innerHTML += aliasMenu[i]['alias'] + '<br>';
			cnt++;
		}
	}
	document.getElementById('associatedForwards').innerHTML = ''
	for (var i in forwardMenu) {
		if (forwardMenu[i]['id'] == fldVal) {
			document.getElementById('associatedForwards').innerHTML += forwardMenu[i]['forward'] + '<br>';
			cnt++;
		}
	}
	if (cnt > 0) {
		document.getElementById('aliForHeader').style.display = 'block';
		document.getElementById('aliForData').style.display = 'block';
	} else {
		document.getElementById('aliForHeader').style.display = 'none';
		document.getElementById('aliForData').style.display = 'none';
	}
}


// Validates the final signup page for ADSL
// With optional switch also validates the details update page within the Access Point Manager
function CheckSubmit(frm, validateDetails) {
	var Msg = '';

	if (validateDetails) { // If the user is updating their details within the Access Point Manager
		if (frm.firstname.value == '') {
		Msg += 'Please enter your first name\n';
		}
		if (frm.lastname.value == '') {
			Msg += 'Please enter your last name\n';
		}
		if (frm.postcode.value == '') {
			Msg += 'Please enter your postcode\n';
		}
		if (frm.email.value == '') {
			Msg += 'Please enter your email address\n';
		}	
		if (frm.username) { // If the user is starting from scratch
			if (frm.username.value == '') {
				Msg += 'Please enter a username\n';
			} else {	
				frm.username.value = frm.username.value.replace(/(\w+)@+\w+\.\w+/, "$1");
				if (frm.username.value.search(/[^\w\.-_]/) >= 0) {
					Msg += 'Please enter a username using only letters, numbers, full stops, hyphens and underscores. These have been removed\n';
					frm.username.value = frm.username.value.replace(/[^\w\.-_]/g, "");
				}
			}

		}
	}
	if (frm.address.value.length < 4) {
		Msg += 'Please enter a valid address\n';
	}
	if (frm.address.value.search(/[^a-zA-Z0-9-,'\n\r_\(\) ]+/) > 0) {
		Msg += 'Please enter only letters, commas or dashes for your address\n';
	}
	if (frm.town.value.length < 2) {
		Msg += 'Please enter your town or city\n';
	}
	if (frm.town.value.search(/[^a-zA-Z0-9- ]+/) > 0) {
		Msg += 'Please enter only letters or dashes for your city\n';
	}
	if (frm.county.value.length < 2) {
		Msg += 'Please enter your county\n';
	}
	if (frm.county.value.search(/[^a-zA-Z0-9- ]+/) > 0) {
		Msg += 'Please enter only letters or dashes for your county\n';
	}
	if (frm.contact_telephone.value == '') {
		Msg += 'Please enter your daytime contact phone number\n';
	}
	if (frm.contact_telephone.value.length < 11 || !checkForTelephone(frm.contact_telephone)) {
		Msg += 'Please enter a valid daytime contact phone number\n';
	}
	if (frm.password.value.length < 4 || frm.password_confirm.value.length < 4) {
		Msg += 'Please enter a password of four or more characters\n';
	}
	if (frm.password.value != frm.password_confirm.value) {
		Msg += 'The password and confirmation password must match\n';
	}

	frm.town.value = frm.town.value.substr(0, 1).toUpperCase() + frm.town.value.substr(1, frm.town.value.length); // Upper case first character of city
	frm.county.value = frm.county.value.substr(0, 1).toUpperCase() + frm.county.value.substr(1, frm.county.value.length); // Upper case first character of county
	
	if (Msg) {
		alert(Msg);
		return false;
	} else {
		return true;
	}
}

// Sets the selected date at today's
function presDate(frm) { 
	var mydate=new Date();
	var mnthnow=mydate.getMonth();
	var yearnow=mydate.getFullYear();
	var yearno=yearnow + "";
	frm('card_details[exmon]').selectedIndex=mnthnow;
	frm('card_details[exyear]').selectedIndex=yearno.substr(3,1)-3
}

// Strips out non number characters
function checkForTelephone(fld) {
	var invalid = false, string = fld.value; 
	//Stripping out any invalid characters.
	for (var i=0, output='', valid="0123456789"; i<string.length; i++) {
		if (valid.indexOf(string.charAt(i)) != -1) {
			output += string.charAt(i);
		} else if (string.charAt(i) != ' ') {
			invalid = true;
		}
	}
	fld.value = output
	if (invalid == true) {
		return false;
	} else {
		return true;
	}
}

// Checks the email address
function checkForEmail(fld) {
	if (fld.value.search(/[\w\-_]+\@[\w\-_]+\.[\w\-_]+/) == -1) {
		return false;
	} else {
		return true;
	}
}

// Checks for alphanumeric characters only
function checkForAlpha(fld) {
	if (fld.value.search(/\w+/) == -1) {
		return false;
	} else {
		return true;
	}
}
