/* 
 * The JavaScript used by the NXNE website.
 */

// Augment the string object to have a startsWith method.
String.prototype.startsWith = function(str)
{
    return (this.match("^" + str) == str)
}

/**
 * Ensures the passed phone number conforms to the format
 * 12223334444.
 * @param {String} numberStr The number to fix.
 * @return The fixed number or undefined if it could not be fixed.
 * @type String
 */
function fixNumber(numberStr)
{
    // First, strip out all the non-numeric characters.
    var str = numberStr.replace(/\D+/g, '');
	
    // If the string's length is not 10 or 11, the number can't
    // possiblity be fixed or valid.
    if (str.length != 10 && str.length != 11) return undefined;
	
    // If the string length is 11 but the first number is not 1,
    // we can't fix it.
    if (str.length == 11 && str[0] != "1") return undefined;
	
    // If the string is length 10, prepend a 1.
    if (str.length == 10) str = "1" + str;
		
    // Otherwise, she's good to go.
    return str;
}

// Set the menu code to run when the website is ready.
jQuery(document).ready(function()
{    
    // Fetch the template directory.
    var templateDir = jQuery("#template_directory").val();
    //alert(templateDir);

    // Make all external links open in a new window.
    jQuery("a[href]").each(function()
    {
        // jQueryize it.
        var $e = jQuery(this);
        var href = $e.attr("href");

        // See if it starts with the NXNE domain.  If not, add the target
        // is blank stuffs.
        if (href.startsWith("http")
            && !href.startsWith("http://localhost")
            && !href.startsWith("http://nxne.com")
            && !href.startsWith("http://www.nxne.com"))
        {
            //alert(href);
            $e.attr("target", "_blank");
        }
    });

    // Validate the newsletter email address.
    jQuery("#newsletter_form").submit(function()
    {
        // Get the email field.
        var email = jQuery("#email").val();
			
        // The e-mail pattern.
        var pattern = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
			
        // See if it matches.
        if (pattern.test(email) == false)
        {
            alert("That e-mail address is invalid.");
            return false;
        }
        else
        {
            //alert("Works for me!");
            return true;
        }
    });
		 
    // Make any button that has a src_over attribute a hover button.
    jQuery("img[src_over]")
    .each(function()
    {
        $a = jQuery(this);
        $a.attr("src_out", this.src);
    })
    .hover(function()
    {
        $a = jQuery(this);
        this.src = $a.attr("src_over");
        this.style.cursor = 'pointer';
    },
    function()
    {
        $a = jQuery(this);
        this.src = $a.attr("src_out");
        this.style.cursor = 'default';
    });	    
 
    //jQuery('ul.sf-menu ul li').css("opacity", ".8");
    //jQuery('ul.sf-menu ul li span').css("opacity", "1.0");
	/*
    jQuery('ul.sf-menu').superfish({
        animation: {
            height: "show"
        },
        autoArrows: false,
        speed: "medium",
        dropShadows: false
    });
	*/
    // sIFR template.
//    var template = named({
//        sWmode:         "opaque",
//        sFlashSrc:      templateDir + "/fonts/SerifaBT.swf",
//        sColor:         "#000000",
//        sLinkColor:     "#000000",
//        sBgColor:       "#ffffff",
//        sHoverColor:    "#000000",
//        nPaddingTop:    0,
//        nPaddingBottom: 0
//    });
//
//    sIFR.replaceElement("h1", template);
//    sIFR.replaceElement("h3", template);
//
//    // Load sIFR.
//    if (typeof sIFR == "function")
//    {
//        sIFR();
//    }
	
    // The location of the web service.
    var registerUrl = "/forms/sms-register.php";
		
    // Set up SMS register.
    jQuery('#sms-register').submit(function()
    {
        // Get the number.
        var number = jQuery('#sms-number').val();
		
        // Check the format.
        var fixedNumber = fixNumber(number);
		
        // If the fixed number is undefined, the number is not valid.
        if (fixedNumber == undefined)
        {
            alert("Number needs to be formatted XXX-XXX-XXXX, as in 555-555-5555");
            return false;
        }
		
        // Clear it.
        jQuery('#sms-number').val("");
		
        // Register it.
        SmsRegister.registerNumber(registerUrl, fixedNumber, function(data)
        {
            alert(SmsRegister.getMessage(data));
        });
		
        return false;
    });
	

    jQuery("#newsletter_submit")
    .click(function()
    {
        jQuery("#newsletter_form,#newsletter_form_interactive").submit();
    });

    // Intercept newsletter sign-up submit and send to email redirector.
    jQuery("#newsletter_form, #newsletter_form_interactive").submit(function()
    {        
        var form = jQuery(this);
        jQuery.get(wp_root + "/forms/emailemailer.php",
            form.serialize(),
            function(json)
            {
                if (json.successful)
                    alert("Sign-up successful.");
                else
                    alert("Sign-up failed.");
            },
            "json");
			
        return false;
    });
	
});



