var links_by_name = { initialized:false };

function set_focus_on_first_item()
//##############################################################
// 10-APR-2006 SI - created
// sets the focus to the first item on the page, if any.
{
	// don't do anything if there is no form
	if ( ! has_pes_form() ) return;


	// 06-AUG-2009 SI - CR 6866377
	// don't do anything for IE because it won't focus on all items
	if ( /msie/i.test(navigator.userAgent) ) return;


	var x = get_first_field();
	if (x)
	{
                // get location of x 
                // if x is off the bottom of the page, 
                // dont set focus on x
                ypos = findPosY(x);
                height = getWindowSize(1);
                if (ypos < height) {
                    x.focus();
                }

	}

	//debug("set_focus_on_first_item() x="+x);
}

function get_jato_link_by_name(name)
//##############################################################
// 11-may-2005 SI - created
// lets you get any link on the page, if you know its name
{
	// cannot do this because jato:href doesn't support ID
	//var link = document.getElementById("status_link");

	init_links_by_name();
	var link = links_by_name[name];
	//debug("name=" + name + " link=" + link);
	return link;
}

function init_links_by_name()
//##############################################################
{
	if (links_by_name.initialized)
	{
		return;
	}

	for (var i=0; i<document.links.length; i++)
	{
		var link = document.links[i];
		var name = link.href;
		var un_mucked_name = un_muck_link_name(name);
		if (! un_mucked_name) continue;
		//debug("name=" + name + " un_mucked_name=" + un_mucked_name);
		links_by_name[un_mucked_name] = link;
	}

	links_by_name.initialized = true;
}

function un_muck_link_name(name)
//##############################################################
// 11-may-2005 SI - used by init_links_by_name
// 16-may-2005 SI - updated to handle links like the following:
// /ifpp/pcc/PCCx10x0xWorkFlow;jsessionid=3A2D06?PCCx10x0xWorkFlow.doSAVE_goSTATUS=&jato.pageSession=
{
	// remove everything up to the "?" 
	var question = name.indexOf("?");
	if (question != -1)
	{
		name = name.substr(question+1);
	}

	// remove everything after an equals sign 
	var equal = name.indexOf("=");
	if (equal != -1)
	{
		name = name.substr(0,equal);
	}

	name = un_muck_name(name);
	return name;
}

function prefill_url(obj)
//##############################################################
{
	if (obj.value == "")
	{
		obj.value = "http://";
	}
}

function is_empty(val)
//##############################################################
{
	return (val == undefined || ! val.match(/\S/) )
}

function deselect_blank_first_item_in_menu(name)
//##############################################################
{
	var menu = get_pes_obj(name);
	if (menu == undefined)
	{
		debug("named menu is null! " + name);
		return;
	}
	deselect_blank_first_item(menu);
}

function deselect_blank_first_item(menu_obj)
//##############################################################
// 31-MAY-2005 SI - quato 3289
// Do not allow the blank value "Select One" entry to be selected in multi-select menus.
// Because the validator relies on get_pes_val() which only
// returns the first value, the REQUIRED rule fires even if multiple values are selected.
// Also, the DbApi would probably complain about trying to insert the blank value.
{
	if (menu_obj == undefined)
	{
		debug("menu_obj is null!");
		return;
	}

	// only execute if this menu allows
	// multiple options to be selected at the same time
	var type = menu_obj.type;
	if (! type.match(/multi/i) )
	{
		// abort
		return;
	}

	var opt		= menu_obj.options[0];
	var value	= opt.value;
	var is_selected	= opt.selected;

	if (is_empty(value) && is_selected)
	{
		//debug("the value for 0th selected is true");
		opt.selected = false;
	}

}

function showPopupText (id) {
	var HTML_Module = "/ifpp/tam/SectionHelpText.jsp?help_id="+id;
	window.open(HTML_Module, "Help", "width=600,height=400,resizeable=yes,scrollbars=yes");

}

function textAreaLimitter(element_name, maxlimit) {  
  
          var element=get_pes_obj(element_name);
          if (element.value.length >  maxlimit ) {  
            element.value= element.value.substring(0,maxlimit );             
          }
}

// TODO - erase the 2 functions below - use pes:country tag instead

function update_memory(name)
//##############################################################
{
	var list = get_pes_obj(name);
	var selectedI = list.selectedIndex;
	var value = list[selectedI].value;
//	alert("selectedI("+selectedI+")value("+value+")");
	var obj = get_pes_obj(name+"_hidden");
	obj.value = value;
//	alert("obj("+obj.value+")");
}

function reload_from_memory(countryTag,name)
//##############################################################
{
	var tagName = get_pes_obj(name);
	var memory = get_pes_obj(name+"_hidden");
//	alert("memory("+memory.value+")");
	var country = get_pes_obj(countryTag);
	var selectedI = country.selectedIndex;
	var countryVal = country[selectedI].value;

	if(memory == null || memory.value =="") 
	{   return;
    }

	createState(countryTag,name);
	for(var j=0; j<tagName.options.length;j++)
	{
		if(memory.value == tagName[j].value)
		{
//	alert("1memory("+memory.value+")");
			tagName.selectedIndex = j;
			break;
		}
	}

}


// TODO - erase the above 2 functions - use pes:country tag instead


// -->

// Returns browser window width if input is 0 , and height if input is non zero
function getWindowSize(n) {
    //alert("in getWindowSize");
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        //alert("type is number");
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return (n==0) ? myWidth : myHeight;
    
}

// Get the x coordinate of an element relative to the browser window (top left is 0,0)
// The offsetLeft property of an element gives the x coordinate of that element
// relative to its offsetParent element, so to find out its x coordinate relative to
// the page, continue adding the offsetLefts as long as there are offsetParents.
// Use offset.x if offsetParent is not supported
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// Get the y coordinate of an element relative to the browser window (top left is 0,0)
// The offsetTop property of an element gives the y coordinate of that element
// relative to its offsetParent element, so to find out its y coordinate relative to
// the page, continue adding the offsetTops as long as there are offsetParents.
// Use offset.y if offsetParent is not supported
function findPosY(obj)
{
        //alert("in findPosY");
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
        //alert("curtop is " + curtop);
	return curtop;
}

