function evt_attachEvent(theElement, eventName, method)
{
	if(theElement.attachEvent)
		theElement.attachEvent("on"+eventName, method);
	else
		theElement.addEventListener(eventName, method, false);
}

function evt_getEventSource(event)
{
	if(event == null)
		return null;

	if(event.srcElement)
		return event.srcElement;

	var node = event.target;

	while(node && node.nodeType != node.ELEMENT_NODE)
		node = node.parentNode;

	return node;
}

function val_getPixels(pixelValue)
{
	if(pixelValue.split)
		return Number(pixelValue.split("px")[0]);

	return pixelValue;
}

function elt_getNextElement(anElement, tagName)
{
	for(anElement = anElement.nextSibling; anElement != null; anElement = anElement.nextSibling)
	{
		if(anElement.nodeType == 1)
		{
			if( tagName )
			{
				if( anElement.tagName == tagName )
				{
					return anElement;
				}
			}
			else
			{
				return anElement;
			}
		}
	}

	return null;
}

function elt_getChildren(anElement)
{
	var children = [];
	

	for(var aChild = anElement.firstChild; aChild; aChild = aChild.nextSibling)
	{
		if(aChild.nodeType == 1)
			children[children.length] = aChild;
	} 

	return children;	
}

function anc_getAncestorWithClass(theElement, className, limit)
{
	for(var ancestor = theElement; ancestor; ancestor = ancestor.parentNode)
	{
		if(limit && ancestor.tagName == limit)
			return null;

		if(ancestor.getAttribute == null)
			return null;

		if(cls_hasClassName(ancestor, className))
			return ancestor;		
	}

	return null;
}


function adjustTime(thisForm, formatToday, todayRawTime)
{
	if(thisForm.Date.value == formatToday)
	{
		var Interval = thisForm.Interval.value;
	
		if(todayRawTime > 86400) // close to 12am
		{
			if(thisForm.Date.length > 0)
			{
				thisForm.Date.remove(0);
			}
			
			//adjust Meridiem
			if(thisForm.MeridiemDropDown.length < 2 && thisForm.MeridiemDropDown.length > 0)
			{
				addAM(thisForm.MeridiemDropDown);
			} 
			thisForm.MeridiemDropDown.selectedIndex = 0;
			
			//adjust hour and min
			if(thisForm.HourDropDown.length < 12)
			{
				var hour = 12;
				var length =  thisForm.HourDropDown.length;
				for(var k = 0; k < length ; k++)
						thisForm.HourDropDown.remove(0);
				
				for(var i = 0; i <= 11; i++)
				{
					var lm=document.createElement('option');
					lm.text= String(i+1);
					lm.value=lm.text + '00' ;	
					addOpt(thisForm.HourDropDown, lm);	
				}
			}
			thisForm.HourDropDown.selectedIndex = 11;
			
			var mincount = parseInt(60 / Interval);
			if(thisForm.MinuteDropDown.length < mincount && thisForm.MinuteDropDown.length > 0)
			{	
				for(var k = 0; k < length ; k++)
						thisForm.MinuteDropDown.remove(0);
				
				for(var i = 0 ; i < 60 ; )
				{
					var lm=document.createElement('option');
					lm.text= String(i) ;
					if(lm.text.length == 1)
					   lm.text = '0' + lm.text;
					lm.value=lm.text;	
					
					addOpt(thisForm.MinuteDropDown, lm);	
					i = parseInt(i) + parseInt(Interval);	
				}	
			}
			
			if(document.getElementById('SelMin').value == '');
				thisForm.MinuteDropDown.selectedIndex = 0;	
			
			return;		
		}
		else if(todayRawTime > 43200)  // in afternoon 
		{
			if(thisForm.MeridiemDropDown.length == 2)
			{
				for(i=0; i<thisForm.MeridiemDropDown.length; i++)
				{
					if(thisForm.MeridiemDropDown.options[i].value=='a')
					thisForm.MeridiemDropDown.remove(i);
				}
			}
		}
		else if(thisForm.SelMeridiem.value == '')
			thisForm.MeridiemDropDown.selectedIndex = 0;
	}
	else
	{
		if(thisForm.MeridiemDropDown.length < 2 && thisForm.MeridiemDropDown.length > 0)
		{
			addAM(thisForm.MeridiemDropDown);
		} 
	}
	adjustHour(thisForm, formatToday, todayRawTime);
	adjustMin(thisForm, formatToday, todayRawTime);
}   

function addOpt(ctrlId, lm)
{
	if(navigator.appName=="Microsoft Internet Explorer")
		ctrlId.add(lm);
	else
	{
		ctrlId.add(lm,null);
	}
}

function addAM(ctrlId)
{
	var lm=document.createElement('option');
	lm.text='AM';
	lm.value='a';
	
	if(navigator.appName=="Microsoft Internet Explorer")
		ctrlId.add(lm, 0);
	else
	{
		ctrlId.add(lm,null, 0);
	}
}

function adjustHour(thisForm, formatToday, todayRawTime)
{
	 var vtodayRawTime = todayRawTime;
	 var hour = Math.floor(vtodayRawTime/3600) % 12;
	
	if(thisForm.Date.value == formatToday && thisForm.MeridiemDropDown.selectedIndex == 0 && hour != 0)
	{
			for(var k = 0; k < 12 ; k++)
				thisForm.HourDropDown.remove(0);
			
			for(var s = hour; s < 12 ; s++)
			{
				var Im;
				lm=document.createElement('option');
				lm.text= String(s);
				lm.value=String(s) + '00' ;
				
				addOpt(thisForm.HourDropDown, lm);
			}
			
			if( thisForm.MeridiemDropDown[thisForm.MeridiemDropDown.selectedIndex].value == 'p' && hour == 12)
			{	
				lm=document.createElement('option');
				lm.text= '12';
				lm.value='1200' ;
				
				addOpt(thisForm.HourDropDown, lm);
				thisForm.HourDropDown.selectedIndex = thisForm.HourDropDown.length - 1;
			}	
			else
				thisForm.HourDropDown.selectedIndex = 0;
	}
	else if(thisForm.HourDropDown.length < 12)
	{
		
		var length =  thisForm.HourDropDown.length;
		for(var k = 0; k < length ; k++)
				thisForm.HourDropDown.remove(0);
		
		for(var i = 1; i <= 12; i++)
		{	
			var lm=document.createElement('option');
			lm.text= String(i);
			lm.value=String(i) + '00' ;	
			if(i == hour)
				lm.selected='selected'; 

			addOpt(thisForm.HourDropDown, lm);
		}
	}
	else if(hour==0)
	{
		hour = 12;
		thisForm.HourDropDown.selectedIndex = hour - 1;
	}
}


function adjustMin(thisForm, formatToday, todayRawTime)
{
	var length =  thisForm.MinuteDropDown.length;
	var Interval = thisForm.Interval.value;
	var hour = Math.floor(todayRawTime/3600) % 12;
	if(hour==0)
		hour = 12;
	
	var min = Math.floor((todayRawTime- Math.floor(todayRawTime/3600)*3600)/60);
	var index = parseInt(min/Interval);	
	Qmin = index * Interval ;
	var mincount = parseInt(60 / Interval);
	
	//if(thisForm.Date.value == formatToday && thisForm.MeridiemDropDown.selectedIndex == 0 && thisForm.HourDropDown.selectedIndex == 0)
	if(thisForm.Date.value == formatToday && thisForm.MeridiemDropDown.selectedIndex == 0 && (thisForm.HourDropDown.options[thisForm.HourDropDown.selectedIndex].text == hour ))
	{
			if(thisForm.MinuteDropDown.length == 0)
			{
				for(var i = 0 ; i < 60 ; )
				{
					var lm=document.createElement('option');
					lm.text= String(i) ;
					if(lm.text.length == 1)
					   lm.text = '0' + lm.text;
					lm.value=lm.text;

					if(i == Qmin)
					{
						lm.selected='selected';
					}
					addOpt(thisForm.HourDropDown, lm);
				}
			}
			
			if(thisForm.MinuteDropDown.length == mincount)
			{
				var minThreshold = Math.floor(min / thisForm.Interval.value);	
				for( i = 0; i < minThreshold ; i++)
				{
					thisForm.MinuteDropDown.remove(0);	
				}	
				thisForm.MinuteDropDown.selectedIndex = 0;
			}
	}
	else if(length < mincount && length > 0)
	{
		
		for(var k = 0; k < length ; k++)
			thisForm.MinuteDropDown.remove(0);
			
		for(var i = 0 ; i < 60 ; )
		{
			var lm=document.createElement('option');
			lm.text= String(i) ;
			if(lm.text.length == 1)
			   lm.text = '0' + lm.text;
			lm.value=lm.text;	
			if(i == Qmin)
				lm.selected='selected';
			
			addOpt(thisForm.MinuteDropDown, lm);
				
			i = parseInt(i) + parseInt(Interval);	
		}
	}
//	else
//		thisForm.MinuteDropDown.selectedIndex = index;
}

