var winPopup;
window.onfocus = ClosePopup;

// for printer friendly and email this page
var newWindow;
// debugger;

var _util = new function()
{
	var m = document.location.pathname.match(/(.*\/Events)\/(\([0-9a-z]*\))*/i)
	var m1 = (m && m[1] != null ? m[1]: "");
	var m2 = (m && m[2] != null ? m[2]: "");
	
	if (m2 > "")
		m2 = "/" + m2;
	this.appRoot = m1 + m2;
}

function utility_GetAppRoot()
{
	return _util.appRoot;
}

function ValidateNumber(aFld)
{
	var s = aFld.value;	
	if (!validateNumeric(s) && s.length > 0)
	{		
		alert("Please enter a number");
		return false;
	}	
	return true;
}

function  validateNumeric( strValue ) {
/******************************************************************************
DESCRIPTION: Validates that a string contains only valid numbers.

PARAMETERS:
   strValue - String to be tested for validity
   
RETURNS:
   True if valid, otherwise false.
******************************************************************************/
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 // ^[-+]?\d+(\.\d+)?
  //check for numeric characters 
  return objRegExp.test(strValue);
}

// for popup calendar
function GetDate(CtrlName) 
 {
  winPopup = window.open('/Events/Popup/Calendar.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName, 'winPopup', 'width=300,height=300,top=300,left=500,toolbars=no,scrollbars=no,status=no,resizable=no');
 }
 
    //popup airports
 function GetAirport(Ctrl_APName, Ctrl_APCode)
 {
  winPopup = window.open('/Events/Popup/Airports.aspx?FormName=' + document.forms[0].name + '&CtrAPName=' + Ctrl_APName + '&CtrAPCode=' + Ctrl_APCode ,  'winPopup', 'width=750,height=800,top=300,left=500,toolbars=no,scrollbars=yes,status=no,resizable=no');
 }
 
// for popup email this page
function PopupEmailThisPage(PageID)
 {
  winPopup = window.open('/Events/Popup/EmailPage.aspx?page=' + PageID, "winPopup", "width=650,height=400,top=300,left=500,toolbars=no,scrollbars=no,status=no,resizable=no");
 } 

function CheckWindow()
{
	if (winPopup != null)
	{
		winPopup.close()
	}
}

function ClosePopup()
{
	 try
	  {
	    if (winPopup && typeof(winPopup.closed) != "'unknown'" && !winPopup.closed)
	    {
		  winPopup.close();
	    }
	    if (newWindow && typeof(newWindow.closed) != "'unknown'" && !newWindow.closed)
	    {
		  newWindow.close();
	    }
	  }
	 catch(e)
	  {
	  }
	
}

// ============================= < CSS Class helper functions ===============================
	// generic functions for changing style rules ***************************************
	function GetStyleSheetClass(cssClassName)
	{
		var ds = document.styleSheets;
		
		for(i = 0; ds.length > i; i++)
		{
			dsi = ds[i];		
			if(dsi.rules != undefined)
				dsir = dsi.rules; //IE
				
			else
				dsir = dsi.cssRules; //Mozilla, Firefox

			for(j = 0; dsir.length > j; j++)
			{
				var selectorText = dsir[j].selectorText.toLowerCase();
				//document.writeln(selectorText + "\\");
				if(selectorText.toLowerCase() == cssClassName.toLowerCase())
				{
					//for (si = 0;si < dsir[j].style.length; si++) {document.writeln(dsir[j].style[si] + "<br/>");}
					return dsir[j];
				}
			}
		}
		return null;
	}
	
	function ChangeStyleSheetRule(cssClassName,theRule,changeTo)
	{
		var cssClass = GetStyleSheetClass(cssClassName);
		
		alert(cssClass);
		
		if (cssClass!= null)
		{
			cssClass.style[theRule] = changeTo;
		}
	}
	
	function GetStyleSheetRuleValue(cssClassName,theRule)
	{
		var cssClass = GetStyleSheetClass(cssClassName);
		if (cssClass!= null)
		{
			return cssClass.style[theRule] ;
		}
		return "";
	}
	
	function CopyStyleSheetRule (fromClass, toClass, theRule)
	{
		var fromValue = GetStyleSheetRuleValue(fromClass, theRule);
		if (fromValue > "")
		{
			ChangeStyleSheetRule(toClass, theRule, fromValue);
		}
		
	}
	
	// specific function for changing style rules for html button ***************************************
	function ChangeHtmlButtonColorTheme(cssClassName,theRule,changeTo)
	{
		var cssTabBgrd1, ButtonReg;
		cssTabBgrd1 = GetStyleSheetClass("TD.TabBgrd1");
		ButtonReg = GetStyleSheetClass("SPAN.ButtonReg");
		
		if (cssTabBgrd1 == null || ButtonReg == null) 
			return;
		ButtonReg.style["color"] = cssTabBgrd1.style["color"];
		ButtonReg.style["backgroundColor"] = cssTabBgrd1.style["backgroundColor"];
		
	}
	// use generic functions to copy TabBgrd1 color theme to Calendar Header class
	function ChangeCalendarHeaderColorTheme(cssClassName,theRule,changeTo)
	{	
		var cssTabBgrd1, CalendarHeader;		
		cssTabBgrd1 = GetStyleSheetClass("TD.TabBgrd1");
		CalendarHeader = GetStyleSheetClass("Table.CalendarHeader");
		
		if (cssTabBgrd1 == null || CalendarHeader == null) 
			return;
		CalendarHeader.style["color"] = cssTabBgrd1.style["color"];
		CalendarHeader.style["backgroundColor"] = cssTabBgrd1.style["backgroundColor"];			
	}
	
	// use generic functions to copy TabBgrd1 color theme to Calendar Next Prev text class
	function ChangeCalendarNextPrevStyle(cssClassName,theRule,changeTo)
	{	
		var cssTabBgrd1, CalendarNextPrevStyle;		
		cssTabBgrd1 = GetStyleSheetClass("TD.TabBgrd1");
		CalendarNextPrevStyle = GetStyleSheetClass("Font.CalendarNextPrevStyle");
		
		if (cssTabBgrd1 == null || CalendarNextPrevStyle == null) 
			return;
		CalendarNextPrevStyle.style["color"] = cssTabBgrd1.style["color"];
//		CalendarNextPrevStyle.style["backgroundColor"] = cssTabBgrd1.style["backgroundColor"];			
	}
	
	// use specific function to copy TabBgrd1 color theme to HTML button
	ChangeHtmlButtonColorTheme();	
	// use specific function to copy TabBgrd1 color theme to calendar header
	ChangeCalendarHeaderColorTheme();
	// use specific function to copy TabBgrd1 color theme to calendar next prev style
	ChangeCalendarNextPrevStyle();

// ============================= CSS Class helper functions > ===============================


// function to populate day drop down based on the selected month and year when using drop down lists for month, day, year
	function FillDayLookup(ddlMonth, ddlDay, ddlYear)
	{
		var SelectedMonthDays;
		var SelectedMonth = parseInt(ddlMonth.value, 10);
		if (SelectedMonth == 0)
		{return;}
		var SelectedYear = parseInt(ddlYear.value, 10);

		switch (SelectedMonth)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				SelectedMonthDays = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				SelectedMonthDays = 30;
				break;
			case 2:
				if (SelectedYear == 0)
				{SelectedMonthDays = 28;}
				else if (SelectedYear % 4 == 0)
				{SelectedMonthDays = 29;}
				else
				{SelectedMonthDays = 28;}
				break;
			default:
				SelectedMonthDays = 0;
				break;
		}

		if (SelectedMonthDays == 0)
		{return;}
		
		var CurrentMonthDays = ddlDay.options.length;
		
		if ((CurrentMonthDays) == SelectedMonthDays)
		{
			return;
		}
		else if (CurrentMonthDays <= SelectedMonthDays)
		{
			for (i=CurrentMonthDays+1; i<=SelectedMonthDays; i++)
			{
				var oDayOption = document.createElement("OPTION");
				oDayOption.value = i;
				oDayOption.text = i;
				ddlDay.options.add(oDayOption);
			}
		}
		else if (CurrentMonthDays > SelectedMonthDays)
		{
			for (i=CurrentMonthDays; i>=SelectedMonthDays; i--)
			{
				ddlDay.options.remove(i);
			}
		}
	}
// function to populate day drop down based on the selected month and year when using drop down lists for month, day, year

function Cvent_findElement(n, doc) {
  var p,i,x;  
	if(!doc) 
		doc=document; 
	/*
	if((p=n.indexOf("?"))>0 && parent.frames.length) 
	{
		doc=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	*/
	if(!(x=doc[n]) && doc.all) 
		x=doc.all[n]; 
	for (i=0;!x&&i<doc.forms.length;i++) 
	{
		x=doc.forms[i][n];
	}
	for(i=0; !x && doc.layers && i<doc.layers.length; i++)
	{
		x=Cvent_findElement(n,doc.layers[i].document);
	}
	if(!x && doc.getElementById) 
		x=doc.getElementById(n); 
	return x;
}



function ToggleDisplay(objName, sDisplay)
{
	if (!Cvent_findElement)
	{
		alert("include utility.js");
	}
	if (!sDisplay)
	{
		sDisplay = "";
	}
	var obj = Cvent_findElement(objName);
	
	if(obj != null)
	{
		if (obj.style.display == "none")
		{
			obj.style.display = sDisplay;
		}
		else
		{
			obj.style.display = "none";
		}
	}
}

//this function is used to set the 
//default focus on a page to the very first control visible and active
//this function is used to set the 
//default focus on a page to the very first control visible and active
function SetDefaultFocus()
{		
    try
    {
        // for each form
        for (f=0; f < document.forms.length; f++)
        {
        // for each element in each form
            for(i=0; i < document.forms[f].length; i++)
            {
              // if it's not a hidden element and it's not disabled
              if (document.forms[f][i].type != "hidden" && document.forms[f][i].disabled != true)
              {       
                    document.forms[f][i].focus();
                    return;
              }      
            }//eof sub-for    
        }//eof main for
    }
    catch(e)
    {
        
    }
}

function DisableEnterKey(e) {

    var key;

    if (!e) {
        e = window.event;
    }

    if (e.keyCode) {
        key = e.keyCode;
    }
    else {
        key = e.which;
    }

    return (key != 13);
}
