

/****************************************Global Variables********************************************************/
var winModalWindow;
var RELOAD = "RELOAD";
var SUBMIT = "SUBMIT";
var _isMozillaversion16 = ((navigator.appName== "Netscape") && (navigator.userAgent.indexOf("Gecko/20040113")!=-1) && (navigator.userAgent.indexOf("rv:1.6")!=-1))
var _isMozillaversion11 = ((navigator.appName== "Netscape") && (navigator.userAgent.indexOf("Gecko/20020826")!=-1) && (navigator.userAgent.indexOf("rv:1.1")!=-1))
var _isNetscapeVersion7 = ((navigator.appName== "Netscape") && (navigator.userAgent.indexOf("Netscape/7")!=-1))?true:false;
var _isIE50 = (navigator.userAgent.indexOf("MSIE 5.0")!=-1)?true:false; 
var _isMac = (navigator.appVersion.indexOf("Mac")!=-1)?true:false; 

var IsDialogWindowOpen = false; //To avoid multiple opening of dialog windows

/****************************************Global Variables********************************************************/
 
/****************************************Global Functions********************************************************/

/*
This function will identify the browser whether it supports modal dialog or not.
If the browser doesn't support modal dialog then new window will be launched and
it will be act as a modal window.
*/
function ShowCustomModalDialog(Page, Width, Height, Scrollbars)
{
	//Check if window is already open, then return
	if(IsDialogWindowOpen)
	{
		return;
	}
	else
	{
	IsDialogWindowOpen = true;
	
	//Check for Scrollbars setting
	if (Scrollbars == null)
	{
		Scrollbars = "no";
	}
	
	if (window.showModalDialog)	//if window.showModalDialog is supported
	{
		if(_isMac && _isIE50)			//if Mac OS and IE version 5.0
		{
			LaunchSimulatedModalWindow(Page, Width, Height);
			IsDialogWindowOpen = false;	
		}
		else
		{
			var retVal = "";
			var isWindowIE50 = (navigator.appVersion.indexOf("MSIE 5.0")!=-1)?true:false;   
   			var isWindows =(navigator.appVersion.indexOf("Windows")!=-1)?true:false; 

			//-----------WorkAround for the Problem in Windows/IE 5.0----------------
			//To solve the problem of opening a new window in IE 5.0, we should use
			//<iframe>. The following code will do the same.
			if(isWindowIE50 && isWindows)
			{
				var SlashAfterVirtualDirectory = Page.indexOf("/", Page.indexOf("/", Page.indexOf("/")+2) + 1);
				var lastQuestionMark = Page.lastIndexOf("?");
				var replaceTo = Page.substring(SlashAfterVirtualDirectory + 1, lastQuestionMark);
				var ReplacedUrl = Page.replace(replaceTo, "_Resources/ModalDialog/QModalDialogLauncher.aspx") + "&DialogName=" + replaceTo;
				retVal = window.showModalDialog(ReplacedUrl, "", "dialogWidth=" + Width + "px;dialogHeight=" + Height + "px;scroll:" + Scrollbars + ";help:off; status:off; center:yes");
				
				IsDialogWindowOpen = false;
			}
			else
			{
				retVal = window.showModalDialog(Page, "", "dialogWidth=" + Width + "px;dialogHeight=" + Height + "px;scroll:" + Scrollbars + ";help:off; status:off; center:yes");

				IsDialogWindowOpen = false;		
			}
			if(retVal == RELOAD)
			{
				self.document.forms[0].submit(); //reload() is not supported by MAC IE 5.2
			}
			else if(retVal == SUBMIT)
			{
				self.document.forms[0].submit();
			}
			else if(retVal != null)
			{
				
				var modalRetValue = retVal.split(",");	
				
				if(self.document.forms[0].elements["hdnModalReturnValue"] != null && modalRetValue[0] != null && modalRetValue[0] != "")
					self.document.forms[0].elements["hdnModalReturnValue"].value = modalRetValue[0];
					
				if(self.document.forms[0].elements["hdnShipmentTypeID"] != null && modalRetValue[1] != null && modalRetValue[1] != "")
					self.document.forms[0].elements["hdnShipmentTypeID"].value = modalRetValue[1];
				
				if(self.document.forms[0].elements["hdnAddressType"] != null && modalRetValue[2] != null && modalRetValue[2] != "")
					self.document.forms[0].elements["hdnAddressType"].value = modalRetValue[2];
	
				if(self.document.forms[0].elements["hdnDataValue"] != null && modalRetValue[3] != null && modalRetValue[3] != "")
					self.document.forms[0].elements["hdnDataValue"].value = modalRetValue[3];
							
				self.document.forms[0].submit();
			}
			else
				return;
		}
	}
	else
	{
		LaunchSimulatedModalWindow(Page, Width, Height, Scrollbars);
		IsDialogWindowOpen = false;
	}
	return;
  }	
}
// Method is used to launch simulated modal window for browsers other than IE
// Also the method launches the simulated modal window for Mac and IE 5.0
function LaunchSimulatedModalWindow(Page, Width, Height, Scrollbars)
{
	if(navigator.appName == "Netscape")
	{
		window.top.captureEvents (Event.CLICK|Event.FOCUS)
	}
	window.top.onclick = IgnoreEvents
	window.top.onfocus = HandleFocus
	
	//Get the screen width and heigth
	var screenW = 800, screenH = 600;
	if (parseInt(navigator.appVersion)>3)
	{
		screenW = screen.width;
		screenH = screen.height;
	}
	else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled()) 
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}
	
	var left = (screenW-Width)/2;
	var top  = (screenH-Height)/2;
	
	//call this method to disable the parent form elements and links.
	if(_isNetscapeVersion7 || _isMozillaversion11 || _isMozillaversion16 || (_isMac && _isIE50))
		disableForms();
		
	winModalWindow =  window.open (Page, "", "dependent=yes,width=" + Width + ",height=" + Height + ", scrollbars=" + Scrollbars + ", help=off, status=off, left=" + left + ", top=" + top);
	window.opener = winModalWindow;
	winModalWindow.focus();
}

function IgnoreEvents()
{
	if (winModalWindow && !winModalWindow.closed) 
	{
		winModalWindow.focus();
		return false
	}
}

//************ Used to close the window **************/
function CloseWindow()
{
	if(window.showModalDialog)
	{
		if(_isMac && _isIE50)
		{
			HandleFocus();
			window.parent.close();
		}
		else
			top.close();
	}
	else
	{
		HandleFocus();
		window.parent.close();
	}
}

//************ Used to close the window on Esc Key Press **************/
function CloseWindowOnEscKeyPress()
{
	if (!window.event)
	{
		event = arguments.callee.caller.arguments[0];
	}
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 27)
	{
		//Close the Browser Window
		CloseWindow();
	}
}

//Call the set focus method through setTimeOut as without setTimeout it gives some problems in Netscape 7 and Mozilla 1.6
function HandleFocus()
{
	if(_isNetscapeVersion7 || _isMozillaversion11 || _isMozillaversion16)
		setTimeout("setFocus()", 50);
	else
		setFocus();
		
	return true;
}

function setFocus()
{
	if (winModalWindow)
	{
		if (!winModalWindow.closed)
		{
			winModalWindow.focus();
		}
		else
		{
			if(navigator.appName == "Netscape")
			{
				window.top.releaseEvents (Event.CLICK|Event.FOCUS)
			}
			window.top.onclick = ""
			if(_isNetscapeVersion7 || _isMozillaversion11 || _isMozillaversion16 || (_isMac && _isIE50))
				enableForms();
		}
    }
	return false
}

/*
Parent page which has child window should call this function on onUnload event 
to close the child window if it exists.
*/
function CloseChildOnParentUnLoad()
{
 if(window.opener.name == "ModalChild")
 {
	window.opener.close();
 }
 
}

/*
To close the child and relod the parent from the child.
This should be called from the child.
*/
function CloseChildReloadParent()
{
	if (window.showModalDialog)
	{
		window.returnValue = RELOAD;
		window.close();
	}
	else
	{
		window.opener.location.reload();
		window.close();
	}
}

/*
To close the child and submit the parent from the child.
This should be called from the child.
*/
function CloseChildSubmitParent()
{
	if (window.showModalDialog)
	{
		window.returnValue = SUBMIT;
		window.close();
	}
	else
	{
		window.opener.document.forms[0].submit();
		window.close();
	}
}

function CloseChildWindow()
{
	window.close();
}

function CheckLength(field,len)
{
	var maxlen = parseInt(len);
	if (field.value.length > maxlen)
		field.value = field.value.substring(0, maxlen);
}

function changeCursorOnMouseOver(field)
{
	var ie50 = (navigator.appVersion.indexOf("MSIE 5")!=-1)?true:false;  
	var windows =(navigator.appVersion.indexOf("Windows")!=-1)?true:false; 
	if(ie50 && windows)
	{
		field.style.cursor = 'hand';
		field.style.color = "C86837";
	}
	else
	{
		field.style.cursor = 'pointer';
		field.style.color = "C86837";
	}
}

function changeCursorOnMouseOut(field)
{
	field.style.cursor = 'default';
	field.style.color = "928D57";
}


// Since links in Netscape can't be disabled, preserve onclick event handlers while they're "disabled." 
// Restore when reenabling the main window.
var IELinkClicks
// Disable form elements and links and the anchors(Anchors should have the name or Id property).
function disableForms() 
{
	var index;
	IELinkClicks = new Array()
	disableFormElements();
    for (index = 0; index < document.links.length; index++) 
    {
		IELinkClicks[index] = document.links[index].onclick
        document.links[index].onclick = IgnoreEvents
    }
    var linkLength = document.links.length
    for (index = 0; index < document.anchors.length; index++) 
    {
		IELinkClicks[linkLength+index] = document.anchors[index].onclick
		document.anchors[index].onclick = IgnoreEvents
    }
}

/**
* The method disables various form elements of the passed form object.
*/
function disableFormElements()
{
	formObj = document.forms[0];
	if(!formObj || formObj == null)
	{
		return;
	}
	
	var elements = formObj.elements;
	var index;
	for(index = 0; index < elements.length; index++)
	{
		try
		{
			if(elements[index].type != "hidden")
			{
				if(elements[index].type == "text")
				{
					elements[index].readonly = true;
				}
				else if(elements[index].type == "select-one")
				{
					//elements[index].onchange = "window.event.returnValue = false; return false;"
				}
				else if(elements[index].type != "radio" && elements[index].type != "checkbox")
				{
					elements[index].disabled = true;
				}
			}
		}
		catch(exception)
		{
			//dont do anything
		}
	}
}


// Restore IE form elements and links to normal behavior.
function enableForms() 
{
	enableFormElements();
    for (index = 0; index < document.links.length; index++) 
    {
		document.links[index].onclick = IELinkClicks[index]
    }
    var linksLength = document.links.length;
    for (index = 0; index < document.anchors.length; index++) 
    {
		document.anchors[index].onclick = IELinkClicks[linksLength+index]
    }
   
}
/**
* The method enables various form elements of the passed form object.
*/
function enableFormElements()
{
	formObj = document.forms[0];
	if(!formObj || formObj == null)
	{
		return;
	}
	
	var elements = formObj.elements;
	var index;
	for(index = 0; index < elements.length; index++)
	{
		try
		{
			if(elements[index].type != "hidden")
			{
				if(elements[index].type == "text")
				{
					elements[index].readonly = false;
				}
				else if(elements[index].type == "select-one")
				{
					//elements[index].onchange = "window.event.returnValue = true; return true;"
				}
				else if(elements[index].type != "radio" && elements[index].type != "checkbox")
				{
					elements[index].disabled = false;
				}
			}
		}
		catch(exception)
		{
			//dont do anything
		}
	}
}

