﻿function FormatNumber(num, decimalNum, bolLeadingZero, bolParens, bolCommas)
/**********************************************************************
IN:
NUM - the number to format
decimalNum - the number of decimal places to format the number to
bolLeadingZero - true / false - display a leading zero for
numbers between -1 and 1
bolParens - true / false - use parenthesis around negative numbers
bolCommas - put commas as number separators.
 
RETVAL:
The formatted number!
**********************************************************************/
{
    if (isNaN(parseInt(num))) return "NaN";

    var tmpNum = num;
    var iSign = num < 0 ? -1 : 1; 	// Get sign of number

    // Adjust number so only the specified number of numbers after
    // the decimal point are shown.
    tmpNum *= iSign; 				// Readjust for sign

    tmpNum = tmpNum.toFixed(2);

    // Create a string object to do our formatting on
    var tmpNumStr = new String(tmpNum);

    // See if we need to strip out the leading zero or not.
    if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
        if (num > 0)
        tmpNumStr = tmpNumStr.substring(1, tmpNumStr.length);
    else
        tmpNumStr = "-" + tmpNumStr.substring(2, tmpNumStr.length);

    // See if we need to put in the commas
    if (bolCommas && (num >= 1000 || num <= -1000)) {
        var iStart = tmpNumStr.indexOf(".");
        if (iStart < 0)
            iStart = tmpNumStr.length;

        iStart -= 3;
        while (iStart >= 1) {
            tmpNumStr = tmpNumStr.substring(0, iStart) + "," + tmpNumStr.substring(iStart, tmpNumStr.length)
            iStart -= 3;
        }
    }

    // See if we need to use parenthesis
    if (bolParens && num < 0)
        tmpNumStr = "(" + tmpNumStr.substring(1, tmpNumStr.length) + ")";

    return tmpNumStr; 	// Return our formatted string!
}



function subscribe() {
    //Check for valid email address
    var email = $('#subemail').val();
    var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,6})+$/;

    if (filter.test(email) == false) {
        showsubscribe(false, "The email address you entered is invalid, please check your email address and try again.")
        return false;
    }

    //Build json variable to send to function based on what is pressed
    var tojson = { 'Email': email, 'Name': '' };

    //Do the ajax, if success then edit checkout accordingly
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/signup.aspx/Subscribe",
        data: JSON.stringify(tojson),
        dataType: "json",
        success: function(result) {
            switch (result.d) {
                case 1:
                    msg = "The email address you entered is invalid, please check your email address and try again.";
                    success = false;
                    break;
                case 2:
                    msg = "The email address entered has already been subscribed to our newsletter.";
                    success = false;
                    break;
                case 3:
                    msg = "An error ocurred whilst adding your details please double check them and try again.";
                    success = false;
                    break;
                default:
                    msg = "You were successfully subscribed to our newsletter.";
                    success = true;
            }

            showsubscribe(success, msg);
        },
        error: function(xhr, status, error) {
            showsubscribe(false, xhr.responseText);
        }
    });

    return false;
}

function showsubscribe(success, msg) {
    $('#newsletterbox-msg').html(msg);

    if (success) {
        $('#newsletterbox-ttl').html("Thank You For Subscribing");
        //$('#newsletterbox-ttl').attr("class", "success");
    } else {
        $('#newsletterbox-ttl').html("An Error Ocurred");
        //$('#newsletterbox-ttl').attr("class", "error");
    }

    $.fancybox({
        'content': $('#newsletterbox').html(),
        'showCloseButton': true
    });
}

function checkEmail(strng) {
    var error = "";

    var emailFilter = /^.+@.+\..{2,6}$/;

    if (!(emailFilter.test(strng))) {
        return false;
    }

    var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\] ']/

    if (strng.match(illegalChars)) {
        return false;
    }

    return true;
}

function nl_validator(theForm) {
    if (!checkEmail(theForm.newsletter.value)) {
        alert("Please enter a valid Email Address");
        theForm.newsletter.focus();
        return false;
    }

    return (true);
}

