﻿
/**
* Function to submit a HTML form into a Highslide iframe popup.
*/
function submitToHighslide(form) {

    // identify the submit button to start the animation from
    var anchor;
    for (var i = 0; i < form.elements.length; i++) {
        if (form.elements[i].type == 'submit') {
            anchor = form.elements[i];
            break;
        }
    }

    // open an expander and submit our form when the iframe is ready
    hs.overrides.push('onAfterExpand');
    hs.htmlExpand(anchor, {
        objectType: 'iframe',
        src: 'about:blank',
        width: 400,
        height: 300,
        objectHeight: 265,
        onAfterExpand: function(expander) {
            form.target = expander.iframe.name;
            form.submit();
        }
    });
    // return false to delay the sumbit until the iframe is ready
    return false;
}
function KeyDownHandler(btn) {
    // process only the Enter key
    if (event.keyCode == 13) {
        // cancel the default submit
        event.returnValue = false;
        event.cancel = true;
        // submit the form by programmatically clicking the specified button
        btn.click();
    }
}
function setVisibility(id, visibility) {
    if (document.getElementById(id) != null) {
        document.getElementById(id).style.display = '';
    }
    if (document.getElementById('category') != null) {
        document.getElementById('category').style.display = 'none';
    }
    
}

function resetVisibility() {
    var producten = 20;

    for (var i = 0; i <= producten; i++) {
        var id = 'prod' + i;
        var id2 = 'cat' + i;
        if (document.getElementById(id) != null) {
            document.getElementById(id).style.display = 'none';
        }
        if (document.getElementById(id2) != null) {
            document.getElementById(id2).style.display = 'none';
        }
    }
    if (document.getElementById('category') != null) {
        document.getElementById('category').style.display = '';
    }

}
function checkValidation(formInput) {

    if (typeof (formInput) != "object") {
        alert("Validation not supported on this browser.");
        return (false);
    }

    var message;

    if (stringEmpty(formInput.value)) {
        message = "Error! There is no input value entered.";
        alert(message);
    } else if (noAtSign(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
        alert(message);
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain at least one character before the '@' character";
        alert(message);
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        alert(message);
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        alert(message);
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" must contain a period ('.') character.";
        alert(message);
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" must contain a two, three or four character suffix.";
        alert(message);
    } else {
        return (true);
        //message = "Success! The email address \"" + formInput.value + "\" validates OK.";
        //alert(message);
    }

    var objType = typeof (formInput.focus);
    if (objType == "object" || objType == "function") {
        formInput.focus();
    }

    return (false);
}

function checkValid(formName, fieldName) {
    //if (checkValidation(fieldName) == true) {
    // submitToHighslide(formName);
    document.location = 'Search.aspx?searchfor=' + fieldName.value; 
        //alert('E-Mail Address Validates OK');
    //}
    return (false);
}

function stringEmpty(formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if (formField.length < 1) {
        return (true);
    } else {
        return (false);
    }
}

function noAtSign(formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf('@', 0) == -1) {
        return (true)
    } else {
        return (false);
    }
}

function nothingBeforeAt(formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if (formField.indexOf('@', 0) < 1) {
        return (true)
    } else {
        return (false);
    }
}

function noLeftBracket(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if (formField.indexOf('[', 0) == -1 && formField.charAt(formField.length - 1) == ']') {
        return (true)
    } else {
        return (false);
    }
}

function noRightBracket(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf('[', 0) > -1 && formField.charAt(formField.length - 1) != ']') {
        return (true);
    } else {
        return (false);
    }
}

function noValidPeriod(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']')
        return (false);

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf('.', 0) == -1)
        return (true);

    return (false);
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return (false);
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf('.', len - 1) + 1;
    if ((len - pos) < 2 || (len - pos) > 4) {
        return (true);
    } else {
        return (false);
    }
}