// "Internal" function to return the decoded value of a cookie
//

var astrBreadCrumbsArray = new Array();
var astrBreadCrumbsSubArray;

var objReportWindow;

function fnOpenReport(strPath, intLeft, intTop)
{
	var params = "width=900,height=600,left=" + intLeft + ",top=" + intTop + ",scrollbars=yes,resizable=yes";
	
	/* rwd test section
	var xx = document.getElementById('notThere');
	if ( xx == null )		alert("notThere is null");
	if ( xx == undefined )	alert("notThere is undefined");
	if ( xx == "undefined") alert("notThere is string undefined");
		 					alert("xx is " + xx );
	// end rwd
	*/
	if (objReportWindow==null)
	{
	//	alert("fresh open");
		objReportWindow = window.open( strPath, "displayWindow", params);
	}
	else
	{
		try
		{
		//	alert("already open");
			if (false) {
				objReportWindow.navigate( strPath, "displayWindow", params);
				objReportWindow.document.location.reload();
				objReportWindow.focus();
			}
			else {
				objReportWindow.close();
				objReportWindow = window.open( strPath, "displayWindow", params);
				objReportWindow.focus();
			}
		}
		catch(e){
			objReportWindow = window.open( strPath, "displayWindow", params);
		}
	}
}

function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape( document.cookie.substring( offset, endstr ));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) 
{
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//
//  Function to create or update a cookie.
//    name - String object containing the cookie name.
//    value - String object containing the cookie value.  May contain
//      any valid string characters.
//    [expires] - Date object containing the expiration data of the cookie.  If
//      omitted or null, expires the cookie at the end of the current session.
//    [path] - String object indicating the path for which the cookie is valid.
//      If omitted or null, uses the path of the calling document.
//    [domain] - String object indicating the domain for which the cookie is
//      valid.  If omitted or null, uses the domain of the calling document.
//    [secure] - Boolean (true/false) value indicating whether cookie transmission
//      requires a secure channel (HTTPS).  
//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");
//
//  Note that trailing omitted parameters do not require a placeholder.
//
//  To set a secure cookie for path "/myPath", that expires after the
//  current session, you might code:
//
//      SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
//alert("SetCookies...");
//alert(name + value + expires + path + domain + secure);
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function nextDate()
{
	var expdate = new Date ();
	FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
	expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000)); 
	return expdate;
}

function fnDivScrollingSize( obj1, obj2, obj3, obj4)
{
	try
	{
		/*obj1.style.height=parent.mainframe.document.getElementById("rightframe").height - 90;
		obj1.style.width=parent.mainframe.document.getElementById("rightframe").width - 15;   olddd*/

		obj1.style.height = top.document.getElementById("rightframe").height - 90;
		//alert(obj1.style.height);
		//obj1.style.height = '200px';
		obj1.style.width = top.document.getElementById("rightframe").width - 15;   //new
		//<<<for mandatory check box
		if (obj4 != null)
		{	
			 obj1.style.height = parseInt(obj1.style.height) - parseInt(obj4.offsetHeight);
		}
	}
	catch ( ex ) {
		alert("Exception:" + ex );
	}
}

function fnDivScrollingSize_Env(obj1, obj2, obj3)
{
	obj1.style.height = top.document.getElementById("rightframe").height - 158;
	obj1.style.width = top.document.getElementById("rightframe").width - 15;
}

function getQueryVariable(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0; i < vars.length; i++)
  {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {

      return pair[1];
    }
  } 
  //alert('Query Variable ' + variable + ' not found');
}
		
 function fnConfirmDelete()
 {	
	var okClicked = false;
	
	try	{
		okClicked = showModalDialog("frmConfirmDialog.aspx", null, "dialogWidth: 320px; dialogHeight: 130px;status:no;help:no; edge: Raised");
	}
	catch(e)
	{
		okClicked = false;
	}	
	if (okClicked == true) {
		return true;
	}
	else{
		return false;
	}
 }


//<<deleteconfirmation for table control
//<<<this is used by the pages which deleted the items from delete menu of table control...
//<<this feature is applied because of safari which was crashing the onclick event in anchor tag
function fnConfirmDeleteForControl( strid, strRowID )
{
	//alert("Came Here");
	var okClicked;
	var objTxt;
	objTxt = null;
	okClicked = false;
	try	{
		okClicked = showModalDialog("frmConfirmDialog.aspx", null, "dialogWidth: 320px; dialogHeight: 130px;status:no;help:no; edge: Raised");		
		}
		catch(e){
		}	
		if (okClicked == true) 
		{
			__doPostBack(strid,'');
		}
		else
		{}
		//alert('out');
}


function fnClearBreadCrumbs()
{
	astrBreadCrumbsArray=null;
	astrBreadCrumbsSubArray=null;
	DeleteCookie('elsBreadCrumbs');
	var frame = null;
	frame = top.document.getElementById('mainrightframe');
	
	if (frame == null)
	{
		frame = null;
		frame = top.document.getElementById('mainrightframe');
		if (frame == null)
		{
			return;
		}
	}
	frame.rows = "0px,*";

	fnHideFrame();
}

function fnLinkClick(txtPage)
{
	//alert('clicked');
	var astrTmp = new Array();
	var astrTmp1;
	var intCtr = 0;
	var intIncr = 0;
	
	if (GetCookie('elsBreadCrumbs') != null)
	{
		astrBreadCrumbsArray = GetCookie('elsBreadCrumbs').split("<~>");
	}

	for (intCtr=0; intCtr < astrBreadCrumbsArray.length; intCtr++)
	{
		astrTmp1 = null;
		astrTmp1 = astrBreadCrumbsArray[intCtr].split(",");
		
		if (astrTmp1 != null)
		{
			if (astrTmp1[0] == txtPage)
			{
				astrTmp[intIncr] = astrBreadCrumbsArray[intCtr];
				intIncr++;
				break;
			}
			else
			{
				astrTmp[intIncr] = astrBreadCrumbsArray[intCtr];
				intIncr++;
			}
		}
	}
	
	astrBreadCrumbsArray = null;
	astrBreadCrumbsArray = astrTmp;

	astrTmp1 = astrBreadCrumbsArray[astrBreadCrumbsArray.length -1].split(",");
	var frame = null;
	frame = top.document.getElementById('rightframe_Site'); //new
	if (frame != null)
	{
		//alert('if');		
	}
	else
	{
		frame = null;
		frame = top.document.getElementById('rightframe');
		//alert('eeeeelse');
		if (frame == null)
		{
			frame = null;
			//alert('hello');
			frame = parent.parent.document.getElementById('mainrightframe');
			//alert('hi');
			if (frame == null)
			{
				return;
			}
		}
	}
	frame.src = ""; //extra added to make compatible with safari & ,mozilla
	frame.src = astrTmp1[0];
	fnStoreBreadCrumbs(astrBreadCrumbsArray);
	fnPaintBreadCrumbs();
}

function fnPaintBreadCrumbs()
{
	var strText = "";
	var intCtr = 0;
	var astrTmp1;
	
	if (GetCookie('elsBreadCrumbs') != null)
	{
		astrBreadCrumbsArray = GetCookie('elsBreadCrumbs').split("<~>");
	}

	for (intCtr=0; intCtr < astrBreadCrumbsArray.length - 1; intCtr++)
	{
		astrTmp1 = null;
		astrTmp1 = astrBreadCrumbsArray[intCtr].split(",");
		
		if (astrTmp1 != null)
		{
			if (strText == "")
			{
				strText = strText + "<a class='BreadCrumbsHyperLinkText' onclick=\"fnLinkClick('" + astrTmp1[0] + "');\">" + astrTmp1[2] + "</a>";
			}
			else
				strText = strText + "<span class='ELLISNormalText'>&nbsp;>></span><a class='BreadCrumbsHyperLinkText' onclick=\"fnLinkClick('" + astrTmp1[0] + "');\">" + astrTmp1[2] + "</a>";
		}
	}
	
	var frame = null;
	frame = top.document.getElementById('mainrightframe');

	if (frame == null)
	{
		//frame=null;
		
		frame = parent.parent.document.getElementById('mainrightframe');
		if (frame == null) 
		{
			return;
		}
	}

	if (strText != "")
		frame.rows = "20px,*";
	else
	{
		frame.rows = "0px,*";
	}

	fnHideFrame();

	var objDiv;
	var iframeNavPass;
	var iframeNavCollect;

	//objDiv=null;
	//if (parent.rightframetop == null)
	if (top.document.getElementById('rightframetop') == null)
	{
		//objDiv=parent.rightframetop.document.getElementById('breadCrumbs');	 //old
		iframeNavPass = top.document.getElementById('rightframetop');
		iframeNavCollect = fnFrameDoc(iframeNavPass);
		if (iframeNavCollect)
		{
			if (iframeNavCollect.getElementById('breadCrumbs'))
			{
				objDiv = iframeNavCollect.getElementById('breadCrumbs');
			}
		}
	}
	else
	{
		//objDiv=top.rightframetop.document.getElementById('breadCrumbs');		
		iframeNavPass = top.document.getElementById('rightframetop');
		iframeNavCollect = fnFrameDoc(iframeNavPass);
		if (iframeNavCollect)
		{
			if (iframeNavCollect.getElementById('breadCrumbs'))
			{
				objDiv = iframeNavCollect.getElementById('breadCrumbs');
			}
		}
	}

	if (objDiv == null)
	{
		return;
	}

	if (objDiv == null)		// rwd says: we already returned
	{}
	else
	{
		objDiv.innerHTML = strText + "<span class='ELLISNormalText' style='padding-top:2px'>&nbsp;>></span>";
	}
}


function fnHideFrame()
{
	var frame = null;
	var frame = parent.document.getElementById('mainrightframegroup');
	if (frame==null)
	{
		return;
	}
	frame = parent.parent.document.getElementById('mainrightframe');
	
	if (frame != null)
	{
		frame.rows="0px,*";
	}
}

function fnAddItem(strPage, strMenuID, strCaption, blnClear)
{	
	var astrTmp=new Array();
	var astrTmp1;
	var intCtr=0;
	var intIncr=0;

	if (GetCookie('elsBreadCrumbs') != null)
	{
		astrBreadCrumbsArray = GetCookie('elsBreadCrumbs').split("<~>");
	}

	if (blnClear == true)
	{
		astrBreadCrumbsArray = null;
		astrBreadCrumbsArray = new Array();
	}

	for (intCtr=0; intCtr < astrBreadCrumbsArray.length; intCtr++)
	{	
		astrTmp1 = null;	
		astrTmp1 = astrBreadCrumbsArray[intCtr].split(",");
		
		if (astrTmp1 != null)
		{
			if (astrTmp1[0]==strPage)
			{
				if (intCtr==astrBreadCrumbsArray.length-1)
				{
					return;
				}
				astrTmp[intIncr] = astrBreadCrumbsArray[intCtr];
				astrBreadCrumbsArray = null;
				astrBreadCrumbsArray = astrTmp;
				intIncr++;
				fnStoreBreadCrumbs(astrBreadCrumbsArray);
				fnPaintBreadCrumbs();
				return;
			}
			else
			{
				astrTmp[intIncr]=astrBreadCrumbsArray[intCtr];
				intIncr++;
			}
		}
	}

	astrBreadCrumbsSubArray = new Array();
	astrBreadCrumbsSubArray[0] = strPage;
	astrBreadCrumbsSubArray[1] = strMenuID;
	astrBreadCrumbsSubArray[2] = strCaption;
	astrBreadCrumbsArray[astrBreadCrumbsArray.length] = astrBreadCrumbsSubArray;
	
	fnStoreBreadCrumbs(astrBreadCrumbsArray);
	fnPaintBreadCrumbs();
}

function fnStoreBreadCrumbs(astrValues)
{
	//alert('store innnn');
	var intCtr = 0;
	var astrTmp1;
	var strText = "";
	
	for (intCtr=0; intCtr < astrBreadCrumbsArray.length; intCtr++)
	{
		astrTmp1 = null;
		astrTmp1 = astrBreadCrumbsArray[intCtr];
		//'alert(astrTmp1);
		if (astrTmp1 != null)
		{
			if (strText == "")
			{
				strText = astrTmp1;
			}
			else
			{
				strText = strText + "<~>" + astrTmp1;
			}
		}
	}
	//alert(strText);
	SetCookie('elsBreadCrumbs', strText);
}

//<<<<<<**********TO BLOCK RIGHT CLICK****************

if (window.Event)
	document.captureEvents(Event.MOUSEUP);

function fnCloseWindow(blnReturnValue)
{
	this.returnValue = blnReturnValue;
	window.close();
}

function nocontextmenu()
{
	event.cancelBubble = true, event.returnValue = false;
	return false;
}

function norightclick(e) 
{
	/*if (window.Event) 
	{
		if (e.which == 2 || e.which == 3) return false;
	}
	else if (event.button == 2 || event.button == 3) 
	{
		event.cancelBubble = true, event.returnValue = false;
		return false;
	}*/
}
//document.oncontextmenu = nocontextmenu;
//document.onmousedown = norightclick;
//document.onmouseup = norightclick;

function fnLoadProducts(strID)
{
	//alert("fnLoadProducts(" + strID + ") called...");
	var strCombinedValue;
	var astrTemp;
	var astrNames;
	var astrIds;
	var intLeft = screen.width /2 - 350/2;
    var intTop = screen.height /2 - 350/2;
    var objProdId = document.getElementById('txtProductID'); 
	var objProdNames = document.getElementById('txtProductAssign');
	
	if (strID != null || strID != "") {			// * * * * always TRUE !
		//objProdId.value = strID;
	} else {
		strID = objProdId.value;
	}
	
	var strPath;
	//alert("SSSSSSS");
	strPath = 'frmProductHierarchy.aspx?ID=' + strID + '&NAMES=' + objProdNames.value;
	//alert(strPath);
	
	strCombinedValue = showModalDialog( strPath, null, "dialogWidth: 350px; dialogHeight: 390px;scroll:no;status:no;help:no; edge: Raised");
	//trial--strCombinedValue = window.open(strPath, null, "dialogWidth: 350px; dialogHeight: 360px;scroll:no;status:no;help:no; edge: Raised");
	if (strCombinedValue == null)
	{
		return;
	}
	else
	{
		astrTemp = strCombinedValue.split('_');
		if (astrTemp.length > 0)
		{
			objProdId.value = astrTemp[0];
			objProdNames.value = astrTemp[1];
		}
	}
}

function Querystring_get(key, default_) {
	
	var value = this.params[key];
	if (value == null) value = default_;
	
	return value
}

function Querystring(qs) { // optionally pass a querystring to parse
	this.params = new Object();
	this.get = Querystring_get;
	//alert("D");
	
	if (qs == null)
		qs = location.search.substring( 1, location.search.length);

	if (qs.length == 0) return;
	//alert(qs);
// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	//alert(unescape(qs));
	var strTemp = unescape(qs);
	var args = strTemp.split('&'); // parse out name/value pairs separated via &
	//alert(args.length);
// split out each name=value pair
	for (var i=0; i < args.length; i++) {
		var value;
		var pair = args[i].split('=');
		var name = unescape(pair[0]);
		
		//alert(name);

		if (pair.length == 2)
		{
			value = unescape(pair[1]);
			//alert(value);
		}
		else
		{
			value = name;
			//alert(value);
		}
		this.params[name] = value;
	}
}
//
function fnFrameDoc(objFrame)
{
	var objDoc;
	if (objFrame.contentDocument) 
	{
		objDoc = objFrame.contentDocument;
		return objDoc;
	} 
	else if (objFrame.contentWindow) 
	{
		objDoc = objFrame.contentWindow.document;
		return objDoc;
	}
	else if (objFrame.document) 
	{
		objDoc = objFrame.document;
		return objDoc;
	}
}
