//Franciscus Adam
//IMLP E-Wizard
//Manager: Mike Motley
//Creeated: October 30, 2000
//Updated: November 20, 2000
//Save and Recall System

var EditFlag;

function checkEliteNetUser(search) {
var checkPrjName,stPrj,enPrj,Name,value;
Name=search;
checkPrjName=document.cookie;
stPrj=checkPrjName.indexOf(Name);

if(stPrj!=-1) {
	stPrj+=Name.length+1;
      enPrj=checkPrjName.indexOf(";",stPrj);
	if(enPrj!=-1)
		value=checkPrjName.substring(stPrj,enPrj);
	else
	   value=checkPrjName.substring(stPrj,checkPrjName.length);
	return value;
} else {
        return "null";
}
}

/*
** Function to get all the Cat #. Added for Distributor Wizard - Muthaiya
*/
function getAllCat() {
		var OL = top.onlink;
		var PO_OL = top.onlink;
		var DynObj = top.onlink.RefDynaObj();
	var x = PO_OL.codeset.DYNAPO;
	if(x!=null)
		x.close();

	OL["ADD_FLAG"]=true;

	if (!OL.Loaded(OL.PG)||!OL.RefDynaObj()) {
		alert("\nGE Industrial Systems Sales Message\nTo generate a Bill of Material, \nyou must first configure a product.\n");
		return;
	}

	PO_OL.codeset.OpenPO(true);

	var orderType = PO_OL.items.RefOrderType();

	var mainobjs = PO_OL.items.RefItems();

	var itemindex = (mainobjs.length-1);

	var mainobj = mainobjs[itemindex];

	var submainobjs = PO_OL.items.RefSubItems();

	var itemindexsub = (submainobjs.length-1);

	var submainobj = submainobjs[itemindexsub];
	tmpCatNo='';
	tmpCatQty='';
	noOfItems=0;
	tmpComma='';
	if (mainobj.CATNO.length!=0) {
		tmpCatNo = mainobj.CATNO;
		tmpCatQty='1';
		noOfItems=1;
		tmpComma=',';
	}
	for(subcount = 0; subcount < submainobjs[itemindex].length; subcount++) {
		var displaySubStorage = submainobjs[itemindex];
		tmpCatNo = tmpCatNo+tmpComma+displaySubStorage[subcount].SUBCAT;
		tmpCatQty = tmpCatQty+tmpComma+displaySubStorage[subcount].QTY;
		tmpComma=',';
	}
	noOfItems=noOfItems+subcount;
}

/*
** Function to add the items into distributor's BOM - Muthaiya
*/
function doDistributorAdd() {
	getAllCat();
	parmsToURL = "distAct=1&sessionId="+top.dist_parms.sessionId+"&baseURL="+top.dist_parms.baseURL+"&distItem="+noOfItems+"&distCat="+tmpCatNo+"&distQty="+tmpCatQty;
	var strProcessCart = 'http://'+top.location.host+'/industrialsystems/wizards/distributor/dist_add.htm?'+parmsToURL;
	window.open(strProcessCart,'shoppingAddCart','width=455,height=230,screenX=150,screenY=140,status=no,scrollbars=0,resizable=0,menubar=no,toolbar=no','true');
}

/*
** Function to view distributor's BOM - Muthaiya
*/
function doDistributorView() {
	strScreenFmt='width=800,height=500,screenX=0,screenY=0,status=no,scrollbars=1,resizable=1,menubar=no,toolbar=no';
	var strViewCartURL = top.dist_parms.baseURL+"/processCart.html?distAct=2&sessionId="+top.dist_parms.sessionId+"&baseURL="+top.dist_parms.baseURL;
	window.open(strViewCartURL,'shoppingViewCart',strScreenFmt,'true');
}

//Functions to close any open windows related to save and recall system when the main browser is being unloaded
function childclose() {
if (editpage != null)
editpage.close();
if (updatepage != null)
updatepage.close();
if (cwcadd != null)
cwcadd.close();
}

//Functions to close any open windows related to save and recall system when the main browser is being unloaded
function childclosethree() {
if (loadpage7 != null)
loadpage7.close();
if (loadpage8 != null)
loadpage8.close();
}

//Function to add a product to the bill of material
function GEAddToBOM() {
/*
** The following 'if' condition is added to check whether
** the customer is a distrubutor's customer or indsys's customer
*/
var domainname = ckdom();
if (top.location.pathname.indexOf('dist_home.htm')>=0) {
	doDistributorAdd();
	return;
}
var checkPrjName, stPrj, enPrj, Name, value, URL;
EditFlag=checkEliteNetUser("EditFlag");
RebateSubmitted = checkEliteNetUser("RebateStatus");
//if selected project is submitted for rebate then no more modifications allowed.
if(RebateSubmitted=='1')
{
 alert('Selected project has already been submitted for rebate. \nFurther modifications are not allowed');
 return;
}
//
document.cookie="Negotiated=0;path=/;domain="+domainname;

Name="ProjectName";
checkPrjName=document.cookie;
stPrj=checkPrjName.indexOf(Name);

	if (top.updatepage != null &&  !top.updatepage.closed)
	top.updatepage.close();

	if(!cookieCheck('SMSESSION')) {
		//Non-elitenet users
		//top.cwcadd=window.open('/industrialsystems/wizards/csp/order/geaddtopo.htm','BillofMaterial','width=460,height=270,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
	// Commented below as this was calling geaddtopo.htm from csp wizard ...look above
	top.cwcadd=window.open('../order/geaddtopo.htm','BillofMaterial','width=460,height=270,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');

	} else { //Elitenet users

				// Calling Java Sockets to update the GE 1 Stop catalog no's

				var newDesc = top.onlink.BuildTarget("TEXT",window,"ABOM.NEWDESC");
				var newPrice = top.onlink.BuildTarget("TEXT",window,"ABOM.PRICE");

				if ((newDesc == "") || (newDesc == "null") || (newDesc == "undefined") || (newPrice == "") || (newPrice == "null") || (newPrice == "undefined"))
				{
					//Do Nothing ..
				}
				else
				{
					//alert(top.onlink.BuildTarget("TEXT",window,"ABOM.SUBCAT"));
					//alert(top.onlink.BuildTarget("TEXT",window,"ABOM.TCATNO"));
					//alert(top.onlink.BuildTarget("TEXT",window,"ABOM.PRICE"));
					//alert(top.onlink.BuildTarget("TEXT",window,"ABOM.NEWDESC"));

					newWindow = window.open("/EnetWizards/Dispatcher?REQUEST=ADDTOBOM&currentCatNo="+ top.onlink.BuildTarget("TEXT",window,"ABOM.SUBCAT") + "&templateCatNo=" + top.onlink.BuildTarget("TEXT",window,"ABOM.TCATNO") + "&price=" + top.onlink.BuildTarget("TEXT",window,"ABOM.PRICE") + " &Desc=" + top.onlink.BuildTarget("TEXT",window,"ABOM.NEWDESC") + "",null,"height=1,width=1,status=no,toolbar=no,menubar=no,location=no");
					//newWindow.close();
					newWindow.blur();
				}
				//Ends Here
		if (stPrj!=-1) 
		{
			stPrj+=Name.length+1;
			enPrj=checkPrjName.indexOf(";",stPrj);
				if (enPrj!=-1) {
					value=checkPrjName.substring(stPrj,enPrj);
				} else {
					value=checkPrjName.substring(stPrj,checkPrjName.length);
				}

			if(EditFlag=="true"){
                        top.editpage=window.open('/industrialsystems/wizards/quotationmanager/update.htm', 'update','width=460,height=270,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
			} else {
                        top.editpage=window.open('/industrialsystems/wizards/quotationmanager/geeliteaddtopo.htm', 'BillofMaterial','width=460,height=270,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
			}
		} 
		else 
		{
			if(EditFlag=="true"){
			top.updatepage=window.open('/industrialsystems/wizards/quotationmanager/update.htm', 'update','width=460,height=270,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
			} else {
			top.updatepage=window.open('/industrialsystems/wizards/quotationmanager/geeliteaddtopo.htm', 'BillofMaterial','width=460,height=270,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
			}
		}
	}
	return false;
}

//Function to view the bill of material
function GEViewBOM() {
/*
** The following 'if' condition is added to check whether
** the customer is a distrubutor's customer or indsys's customer
*/


if (top.location.pathname.indexOf('dist_home.htm')>=0) {
	doDistributorView();
	return;
}
var qmdmvbom0 = ldqmPage(self.document.domain,0);
var qmdmvbom1 = ldqmPage(self.document.domain,1);
var domainname = ckdom();
var dt = new Date();
var checkPrjName, stPrj, enPrj, Name, value, URL;
Name="ProjectName";
checkPrjName=document.cookie;
stPrj=checkPrjName.indexOf(Name);
	if(!cookieCheck('SMSESSION')) {
		if(top.onlink['BringUpPO']!=null) {
			top.onlink.BringUpPO(true);
		}
	} else {
		if (stPrj!=-1) {
			stPrj+=Name.length+1;
			enPrj=checkPrjName.indexOf(";",stPrj);
				if (enPrj!=-1) {
					value=checkPrjName.substring(stPrj,enPrj);
				} else {
					value=checkPrjName.substring(stPrj,checkPrjName.length);
				}

			value=escape(value);
			document.cookie="EditFlag=false;path=/;domain="+domainname;

			if (value == null || value == 'null' || value == '')
				top.window.parent.location= qmdmvbom0+"/industrialsystems/extranets/commoncommunity/commonsegment/quotationmanager/searchresult.jsp?option=10&from=VB&nocache="+dt.getTime();
			else
				top.window.parent.location=qmdmvbom1+"/industrialsystems/extranets/commoncommunity/commonsegment/quotationmanager/frameset.jsp?option=open&nocache="+dt.getTime();
		} else {
			top.window.parent.location= qmdmvbom0+"/industrialsystems/extranets/commoncommunity/commonsegment/quotationmanager/searchresult.jsp?option=10&from=VB&nocache="+dt.getTime();
		}
	}
	return false;
}

function ldqmPage(domain, security)
 {
 var sitepath;
 if (security == 0)
 	sitepath = "http://";
 else
    sitepath = "https://";

    if (domain == "www.geindustrial.com" || domain == "geindustrial.com")
 	   sitepath = sitepath + "elitenet.geindustrial.com";
    else if(domain=="www-test.geindustrial.com")
   	sitepath = sitepath + "elitenet-test.geindustrial.com";
	 else
	 	sitepath = sitepath + "teamsite-elitenet-dev.indsys.ge.com";
    return sitepath;
 }

function ckdom()
{
	var selfdomain ="";
	var ckdomain="";
	selfdomain = self.document.domain;
	if (selfdomain == "teamsite-cwc-review.indsys.ge.com")
		ckdomain = ".ge.com";
	else
		ckdomain = ".geindustrial.com";
	return ckdomain;
}