// ***  ps_library.js  ***

function upFrames (menuHref, bodyHref) // Used to update two frames.
    {
    window.location.href=bodyHref;
}

function updateFrames (menuHref, bodyHref) // Used to update two frames.
    {
    window.location.href=bodyHref;
}

function upThreeFrames(menuHref, bodyHref, bannerHref)
	{
	window.location.href = bodyHref;
}

function pop (strFile, strTitle)  // Used to open link in new window
    {
    window.open (strFile,strTitle,"width=600,height=300;")
}

//----------------------------------------------------------------------------
// email address validate
//----------------------------------------------------------------------------

function validateEmailAddress(strEmailAddress) {
	var dotPosn = strEmailAddress.indexOf(".");
	var atSignPosn = strEmailAddress.indexOf("@");
	return (dotPosn > 0) && (atSignPosn > 0);
}

//----------------------------------------------------------------------------
// Checks on the Contact Us form
//----------------------------------------------------------------------------

function checkContactUsFields() {
    // First clear the fields
    var emailElem = document.getElementById("emailErrors");
    var email2Elem = document.getElementById("email2Errors");
    var nameElem = document.getElementById("nameErrors");
    var locationElem = document.getElementById("locationErrors");

    emailElem.firstChild.nodeValue = " ";
    email2Elem.firstChild.nodeValue = " ";
    nameElem.firstChild.nodeValue = " ";
    locationElem.firstChild.nodeValue = " ";

    // Check name
    var theName = document.forms.frmContactUs.txtName.value;
    if (theName.length == 0) {
        nameElem.firstChild.nodeValue = "Error: please supply a name";
        return false;
    }

    // Check the email addresses
    var email1 = document.forms.frmContactUs.txtEmail.value;
    var email2 = document.forms.frmContactUs.txtEmail2.value;
    if (email1.length == 0) {
        emailElem.firstChild.nodeValue = "Error: please supply an email address";
        return false;
    }
    else if (email1 != email2) {
        email2Elem.firstChild.nodeValue = "Error: the two email addresses differ";
        return false;
    }

    // Check the location
    var theLocation = document.forms.frmContactUs.txtLocation.value;
    if (theLocation.length == 0) {
        locationElem.firstChild.nodeValue = "Error: please supply a location";
        return false;
    }

    // everything is ok if we get here
    return true;
}

//----------------------------------------------------------------------------
// checks on the Contact Club login form
//----------------------------------------------------------------------------


function validateContactClubForm() {
    var cMessage = '';
    with (document.forms["login"]) {
        if (email.value == '') cMessage += '   E-mail Address\n';
        if (password.value == '') cMessage += '   Password\n';
        if (cMessage != '') {
            alert('Missing Mandatory Field(s)\n\n' + cMessage);
            email.focus();
            return false;
        }
        else {
            for (i = 0; i < elements.length; i++) {
                if (elements[i].type == 'text' || elements[i].type == 'textarea') {
                    elements[i].value = elements[i].value.replace(/'/gi, '`');
                }
            }
            return true;
        }
    }
}

function contactClubForgotten() {
    window.open("ccforgotten.asp?", "forgotten", "scrollbars=no,status=no,width=365,height=200,resizable=no,left=130,top=25");
}


//----------------------------------------------------------------------------
// checks on the Contact Club registration form
//----------------------------------------------------------------------------



function validateCCRegForm() {
    var cMessage = '';
    with (document.forms["registration"]) {
        if (firstname.value == '') cMessage += '   Firstname\n';
        if (surname.value == '') cMessage += '   Surname\n';
        if (email.value == '') cMessage += '   E-mail Address\n';
        if (password1.value == '') cMessage += '   Password\n';
        if (password2.value == '') cMessage += '   Verify Password\n';
        if (password1.value != password2.value) cMessage += '   Passwords do not match\n';
        if (details.value == '') cMessage += '   Details\n';
        if (cMessage != '') {
            alert('Missing Mandatory Field(s)\n\n' + cMessage);
            firstname.focus();
            return false;
        }
        else {
            var maxCharsOK = checkTextareaLength();
            if (maxCharsOK) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}

function checkCCRegTextareaLength() {
    var maxchars = 1000;
    if (document.registration.details.value.length > maxchars) {
        alert('Messages must be fewer than 1000 characters long. Please remove ' +
    (document.registration.details.value.length - maxchars) + ' characters');
        return false;
    }
    else
        return true;
}

function CCRegcalculator() {
    var strDetails = '';
    with (document.forms["registration"]) {
        strDetails = details.value;
    }
    window.open("cccalculator.asp?AboutYou=" + strDetails, "calculator", "scrollbars=no,status=no,width=200,height=120,resizable=no,left=130,top=25");
}

//----------------------------------------------------------------------------
// checks on the Forum login form
//----------------------------------------------------------------------------


function validateDfLoginForm() {
    var cMessage = '';
    with (document.forms["login"]) {
        if (email.value == '') cMessage += '   E-mail Address\n';
        if (password.value == '') cMessage += '   Password\n';
        if (cMessage != '') {
            alert('Missing Mandatory Field(s)\n\n' + cMessage);
            email.focus();
            return false;
        }
        else {
            for (i = 0; i < elements.length; i++) {
                if (elements[i].type == 'text' || elements[i].type == 'textarea') {
                    elements[i].value = elements[i].value.replace(/'/gi, '`');
                }
            }
            return true;
        }
    }
}

//----------------------------------------------------------------------------
// checks on the Forum login form
//----------------------------------------------------------------------------

function checkDfForgottenFields() {
    // First clear the error field
    var emailElem = document.getElementById("emailAddressErrors");
    emailElem.firstChild.nodeValue = " ";

    // Check the email addresses
    var email = document.forms.frmForgottenPassword.email.value;
    if (email.length == 0 || !validateEmailAddress(email)) {
        emailElem.firstChild.nodeValue = "Error: please supply a valid email address";
        return false;
    }

    // everything is ok if we get here
    return true;
}

//----------------------------------------------------------------------------
// checks on the add topic form
//----------------------------------------------------------------------------

function checkDfAddTopicFields() {
    missinginfo = "";
    if (document.form.name.value == "") {
        missinginfo += "\n     -  Name";
    }
    if (document.form.subject.value == "") {
        missinginfo += "\n     -  Topic";
    }
    if (document.form.message.value == "") {
        missinginfo += "\n     -  Message";
    }

    if (missinginfo != "") {
        missinginfo = "_____________________________\n" +
		"Sorry!  This topic can't be added\n" +
		"because you failed to include your:\n" +
		missinginfo + "\n_____________________________" +
		"\nPlease re-enter and try again!";
        alert(missinginfo);
        return false;
    }

    var maxCharsOK = checkDfAddTopicTextareaLength();
    if (maxCharsOK) {
        return true;
    }
    else {
        return false;
    }
}

function checkDfAddTopicTextareaLength() {
    var maxchars = 1500;
    if (document.form.message.value.length > maxchars) {
        alert('Messages must be fewer than 1500 characters long. Please remove ' +
    (document.form.message.value.length - maxchars) + ' characters');
        return false;
    }
    else
        return true;
}

//----------------------------------------------------------------------------
// checks on the forum registration form
//----------------------------------------------------------------------------

/**
* Checks that username, location, password and email address are all populated
* and less than a certain length, and that the email address is in
* valid format
*/
function checkDfRegistrationFields() {
    // First clear the fields
    var emailElem = document.getElementById("emailAddressErrors");
    var email2Elem = document.getElementById("emailAddress2Errors");
    var userNameElem = document.getElementById("userNameErrors");
    var locationElem = document.getElementById("locationErrors");
    var passwordElem = document.getElementById("passwordErrors");
    var password2Elem = document.getElementById("password2Errors");
    var agreedToTermsElem = document.getElementById("agreedtotermsErrors");

    emailElem.firstChild.nodeValue = " ";
    email2Elem.firstChild.nodeValue = " ";
    userNameElem.firstChild.nodeValue = " ";
    locationElem.firstChild.nodeValue = " ";
    passwordElem.firstChild.nodeValue = " ";
    password2Elem.firstChild.nodeValue = " ";

    // Check username
    var theUserName = document.forms.frmRegister.username.value;
    if (theUserName.length == 0) {
        userNameElem.firstChild.nodeValue = "Error: please supply a user name";
        return false;
    }

    // Check the email addresses
    var email = document.forms.frmRegister.emailaddress.value;
    if (email.length == 0 || !validateEmailAddress(email)) {
        emailElem.firstChild.nodeValue = "Error: please supply a valid email address";
        return false;
    }

    // Check the second email addresses
    var email2 = document.forms.frmRegister.emailaddress2.value;
    if (email2 != email) {
        email2Elem.firstChild.nodeValue = "Error: you entered two different email addresses";
        return false;
    }

    // Check the location address
    var location = document.forms.frmRegister.location.value;
    if (location.length == 0) {
        locationElem.firstChild.nodeValue = "Error: please supply a location";
        return false;
    }

    // Check password
    var thePassword = document.forms.frmRegister.password.value;
    if (thePassword.length == 0) {
        passwordElem.firstChild.nodeValue = "Error: please supply a password";
        return false;
    }

    // Check password
    var thePassword2 = document.forms.frmRegister.password2.value;
    if (thePassword2 != thePassword) {
        passwordElem.firstChild.nodeValue = "Error: you entered two different passwords";
        return false;
    }

    // Check that the "agree to terms" box is ticked
    var agreedToTerms = document.forms.frmRegister.agreedtoterms;
    if (!agreedToTerms.checked) {
        agreedToTermsElem.firstChild.nodeValue = "You cannot register unless you agree to the terms of use";
        return false;
    }

    // Check for data in the hidden "Notes" field. If its changed the form must be submitted by a bot so chuck it out
    var notes = document.forms.frmRegister.Notes.value;
    if (notes != 'Ignore this text') {
        return false;
    }

    // everything is ok if we get here
    return true;
}

//----------------------------------------------------------------------------
// checks on the forum update user form
//----------------------------------------------------------------------------

function checkDfUpdateUserFields() {
    // First clear the fields
    var emailElem = document.getElementById("emailAddressErrors");
    var email2Elem = document.getElementById("emailAddress2Errors");
    var locationElem = document.getElementById("locationErrors");
    var passwordElem = document.getElementById("passwordErrors");
    var password2Elem = document.getElementById("password2Errors");

    emailElem.firstChild.nodeValue = " ";
    email2Elem.firstChild.nodeValue = " ";
    locationElem.firstChild.nodeValue = " ";
    passwordElem.firstChild.nodeValue = " ";
    password2Elem.firstChild.nodeValue = " ";

    // Check the email addresses
    var email = document.forms.frmUpdateUser.emailaddress.value;
    if (email.length == 0 || !validateEmailAddress(email)) {
        emailElem.firstChild.nodeValue = "Error: please supply a valid email address";
        return false;
    }

    // Check that the two email addresses are equal if necessary
    var existingEmailAddress = document.forms.frmUpdateUser.existingemailaddress.value;
    var email2 = document.forms.frmUpdateUser.emailaddress2.value;
    var emailsNeedToBeEqual = (email != existingEmailAddress) || (email2.length > 0);
    if (emailsNeedToBeEqual && email2 != email) {
        email2Elem.firstChild.nodeValue = "Error: you entered two different email addresses";
        return false;
    }

    // Check the location address
    var location = document.forms.frmUpdateUser.location.value;
    if (location.length == 0) {
        locationElem.firstChild.nodeValue = "Error: please supply a location";
        return false;
    }

    // Check password
    var thePassword = document.forms.frmUpdateUser.password.value;
    var thePassword2 = document.forms.frmUpdateUser.password2.value;
    if (thePassword2 != thePassword) {
        passwordElem.firstChild.nodeValue = "Error: you entered two different passwords";
        return false;
    }

    // everything is ok if we get here
    return true;
}

//----------------------------------------------------------------------------
// checks on the add reply form
//----------------------------------------------------------------------------

function checkDfAddReplyFields() {
	missinginfo = "";
	if (document.form.name.value == "") {
		missinginfo += "\n     -  Name";
	}
	if(document.form.message.value == "") {
		missinginfo += "\n     -  Message";
	}

	if (missinginfo != "") {
		missinginfo ="_____________________________\n" +
		"Sorry!  This reply can't be added\n" +
		"because you failed to include your:\n" +
		missinginfo + "\n_____________________________" +
		"\nPlease re-enter and try again!";
		alert(missinginfo);
		return false;
	}
	
	var maxCharsOK = checkDfAddReplyTextareaLength();
	if (maxCharsOK) {
		return true;
	}
	else {
		return false;
	}
}

function checkDfAddReplyTextareaLength() {
	var maxchars = 1500;
	if(document.form.message.value.length > maxchars) {
   alert('Messages must be fewer than 1500 characters long. Please remove '+
    (document.form.message.value.length - maxchars)+ ' characters');
   return false; }
 else
   return true; 
}
