﻿// BVSC Third Sector Database JavaScript Library

// Go to anchor function
function goToAnchor(pageName, anchorName) {
    location.href = pageName + "#" + anchorName;
}

// Finds a server control on the client side by id
function findObjWithClientId(Id) {
    var ctrls = document.getElementsByTagName("body")[0].getElementsByTagName("*");
    for (var count = 0; count < ctrls.length; count++) {
        var index = ctrls[count].id.indexOf(Id);
        if (index != -1) {
            if ((ctrls[count].id.length - index) == Id.length) {
                return ctrls[count];
            }
        }
    }
    return null;
}

// Confirm delete function
function confirmDelete(name) {
    return confirm("Are you sure you want to delete this " + name + "?");
}


// Checks for enter key and forces a postback to occur with the target event argument to be the same as the actionName parameter
function performPostbackOnEnter(e, actionName) {
    var characterCode;

    if (e && e.which) {
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;
    }

    if (characterCode == 13) {
        __doPostBack('__Page', actionName);
        return false;
    }
    else {
        return true;
    }
}


// Treeview node clicked postback routine 
// (causes postback to handle tree check box check events)
function treeViewNodeClickedPostback(mEvent) {
    var o;
    // Internet Explorer    
    if (mEvent.srcElement)
    {
        o = mEvent.srcElement;
    }
    // Netscape and Firefox
    else if (mEvent.target)
    {
        o = mEvent.target;
    }
    if (o.tagName == "INPUT" && o.type == "checkbox")
    {
       __doPostBack("","");
    } 
}


// Submit Postback routine
// (causes a postback for page load to pick up)
function submitPostback(actionName) {
    __doPostBack('__Page', actionName);
}


// Swap Checkbox Checks routine
function swapCheckboxChecked(checkedName, otherName) {
    var checkedCheckBox = findObjWithClientId(checkedName);
    if (checkedCheckBox.checked == false) {
        return;
    }
    var newState = !checkedCheckBox.checked;
    var otherCheckBox = findObjWithClientId(otherName);
    otherCheckBox.checked = newState;
}


// Trim routine for strings
function trimAll(stringToTrim) {
    while (stringToTrim.substring(0, 1) == ' ') {
        stringToTrim = stringToTrim.substring(1, stringToTrim.length);
    }
    while (stringToTrim.substring(stringToTrim.length - 1, stringToTrim.length) == ' ') {
        stringToTrim = stringToTrim.substring(0, stringToTrim.length - 1);
    }
    return stringToTrim;
}


// Valid postcode routine
function isValidPostCode(toCheck) {
    // Permitted letters depend upon their position in the postcode.
    var alpha1 = "[abcdefghijklmnoprstuwyz]";
    var alpha2 = "[abcdefghklmnopqrstuvwxy]";
    var alpha3 = "[abcdefghjkstuw]";
    var alpha4 = "[abehmnprvwxy]";
    var alpha5 = "[abdefghjlnpqrstuwxyz]";

    // Array holds the regular expressions for the valid postcodes
    var pcexp = new Array();
    // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));
    // Expression for postcodes: ANA NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));
    // Expression for postcodes: AANA  NAA
    pcexp.push(new RegExp("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$", "i"));
    // Exception for the special postcode GIR 0AA
    pcexp.push(/^(GIR)(\s*)(0AA)$/i);
    // Standard BFPO numbers
    pcexp.push(/^(bfpo)(\s*)([0-9]{1,4})$/i);
    // c/o BFPO numbers
    pcexp.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
    // Overseas Territories
    pcexp.push(/^([A-Z]{4})(\s*)(1ZZ)$/i);

    // Load up the string to check
    var postCode = toCheck;

    // Assume we're not going to find a valid postcode
    var valid = false;

    // Check the string against the types of post codes
    for (var i = 0; i < pcexp.length; i++) {
        if (pcexp[i].test(postCode)) {

            // The post code is valid - split the post code into component parts
            pcexp[i].exec(postCode);

            // Copy it back into the original string, converting it to uppercase and
            // inserting a space between the inward and outward codes
            postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();

            // If it is a BFPO c/o type postcode, tidy up the "c/o" part
            postCode = postCode.replace(/C\/O\s*/, "c/o ");

            // Load new postcode back into the form element
            valid = true;

            // Remember that we have found that the code is valid and break from loop
            break;
        }
    }

    // Return with either the reformatted valid postcode or the original invalid 
    // postcode
    if (valid) { return postCode; } else return false;
}


// Checks if a email address is valid
function isEmailAddressValid(emailAddress) {
    emailAddress = trimAll(emailAddress);
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(emailAddress)) {
        return false;
    }
    else {
        return true;
    }
}


// Confirm Affiliate Information Correct Routine
function confirmAffiliateInformationCorrect() {
    // Get all of the controls to validate
    
//    var uiAimTextarea = findObjWithClientId('uiAimTextarea');
//    var uiTargetGroupsTextarea = findObjWithClientId('uiTargetGroupsTextarea');
    //    var uiActivitiesTextarea = findObjWithClientId('uiActivitiesTextarea');
    var uiBriefDescriptionTextarea = findObjWithClientId('uiBriefDescriptionTextbox');
    var uiIsConfirmedCheckbox = findObjWithClientId('uiIsConfirmedCheckbox');
    var uiAuthorisedNameTextbox = findObjWithClientId('uiAuthorisedNameTextbox');
    var uiAuthorisedEmailTextbox = findObjWithClientId('uiAuthorisedEmailTextbox');
    var uiAuthorisedPositionTextbox = findObjWithClientId('uiAuthorisedPositionTextbox');
    var uiContactNameTextbox = findObjWithClientId('uiContactNameTextbox');
    var uiRelationshipTextbox = findObjWithClientId('uiRelationshipTextbox');
    var uiOrganisationName = findObjWithClientId('uiOrganisationName');
    var uiPublicAddress1Textbox = findObjWithClientId('uiPublicAddress1Textbox');
    var uiPublicAddressTownTextbox = findObjWithClientId('uiPublicAddressTownTextbox');
    var uiPublicAddressPostcodeTextbox = findObjWithClientId('uiPublicAddressPostcodeTextbox');    
//    var uiTelephoneTextbox = findObjWithClientId('uiTelephoneTextbox');
    //    var uiEmailTextbox = findObjWithClientId('uiEmailTextbox');

//    if (trimAll(uiAimTextarea.value) == "") {
//        alert('Please enter the Aim of Your Organisation!');
//        return false;
//    }
//    if (trimAll(uiTargetGroupsTextarea.value) == "") {
//        alert('Please enter your Target Groups and Communities!');
//        return false;
//    }
//    if (trimAll(uiActivitiesTextarea.value) == "") {
//        alert('Please enter the Main Activities of Your Organisation!');
//        return false;
    //    }

    if (trimAll(uiBriefDescriptionTextarea.value) == "") {
        alert('Please enter the Brief Description of Your Organisation, including its purpose or mission statement!');
        return false;
    }
    
    if (uiIsConfirmedCheckbox.checked == false) {
        alert('Please tick the checkbox to confirm that you support BVSC\'s vision, mission and values!');
        return false;
    }
    
    if (trimAll(uiAuthorisedNameTextbox.value) == "") {
        alert('Please enter the name of the authorised representative of your Organisation!');
        return false;
    }
    
    if (trimAll(uiAuthorisedEmailTextbox.value) == "") {
        alert('Please enter the Email address of the authorised representative of your Organisation!');
        return false;
    }
    
    if (isEmailAddressValid(uiAuthorisedEmailTextbox.value) == false) {
        alert('Please enter a valid Email address of your authorised representative!');
        return false;
    }
    
    if (trimAll(uiAuthorisedPositionTextbox.value) == "") {
        alert('Please enter the position of the authorised representative of your Organisation!');
        return false;
    }
    if (trimAll(uiContactNameTextbox.value) == "") {
        alert('Please enter the references contact name!');
        return false;
    }
    if (trimAll(uiRelationshipTextbox.value) == "") {
        alert('Please enter the references position!');
        return false;
    }  
    if (trimAll(uiOrganisationName.value) == "") {
        alert('Please enter the references organisation name!');
        return false;
    }
    if (trimAll(uiPublicAddress1Textbox.value) == "") {
        alert('Please enter the first line of your references Address!');
        return false;
    }
    if (trimAll(uiPublicAddressTownTextbox.value) == "") {
        alert('Please enter the town/city of your references Address!');
        return false;
    }       
    if (trimAll(uiPublicAddressPostcodeTextbox.value) == "") {
        alert('Please enter the Postcode of your references Address!');
        return false;
    }
    if (isValidPostCode(uiPublicAddressPostcodeTextbox.value) == false) {
        alert('Please enter a valid Postcode for your references Address!');
        return false;
    }
//    if (trimAll(uiTelephoneTextbox.value) == "") {
//        alert('Please enter the Telephone number of your reference!');
//        return false;
//    }
//    if (trimAll(uiEmailTextbox.value) == "") {
//        alert('Please enter the Email address of your reference!');
//        return false;
//    }
//    if (isEmailAddressValid(uiEmailTextbox.value) == false) {
//        alert('Please enter the Email address of your reference!');
//        return false;
//    }
          
   
    // Ask the user is they want to continue
    return confirm("Are you sure you want to create the application form with the information you have entered?\n\nThis information cannot be altered once you click OK, if you would like to alter any of the information click Cancel!");
}


