
//This code defines a function that will toggle the visibility of an element based on its ElementID
function ToggleVisibility(ElementID)
{
    var CurrentDisplaySetting = document.getElementById(ElementID).style.display;
    if(CurrentDisplaySetting.indexOf("none") >= 0)
    {
        // The current display is hidden ... so show it.
        document.getElementById(ElementID).style.display = "";
    }
    else
    {
        // The current display currently shown ... so hide it.
        document.getElementById(ElementID).style.display = "none";
    }
}

// This function is a "work around" to allow users to view embedded content in MSIE without first having
// to click on the embedded object to activate it
function ShowEmbeddedContent(objecttag)
{
    document.write(objecttag);
}

// This function opens a popup in the center of the screen and assigned the given parameters to it
function OpenCenterPopUp(url, title, width, height)
{
	var TopLeftX = Math.round(screen.width/2 - width/2);
	var TopLeftY = Math.round(screen.height/2 - height/2);
	var NewWindow = window.open(url,title,'top='+TopLeftY+', left='+TopLeftX+', width='+width+',height='+height+',directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no');
	NewWindow.focus();
}

// This function is similiar to OpenCenterPopUp, except that it allows the user to see things like the toolbar, location, etc.
function OpenNewWindow(url, title, width, height)
{
	var TopLeftX = screen.width/2 - width/2;
	var TopLeftY = screen.height/2 - height/2;
	var NewWindow = window.open(url,title,'top='+TopLeftY+', left='+TopLeftX+', width='+width+',height='+height+',directories=yes,location=yes,menubar=yes,status=yes,toolbar=yes,resizable=yes');
	NewWindow.focus();
}

function OpenCalendarPopUp(FormElement)
{
	OpenCenterPopUp('/Calendar.aspx?FormElement=' + FormElement, 'CalendarWindow', 175, 135);
}

function ChangeClass(TableName, CellIndex, NewClass)
{
	var TableElement, TableCells;
	
	if(document.getElementById)
		TableElement = document.getElementById(TableName);
	else 
		TableElement = document.all(TableName);
	
	if(TableElement == null)
		return;
		
	if(TableElement.getElementsByTagName)
		TableCells = TableElement.getElementsByTagName("TD");
	else 
		TableCells = TableElement.all.tags("TD");
	
	if(TableCells == null)
		return;
		
	if(TableCells.length <= CellIndex)
		return;
	
	TableCells[CellIndex].className = NewClass;
}

function SetDefaultButton(btn, event)
{
    if(document.all)
    {
		if(event.keyCode == 13)
		{
            event.returnValue=false;
            event.cancel = true;
            btn.click();
        }
    }
    else if(document.getElementById)
    {
        if(event.which == 13)
        {
            event.returnValue=false;
            event.cancel = true;
            btn.click();
        }
    }
    else if(document.layers)
    {
        if(event.which == 13)
        {
            event.returnValue=false;
            event.cancel = true;
            btn.click();
        }
    }
}

// Set the focus to the first textbox
function SetFocus()
{
	var NumElements = document.forms[0].elements.length;
	for (i=0; i < NumElements; i++)
	{
   		if (document.forms[0].elements[i].type == "textarea" ||  document.forms[0].elements[i].type == "text")
		{
			document.forms[0].elements[i].focus();
		    break;
   		}
	}
}

function SetFocus(Element)
{
	if(Element != null)
	{
		if(document.getElementById(Element) != null)
		{
			document.getElementById(Element).focus();
		}
	}
}

function ValidateUSNumber(sender, args)
{
	var SelectedCountryID = document.getElementById("ddlCountry").value;

	// Check to see if the United States is the selected country, and if it is perform the validation
	if(SelectedCountryID == document.getElementById("hdnUSCountryID").value)
	{
		// Check thisNumber against the regular expression for valid U.S. Numbers
		var thisNumber = new String(args.Value); 
		var thisExp = new RegExp("(^1?[-.]?\\d{10}$)|(^1?[-.]?\\d{3}[-.]\\d{3}[-.]\\d{4}$)|(^1?[-.]?[-.]?\\(\\d{3}\\)\\d{3}[-.]\\d{4}$)|(^1?[-.]?\\(\\d{3}\\) \\d{3}[-.]\\d{4}$)");
		if(!thisNumber.match(thisExp))
		{
			args.IsValid = false;
			return;
		}
	}
	args.IsValid = true;
}

//This function resets all the controls form.
function ResetForm()
{
	if (document.getElementById('hdnSubmitted').value == 1)
	{
		var form, elements, i, elm; 
		form = document.getElementById("Form1");
				
		elements = form.elements;
		for( i=0, elm; i < elements.length; i++)
		{
			elm = elements[i];
			if (elm.getAttribute('type') == "text")
			{
				elm.value = '';
			}
			else if (elm.getAttribute('type') == "checkbox" || elm.getAttribute('type') == "radio")
			{
				elm.checked = false;
			}
			else if (elm.name.indexOf("ddl") > -1)
			{
				elm.options[0].selected = true;
			}
		}
	}
}

function ScrollIt()
{
	window.scrollTo(document.Form1.PageX.value, document.Form1.PageY.value);
}

function setcoords()
{
	var myPageX;
	var myPageY;
	if (document.all)
	{
		myPageX = document.body.scrollLeft;
		myPageY = document.body.scrollTop;
	}
	else
	{
		myPageX = window.pageXOffset;
		myPageY = window.pageYOffset;
	}
	document.Form1.PageX.value = myPageX;
	document.Form1.PageY.value = myPageY;
}

function MaxLength(field, maxlimit)
{
	// This function sets MaxLength for the MultiLine Textbox that it is passed.
	// It is called in the asp:TextBox tag like this: onKeyDown="MaxLength(this,500);" 
	// Created by Cal Zant
	if (field.value.length >= maxlimit)
		field.value = field.value.substring(0, maxlimit-1);
}

function ConfirmDelete(Command, Title, Resource)
{
	if(confirm('Are you sure that you want to delete "' + Title + '" from your ' + Resource + '?')) 
	{
		eval(Command);
	}		
}

function RefreshPage(URL)
{
	setTimeout("window.location.replace('" + URL + "')", 750);
}

function IsDaylightSavings(thisDate)
{
	var S2005 = new Date("April 3, 2005 02:00:00");
	var E2005 = new Date("October 30, 2005 02:00:00");
	var S2006 = new Date("April 2, 2006 02:00:00");
	var E2006 = new Date("October 29, 2006 02:00:00");
	var S2007 = new Date("April 1, 2007 02:00:00");
	var E2007 = new Date("October 28, 2007 02:00:00");
	var S2008 = new Date("April 6, 2008 02:00:00");
	var E2008 = new Date("October 26, 2008 02:00:00");
	var S2009 = new Date("April 5, 2009 02:00:00");
	var E2009 = new Date("October 25, 2009 02:00:00");
	var S2010 = new Date("April 4, 2010 02:00:00");
	var E2010 = new Date("October 31, 2010 02:00:00");
	
	var IsDS = false;
	if(thisDate > S2005 && thisDate < E2005) IsDS = true;
	else if(thisDate > S2006 && thisDate < E2006) IsDS = true;
	else if(thisDate > S2007 && thisDate < E2007) IsDS = true;
	else if(thisDate > S2008 && thisDate < E2008) IsDS = true;
	else if(thisDate > S2009 && thisDate < E2009) IsDS = true;
	else if(thisDate > S2010 && thisDate < E2010) IsDS = true;
	
	return IsDS;
}

// Appends the given content to the div tag with the given DivTagID.
function AppendToDivTag(DivTagID, Content)
{
	if (document.layers)
	{
		var oLayer = document.layers[DivTagID].document;
		oLayer.open();
		oLayer.write(document.layers[DivTagID].value + Content);
		oLayer.close();
	}
	else if(parseInt(navigator.appVersion)>=5 && navigator.appName == "Netscape")
		document.getElementById(DivTagID).innerHTML = document.getElementById(DivTagID).innerHTML + Content;
	else if (document.all)
		document.all[DivTagID].innerHTML = document.all[DivTagID].innerHTML + Content;
}

// Write the given content to the div tag with the given DivTagID, and replace any content that is already there.
function WriteToDivTag(DivTagID, Content)
{
	if (document.layers)
	{
		var oLayer = document.layers[DivTagID].document;
		oLayer.open();
		oLayer.write(Content);
		oLayer.close();
	}
	else if(parseInt(navigator.appVersion)>=5 && navigator.appName == "Netscape")
		document.getElementById(DivTagID).innerHTML = Content;
	else if (document.all)
		document.all[DivTagID].innerHTML = Content;
}

function GetDivTagContents(DivTagID)
{
	if (document.layers)
		return oLayer = document.layers[DivTagID].value;
	else if(parseInt(navigator.appVersion)>=5 && navigator.appName == "Netscape")
		return document.getElementById(DivTagID).innerHTML;
	else if (document.all)
		return document.all[DivTagID].innerHTML;
}

function Replace(string, OldValue, NewValue)
{
	// Replaces OldValue with NewValue in string
	var strLength = string.length;
	var OldLength = OldValue.length;
	if ((strLength == 0) || (OldLength == 0)) return string;

	var i = string.indexOf(OldValue);
	if ((!i) && (OldValue != string.substring(0,OldLength))) return string;
	if (i == -1) return string;

	var NewString = string.substring(0,i) + NewValue;

	if (i+OldLength < strLength)
		NewString += Replace(string.substring(i+OldLength,strLength),OldValue,NewValue);

	return NewString;
}


// This function looks for form element who's ClientID contains the given ElementID.  If a
// ContentPlaceHolderID parameter is provided it will also require that the ClientID also contain that value
// as well.  It returns the first match.  NOTE: It is really designed for use with MasterPages in ASP.NET 2.0
// ... but will work with any version or site.
// Example: GetElement("btnSubmit");
// It would try to find an element who's ClientID contained the text "btnSubmit". Any of the following
// ClientIDs would result in a match: "btnSubmit", "ctl00_Header_btnSubmit", "ctl00_MainContent_btnSubmit",
// "k3hf75btnSubmitjfh493", , "btnSubmitPage".
// Example: GetElement("btnSubmit", "MainContent");
// It would try to find an element who's ClientID contained the text "btnSubmit", and "MainContent".  An
// element with the ClientID of "ctl00_MainContent_btnSubmit" would result in a match, but
// "ctl00_Header_btnSubmit" would not.
function GetElement(ElementID, ContentPlaceHolderID)
{
    var Element;
    if(ContentPlaceHolderID == null)
    {
        // Try to find the element the old fashioned way ... before master pages put a prefix on the id.
        Element = OldSchoolSearch(ElementID);
        if(Element != null)
            return Element;
    }
    
    var FormCount = document.forms.length;
    for(FormIndex = 0; FormIndex < FormCount; FormIndex++)
    {
	    var ElementCount = document.forms[FormIndex].elements.length;
	    for(ElementIndex = 0; ElementIndex < ElementCount; ElementIndex++)
	    {
	        Element = document.forms[FormIndex].elements[ElementIndex];
    		
            if(ContentPlaceHolderID == null)
            {
		        if(Element.id.indexOf(ElementID) >= 0)
			        return Element;
	        }
	        else
	        {
		        if(Element.id.indexOf(ElementID) >= 0 && ElementName.indexOf(ContentPlaceHolderID) > 0)
			        return Element;
	        }
	    }
    }
    
	return null;
}

function OldSchoolSearch(ElementID)
{
    // Tries to find the element the old fashioned way ... before master pages put a prefix on the id.
    var Element;
    
	if (document.layers)
	{
		Element = document.layers[ElementID]
	}
	else if(parseInt(navigator.appVersion)>=5 && navigator.appName == "Netscape")
		Element = document.getElementById(ElementID);
	else if (document.all)
		Element = document.all[ElementID];
		
    return Element;    
}

function AdvancePhoneFields(CurrentField, NextField)
{
    if (CurrentField.value.length == 3)
	{
		NextField.focus()
	}
}
