﻿function trimSpaces(stringToTrim) {
    var newTrimmedString = "";
    var startPos = 0;
    var endPos = 0;

    if (stringToTrim != "") {
        for (startPosCounter = 0; startPosCounter <= stringToTrim.length - 1; startPosCounter++) {
            currentChar = stringToTrim.charAt(startPosCounter)

            if (currentChar != " ") {
                startPos = startPosCounter;
                break;
            }
        }

        for (startPosCounter = stringToTrim.length - 1; startPosCounter >= 0; startPosCounter--) {
            currentChar = stringToTrim.charAt(startPosCounter)

            if (currentChar != " ") {
                endPos = startPosCounter + 1;
                break;
            }
        }

        newTrimmedString = stringToTrim.substring(startPos, endPos);
        return newTrimmedString;
    }
    else
    { return stringToTrim; }
}


function enableUseOtherSoftware(yesOrNo) {
    if (!yesOrNo) { // enable!
        disableIt(document.getElementById("label_txtOtherSoftware"));
        disableIt(document.forms.frmQuestion.txtOtherSoftware);
        disableIt(document.getElementById("label_txtReasonForOtherSoftware"));
        disableIt(document.forms.frmQuestion.txtReasonForOtherSoftware);

    } else {
        enableIt(document.getElementById("label_txtOtherSoftware"));
        enableIt(document.forms.frmQuestion.txtOtherSoftware);
        enableIt(document.getElementById("label_txtReasonForOtherSoftware"));
        enableIt(document.forms.frmQuestion.txtReasonForOtherSoftware);
    }
}

function enableUserLodgement(yesOrNo) {
    if (!yesOrNo) { // enable!
        disableIt(document.getElementById("label_txtScheme"));
        disableIt(document.forms.frmQuestion.txtScheme);
        disableIt(document.getElementById("label_selSwitchSchemes"));
        disableIt(document.forms.frmQuestion.selSwitchSchemes);

        disableIt(document.getElementById("label_numLodgementsYear"));
        disableIt(document.forms.frmQuestion.numLodgementsYear);

    } else {
        enableIt(document.getElementById("label_txtScheme"));
        enableIt(document.forms.frmQuestion.txtScheme);
        enableIt(document.getElementById("label_selSwitchSchemes"));
        enableIt(document.forms.frmQuestion.selSwitchSchemes);

        enableIt(document.getElementById("label_numLodgementsYear"));
        enableIt(document.forms.frmQuestion.numLodgementsYear);
    }

}

function disableIt(thingToDisable) {
    thingToDisable.className = "disabled"
    thingToDisable.value = "";
    thingToDisable.disabled = true;
}

function enableIt(thingToEnable) {
    thingToEnable.className = "not_disabled"
    thingToEnable.disabled = false;
    thingToEnable.value = "";
}


function checkForm() {
    var formObj = document.forms.frmQuestion;

    var errorString = '';
    var hasChecked = false;

    for (i = 0; i < formObj.txtFrequencyOfUse.length; i++) {
        if (formObj.txtFrequencyOfUse[i].checked) {
            hasChecked = true;
        }
    }
    if (!hasChecked) {
        errorString += 'Please select how often you use the calculator.\n'
    }
    hasChecked = false;



    for (i = 0; i < formObj.selUseOtherSoftware.length; i++) {
        if (formObj.selUseOtherSoftware[i].checked) {
            hasChecked = true;
        }
    }

    if (!hasChecked) {
        errorString += 'Please select if you use other SAP software.\n'
    }

    hasChecked = false;


    for (i = 0; i < formObj.selMemberOfOtherScheme.length; i++) {
        if (formObj.selMemberOfOtherScheme[i].checked) {
            hasChecked = true;
        }
    }

    if (!hasChecked) {
        errorString += 'Please select if you are a member of another scheme.\n'
    }

    hasChecked = false;


    for (i = 0; i < formObj.selAwareOfOurLodgeScheme.length; i++) {
        if (formObj.selAwareOfOurLodgeScheme[i].checked) {
            hasChecked = true;
        }
    }

    if (!hasChecked) {
        errorString += 'Please select if you are aware of our lodgement scheme.\n'
    }

    hasChecked = false;

    for (i = 0; i < formObj.selInterestedInScheme.length; i++) {
        if (formObj.selInterestedInScheme[i].checked) {
            hasChecked = true;
        }
    }

    if (!hasChecked) {
        errorString += 'Please select if you would be interested in joining this scheme.\n'
    }

    hasChecked = false;

    if (errorString == '') {
        formObj.action = 'questionnaire.asp?action=submit';
        formObj.submit();
       return true;
      } else {
      alert(errorString);
      return false;
      }

}

function checkOptInForm(theName, theEmail,theForm,actionPage) {
     if (theName != '' && theEmail != '') {
        theForm.action = actionPage + "?action=optIn"
        theForm.submit();
        return true;
    } else {
        alert("Please make sure you have entered your name and email address");
        return false;   
    }
}

function isValidEmail(emailStr) {
    var email_errMsg_Header = "";
    var emailPat = /^(.+)@(.+)$/
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
    var validChars = "\[^\\s" + specialChars + "\]"
    var quotedUser = "(\"[^\"]*\")"
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
    var atom = validChars + '+'
    var word = "(" + atom + "|" + quotedUser + ")"
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$")
    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$")
    var matchArray = emailStr.match(emailPat)

    if (matchArray == null) {
        return email_errMsg_Header + "The email address supplied seems to be invalid. (check @ and .'s).\n";
    }

    var user = matchArray[1]
    var domain = matchArray[2]

    if (user.match(userPat) == null) {
        return email_errMsg_Header + 'The username in the email address is invalid.\n'
    }

    var IPArray = domain.match(ipDomainPat)
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                return email_errMsg_Header + 'The destination IP address is invalid.\n'
            }
        }
        return true
    }

    var domainArray = domain.match(domainPat)

    if (domainArray == null) {
        return email_errMsg_Header + 'The email address supplied includes an invalid domain name.\n'
    }

    var atomPat = new RegExp(atom, "g")
    var domArr = domain.match(atomPat)
    var len = domArr.length

    if (domArr[domArr.length - 1].length < 2 || domArr[domArr.length - 1].length > 4) {
        return email_errMsg_Header + 'The email address supplied does not have a correct suffix (.co.uk, .com...).\n'
    }
    if (len < 2) {
        return email_errMsg_Header + 'The email address supplied is missing a hostname.\n'
    }

    //If we've gotten this far, everything's valid	
    return true;
}

//=====================================================================================================================================================
//=== BEGIN: POPUP A NEW WINDOW																														===
//=== --------------------------------------------------------------------------------------------------------------------------------------------- ===
//=== popUpWindow(targetURL, targetWindowName, targetWidth, targetHeight, customParameters)															===
//=== 																																				===
//=== Pops up a new window with the parsed targetURL loaded.																						===
//=== 																																				===
//=== #REQUIRED#																																	===
//===   [targetURL]: Options (---)																													===
//===        - the url you wish to be opened																										===
//=== 																																				===
//=== #NON-REQUIRED#																																===
//===   [targetWindowName]: Options (---)																											===
//===        - Determine the name of the new window (defaults to "popUpWindow")																		===
//=== 																																				===
//===   [targetWidth]: Options (---)																												===
//===        - Determine the width of the new window (defaults to "400px")																			===
//=== 																																				===
//===   [targetHeight]: Options (---)																												===
//===        - Determine the height of the new window (defaults to "400px")																			===
//=== 																																				===
//===   [customParameters]: Options (see example)																									===
//===        - Determine any other parameters of the new window (defaults to "directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no")	===
//=== 																																				===
//=== -- EXAMPLE ---------------------------------------------------------------------------------------------------------------------------------- ===
//=== popUpWindow('http://www.adobe.com/products/acrobat/readstep.html','popupAcrobat','600','600','directories=yes,								===
//===             status=yes,resizable=yes,scrollbars=yes,location=1,menubar=yes,left=0,top=0')														===
//=====================================================================================================================================================
var imageLibraryPopupWindow;

function popUpWindow(targetURL, targetWindowName, targetWidth, targetHeight, customParameters) {
    var windowXPosition = 0;
    var windowYPosition = 0;
    var windowName = "popUpWindow"
    var windowWidth = 400;
    var windowHeight = 400;
    var windowParams = "directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no";

    if (!targetURL)
    { return false; }
    else {
        //if there were any parsed option parameters then set the windows parameters to these
        if (targetWindowName)
        { windowName = targetWindowName; }

        if (targetWidth)
        { windowWidth = targetWidth; }

        if (targetHeight)
        { windowHeight = targetHeight; }

        if (customParameters)
        { windowParams = customParameters; }

        //set the new window with all the parameters and open the new window
        imageLibraryPopupWindow = window.open(targetURL, windowName, "toolbar=no,width=" + windowWidth + ",height=" + windowHeight + ",screenX=" + windowXPosition + ",screenY=" + windowYPosition + "," + windowParams);
        imageLibraryPopupWindow.focus();
    }
}
//========================================================================================================================================================
//=== END: POPUP A NEW WINDOW																														   ===
//========================================================================================================================================================