﻿var submitNL = function(checkForDate) {
    var dob = [];
    if (checkForDate) {
        var n_day = $get('dobday');
        var n_month = $get('dobmonth');
        var n_year = $get('dobYear');

        dob[0] = n_day[n_day.selectedIndex].value;
        dob[1] = n_month[n_month.selectedIndex].value;
        dob[2] = n_year[n_year.selectedIndex].value;
    }


    var nlOptions = [];
    jQuery("#newsOptions tr").each(function() {
        var currRow = jQuery(this);
        var currOption = [];
        currOption[0] = currRow.find("td").eq(1).find("span:first").html();
        currOption[1] = currRow.find("td:first").find("input[type=checkbox]:first").attr("checked");
        nlOptions.push(currOption);
    });

    Utils.ApplyForNewsletter(
            dob,
            jQuery('#theEmail').val(),
            nlOptions,
            function(result) {
                
            }
        );

    return true;
};

///Given the particular input field, the function returns true if the field value is not empty
///else, it returns false
function validate_required(field) {
    if (field.value === null || field.value === "")
    { return false; }
    else {return true; }
}

///Given the particular input field, the function returns true if the field value is a valid email address
///else, it returns false
function validate_email(field) {
        apos = field.value.indexOf("@");
        dotpos = field.value.lastIndexOf(".");
        if (apos < 1 || dotpos - apos < 2)
        { return false; }
        else { return true; }
}

function nl_validateForm(emailField, buttonID) {
    var theButton = $get(buttonID);
    if (validate_email($get(emailField))) {
        theButton.disabled = false;
    }
    else {
        theButton.disabled = true;
    }
}

///Clears all the fields in the Contact Form
function ClearContactExpertForm() {
    document.getElementById('ctl00_ContentHolder_ctl00_txtName').value = '';
    document.getElementById('ctl00_ContentHolder_ctl00_txtSurname').value = '';
    document.getElementById('ctl00_ContentHolder_ctl00_txtEmail').value = '';
    document.getElementById('ctl00_ContentHolder_ctl00_txtQuery').value = '';
}

function RequiredField(fieldname, errorMessage) {
    var theField = document.getElementById(fieldname);
    if (($.trim(theField.value) === "") || ($.trim(theField.value) === "Enter a Keyword Here")) {
        alert(errorMessage);
        return false;
    }
    else {
        return true;
    }
}

function printPage() {
    window.print();
}

if (location.href.indexOf("juniornews") < 0) {
    (function() {
        function clickIE4() {
            if (event.button === 2) {
                return false;
            }
        }

        function clickNS4(e) {
            if (document.layers || document.getElementById && !document.all) {
                if (e.which === 2 || e.which === 3) {
                    return false;
                }
            }
        }

        if (document.layers) {
            document.captureEvents(Event.MOUSEDOWN);
            document.onmousedown = clickNS4;
        }
        else if (document.all && !document.getElementById) {
            document.onmousedown = clickIE4;
        }

        document.oncontextmenu = function() {
            return false;
        };
    })();
}
/// <summary>
/// Used as a workaround for IE's bug, that prevents a string of <option>s to be added to a Select object
/// </summary>
function select_innerHTML(objeto, innerHTML) {
    objeto.innerHTML = "";
    var selTemp = document.createElement("micoxselect");
    var opt;
    selTemp.id = "micoxselect1";
    document.body.appendChild(selTemp);
    selTemp = document.getElementById("micoxselect1");
    selTemp.style.display = "none";
    if (innerHTML.toLowerCase().indexOf("<option") < 0) {//se não é option eu converto
        innerHTML = "<option>" + innerHTML + "</option>";
    }
    innerHTML = innerHTML.replace(/<option/g, "<span").replace(/<\/option/g, "</span");
    selTemp.innerHTML = innerHTML;


    for (var i = 0; i < selTemp.childNodes.length; i++) {
        var spantemp = selTemp.childNodes[i];

        if (spantemp.tagName) {
            opt = document.createElement("OPTION");

            if (document.all) { //IE
                objeto.add(opt);
            } else {
                objeto.appendChild(opt);
            }

            //getting attributes
            for (var j = 0; j < spantemp.attributes.length; j++) {
                var attrName = spantemp.attributes[j].nodeName;
                var attrVal = spantemp.attributes[j].nodeValue;
                if (attrVal) {
                    try {
                        opt.setAttribute(attrName, attrVal);
                        opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
                    } catch (e) { }
                }
            }
            //getting styles
            if (spantemp.style) {
                for (var y in spantemp.style) {
                    if(spantemp.style.hasOwnProperty(y)) {
                        try { 
                            opt.style[y] = spantemp.style[y]; 
                        } catch (exp) { 
                        }
                    }
                }
            }
            //value and text
            opt.value = spantemp.getAttribute("value");
            opt.text = spantemp.innerHTML;
            //IE
            opt.selected = spantemp.getAttribute('selected');
            opt.className = spantemp.className;
        }
    }
    document.body.removeChild(selTemp);
    selTemp = null;
}

function SelectOptionInList(lstSelectList, intID) {
    try {
        var intIndex = 0;
        // Loop through all the options
        for (intIndex = 0; intIndex < lstSelectList.options.length; intIndex++) {
            // Is this the ID we are looking for?
            if (lstSelectList.options[intIndex].value === intID) {
                // Select it
                lstSelectList.selectedIndex = intIndex;
                // Yes, so stop searching
                break;
            }
        }
    }
    catch (expError) {
        alert("ClientUtilities1.js::SelectOptionInList( ).\n" +
                              "Error:" + expError.number + ", " + expError.description);
    }
} // SelectOptionInList


$.fn.textwatermark = function(opts) {
    return $(this).each(function() {
        var currBox = $(this);

        if (!currBox.val()) {
            currBox.val(opts.watermark);
        }

        currBox.focus(function() {
            if ($.trim(currBox.val()) === opts.watermark) {
                currBox.val('');
            }
            
        }); //end of 'currBox.focus(function() {'
        currBox.blur(function() {
            if ($.trim(currBox.val()) !== '') {
                return;
            }
            currBox.val(opts.watermark);
        }); //end of 'currBox.blur(function() {'
    }); //end of 'return $(this).each(function() {'
};   //end of '$.fn.textwatermark'
