// Change the button image
function buttonchg(p_id, p_state) {

	var h_button = document.getElementById(p_id);

	h_button.src = h_button.src.substring(0,h_button.src.lastIndexOf("_")+1) + p_state + ".gif";
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

//	Identify if the string contains non-alphanumeric characters
function nonWord(p_string) {
	var h_regexp = /[^\w]|[_]/;

	if (p_string.match(h_regexp)) {
	    return true;
	}

	return false;
}

//	Check that string is at least 4 letters long
function checkFLL(p_string) {
	var h_regexp = /^[a-zA-Z]{4}/;

	if (p_string.match(h_regexp)) {
	    return true;
	}
	return false;
}

//	Check if a non-numeric character follows a number
function checkLFN(p_string) {
	var h_regexp = /\d+[^0-9]/;

	if (p_string.match(h_regexp)) {
	    return true;
	}
	return false;
}

//	Highlight the form field with an error
function highlightError(p_ele, p_focus, p_clear) {
	if (p_focus == 1) { p_ele.focus(); }
	if (p_clear == 1) { p_ele.value = ''; }
	p_ele.className = 'error';
}

//	Validate the appropriate form
function validateForm_pre071206 (p_form, p_switch) {
	
	var h_form = document.getElementById(p_form);
	var h_return = false;

	if (parseInt(p_switch) == 1) {
		h_return = validateUsername(h_form);
	}
	else {
	    h_return = validatePassword(h_form);
	}

	return h_return;
}

//	Validate the Change Password form
function validatePassword_pre071206 (p_form) {

	var h_user = p_form.txtUsername;
	var h_epass	= p_form.txtPassword;
	var h_sch = p_form.txtSchcode;

	var h_npass = p_form.txtPasswordN;
	var h_cpass = p_form.txtPasswordC;

	if (!verifyLength(trim(h_user.value), 1)) {
	    alert(h_user.alt + ' needed.\nPlease enter a ' + h_user.alt + '.');
		highlightError(h_user, 1, 0);
		return false;
	}

	if (!verifyLength(trim(h_epass.value), 1)) {
	    alert(h_epass.alt + ' needed.\nPlease enter a ' + h_epass.alt + '.');
		highlightError(h_epass, 1, 1);
		return false;
	}

	if (!verifyLength(trim(h_sch.value), 1)) {
	    alert(h_sch.alt + ' needed.\nPlease enter a ' + h_sch.alt + '.');
		highlightError(h_sch, 1, 0);
		return false;
	}

	if (!verifyLength(trim(h_npass.value), 1)) {
	    alert(h_npass.alt + ' needed.\nPlease enter a ' + h_npass.alt + '.');
		highlightError(h_npass, 1, 1);
		return false;
	}

	if (!verifyLength(trim(h_npass.value), 4)) {
	    alert('Your password must have at least 4 letters!');
		highlightError(h_npass, 1, 1);
		return false;
	}

	if (nonWord(h_npass.value)) {
	    alert(h_npass.alt + ' contains invalid characters.\nPlease enter letters or numbers only!');
		highlightError(h_npass, 1, 1);
		return false;
	}

	if (h_npass.value != h_cpass.value) {
	    alert('Passwords do not match.\nPlease re-enter!');
		highlightError(h_npass, 1, 1);
		highlightError(h_cpass, 0, 1);
		return false;
	}
	
	return true;
}


//	Validate the New Username form
function validateUsername_pre071206 (p_form) {

	var h_user		=	p_form.txtUsername;
	var h_pass		=	p_form.txtPassword;
	var h_sch		=	p_form.txtSchcode;

	var h_nuser		=	p_form.txtUsernameN;

	if (!verifyLength(trim(h_user.value), 1)) {
	    alert(h_user.alt + ' needed.\nPlease enter a ' + h_user.alt + '.');
		h_user.focus();
		h_user.className = 'error';
		return false;
	}

	if (!verifyLength(trim(h_pass.value), 1)) {
	    alert(h_epass.alt + ' needed.\nPlease enter a ' + h_epass.alt + '.');
		h_epass.focus();
		h_epass.className = 'error';
		return false;
	}

	if (!verifyLength(trim(h_sch.value), 1)) {
	    alert(h_sch.alt + ' needed.\nPlease enter a ' + h_sch.alt + '.');
		h_sch.focus();
		h_sch.className = 'error';
		return false;
	}

	if (!verifyLength(trim(h_nuser.value), 1)) {
	    alert(h_nuser.alt + ' needed.\nPlease enter a ' + h_nuser.alt + '.');
		h_nuser.focus();
		h_nuser.className = 'error';
		return false;
	}

	if (!checkFLL(trim(h_nuser.value)) || nonWord(trim(h_nuser.value)) || checkLFN(trim(h_nuser.value)) ){
	    alert('Your new Username must be at least 4 letters long!\nIt must be made up of only letters and numbers.\nNo spaces allowed.\nNumbers may be included at the end of the Username only.\nPlease enter a user name that meets these requirements.');
		h_nuser.focus();
		h_nuser.className = 'error';
		return false;
	}

	return true;

}

//**************************************************************************************************
//	Function Name	:	checkSchoolCode
//
//	Purpose			:	To identify the appropriate school code from the username where a school code
//						does not exist
//	Date			:	07 Dec 2006
//	Change Log		:	07-12-06		Created
//
//**************************************************************************************************

function checkSchoolCode (p_schcode, p_username) {

	var h_schcode = document.getElementById('txtSchcode');


	if (checkLFN(p_username.value)) {
		var h_tail = p_username.value.substr(p_username.value.length-4, 4);
		h_schcode.value = h_tail;
		return true;
	}

	if (verifyLength(p_schcode.value, 1)) {
		h_schcode.value = p_schcode.value;
		return true;
	}

	alert('Unable to identify school code.\nPlease insert your school code.');
	p_schcode.focus();
	return false;
}


//**************************************************************************************************
//	Function Name	:	validateUsername
//
//	Purpose			:	To verify that the correct username has been submitted
//	Date			:	07 Dec 2006
//	Change Log		:	07-12-2006		Created
//
//**************************************************************************************************


function validateUsername (p_username) {

	var h_username = p_username.value;

	var h_status = document.getElementById('custatus');

	if (!verifyLength(h_username,1)) {
		h_status.innerHTML = "Username needs to be at least 4 letters long!"
		p_username.style.borderColor = "#D03E3F";
		p_username.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}

	if (!checkFLL(trim(h_username)) || nonWord(trim(h_username)) || checkLFN(trim(h_username)) ){
	    h_status.innerHTML = 'Username is invalid.</br>Please submit a username that meets the stated rules.';
		p_username.style.borderColor = '#D03E3F';
		p_username.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}

	changeDetailsAssignment(1);
	h_status.style.display = 'none';
	p_username.style.borderColor = '#A9A9A9';
	return true;

}

//**************************************************************************************************
//	Function Name	:	validatePassword
//
//	Purpose			:	To verify that the new password adheres to the password rules and that it matches
//						the password confirmation!
//	Date			:	07 Dec 2006
//	Change Log		:	07-12-2006		Created
//
//**************************************************************************************************

function validatePassword(p_opass, p_pass1, p_pass2) {

	var h_opass = p_opass.value;
	var h_pass1 = p_pass1.value;
	var h_pass2 = p_pass2.value;

	var h_status = document.getElementById('cpstatus');

	p_opass.style.borderColor = "#A9A9A9";
	p_pass1.style.borderColor = "#A9A9A9";
	p_pass2.style.borderColor = "#A9A9A9";

	if (!verifyLength(trim(h_opass), 1)) {
		h_status.innerHTML = 'Your ' + p_opass.alt + ' is required.</br>Please enter your ' + p_opass.alt.toLowerCase() + '.';
		p_opass.style.borderColor = "#D03E3F";
		p_opass.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}

	if (!verifyLength(trim(h_pass1), 1)) {
		h_status.innerHTML = 'Your ' + p_pass1.alt + ' is required.</br>Please enter your ' + p_pass1.alt.toLowerCase() + '.';
		p_pass1.style.borderColor = "#D03E3F";
		p_pass1.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}

	if (!verifyLength(trim(h_pass1), 4)) {
	    h_status.innerHTML = 'Your ' + p_pass1.alt.toLowerCase() + ' must have at least 4 letters!';
		p_pass1.style.borderColor = "#D03E3F";
		p_pass1.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}

	if (nonWord(h_pass1)) {
	    h_status.innerHTML = 'Your ' + p_pass1.alt.toLowerCase() + ' contains invalid characters.</br>Please enter letters or numbers only!';
		p_pass1.style.borderColor = "#D03E3F";
		p_pass1.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}

	if (h_pass1 != h_pass2) {
	    h_status.innerHTML = 'Your ' + p_pass1.alt.toLowerCase() + ' does not match your ' + p_pass2.alt.toLowerCase() + '.</br>Please correct your ' + p_pass2.alt.toLowerCase() + '.';
		p_pass2.style.borderColor = "#D03E3F";
		p_pass2.value = '';
		p_pass2.focus();
		h_status.style.color = '#D03E3F';
		h_status.style.display = 'block';
		return false;
	}
	
	changeDetailsAssignment(2);
	h_status.style.display = 'none';
	p_opass.style.borderColor = "#A9A9A9";
	p_pass1.style.borderColor = "#A9A9A9";
	p_pass2.style.borderColor = "#A9A9A9";
	return true;

}


//**************************************************************************************************
//	Function Name	:	changeDetailsAssignment
//
//	Purpose			:	To verify that the correct username has been submitted
//						This function applies directly to the myDetails.asp and should be used for it alone
//						If any of the input text fields are changed then this needs to be updated
//
//	Date			:	07 Dec 2006
//	Change Log		:	07-12-2006		Created
//
//**************************************************************************************************

function changeDetailsAssignment(p_type) {

	var h_chgdetails = document.frmChangeDetails;

	h_chgdetails.nUsername.value = document.getElementById('txtUsername').value;
	h_chgdetails.oPassword.value = document.getElementById('txtOPassword').value;
	h_chgdetails.nPassword1.value = document.getElementById('txtNPassword1').value;
	h_chgdetails.nPassword2.value = document.getElementById('txtNPassword2').value;
	h_chgdetails.chgType.value = parseInt(p_type);

}

function resetForm(p_form, p_ele) {
	var h_ele = document.getElementById(p_ele);
	var h_value = h_ele.value;

	var h_form = document.getElementById(p_form);
	
	h_form.reset();
	h_ele.value = h_value;

}

function verifyLength(p_string, p_length) {

	if (p_string.length < p_length) {
	    return false;
	}
	return true;
}

function setCookie(p_name, p_value, p_expdays) {

	var h_expdate = new Date();

	h_expdate.setDate(h_expdate.getDate() + p_expdays);

	document.cookie=p_name + "=" + escape(p_value) +
		((p_expdays==null) ? "" : ";expires=" + h_expdate)

}

function getCookie(p_name){

	if (document.cookie.length>0) {

	  var h_start = document.cookie.indexOf(p_name + "=");

	  if (h_start!=-1){
		h_start = h_start + p_name.length + 1; 
		var h_end = document.cookie.indexOf(";",h_start);
		if (h_end==-1) h_end = document.cookie.length;

		return unescape(document.cookie.substring(h_start,h_end))
		} 
	  }
	return null
}

function setSchoolCode(p_value) {

	setCookie('HCSchoolCode', p_value, 1024);
	return true;
}


