//
// $Header: arucommon.js@@/main/14 \
// Checked in on Fri Nov  7 08:47:31 PST 2008 by rkada \
// Copyright (c) 2002, 2008 by Oracle Corporation. All Rights Reserved. \
// $
//

//
// Detect browser capabilities
//

// for Opera 6+
var OPERA = (window.opera) ? 1 : 0;
var IE4 = (!window.opera &&  document.all && !document.getElementById) ? 1 : 0;
// for IE5+
var IE5 = (!window.opera &&  document.all &&  document.getElementById) ? 1 : 0;
var NS4 = (!window.opera && !document.all && !document.getElementById &&
             document.layers) ? 1 : 0;
// for Mozila and NS6+
var NS6 = (!window.opera && !document.all &&  document.getElementById) ? 1 : 0;
// for the standard W3C Level 1 DOM, supported by NS6+, Mozilla, IE5+, Opera7+
var DOM = (document.getElementById) ? 1 : 0;
var V4  = (NS4 || IE4) ? 1 : 0;

//Function to disable the href parameter in a link whose id is provided.
function disable_link_by_id (id)
{
   // Reset the href to '#' in the given element.
   document.getElementById(id).href = '#';
   document.getElementById(id).onclick = "";
}

//
// Dynamically change form method based on input data.
// GET method has a limit on amount of data that can be passed.
//
function dynamic_form_method(theForm) {


    // Process only forms that have method as GET.
    if (theForm.method == "POST") return;

    var formElems = theForm.elements;
    var length = 0;

    for (i = 0; i < formElems.length; i++) {

	if (formElems[i].value)
	{
		length += formElems[i].value.length;
	}

	// If the total input data is over 1K (1024 bytes limit) then
    	// set the form method to POST.
	if (length > 1024) {
		theForm.method = 'POST';
		break;
    	}
    }
}

//
// Form submit function when the users presses the ENTER key.
//
function aruforms_enter_submit(DnEvents) {

  //
  // Detect browser.
  //
  k = ( DnEvents.keyCode ) ? DnEvents.keyCode : DnEvents.which;
  elem = (DnEvents.srcElement) ? DnEvents.srcElement : DnEvents.target;

  //
  // If RETURN has been pressed, check whether
  // element is a textarea or has any value.
  // Also, ignore if it is a select box.
  //
  if (k == 13)
  {
    if ((elem.type == "textarea") || (is_empty(elem.value)) ||
        (elem.type == "select-one") || (elem.type == "select-multiple"))
    {
      return false;
    }

    var theForm = document.forms[0];
    dynamic_form_method(theForm);

    document.forms[0].submit();
  }
}


//
// Form submit function.
//
function aruforms_submit(target_form, form_number) {

  if (!form_number)
  {
     form_number = 0;
  }

  var theForm = document.forms[form_number];
  dynamic_form_method(theForm);

  if ( target_form )
  {
    var old_action = document.forms[form_number].action;
    
    if (target_form.indexOf('/') == -1)
      {
        var pos = old_action.lastIndexOf('/');
        target_form  = old_action.substr(0,pos+1) + target_form;
      }
    document.forms[form_number].action = target_form; 
  }
  document.forms[form_number].submit();
}

//
// This function stops the control propagation to higher levels in the
// hierarchy of event handlers.
//
function stop_event_propagation(evt) {
 //
 // cancelBubble for Microsoft Model; stopPropagation for W3C model
 //
 evt.cancelBubble = true;              
 if (evt.stopPropagation) evt.stopPropagation();
}

//
// Cookie utilities
//
function IsCookieSet (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 1;
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return 0;
}

function SetCookie(name, value, path){
  if (IsCookieSet(name) == 0) {
        document.cookie = name + "=" + escape (value) +
        ((path) ? "; path=" + path : "");
  }
}

//
// Function to dynamically populate child popup.
//
function popup_populate(parent_abbr, child_abbr) {

  var parentRef = eval("document.forms[0]." + parent_abbr);
  var parentVal;
  if (parentRef.type != "hidden") {

     if(parentRef.selectedIndex != -1)
     {
         parentVal = parentRef.options[parentRef.selectedIndex].value;
     }

     //	
     // if separator item like '----E-Business suite----' is selected it goes 
     // back  to 'select a product family'
     //
     if (parentRef.selectedIndex != -1 &&  parentVal == '' &&
         parentRef.type == 'select-one') {
        parentRef.selectedIndex = 0;
     }
  }
  else {parentVal = parentRef.value;}
  var childRef = eval("document.forms[0]." + child_abbr);
  var childVal;
  var childValselected;	

  var childName = childRef.name;
  var noneName = "document.forms[0]." + childName + "_none";
  var noneRef = eval("document.forms[0]." + childName + "_none");
  if (parentRef.selectedIndex != -1 && parentVal == '' && noneRef)
  {
    childRef.options.length = 1;
    childRef.options[0].value = '';
    childRef.options[0].text  = noneRef.value;
    childRef.selectedIndex = 0;
    return;
  }
                         
  //
  // Code to frame a regular expression for the child items that are selected,
  // considering the fact that selectedIndex property will not work for
  // MULTIPLE choices, we need to set the child elements that were selected.
  //
  if (childRef.options.length > 0)
  {
     for (var i = 0; i < childRef.options.length; i++) {

	     if ( (childRef.options[i].selected) &&
			(childRef.options[i].value != '') )
	     {			
	        childVal += '^'+childRef.options[i].value+'$'+'|';
	     }	
     }
  }
  var str = ''+childVal+'';
  var newre = str.replace( /undefined/,'');
  newre = newre.substr(0,newre.length - 1);

  var re = new RegExp(newre);

  var sNewArray;
  var sSwapArray = [];
  var sTempArray = [];
  var child_values = eval("orig_values_"+child_abbr);

  //
  // If we are setting the LOV to its original state then get its
  // values from an array storing the original values.
  //
  if (!parentVal && parentRef.selectedIndex != -1) {

        childRef.options.length = child_values.length;

        for (var i = 0; i < child_values.length; i++) 
        {
  	    childRef.options[i].value = child_values[i];
            childRef.options[i].text =
            eval("orig_"+child_abbr+"_"+child_values[i]) ;
                  
            if (re.test(child_values[i]))
             childRef.options[i].selected = true;
        }
     
  }
  else
  {
     if (parentRef.type != "hidden") {
     //
     // Loop through the parent array, for each selected element in the
     // parent array fetch the corresponding children and populate
     // unique elements into the sSwapArray.
     // This part handles single and multiple parent element selection.
     //
     for (var i = 0; i < parentRef.options.length; i++)
     {
       if ( (parentRef.options[i].selected) &&
		(parentRef.options[i].value != '') )
       {
	  var	 parentVal = parentRef.options[i].value;
	  if (eval("typeof(c_"+parent_abbr+"_"+child_abbr+"_"+parentVal+")")
	                       != "undefined")
	  { 	
	  	sTempArray =
			eval("c_"+parent_abbr+"_"+child_abbr+"_"+parentVal);
		for (var j = 0; j < sTempArray.length; j++)
     		{
		  if ( element_exists(sSwapArray,sTempArray[j]))
		  {
			continue;
		  }
		  sSwapArray = sSwapArray.concat(sTempArray[j]);
		}
	  }
       }
     }
	sNewArray = eval(sSwapArray);
     }
     else {
	if (eval("typeof(c_"+parent_abbr+"_"+child_abbr+"_"+parentVal+")")
                       != "undefined")
	sNewArray = eval("c_"+parent_abbr+"_"+child_abbr+"_"+parentVal);
     }
  }		  

  //
  // Populate the child select list from the corresponding js array element.
  //
  if(sNewArray) {

     childRef.options.length = sNewArray.length;
        
     for (var i = 0; i < sNewArray.length; i++) {
        childRef.options[i].value = sNewArray[i];

        //
        //Have to get the text from the object ObjectText.
        //
        childRef.options[i].text =
              eval("orig_"+child_abbr+"_"+sNewArray[i]);

        //
        //Slight hack: adds in <prod family> to end of "any product" option
        //
        if (parentVal && (childRef.options[i].text == "any product") ||
                         (childRef.options[i].text == "select a product")) {
           childRef.options[i].text += " in " +
               parentRef.options[parentRef.selectedIndex].text;
        }
	else if (parentVal && childRef.options[i].text ==
		"select a product before selecting a release") {
	   childRef.options[i].text = "select a release in " +
               parentRef.options[parentRef.selectedIndex].text;
        }
	else if (parentVal && childRef.options[i].text ==
		"Choose an Organization first") {
	   childRef.options[i].text = "responsibilities in " +
               parentRef.options[parentRef.selectedIndex].text;
        }

	//
        // Automatically set the current child/children in the select list.
        //
	var str = childRef.options[i].value;
	childRef.options[i].selected = false;
	if ( (re.test(str)) && (newre.length > 0) )
	{
          childRef.options[i].selected = true;
	  childValselected = true;
	}
     }
  }
  //
  // Select the initial option if childValselected is not defined.
  //
  if (!childValselected)
     childRef.selectedIndex = 0;
}

//
// Function to check if an element exists in an array.
//
function element_exists(sArray,sElement)
{
	for (var j = 0; j < sArray.length; j++)
	{
		if ( sArray[j] == sElement )
		{
			return 1;
		}
	}
	return 0;
}

//
// Help window utilities
//
function ARUHelp(url) {
  var w =
      window.open(url, 'ARUHelp', 'resizable,scrollbars,width=300,height=300');
  w.focus();
}

function ARUHelp2(url) {
  var w =
      window.open(url, 'ARUHelp', 'resizable,scrollbars,width=690,height=700');
  w.focus();
}

//
// Calendar/DatePicker utility
//
var DatePicker_win;

function getDatePicker(field, time)
{
  if (NS4) window.captureEvents(Event.FOCUS);
  window.onfocus = function()
    {if (DatePicker_win && DatePicker_win.close) DatePicker_win.close();};

  var d = eval("document.forms[0]."+field);

  // For bug 3890372 - in Aruchkin, there is a hidden field with the same
  // name for every field so that the values can be carried from page to
  // page through cgi parameters.
  var val = '';
  if (typeof(d.length) == "undefined" || d[0] == undefined) {
    val = d.value;
  }
  else {
    for (var i = 0; i < d.length; i++) {
      if (d[i].type != "hidden") {
        val = d[i].value;
        break;
      }
    }
  }
  
  var u = '/WebApp/DatePicker/get?field='+field+'&datetime='+val+
          '&time='+time;
  var w = window.open(u, 'DatePicker',
                      'resizable,scrollbars,width=320,height=300');

  if (w.opener == null) w.opener = self;
  DatePicker_win = w;
  w.focus();
}

function stopAction(event)
{
   if((navigator.appName.indexOf('Microsoft')>-1) ||
      (navigator.appName.indexOf('MSIE')>-1))
    event.returnValue=false;
   else
    event.preventDefault(false);
}

function getDatePickerDyn(field, time, event)
{
  var pos       = field.lastIndexOf('_');
  var filed_num = field.substr(pos+1) ;
  field         = field.substr(0,pos+1);
  filed_num     = filed_num - 1;
  field         = field + filed_num;
  getDatePicker(field,time);
  stopAction(event); 
}

function is_positive_integer(inputVal) {
  n = parseInt(inputVal);
  if (isNaN(n) || n < 0)
    return false;
  else
    return true;
}


function is_empty(str) {
  if (str == null || str == "") {
    return true
  }
  return false
}

//
// Used to check and uncheck checkboxes in a form.
// If the checkbox name begins with "prefix_str", then the check box will
// be checked or unchecked based on the boolean "check" parameter.
//
function set_checkboxes(form, prefix_str, check) {
   var offset1 = prefix_str.length;
   for (i=0; i< form.elements.length; i++) {
      if (form.elements[i].type == 'checkbox') {
         if (form.elements[i].name.substring(0, offset1) == prefix_str) {
            form.elements[i].checked = check;
         }
      } 
   }
}

//
// Alert for Nav popups when the selected range is very large.
//
function NavAlert(msg) {  
  arrayOfvalues = msg.split(":");

  if ( arrayOfvalues[1] > 500 ) {
    alert('Note : You are selecting a large range to display. \n'+
          'Depending on your browser and system resources, the\n'+
          'resulting page could take a long time to render.');
  }
}       


//
// Prints Header if a page is opened without a frame
// (page without toolbar)
//
function ARUHeader(header)
{
var myHeaderText = header;
if (! myHeaderText )
{
	myHeaderText = 'Automated Release Updates';
}

    document.write(
"<table width=100% border=0 cellpadding=0 cellspacing=0>" +
"<tr>" +
"<td width=100% height=71 background='/olaf/images/headerBg.jpg'>" +
"&nbsp;<p>&nbsp;&nbsp;" +
"<img src='/olaf/images/FNDSSCORP.gif'>&nbsp;" +
"<font class=OraBrandHeader>" + myHeaderText + "</font>" +
"&nbsp;" +
"<div align=right>" +
"<a href='/' class=OraGlobalLink>Home</a>" +
"&nbsp;&nbsp;" +
"<a href=\"/search/search.html\" target='_blank' class=OraGlobalLink>" +
"Help</a>" +
"&nbsp;&nbsp;" +
"</div>" +
"</td>" +
"</tr>" +
"</table>"
    );
}

//  Returns true if s is an Integer.
function isInteger(s)
{
        var reInteger = /^\d+$/;        
        return reInteger.test(s);
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace(s)
{
        var reWhitespace = /^\s+$/;
        // Is s empty?
        return (is_empty(s) || reWhitespace.test(s));
}

//
// Handle ENTER key press in Locate file field.
//
function locate_file_enter(DnEvents) {

  //
  // Detect browser.
  //
  k = ( DnEvents.keyCode ) ? DnEvents.keyCode : DnEvents.which;

  //
  // Set hidden field if user presses ENTER key.
  //
  if (k == 13)
  {
	document.view.find_button.value='find';
  }
}

//
// Set Focus on to specified anchor.
//
function focus_anchor(anchor_name) {

   anchor_exists = 0;
   for (i=0;i<document.anchors.length;i++)
   { 
      if (document.anchors[i].name == anchor_name) 
      {
        anchor_exists = 1;
        break;
      }
   }
   if (anchor_exists)
   {
      document.location.hash=anchor_name;
      return true;
   }
   return false;
}	

function callUrl(form, url, button)
{
   var qry_str = '';
   qry_str = url+form[button].options[form[button].selectedIndex].value;
   window.location = qry_str;
   return true; 
}

//
// This function validates that nor more niether less than two check boxes are
// clicked on the form.
//


function check_checkboxes(form,link,url)
{
   var count = 0;
   var qry_str = '';

   for (i=0; i< form.elements.length; i++)
   {
      if (form.elements[i].type == 'checkbox')
      {
         if (form.elements[i].checked)
         {
             count++;

             if (count > 1)
             {
                 qry_str = url+'?hist_id2='+qry_str+'&hist_id1=';

              }
             qry_str = qry_str+form.elements[i].value;

         }
      }
   }
   if((count == 0) || (count >2) || (count < 2))
   {
      alert("Please click two check boxes to find the difference.");
      return false;
   }
   else
   {
       link.href = qry_str;
       return true;
   }

}

function addSearch(host)
{
  if ((typeof window.sidebar == "object") && (typeof
  window.sidebar.addSearchEngine == "function"))
  {
	window.sidebar.addSearchEngine(
      	"http://" + host + "/plugin.src",
      	"http://" + host + "/plugin.gif",
      	"ISD",
      	"General");
  }
  else
  {
      alert("Netscape 6, Mozilla, or Firefox is needed to install the "+
            "ISD Search Plugin to your browser");
  }
}


//
// This function is used by setRowColor() to find table cells (TD) node
//
function findNode(startingNode, tagName)
{
  //
  // on Firefox, the TD node might not be the firstChild node of the TR node
  //
  myElement=startingNode;
  var i=0;
  while (myElement && (!myElement.tagName ||
 	(myElement.tagName && myElement.tagName!=tagName)))
  {
    myElement=startingNode.childNodes[i];
    i++;
  }  
  if (myElement && myElement.tagName && myElement.tagName==tagName)
  {
    return myElement;
  }
  //
  // on IE, the TD node might be the firstChild node of the TR node  
  //
  else if (startingNode.firstChild)
    	return findNode(startingNode.firstChild, tagName);
  return 0;
}

//
// Highlight table row.
// 

function setRowColor(myElement, Color)
{
  while (myElement && (myElement.tagName && myElement.tagName!="TR") ||
	!myElement.tagName)
  {
    myElement=myElement.parentNode;
  }
  //
  // If you don't want a particular row to be highlighted, set it's id to
  // "header"
  //
  if (!myElement || (myElement && myElement.id && myElement.id=="header") )
    return;

  //
  // Highlight every cell on the row
  //
  if (myElement)
  {
    var tableRow=myElement;

    //
    // myElement is a <TR>, then find the first TD
    //
    var tableCell=findNode(myElement, "TD");    

    //
    // Loop through every sibling (a sibling of a cell should be a cell)
    // We then highlight every siblings
    //
    while (tableCell)
    {
      //
      // Make sure it's actually a cell (a TD)
      //
      if (tableCell.tagName=="TD")
      {
        //
        // Assign the background color
        //
        tableCell.style["backgroundColor"]=Color;
        
      }
      //
      // Go to the next cell in the row
      //
      tableCell=tableCell.nextSibling;
    }
  }
}


function keyEnter(e)
{
  //
  // Detect browser.
  //
  k = ( e.keyCode ) ? e.keyCode : e.which;

  //
  // Set hidden field if user presses ENTER key.
  //
  if (k == 13)
  {
	return true;
  }
  return false;
}

function check_uncheck(form)
{
   if (form.toggle_all.checked == true)
   {
      for (i = 0; i < form.elements.length; i++)
      {
         if (form.elements[i].checked == false)
         {
           form.toggle_all.checked = false;
           break;
         }
      }
   }
}

//
// Highlight table row.
// Introduced in bug 7282595
// 

function setRowClass(myElement, Class, isSet)
{
  while (myElement && (myElement.tagName && myElement.tagName!="TR") ||
        !myElement.tagName)
  {
    myElement=myElement.parentNode;
  }
  //
  // If you don't want a particular row to be highlighted, set it's id to
  // "header"
  //
  if (!myElement || (myElement && myElement.id && myElement.id=="header") )
    return;

  //
  // Highlight every cell on the row
  //
  if (myElement)
  {
    var tableRow=myElement;

    //
    // myElement is a <TR>, then find the first TD
    //
    var tableCell=findNode(myElement, "TD");    

    //
    // Loop through every sibling (a sibling of a cell should be a cell)
    // We then highlight every siblings
    //
    while (tableCell)
    {
      //
      // Make sure it's actually a cell (a TD)
      //
      if (tableCell.tagName=="TD")
      {
        //
        // Assign the highlight class
        //
        var thisClass = tableCell.className;
        if(isSet)
        {
            tableCell.className = thisClass + ' ' + Class;
        }
        else
        {
            tableCell.className = thisClass.replace(' '+Class, '');
        }
      }
      //
      // Go to the next cell in the row
      //
      tableCell=tableCell.nextSibling;
    }
  }
}

//
// Highlight the Elements
// Introduced in bug 7282595
//
function setClass(myElement, Class)
{
    //
    // Assign the 'Class' class to 'myElement'
    //
    myElement.className = Class;
}


//
// trim funcation
//
function trim(s)
{
   return rtrim(ltrim(s));
}

//
// left trim funcation
//
function ltrim(s)
{
   var l=0;
   while(l < s.length && s[l] == ' ')
   {	
	   l++; 
   }
   return s.substring(l, s.length);
}

//
// right trim funcation
//

function rtrim(s)
{
   var r = s.length -1;
   while(r > 0 && s[r] == ' ')
   {   
      r -=1;
   }
   return s.substring(0, r+1);
}

//
// Clear the quick query text box . 
//
function clearDefaultText(a)
{
   var val = a.value;
   val = trim(val);
   if (val.indexOf('Search....') !=-1 )
   {
      a.value     = "";
   }
   a.className = "OraSearch";
}

//
// Set the quick query text box to Default text.
//
function setDefaultText(a)
{
   var val = a.value;
   val = trim(val);
   if (val == '' )
   {
      a.value     = "Search....";
      a.className = "WaterMark";
   }
}
