var buttonMouseoverBackgroundColour = '#6699FF';
var buttonMouseoutBackgroundColour = '#003366';
var linkMouseoverTextColour = '#6699FF';
var linkMouseoutTextColour = 'blue';

function setBackgroundColour(control, colour)
{
	if(control!=null && control.style)
	{
		control.style.background = colour;
	}
}

function setTextColour(control, colour)
{
	if(control!=null && control.style)
	{
		control.style.color = colour;
	}
}

function textUnderline(control, underline)
{
	if(control!=null && control.style)
	{
		control.style.textDecorationUnderline = underline;
		/* test to try & get working in non-ie browsers
		if(underline==true)
		{
			control.style.textDecoration = 'underline';
		}
		else
		{
			control.style.textDecoration = 'none';
		}*/
	}
}

function buttonMouseover(control)
{
	setBackgroundColour(control, buttonMouseoverBackgroundColour);
}

function buttonMouseout(control)
{
	setBackgroundColour(control, buttonMouseoutBackgroundColour);
}

function linkMouseover(control)
{
	setTextColour(control, linkMouseoverTextColour);
	textUnderline(control, true);
}

function linkMouseout(control)
{
	setTextColour(control, linkMouseoutTextColour);
	textUnderline(control, false);
}

function toggleDisplay(elementName)
{
	var elem = document.getElementById(elementName);
	if(elem!=null && elem.style && elem.style.display)
	{
		if (elem.style.display=="inline")
		{
			elem.style.display="none";
		}
		else
		{
			elem.style.display="inline";
		}
	}			
}

function showControl(controlName)
{
	var elem = document.getElementById(controlName);
	if(elem!=null && elem.style)
	{
		elem.style.display="inline";
	}
}

function hideControl(controlName)
{
	var elem = document.getElementById(controlName);
	if(elem!=null && elem.style)
	{
		elem.style.display="none";
	}
}

function isExplorer()
{
	return (navigator.appName.indexOf("Microsoft") != -1) || (navigator.appName.indexOf("MSIE") != -1);
}

//function resizeButton(buttonName, fractionOfOriginal)
//{
//	//function to resize buttons in IE due to excessive padding
//	if(isExplorer())
//	{
//		var btn = document.getElementById(buttonName);
//		//only set width if it hasn't been set before
//		if(btn!=null && btn.style && btn.style.width=='')
//		{
//			//sets width as fraction of original, around 0.7-0.8 usually
//			btn.style.width = parseInt(btn.offsetWidth * fractionOfOriginal, 10);
//		}
//	}
//}

function resizeButton(buttonName)
{
	//function to resize buttons in IE due to excessive padding
	if(isExplorer())
	{
		var btn = document.getElementById(buttonName);
		//only set width if it hasn't been set before
		if(btn!=null && btn.style && btn.style.width=='')
		{
			//trial & error has given the below calculation
			var fractionOfOriginal = 0.85 - (0.000575 * btn.offsetWidth);
			if(fractionOfOriginal<0.675){fractionOfOriginal=0.675;}
			//sets width as fraction of original
			btn.style.width = parseInt(btn.offsetWidth * fractionOfOriginal, 10);
		}
	}
}

//function customPostBack(eventTarget, eventArgument) {
//	//copy of microsoft postback function
//	var theform;
//	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
//		theform = document.__aspnetForm;
//	}
//	else {
//		theform = document.forms["__aspnetForm"];
//	}
//	theform.__EVENTTARGET.value = eventTarget;
//	theform.__EVENTARGUMENT.value = eventArgument;
//	theform.submit();
//}

function customPostBackSubmitButton(eventTarget) {
	var button = document.getElementById(eventTarget);
	if(button!=null)
	{
		button.click();
	}
}

function windowOpener(url, height, width)
{
	return window.open(url, 'Win2UEPopup', config='height=' + height + ',width=' + width + ',toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');
}

function returnConfirm(text)
{
	if(confirm(text)==true)
	{
		return true;
	}
	else
	{
		event.cancelBubble = true;
		event.returnValue = false;
		return false;
	}
}

function keypressHandler(theKeyCode, theForm, theButton)
{
	if (event.keyCode==theKeyCode)
	{
		if(isExplorer()) window.external.AutoCompleteSaveForm(document.getElementById(theForm));
		if(document.getElementById(theButton)) 
		{
			event.returnValue = false;
			document.getElementById(theButton).click();
		}
	}
}

function enterHandler(theForm, theButton)
{
	keypressHandler(13, theForm, theButton);
}

//Function that only allows key presses for keyCodes 48-57 (0-9)
function onlyAllowIntegers()
{
	if (event.keyCode < 48 || event.keyCode > 57) event.returnValue = false;
}

//Function that only allows key presses for keyCodes 48-57 (0-9), 44 (,) and 46 (.)
function onlyAllowNumbers()
{
	if ((event.keyCode < 48 || event.keyCode > 57) && (event.keyCode != 46) && (event.keyCode != 44)) event.returnValue = false;
}

//only allow 0-9 and '/'
function onlyAllowNumbersForDate()
{
	if (event.keyCode < 47 || event.keyCode > 57) event.returnValue = false;
}

//go back to page position after postback
function loadAndGoToPosition(bookmarkID)
{
	var aspForm = document.getElementById('__aspnetForm');
	var bookmark = document.getElementById(bookmarkID);
	if(aspForm!=null && bookmark!=null){	
		//for asp.net postbacks	
		aspForm.doSubmit = aspForm.submit;
		aspForm.submit = function(){			
			aspForm.onsubmit();
			aspForm.doSubmit();			
		}
		
		//gets called automatically by submit buttons
		aspForm.onsubmit = function(){			
			//save the page position
			if(document.body.scrollTop == 0)
			{bookmark.value = document.documentElement.scrollTop;}
			else
			{bookmark.value = document.body.scrollTop;}			
		}
		
		//scroll to the saved page position
		scrollTo(0,parseInt(bookmark.value, 10));		
	}
}


String.prototype.trim = function(){return this.replace(/^\s*/, "").replace(/\s*$/, "");}
String.prototype.padL = function (nLength, sChar){
	var sreturn = this;
	while (sreturn.length < nLength){
		sreturn = String(sChar) + sreturn;
	}
	return sreturn;
 }

function dateOnkeydown() {
  if (window.event.srcElement.readOnly) return;
  var key_code = window.event.keyCode;
  var oElement = window.event.srcElement;
  if (window.event.shiftKey && String.fromCharCode(key_code) == "T") {
        var d = new Date();
       oElement.value = String(d.getMonth() + 1).padL(2, "0") + "/" +
                         String(d.getDate()).padL(2, "0") + "/" +
                        d.getFullYear();
       window.event.returnValue = 0;
    }
    if (!window.event.shiftKey && !window.event.ctrlKey && !window.event.altKey) {
        if ((key_code > 47 && key_code < 58) ||
          (key_code > 95 && key_code < 106)) {
            if (key_code > 95) key_code -= (95-47);
           oElement.value =
               oElement.value.replace(/[mdy]/, String.fromCharCode(key_code));
        }
        if (key_code == 8) {
            if (!oElement.value.match(/^[mdy0-9]{2}\/[mdy0-9]{2}\/[mdy0-9]{4}$/))
               oElement.value = "mm/dd/yyyy";
           oElement.value = oElement.value.replace(/([mdy\/]*)[0-9]([mdy\/]*)$/,
                function ($0, $1, $2) {
                    var idx = oElement.value.search(/([mdy\/]*)[0-9]([mdy\/]*)$/);
                    if (idx >= 5) {
                        return $1 + "y" + $2;
                    } else if (idx >= 2) {
                        return $1 + "d" + $2;
                    } else {
                       return $1 + "m" + $2;
                    }
                } );
           window.event.returnValue = 0;
        }
    }
    if (key_code != 9) {
       event.returnValue = false;
    }

 }
 
 function getElementById2(id){ 
    var obj = null; 
    if(document.getElementById){ 
        obj = document.getElementById(id); 
    }else if(document.all){ 
        obj = document.all[id]; 
    } 
    return obj; 
} 
function getElement(partOfId)
{
    for(i=0; i<document.forms[0].elements.length; i++){
        var elem = document.forms[0].elements[i];
        if(elem.id.indexOf(partOfId)>=0)return(elem);
    }
    return null;
}
function getElementByType(partOfId, type)
{
    var elems = document.getElementsByTagName(type);
    for(i=0; i<elems.length; i++){
        var elem = elems[i];
        if(elem.id.indexOf(partOfId)>=0)return(elem);
    }
    return null;
}
