// BEGINNING OF FILE
// Copyright © McDonagh Brothers
// Script by: Peter S. Anderson

/*
**************************************************************
Function Index:
	ScrapeParentContent()
	winOpen(p,n,w,h,a)
	winPrint(q)
	winHelp(id)
	ExpandCollapse(oBlock, oMark)
	CheckUnCheck(iVal)
	isDate(dateStr)
	testEmail(email)

Revisions:
	Created:	04/28/2004	PSA
	Modified:	05/18/2004	PSA		Added CheckUnCheck
	Modified:	06/02/2004	PSA		Added isDate, testEmail
	Modified:   06/15/2004	PSA		Added winOpen, winPrint, winHelp
	Modified:   06/28/2004	PSA		Added ScrapeParentContent
	
**************************************************************
*/

// Get the content from the parent window and write it to the PrintView window.
function ScrapeParentContent()
{
	var oParentDiv = window.opener.document.getElementById("fmeScrape");
	var oThisDiv = document.getElementById("fmeScraper");
	var sHtml = oParentDiv.innerHTML;
	oThisDiv.innerHTML = sHtml;
}

// Open a Pop-up Window
function winOpen(p,n,w,h,a)
{
	var attr;
	if (a != '')
	{
		attr = ',' + a;
	}
	else
	{
		attr = a;
	}
	window.open(p,n,'width='+w+',height='+h+',status=1'+a);
}

// Open a PrintView window
function winPrint()
{
	var a = ",scrollbars=yes,resizable=yes,toolbar=yes";
	winOpen("/PrintView.aspx", "PrintView", 660, 500, a);
}

// Open a Categorize window
function winCat(id)
{
	var a = ",scrollbars=yes,resizable=yes,toolbar=yes";
	winOpen("/MyInformeDesign_Categorize.aspx?id=" + id, "Categorize", 320, 240, a);
}

// Open a Password Change Window
function winPasswordChange(id)
{
	var a = ",scrollbars=no,resizable=no,toolbar=no";
	winOpen("PasswordChange.aspx?id=" + id, "ChangePassword", 400, 350, a);
}

// Open a Glossary Window
function gt(id, nm)
{
	winOpen("/Glossary_Term.aspx?id=" + id + "&nm=" + nm, "Term", 320, 240, "resizable=yes,scrollbars=yes");
}

// Expand or Collapse the CheckBox Tree for Taxonomy Lists
function ExpandCollapse(oBlock, oMark)
{
	if (oBlock.style.display == 'none')
	{
		oBlock.style.display = 'block';
		oMark.innerHTML = '<b>&nbsp;-&nbsp;</b>';
	}
	else
	{
		oBlock.style.display = 'none';
		oMark.innerHTML = '<b>&nbsp;+&nbsp;</b>';
	}
}

// Checks or UnChecks the CheckBox Tree with a matching iVal
function CheckUnCheck(iVal)
{
	var el = document.forms[0].elements;
	for (var i = 0; i < el.length; ++i)
	{
		if (el[i].type.toLowerCase() == "checkbox" && el[i].value == iVal)
		{
			if (el[i].checked == true)
			{
				el[i].checked = false;
			}
			else
			{
				el[i].checked = true;
			}
		}
	}
}

function CheckUnCheckGroup(obj)
{
    var collection = null;
    if (obj.parentElement.parentElement.nextSibling)
    {
        collection = obj.parentElement.parentElement.nextSibling;
    }
    else
    {
        collection = obj.parentElement.parentElement;
    }
    for (var i = 1; i < collection.all.length; ++i)
    {
        var el = collection.all[i];
        if (el.tagName == "INPUT")
        {
            if (el.type.toLowerCase() == "checkbox" && el.value != obj.value)
            {
                el.checked = obj.checked;
            }
        }
    }
}

// Checks for a valid Date
function isDate(dateStr)
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is format OK?

	if (matchArray == null)
	{
		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	// parse date into variables
	month = matchArray[1];
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) // check month range
	{ 
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31)
	{
		//alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31)
	{
		//alert("Month " + month + " doesn't have 31 days!")
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap))
		{
			//alert("February " + year + " doesn't have " + day + " days!");
			return false;
		}
	}
	return true;  // date is valid
}

// Checks for a valid Email Address
function testEmail(email)
{
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(email);
}
/*
**************************************************************
 END OF FILE
**************************************************************
*/
