var imagePath = '/Images/';

function ConfirmMessage(Message) 
{
    return confirm(Message);
}

function EnableDisableControl(Control, Enable) {
    //alert(Enable);
    Control.disabled = !Enable;
    return true;
}

function IsMaxLength(TextBox, MaxLength)
{
    return (TextBox.value.length < MaxLength);
    /*
    if (TextBox.value.length < MaxLength)
    {
        return true;
    }
    else
    {
        alert("You have exceeded the maximum number of characters allowed for this field.");
        return false;
    }
    */
}

function UserAutoOpen(source, eventArgs)
{
    var StatusWindow = null;
    
    StatusWindow = window.open("/Admin/Accounts/User.aspx?UserId=" + eventArgs.get_value(), 'status', 'width=900,height=900,scrollbars=yes,toolbar=no,status=yes,resizable=yes', true);
    StatusWindow.focus();
    return false;
}

function OrderAutoOpen(source, eventArgs) {
    var StatusWindow = null;
    StatusWindow = window.open("/OrderResults.aspx?OrderId=" + eventArgs.get_value(), 'status', 'width=900,height=900,scrollbars=yes,toolbar=no,status=yes,resizable=yes', true);
    StatusWindow.focus();
    return false;
}

function OrderAutoOpen(OrderId) {
    var StatusWindow = null;
    StatusWindow = window.open("/OrderResults.aspx?OrderId=" + OrderId, 'status', 'width=900,height=900,scrollbars=yes,toolbar=no,status=yes,resizable=yes', true);
    StatusWindow.focus();
    return false;
}

function ShowHideList(icon, id)
{
	//var plusImg = new Image();	
	//plusImg.src = '/Images/plus_blue.gif';
	//var minusImg = new Image();	
	//minusImg.src = '/Images/minus_blue.gif';

	var list = getElement(id);
	//alert(list.style);
	if(list.style.display == 'none')
	{
	    //alert("Expand");
		list.style.display = 'table-row';
		icon.src = icon.src.replace('Plus', 'Minus');
		//minusImg.src;
		icon.alt = 'Collapse List';
	}
	else
	{
	    //alert("Collapse");
	    list.style.display = 'none';
		icon.src = icon.src.replace('Minus', 'Plus');
		//plusImg.src;
		icon.alt = 'Expand List';
	}
}

function ShowHideDiv(id)
{
    var list = getElement(id);
    
    if (list.style.display == 'none')
        list.style.display = 'block';
    else
        list.style.display = 'none';
}

function ShowHideContainer(Icon, Id)
{	
	var Container = getElement(Id);
	if(Container.style.display == 'none')
	{	
	    //alert(Container.id);
		Container.style.display = 'block';
		Icon.src = Icon.src.replace('Expand', 'Collapse');
		Icon.alt = 'Click To Collapse';
	}
	else
	{
	    //alert('Collapse');
		Container.style.display = 'none';
		Icon.src = Icon.src.replace('Collapse', 'Expand');
		Icon.alt = 'Click To Expand';
	}
}

function ShowHidePanel(Icon, Id) {
    var Container = getElement(Id);
    if (Container.style.display == 'none') {
        //alert(Container.id);
        Container.style.display = 'block';
        Icon.src = Icon.src.replace('Plus', 'Minus');
        Icon.alt = 'Click To Collapse';
    }
    else {
        //alert('Collapse');
        Container.style.display = 'none';
        Icon.src = Icon.src.replace('Minus', 'Plus');
        Icon.alt = 'Click To Expand';
    }
}

function loadTutorial()
{
	//if (confirm("Would you like to see a brief tutorial on how the Transnetyx PCR system works?"))
	//{
		var width = 1024;
		var height = 768;
		var winLeft = (screen.width - width) / 2;
		var winTop = (screen.height - height) / 2;
		window.open("/Tutorials/PlacingAnOrder.html", "Tutorial", "alwaysRaised=1,top=" + winTop + ",left=" + winLeft + ",height=" + height + ",width=" + width + ",menubar=0,resizable=0,status=0,scrollbars=0,toolbar=0");
		return false;
		//alert("Show Tutorial");
	//}
}

function AutoPrintWindowOnLoad()
{
	window.resizeTo(600,800);
	window.moveTo(0,0);
	window.print();
	window.moveTo(100,100);
	
	window.close();	
}

function OpenOrganization(obj)
{
	var StatusWindow = null;
	StatusWindow = window.open(obj.href, 'status', 'width=800,height=800,scrollbars=yes,toolbar=no,status=yes,resizable=yes', true);
	StatusWindow.focus();
	return false;
}

function OpenSupplyRequest(obj)
{
	var StatusWindow = null;
	StatusWindow = window.open(obj.href, 'status', 'width=700,height=800,scrollbars=yes,toolbar=no,status=yes,resizable=yes', true);
	StatusWindow.focus();
	return false;
}

function OpenOrderStatus(obj)
{
	var StatusWindow = null;
	StatusWindow = window.open(obj.href, 'status', 'width=400,height=300,scrollbars=yes,toolbar=no,status=yes,resizable=yes', true);
	StatusWindow.focus();
	return false;
}

function OpenMessage(obj) 
{
	var MessageWindow = window.open(obj.href, 'Message', 'width=800,height=800,scrollbars=yes,status=yes,resizable=yes');
	MessageWindow.focus();
	return false;
}

function OpenOrder(obj) 
{
	var OrderWindow = window.open(obj.href, 'Order', 'width=800,height=800,scrollbars=yes,status=yes,resizable=yes');
	OrderWindow.focus();
	return false;
}

function OpenPlate(obj) 
{
	var PlateWindow = window.open(obj.href, 'Plate', 'width=800,height=800,scrollbars=yes,status=yes,resizable=yes');
	PlateWindow.focus();
	return false;
}

function OpenUser(obj)
{
	var userWin = window.open(obj.href, 'user', 'width=1075,height=900,scrollbars=yes,status=yes,resizable=yes,left=5,top=5;');
	userWin.focus();
	return false;
}

function OpenProbe(obj) 
{
	var ProbeWindow = window.open(obj.href, 'modify', 'width=1000,height=800,scrollbars=yes,status=yes,resizable=yes');
	ProbeWindow.focus();
	return false;
}

function OpenSample(obj) {
    var sampleWin = null;

    if (sampleWin == null || sampleWin.closed)
        sampleWin = window.open(obj.href, 'OrderSample', 'width=490, height=600, status=yes, resizable=yes, scrollbars=yes');

    sampleWin.focus();

    return false;
}

function OpenSamples(obj) {
    var sampleWin = null;

    if (sampleWin == null || sampleWin.closed)
        sampleWin = window.open(obj.href, 'OrderSample', 'width=650, height=800, status=yes, resizable=yes, scrollbars=yes');

    sampleWin.focus();

    return false;
}

function OpenWindow(obj) 
{
	var NewWindow = window.open(obj.href, 'NewWindow', 'width=800,height=800,scrollbars=yes,status=yes,resizable=yes');
	NewWindow.focus();
	return false;
}
			
function ImpersonateUserFromPopup(obj)
{
	window.opener.location = obj;
	window.close();
}

function OpenInParent(obj)
{
	//var parent = window.opener();
	
	//parent.location(obj.href);
	
	self.opener.location = obj.href;
	
	return false;
}

			
function RefreshParentWindow()
{
	self.opener.location = self.opener.location;
	
	return false;
}


function getElement(id) {
	if(document.all) {
		return document.all[id];
	} else if(document.getElementById) {
		return document.getElementById(id);
	} else if(document.layers) {
		return document.layers[id];
	} else {
		alert('This browser doesn\'t support "document.all", "document.layers", or "document.getElementById". (Fix this).');
		return null;
	}
}

function changeText(obj, text) {
	if(obj) {
		if(obj.innerHTML) {
			obj.innerHTML = text;
		} else if(obj.childNodes) {
			var newText = document.createTextNode(text);
			for(var i = 0; i < obj.childNodes.length; i++) {
				if(obj.childNodes[i].nodeType == Node.TEXT) {
					obj.replaceChild(newText, obj.childNodes[i]);
					return;
				}
			}
			obj.appendChild(newText);
		}
	}
}

function getText(obj) {
	if(obj) {
		if(obj.innerText) {
			return obj.innerText;
		} else if(obj.childNodes) {
			for(var i = 0; i < obj.childNodes.length; i++) {
				if(obj.childNodes[i].nodeType == Node.TEXT) {
					return obj.childNodes[i].value;
				}
			}
		}
	}
	return '';
}

function Point(x, y) {
	this.x = x;
	this.y = y;
}

function getPosition(el) {
	var r = new Point(el.offsetLeft, el.offsetTop);
	
	if(el.offsetParent) {
		var tmp = getPosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}

	return r;
}

function numberPressed(e) {
	var key;
	if(window.event) {
		key = window.event.keyCode;
	} else if(e) {
		key = e.which;
	} else {
		key = 0;
	}

	if(key < 47 || key > 58) {
		return false;
	} else {
		return true;
	}
}

function showHideHelp(idNumber) {
	var img = getElement('img_' + idNumber);
	var obj = getElement('item_' + idNumber);
	
	var root = getImageRoot(img.src);
	
	if(obj.style.display == 'none') {
		obj.style.display = 'block';
		img.src = imagePath + root + '_down.gif';
	} else {
		obj.style.display = 'none';
		img.src = imagePath + root + '_right.gif';
	}
	
	return false;
}

function showHideInstructions(idNumber) {
	var img = getElement('img_' + idNumber);
	var obj = getElement('inst_' + idNumber);
	
	var root = getImageRoot(img.src);
	
	if(obj.style.display == 'none') {
		obj.style.display = 'block';
		img.src = imagePath + root + '_down.gif';
	} else {
		obj.style.display = 'none';
		img.src = imagePath + root + '_right.gif';
	}
	
	return false;
}

function confirmMessage(msg) {
	return confirm(msg);
}

function preloadButtons() {
	for(var i = 0; i < arguments.length; i++) {
		var up = new Image();
		var over = new Image();
		var down = new Image();
		
		up.src = imagePath + arguments[i] + '_up.gif';
		over.src = imagePath + arguments[i] + '_over.gif';
		down.src = imagePath + arguments[i] + '_down.gif';
	}
}

function getImageRoot(str) {
	var slash = str.lastIndexOf('/') + 1;
	return str.substring(slash, str.lastIndexOf('_')); 
}

function getImageExtension(str) {
    var extension = str.lastIndexOf('.');
    return str.substring(extension, extension + 4);
}

function btn_over(obj) {
	obj.src = imagePath + 'Buttons/' + getImageRoot(obj.src) + '_Over' + getImageExtension(obj.src);
}

function btn_out(obj) {
    obj.src = imagePath + 'Buttons/' + getImageRoot(obj.src) + '_Up' + getImageExtension(obj.src);
}

function btn_down(obj) {
    obj.src = imagePath + 'Buttons/' + getImageRoot(obj.src) + '_Down' + getImageExtension(obj.src);
}

function GetImageState(Path) {
    var Dash = Path.lastIndexOf('-');
    var Extension = Path.lastIndexOf('.');
    return Path.substring(Dash, Extension);

}

function ButtonOver(Button) {

    var ImageState = GetImageState(Button.src);
    Button.src = Button.src.replace(ImageState, '-Over');
}

function ButtonOut(Button) {
    var ImageState = GetImageState(Button.src);
    Button.src = Button.src.replace(ImageState, '-Up');
}

function ButtonDown(Button) {
    var ImageState = GetImageState(Button.src);
    Button.src = Button.src.replace(ImageState, '-Down');
}

function ButtonOn(Button) {
    var ImageState = GetImageState(Button.src);
    Button.src = Button.src.replace(ImageState, '-On');
}

function ButtonOff(Button) {
    var ImageState = GetImageState(Button.src);
    Button.src = Button.src.replace(ImageState, '-Off');
}
