var win=null;
function NewWindow(sURL,sName,sSize,sMode)
{

	// default to large
	var w=750;
	var h=500;
	var t=20;
	var l=20;
	var mw=600;
	var mh=400;	
	var winName='';


	try
	{
 	if (parent.screenTop != null)
		{
		t=parent.screenTop+10;
		}
	if (parent.screenLeft != null)
		{
		l=parent.screenLeft+10;
		}
	}
	catch (er)
	{
	t = 20;
	l = 20;
	}

	if(sSize.toLowerCase()=='full' || sSize.toLowerCase()=='f'){w=900;h=600;t=0;l=0;mw=800;mh=600};
	if(sSize=='' || sSize.toLowerCase()=='large' || sSize.toLowerCase()=='l'){w=750;h=500;mw=750;mh=500};
	if(sSize.toLowerCase()=='medium' || sSize.toLowerCase()=='m'){w=650;h=400;mw=650;mh=400};
	if(sSize.toLowerCase()=='small' || sSize.toLowerCase()=='s'){w=400;h=200;;mw=400;mh=200};
	if(sSize.toLowerCase()=='help' || sSize.toLowerCase()=='h'){w=650;h=400;mw=650;mh=400};
	if(sSize.toLowerCase()=='print' || sSize.toLowerCase()=='p'){w=750;h=500;mw=750;mh=500};
	if(sSize.toLowerCase()=='flowchart' || sSize.toLowerCase()=='f'){w=790;h=515;mw=790;mh=515};	

	if(sSize.toLowerCase()=='help' || sSize.toLowerCase()=='h')
	{
		settings='width='+w+', height='+h+', top='+t+', left='+l+', scrollbars=yes, location=no, directories=no, status=yes, menubar=no, toolbar=yes, resizable=yes';
	}
	else if (sSize.toLowerCase()=='print' || sSize.toLowerCase()=='p')
	{
		settings='width='+w+', height='+h+', top='+t+', left='+l+', scrollbars=yes, location=no, directories=no, status=yes, menubar=yes, toolbar=no, resizable=yes';
	}
	else
	{
		settings='width='+w+', height='+h+', top='+t+', left='+l+', scrollbars=yes, location=no, directories=no, status=yes, menubar=no, toolbar=no, resizable=yes';
	}
	
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	modalsettings='width='+w+', height='+h+', top='+TopPosition+', left='+LeftPosition+', scrollbars=yes, location=no, directories=no, status=yes, menubar=no, toolbar=no, resizable=yes';

    if (sURL.indexOf('TemplateType=') < 0)
    {
        sURL += sURL.indexOf('?') < 0 ? '?' : '&';
        sURL += 'TemplateType=D';
    }
    if (typeof gWebsiteKey != 'undefined' && gWebsiteKey != 'null' && sURL.indexOf('&WebsiteKey=') < 0)
        sURL += '&WebsiteKey=' + gWebsiteKey;
    if (typeof gHKey != 'undefined' && gHKey != '' && sURL.indexOf('&hkey=') < 0 && sURL.indexOf('&HierarchyCode=') < 0)
        sURL += '&hkey=' + gHKey;

	if(sMode.toLowerCase()=='modal' || sMode.toLowerCase()=='m')
	{
		if (sName.indexOf("-") >= 1)
		{
			winName = sName.substr(0,9);
		}
		else
		{
			winName = sName;
		}
		// First see if the window is already open.
		win = window.open("",winName,modalsettings);
		if (win == null)
		{
		    alert("iMIS was unable to open a new window. Please add your iMIS application server to your allowed popups list.");
		}
        // Check to see if the window is new
		if (win.history.length == 0)
		{
			win.location = sURL;
		}
		win.focus();				
	}
	else
	{	
		if (sName.indexOf("-") >= 1)
		{
			winName = sName.substr(0,9);
		}
		else
		{
			winName = sName;
		}
		// First see if the window is already open.
		win = window.open("",winName,settings);
		if (win == null)
		{
		    alert("iMIS was unable to open a new window. Please add your iMIS application server to your allowed popups list.");
		}
        // Check to see if the window is new
 		if (win.history.length == 0)
		{
			win.location = sURL;
		}
		win.focus();
	}
}

function RefreshBySubmit()
{
	if(gPostBackFormObject != null)
	{
		gPostBackFormObject.submit();
	}
	else
	{
		if (document.forms.length > 0) 
		{
			document.forms[0].submit();
		}
		else
		{
			window.location.href = window.location.href;
		}
	}
}

function SetDefaultStyle(item)
{
	if (item.className != 'VTabSelectedStyle')
		item.className='VTabDefaultStyle';
}

function SetHoverStyle(item)
{
	if (item.className != 'VTabSelectedStyle')
		item.className='VTabHoverStyle';
}

function UpdateFormInputAndSubmit(name, val) 
{
	var docForms = document.forms;
	if (docFormsLen > 0) 
	{
		for (var i = 0, ic = docForms.length; i < ic; i++) 
		{
			if (docForms[i].elements[name] != null) 
			{
				docForms[i].elements[name].value = val;
				docForms[i].submit();
				break;
			}		
		}
	}
}

function toggleElement(targetId, controlImage)
{
	target = document.getElementById(targetId);
	switch (target.style.display)
	{
		case ("block") :
			target.style.display = "none";
			controlImage.src = gWebRoot + "/AsiCommon/Images/+.gif";
			break;
		default :
			target.style.display = "block";
			controlImage.src = gWebRoot + "/AsiCommon/Images/-.gif";
			break;
	}
}

//Append a fields value into another field
function fieldAppend(selectfield,fieldId)
{
	var selval = selectfield.options[selectfield.selectedIndex].value;
	var output = document.getElementById(fieldId);
	output.count = (typeof output.count == 'undefined') ? 1 : ++output.count;
	output.value += selval + ' \n';
}

