﻿// Javascript File

function ShowMySubscriptions()
{
    if (isWkst)
        alert("This function is disabled in the workstation.");
    else
    {
        if (ValidateInputs(false))
            SubmitMySubscriptions("show");
    }
}

function SaveMySubscriptions()
{
    if (isWkst)
        alert("This function is disabled in the workstation.");
    else
    {
        if (ValidateInputs(false))
        {
            BuildPageSubscriptionFlags();
            
            if (document.getElementById("PageSubscriptions").value.length == 0)
                AlertUser("Please select at least one category or interest to continue.", null);
            else
            {
                SubmitMySubscriptions("save");
            }
        }
    }
}

function UnsubscribeMe()
{
    if (isWkst)
        alert("This function is disabled in the workstation.");
    else
    {
        if (ValidateInputs(true))
            SubmitMySubscriptions("unsubscribe");
    }
}

function SubmitMySubscriptions(requestType)
{
    document.getElementById("PageSubRequestType").value = requestType;
    document.getElementById("PageSubForm").submit();
}

function PageSubscription_Click(checkbx)
{
	var masterList = document.getElementById("SelectPageSubList");
	
	if (checkbx.checked)
		masterList.options[masterList.length] = new Option(checkbx.attributes["catId"].value);
	else
	{
		for (var i=0; i<masterList.length; i++)
		{
			if (checkbx.attributes["catId"].value == masterList.options[i].text)
			{
				masterList.options[i] = null;
				break;
			}
		}
	}
	
	AlertUser("", null);
}

function BuildPageSubscriptionFlags()
{
	var masterList = document.getElementById("SelectPageSubList");
	var flagList = "";
	
	for (var i=0; i<masterList.length; i++)
	{
		flagList += "*" + masterList.options[i].text + "*";
	}
	
	document.getElementById("PageSubscriptions").value = flagList;
}

function ValidateInputs(isUnsubscribe)
{
    AlertUser("", null);
    
    // check for required fields
    if (document.getElementById("email").value.length == 0)
    {   
        if (isUnsubscribe)
            AlertUser("A valid email is required to unsubscribe from page updates.", "email");
        else
            AlertUser("A valid email is required to subscribe to page updates.", "email");
        return false;
    }
    
    // check for email length
    if (document.getElementById("email").value.length > 100)
    {
        AlertUser("Your email address cannot exceed 100 characters.", "email");
        return false;
    }
		    
    // check for email validity
    var RegularExpressionEmail = "\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    if (document.getElementById("email").value.search(RegularExpressionEmail)==-1)
    {
        if (isUnsubscribe)
            AlertUser("A valid email is required to unsubscribe from page updates.", "email");
        else
            AlertUser("A valid email is required to subscribe to page updates.", "email");
        return false;
    }
    
    return true;
}

function AlertUser(msg, controlId)
{
    if (msg != null)
    {
        document.getElementById("ErrorMsg").innerHTML = msg;
    }
    
    if (controlId != null)
    {
        document.getElementById(controlId).focus();
        document.getElementById(controlId).select();
    }
}