/**

 * File: manipulate_layers.jsp

 * Purpose: Javascript file to perform layers manipulation and other

 *          javascript functions for the application

 *

 * Copyright (c) 1992-2003 Cisco Inc. All Rights Reserved.

 *

 * This software is the confidential and proprietary information of

 * Cisco Inc. ("Confidential Information").  You shall not

 * disclose such Confidential Information and shall use it only in

 * accordance with the terms of the license agreement you entered into

 * with Cisco Inc.

 *

 **/




/****

    * Function: setInitialFocus

	* Params:	document.lookupIdActionForm.prodid;

	* Purpose:  Sets focus on the specified element

	*

****/

function setInitialFocus(elem)

{

	if(elem)

		elem.focus();

	return true;

}



/****

    * Function: setInitialParams()

	* Purpose:  Inits the first drop-down menu

	*

****/

function setInitialParams()

{

    hide_all_frames(1, 9, 'mdfinfo');

    //hide_frame('buttonframe');

	createLayerObjects("Category", null);

	//alert("Invoked");

    return true;

}



/****

    * Function: function createLayerObjects

	* Params:	nodetype, parentid

	* Purpose:  Loads the relevant data into each drop-down and makes it visible

	*

****/

function createLayerObjects(nodetype, parentid)

{



    loadDataIntoDropDown(currLayer, nodetype, parentid);

    display_frame('mdfinfo' + currLayer);



	currLayer++;



	return true;

}

/****

    * Function: invokeResultPage

	* mdfid, drawprdid, drawfilename, slnnumfmtdesc, slnnumfmtloc

	* Purpose:  Does the actual form submit to access the drawing page

	*

****/

function invokeResultPage(mdfname, drawprdid, drawfilename, slnnumfmtdesc, slnnumfmtloc)

{

    document.fetchDrawingActionForm.mdfid.value = mdfname;

    document.fetchDrawingActionForm.drawprdid.value = drawprdid;

    document.fetchDrawingActionForm.drawfilename.value = drawfilename;

    document.fetchDrawingActionForm.slnumfmtdesc.value = slnnumfmtdesc;

    document.fetchDrawingActionForm.slnumfmtloc.value = slnnumfmtloc;

	//alert(mdfid + "/" + drawprdid + "/" + drawfilename + "/" + slnnumfmtdesc

//			+ "/" + slnnumfmtloc);

    document.fetchDrawingActionForm.submit();



    return true;



}



/****

    * Function: createButtonFrame()

	* Purpose:  Create prev and next buttons (currenty deprecated)

	*

****/

function createButtonFrame()

{

	display_frame_at_posn('buttonframe', currLayer)

	//display_frame_at_posn('mdfinfo' + currLayer, currLayer)

	return true;

}





/****

    * Function: clearAllChosenLists(fromcount, tocount)

    * Params:	fromcount, tocount

	* Purpose:  All lists below current list will be cleared

	*

****/

function clearAllChosenLists(fromcount, tocount)

{



	var selStr = "document.fetchDrawingActionForm.mdfselect"



	for(var i = fromcount; i <= tocount; i++)

	{

		var listelem = eval(selStr + i);

		removeAllFromList( listelem);

	}

}



/****

    * Function: loadDataIntoDropDown

	* Params:	frameid, nodetype, parentid

	* Purpose:  Fill the drop-down with options for specified node type

	*

****/

function loadDataIntoDropDown (frameid, nodetype, parentid)

{

	var selectname = eval("document.fetchDrawingActionForm.mdfselect" + frameid);

	var firstElem = 0;

	var nodeType = "";

	var totElems = 0;

	var currElem = 0;



	//Reset the drop-down in case it has already been loaded once

	removeAllFromList(selectname);



    // Add a title with an option of -1

	//selectname.options.length == 0;

	addTextToList(selMsgStr, "-1", selectname, true);

	for(var i = 0; i < (mdf_array.length - 1); i++)

	{



		if(	(areStringsSame(mdf_array[i].NODE_TYPE, nodetype)) &&

			(areStringsSame(mdf_array[i].MDF_PARENT_ID, parentid)))

		{

			//alert("Node Type/ID: "  + mdf_array[i].NODE_TYPE +

			//					mdf_array[i].MDF_PARENT_ID);



			//Create the option

			//Also, pass the current node type to avoid using it in the

			//next option

			var str = mdf_array[i].NODE_TYPE + ":";

			str +=	mdf_array[i].MDF_ID + ":";

			str +=	mdf_array[i].IS_LEAF;

               addTextToList(	mdf_array[i].MDF_NAME, str,selectname, false);



			nodeType = mdf_array[i].NODE_TYPE;

			currElem = i;



		}

	}



	//Set the first selected to true

	selectname.options[0].selected =  true;



	//Done with the addition, now set the label

	setLabel(frameid, nodeType);



	return true;



}



/****

    * Function: function setLabel

	* Purpose:	id, nodetype

	* Purpose:  Set the display label for the chosen type based on node type

	*

****/

function setLabel(id, nodetype)

{

	var lbl = 0;

	var isIE=document.all?true:false;

	var isNS4=document.layers?true:false;

	var isNS6=navigator.userAgent.indexOf("Gecko")!=-1?true:false;



    for (var ii = 0; ii < 4; ii++)

    {

        if(areStringsSame(NodeTypes[ii], nodetype))

        {

            lbl =  ii;

        }

    }



	//Now write the innerHTML text

	var framename = "label" + id;

	var frame;



	//Do the NS Vs IE check

	if(isNS4)

	{

		frame = document.layers[framename];

		if(frame)

		{

			frame.innerHTML = NodeTypes[lbl];

		}

	}

	else if(isNS6 || isIE)

	{

		frame = document.getElementById(framename);

		if(frame)

		{

			frame.innerHTML = NodeTypes[lbl];

		}

	}



}



/****

    * Function: addABreak()

	* Param:	

	* Purpose: 	Since NS does not seem to add a break after a <div> tag

	*			this function tries to do that

	*

****/

function addABreak()

{

	var isNS4=document.layers?true:false;

	var isNS6=navigator.userAgent.indexOf("Gecko")!=-1?true:false;



	if(isNS4 || isNS6)

	{

		document.writeln("<br>");

	}



}



/****

    * Function: processListChoice

	* Param:	listelem

	* Purpose:  Tied to the onChange method of the listbox

	*

****/

function processListChoice(listelem)

{

	if(listelem.options.length > 0)

    {



		//First shutdown all open child drop-downs and reset them

		var selStr = listelem.name;

		var sidx = selStr.charAt(selStr.length - 1);



		//f this is not a number , this operation may not perform

		var idx = parseInt(sidx);

		if(isNaN(idx))

			return false;



		idx++;

		hide_all_frames(idx, 9, "mdfinfo");

		hide_frame('buttonframe');



		//Blank all the drop-downs too  beyond this

		clearAllChosenLists(idx, 9);



		//This will become the new currLayer value

		currLayer = idx;



    	var idx = listelem.selectedIndex;

		if(listelem.options[idx].value == "-1")

			return false;



		//Hopefully, a new clean slate will emerge

        for( var j=0; j < listelem.options.length; j++ )

        {

            if(listelem.options[j].selected)

            {

            	//alert("Option Value: " + listelem.options[j].value);

            	parseParentId(listelem.options[j].value);

            }

		}

	}

	return true;

}





/****

    * Function: submitAndCreateDrawing

	* Params:	type, id

	* Purpose:  Figures out if element is a leaf node and invokes the drawing page

	*

****/

function submitAndCreateDrawing(type, id)

{

	for(var i = 0; i < (mdf_array.length - 1); i++)

	{

		if((areStringsSame(mdf_array[i].MDF_ID, id)) &&

			areStringsSame(mdf_array[i].NODE_TYPE, type))

		{

			//Node type must not be replicated with parent node type

			{

				var mdfname = mdf_array[i].MDF_NAME;

       			var drawprdid = mdf_array[i].DRAW_PRD_ID;

       			var drawfilename = mdf_array[i].DRAW_FILE_NAME;

       			var slnumfmtdesc = mdf_array[i].SL_NUM_FMT_D;

       			var slnumfmtloc = mdf_array[i].SL_NUM_LOC_D;

       			invokeResultPage(mdfname, drawprdid, drawfilename,

								slnumfmtdesc, slnumfmtloc);

			}

		}

	}



	return true;

}



/****

    * Function: parseParentId

    * Params: 	id

	* Purpose:  Parses the value of selected element and decided on further

	*			display or invoke the drawing page depending on the leaf

	*			characteristics

	*

****/

function parseParentId(id)

{



	//Split the id into corresponding node type, parent id & leaf flag

	var idarr = id.split(/:/); //idarr[0] = node type, [1] = parentid

								//[2] = is_leaf

	var arr = new Array();

	var i = 0;



	//If the chosen element is a leaf, then move to next screen, no processing

	//needed

	if(idarr[2] == 1)

	{

		//alert("This is a leaf node");

		//Invoke a direct submit call for the next drawing page

		submitAndCreateDrawing(idarr[0], idarr[1]);

	}

	else //Not a leaf, Get all the child node-types

	{

		for(i = 0; i < (mdf_array.length - 1); i++)

		{

			//Take care: Node type must not be replicated with parent node type

			if((areStringsSame(mdf_array[i].MDF_PARENT_ID, idarr[1])) &&

				(!areStringsSame(mdf_array[i].NODE_TYPE, idarr[0])))

			{



				//Do another check at this point 

				//if the is_leaf is not 1 , but a drawing still exists

				//then submit the form

				//if(subStringExists(mdf_array[i].DRAW_FILE_NAME, ".gif", -1))

				//{

				//	submitAndCreateDrawing(	mdf_array[i].NODE_TYPE, 

				//							mdf_array[i].MDF_ID);

				//}

				//else

				//{

				//}



				if(!is_in_array(mdf_array[i].NODE_TYPE, arr))

				{

					//alert("Nodes types match, its a " + mdf_array[i].NODE_TYPE);

					arr[arr.length] = mdf_array[i].NODE_TYPE;

				}

			}

		}

	}



	//alert("array length: " + arr.length);



	//node types are sorted as Category, Type, Series, Model strictly

	//for display order if more than one drop-down needs to be

	//created

	var str = "";



	var sorted = sortNodeTypes(arr);



	//alert("sorted length: " + sorted.length);

	//Now create the drop downs

	for(i = 0; i < (sorted.length); i++)

	{

		str += sorted[i] + ", ";

		createLayerObjects(sorted[i], idarr[1]);

	}



	//alert("Sorted Types: " + str);

	return;

}



/****

    * Function: sortNodeTypes(arr)

	* Purpose:  Sorts the node types if more than one peer is found

	*			Node types are sorted as Category, Type, Series, Model

	*			strictly for display order if more than one drop-down

	*			needs to be created

	*

****/

function sortNodeTypes(arr)

{

	var nodetypes = ["Category", "Type", "Series", "Model"];

	var tmpStr;



	for(var i = 1; i < arr.length; i++)

	{

		tmpStr = arr[i];

		var j = i;



		var prevCount = 0;

		var nextCount = 0;

		for(var k = 0; k < nodetypes.length; k++)

		{

			if(arr[j - 1] == nodetypes[k])

				prevCount = k;

			else if(arr[j] == nodetypes[k])

				nextCount = k;

		}

		if(prevCount > nextCount)

		{

			//flip the values

			arr[j] = arr[j-1];

			arr[j - 1] = tmpStr;

		}



	}



	return arr;

}



/****

	*

	*	Layer and Frame Procssing , needed to display and hide layers

	*

****/



/****

    * Function: toggle_frameset

	* Params:	id, maxcount

	* Purpose:  Toggle b/w hide and display

	*

****/

function toggle_frameset(id, maxcount)

{

	var framename = "mdfinfo";

	hide_all_frames(1, 9, framename);

	framename += id;

	display_frame(framename );

}



/****

    * Function: display_frame(framename )

	* Purpose:  makes chosen frame visible

	*

****/

function display_frame(framename )

{

	var frame;

	var isIE=document.all?true:false;

	var isNS4=document.layers?true:false;

	var isNS6=navigator.userAgent.indexOf("Gecko")!=-1?true:false;



	//Do the NS Vs IE check

	if(isNS4)

	{

		frame = document.layers[framename];

		if(frame)

		{

			frame.visibility = "visible";

			frame.zIndex = 2;

		}

	}

	else if(isNS6 || isIE)

	{

		frame = document.getElementById(framename);



		if(frame)

		{

			frame.style.visibility = "visible";

			frame.style.zIndex = 2;

		}

	}



	//return true;

}



/****

    * Function: display_frame_at_posn

	* Params framename, toppos

	* Purpose:  makes frame visible at specified position

	*

****/

function display_frame_at_posn(framename, toppos)

{

	var isIE=document.all?true:false;

	var isNS4=document.layers?true:false;

	var isNS6=navigator.userAgent.indexOf("Gecko")!=-1?true:false;

	var frame;

	var top = ((toppos) * 25) + 5;

	top += "%";



	//Do the NS Vs IE check

	if(isNS4)

	{

		frame = document.layers[framename];

		if(frame)

		{

			frame.visibility = "visible";

			frame.top = top;

			frame.zIndex = 2;

		}

	}else if(isNS6 || isIE)

	{

		frame = document.getElementById(framename);



		if(frame)

		{

			frame.style.visibility = "visible";

			//frame.style.top = "\"" + top + "\"";;

			frame.style.top = 50;

			frame.style.zIndex = 2;

		}

	}



	return true;

}



/****

    * Function: hide_frame

	* Params:	framename

	* Purpose:  Hides the specified frame

	*

****/

function hide_frame(framename )

{

	var frame;

	var isIE=document.all?true:false;

	var isNS4=document.layers?true:false;

	var isNS6=navigator.userAgent.indexOf("Gecko")!=-1?true:false;

	//Do the NS Vs IE check

	if(isNS4)

	{

		frame = document.layers[framename];

		if(frame)

		{

			frame.visibility = "hidden";

			frame.zIndex = -1;

		}

	}else if(isNS6 || isIE)

	{

		frame = document.getElementById(framename);

		if(frame)

		{

			frame.style.visibility = "hidden";

			frame.style.zIndex = -1;

		}

	}





	//return true;

}



/****

    * Function: hide_all_frames

	* fromcount, tocount, name

	* Purpose:  Inits the first drop-down menu

	*

****/

function hide_all_frames(fromcount, tocount, name)

{



	//Convert maxcount into a num if not already one

	//maxcount += 0;

	var frame;

	var isIE=document.all?true:false;

	var isNS4=document.layers?true:false;

	var isNS6=navigator.userAgent.indexOf("Gecko")!=-1?true:false;

	var framename = name;

	for(var i = fromcount; i <= tocount; i++)

	{

		framename += i;

		//Do the NS Vs IE check

		if(isNS4)

		{

			frame = document.layers[framename];

			if(frame)

			{

				frame.visibility = "hidden";

			}

		}else if(isNS6 || isIE)

		{

			frame = document.getElementById(framename);

			if(frame)

			{

				frame.style.visibility = "hidden";

			}

		}



		framename = name;

	}

}







/****

	*

	*	List Processing Functions

	*

****/

function isAlreadyInList(val, listelem)

{



	var val1 = val.toLowerCase();

	var val2;



	for(var i = 0; i < listelem.options.length; i++)

	{

		val2 = listelem.options[i].value.toLowerCase();

		if(val1 == val2)

		{

			return true;

		}

	}



	return false;

}



function addTextToList(optstr,optval, listelem, selectFlag)

{

	//Remove any blank elements added

	removeBlankElementsFromList(listelem);



	if( ( optstr != null ) && (optstr != "" ) )

	{

		var opt = new Option( optstr, optval);

		listelem.options.length++;

		var i = listelem.options.length -1;

		if (i < 0)

			i = 0;

		listelem.options[i].value =  optval;

		listelem.options[i].text =  optstr;



		if(selectFlag || (i == 0))

			listelem.options[i].selected = true;



	}



	return true;

}



function removeBlankElementsFromList(listelem)

{

	for( var i = 0 ; i < listelem.options.length; i++)

	{



		if( (listelem.options[i].name == "") ||

		    (listelem.options[i].value == "") ||

		    (listelem.options[i].value == "__BLANK__") )

		{

			listelem.options[i] = null;

			i--;

		}

	}



	//Reset list size if last element has been removed

	//if( listelem.options.length == 0)

	//	setListFieldLength(listelem, 5);

	return true;

}





/****

    * Utility Functions

	*

****/



<!--



function trimStr(s)

{

   return rtrim(ltrim(s));

}



function ltrim(str)

{

   var whitespace = new String(" \t\n\r");



   var s = new String(str);



   if (whitespace.indexOf(s.charAt(0)) != -1) {

      // We have a string with leading blank(s)...



      var j=0, i = s.length;



      // Iterate from the far left of string until we

      // don't have any more whitespace...

      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)

         j++;



      // Get the substring from the first non-whitespace

      // character to the end of the string...

      s = s.substring(j, i);

   }

   return s;

}



function rtrim(str)

{

   var whitespace = new String(" \t\n\r");



   var s = new String(str);



   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {

      // We have a string with trailing blank(s)...



      var i = s.length - 1;       // Get length of string



      // Iterate from the far right of string until we

      // don't have any more whitespace...

      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)

         i--;





      // Get the substring from the front of the string to

      // where the last non-whitespace character is...

      s = s.substring(0, i+1);

   }



   return s;

}



function stripAllSpaces(s)

{

        s = s.split(" ").join("");

        return s;

}



function areStringsSame( s1, s2)

{

    //precautions

    if (s1+"" == "undefined" || s1 == null || s2+"" == "undefined" || s2 == null)

        return false;



    // Make sure the argument is a string

    s1 += "";

    s2 += "";



    if(s1 == s2)

        return true ;



    return false;

}



function subStringExists(s, sub, start)

{

    // Return immediately if an invalid value was passed in

    if (s+"" == "undefined" || s == null || sub+"" == "undefined" || sub == null)

        return false;



    // Make sure the argument is a string

    s += "";





    //if start is -1 ignore arg

    if(start < 0){

        if (-1 == s.indexOf(sub))

            return false;

        else

            return true;

    }else

    {

        if (-1 == s.indexOf(sub, start))

            return false;

        else

            return true;

    }



    return false;

}





function  is_in_array(val, arr)

{

	for(var i = 0; i < arr.length; i++)

	{

		if(arr[i] == val)

			return true;

	}

	return false;

}





function removeAllFromList(listelem)

{

	while ( listelem.options.length ) listelem.options[0] = null;



/*

    for( var i = 0 ; i < listelem.options.length; i++)

    {

        listelem.options[i] = null;

    }



    //Remove any blank elements added

    removeBlankElementsFromList(listelem);

*/

    return true;

}



function checkData(thisForm, name)

{

	//Remove leading and trailing spaces

    if(trimStr(name) == "")

    {

        alert(errEmptyStrMsg);

        return false;

    }

    else

    {
        thisForm.submit();

    }

    return true;

}

