function PreventDuplicateSubmit() 
{ 
	var cookie_ls = document.cookie
	if (cookie_ls.indexOf(document.location) > -1) 
	{ 
		return false; 
	} 
	else 
	{ 
		var exp = new Date(); 
		exp.setTime(exp.getTime() + 1000 * 5); 
		var expDate = exp.toGMTString(); 
		document.cookie = window.location.href + " from " + document.referrer + "; path=/; expires=" + expDate; 

		return true; 
	} 
} 


function getFormElement(theName)
{
	var theForm = document.forms[0];
	for (var i=0; i<theForm.elements.length; i++) 
	{
		if (theForm.elements[i].name.indexOf("$" + theName) > 0) 
		{
			return theForm.elements[theForm.elements[i].name];
		}
	}	
	return null;
}

function getFormElementFromForm(theForm, theName)
{
	for (var i=0; i<theForm.elements.length; i++) 
	{
		if (theForm.elements[i].name.indexOf("$" + theName) > 0) 
		{
			return theForm.elements[theForm.elements[i].name];
		}
	}	
	return null;
}

function disableSF()
{
	var sfRadio = getFormElement("SuperFacilitator");
	if (sfRadio == null)
		return;
	for (var i=0; i < sfRadio.length; i++) 
	{
		if (sfRadio[i].value == "_superFacilitator_Yes")
			sfRadio[i].checked = true;
		sfRadio[i].disabled = true;
	}
}

function enableSF()
{
	var sfRadio = getFormElement("SuperFacilitator");
	if (sfRadio == null)
		return;
	for (var i=0; i < sfRadio.length; i++) 
	{
		if (sfRadio[i].value == "_superFacilitator_Yes")
			sfRadio[i].checked = true;
		sfRadio[i].disabled = false;
	}
}

function resetRadio(theName)
{
	var radio = getFormElement(theName);
	if (radio == null)
		return;
	for (var i=0; i < radio.length; i++) 
	{
		radio[i].checked = false;
		radio[i].disabled = false;
	}	
}

function getTTElementByName(tagName, elementName)
{
	var elements = document.getElementsByTagName(tagName);
	for(i = 0; i < elements.length; i++)
	{
		var element = elements(i);
		if (element.name.indexOf(elementName) == (element.name.length - elementName.length))
			return element;
	}
	alert ("did not find " + elementName); 
	return null;
}

function setCDQuantityTextBox(textBoxId, enable)
{
	var textBox = getTTElementByName("input", textBoxId);

	if (textBox == null)
	{
		Alert(textBox + "was null");
		return;
	}
	
	textBox.disabled = !enable; 
	if (textBox.disabled)
		textBox.value = "0";
	else
		textBox.value = "1";
}

function clearError(element) 
{
	element.style.backgroundColor = "";
}

function popUpDatePicker(ctl, leftPos, topPos)
{
    var windowWidth = 230;
    var windowHeight = 230;
    var fromLeft = 0;
    var fromTop = 0;
    if (leftPos == null)
    {   
        //XY position not provided, so find the position of the element
        targetElement = getFormElement(ctl);
        xyPosition = findElementPosition(targetElement);
        fromLeft = xyPosition[0];
        fromTop = xyPosition[1] + windowWidth / 2 + 40;
    }
    else
    {
        alert('using provided');
        fromLeft = leftPos;
        fromTop = topPos;
    }
    var PopupWindow=null;
    settings='width=' + windowWidth + ',height=' + windowHeight + ',location=no,directories=no, menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no, dependent=no,left=' + fromLeft + ',top=' + fromTop;
    PopupWindow=window.open('/DatePicker.aspx?Ctl=' + ctl,'DatePicker', settings);
    PopupWindow.focus();
}

function findElementPosition(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
	    do 
	    {
		    curleft += obj.offsetLeft;
		    curtop += obj.offsetTop;	
        } while (obj = obj.offsetParent);
	    return [curleft,curtop];
	}
	return null;
}