﻿// Limit the number of characters in an input
function LimitChars(obj,limit) 
{
    if(String(obj.value).length > limit) 
    {
        obj.value = String(obj.value).substring(0,limit);
    }
}

// Update the counter showing the number of chars left
function UpdateCharCount(obj,limit)
{
    var count = document.getElementById(obj.id + "_Count");

    if(count) 
    {
        count.value = limit - String(obj.value).length;
    }
}

// Terms and Conditions popup
function ShowTerms()
{
	window.open('Terms.aspx', 'tc', 'width=700,height=500,toolbars=0,scrollbars=1');
}

// MSN Header/Footer
function LoadMSN()
{
	var MsnHeader = document.getElementById("msnHeader");
	var MsnFooter = document.getElementById("msnFooter");
	var MsnMpu = document.getElementById("msnMPU");
	
	var hdnMsnHeader = document.getElementById("hdnMsnHeader");
	var hdnMsnFooter = document.getElementById("hdnMsnFooter");
	var hdnMsnMPU = document.getElementById("hdnMsnMPU");

	if (MsnHeader && hdnMsnHeader) swapNinemsnAd(hdnMsnHeader, MsnHeader);
	if (MsnFooter && hdnMsnFooter) swapNinemsnAd(hdnMsnFooter, MsnFooter);
	if (MsnMpu && hdnMsnMPU) swapNinemsnAd(hdnMsnMPU, MsnMpu);
}

window.onload = LoadMSN;


function swapNinemsnAd(tmp, origin) 
{
    cleanWhitespace(tmp)

    var children = tmp.childNodes;
    var parent = origin;
    var newNode = document.createElement("div");

    for (var i = 0; i < children.length; i++) {
        newNode.appendChild(children[i]);
    }

    parent.appendChild(newNode);
}

var notWhitespace = /\S/;
function cleanWhitespace(node) {
    for (var x = 0; x < node.childNodes.length; x++) {
        var childNode = node.childNodes[x]

        if ((childNode.nodeType == 3) && (!notWhitespace.test(childNode.nodeValue))) {
            // that is, if it's a whitespace text node
            node.removeChild(node.childNodes[x])
            x--
        }

        if (childNode.nodeType == 1) {
            // elements can have text child nodes of their own
            cleanWhitespace(childNode)
        }
    }
}

function modalWindow(width, height)
{
    var left = 250;
    var top = 250;
    
    if (screen.width && screen.height)
    {
        left = Math.round((screen.width - width) / 2);
        top = Math.round((screen.height - height) / 2);
    }
    else if (document.all || document.layers)
    {
        left = Math.round((screen.availWidth - width) / 2);
        top = Math.round((screen.availHeight - height) / 2);
    }

    if (window.showModalDialog)
    {
        window.showModalDialog("popup/SendToFriend.aspx", "SendToFriend", "dialogWidth:" + width + "px;dialogHeight:" + height + "px");
    }
    else
    {
        window.open("popup/SendToFriend.aspx", "SendToFriend", "height=" + height + ",width=" + width + ",left=" + left + ",top=" + top + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizeable=no,modal=yes");
    }
}

function ValidateMultipleEmails(sender, e)
{
    var recipients = e.Value.split(";");
    var recipient = "";
    
    var regex = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    
    regex.ignoreCase = true;
    
    e.IsValid = true;
    
    for (var i = 0; i < recipients.length; i++)
    {        
        recipient = recipients[i].replace(/^\s+/, "");
        recipient = recipients[i].replace(/\n/g, "");
    
        if (recipient.length > 0 && !recipient.match(regex))
        {
            e.IsValid = false;
            break;
        }
    }
}
