//helper functions

//returns an element's object (by id)
function g(id)
{
	if(typeof id=='object') return(id);
	return(document.getElementById(id));
}

//returns multiple element array (by name)
function gg(id)
{
	if(typeof id=='object') return(id);
	return(document.getElementsByName(id));
}

//returns an element's .style object
function s(id)
{
	return(g(id).style);
}

//returns an element's .value
function v(id)
{
	return(g(id).value);
}

//get/set an element's attribute value (leave val empty to get the value)
function a(id,attr,val)
{
	var o=g(id).attributes[attr];
	if(o)
	{
		if(val) o.value=val;
		else val=o.value;
		return(val);
	}
	return(null);
}

//set browser status line
function pws(val)
{
	if(window.status!='Submitting Form'){window.status=val};
}

//sets the location of a frame to an obj (should probably be a <a> tag)
function setloc(frame,obj)
{
	try{parent.frames[frame].location.href=g(obj).href}catch(e){window.open(g(obj).href)};
}

//reclass specific tag in an element object and it's subelements
//obj=top level object to pull tags
//tag='<tagname>' to reclass
//size=i.e. 'small', 'med', 'large'
//action=i.e. 'over', 'out'
//class and action are dependent upon CSS entries
function reclass(obj,tag,size,action)
{
	var e=obj.getElementsByTagName(tag);
	for(var i=0;i<e.length;i++)
	{
		e[i].className=size+e[i].id+action;
	}
}

//turn elements on/off, returns the string 'none' or ''
function onoff(id)
{
	return(s(id).display=s(id).display=='none'?'':'none');
}

//get exact X offset of an object on the page
function getX(id)
{
	var o=g(id);
	var X=0;
	while(o)
	{
		X+=o.offsetLeft;
		if(!o.offsetParent) break;
		o=o.offsetParent;
	}
	return(X);
}

//get exact Y offset of an object on the page
function getY(id)
{
	var o=g(id);
	var Y=0;
	while(o)
	{
		Y+=o.offsetTop;
		if(!o.offsetParent) break;
		o=o.offsetParent;
	}
	return(Y);
}

//find which radio button selection is checked, returns the object of the individual radio button
function gradio(id)
{
	var o=gg(id);
	for(var y=0;y<o.length;y++)
	{
		if(o[y].checked)
		{
			return(o[y]);
		}
	}
	return(null);
}


//PICKER FUNCTIONS

var picker_win;

//date picker
function picker_date(divtag,updatetag,stdate,format,X,Y)
{
	if(!stdate) stdate="";
	if(!format) format="date";
	if(!X) X=0;
	if(!Y) Y=document.body.scrollTop;
	var hgt=310;
	if(format=="date") hgt=275;
	if(divtag)
	{
		var div=g(divtag);
		if(div.innerHTML=="")
		{
			div.innerHTML="<IFRAME id=\"detailsframe\" width=\"260\" height=\"220\" src=\"/pcrbin/dropdate.exe?action=picker&format="+format+"&date="+stdate+"&PARENT="+updatetag+"&PARENTCLOSE=parent.g('"+divtag+"').innerHTML=''\"></IFRAME>";
			div.style.position="absolute";
			div.style.left=X;
			div.style.zIndex=5;
			div.style.top=Y;
			document.all.detailsframe.height=hgt;
		}
		else
		{
			div.innerHTML="";
		}
	}
	else //use external window
	{
		if(picker_win=='[object]') picker_win.close();
		picker_win=window.open("/pcrbin/dropdate.exe?action=picker&format="+format+"&date="+stdate+"&PARENT="+updatetag+"&PARENTCLOSE=window.close()","_blank","status=no,toolbar=no,location=no,scrollbars=yes,width=250,height="+hgt+",top=0,left=0");
	}
	return(false);
}
function loadXMLDoc(url, handler)
{
	xm = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
	xm.onreadystatechange = handler;
	xm.open("GET", url, true);
	xm.send(null);
}

//replacement for escape includes (+) symbol
function urlencode(arg){
	var urldata=escape(arg).replace(/\+/g,'%2B');return urldata
}

function Trim(s) 
{
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  { s = s.substring(1,s.length); }
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  { s = s.substring(0,s.length-1); }
  return s;
}

function rtrim(argvalue) {
  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }return argvalue;
}

function ltrim(argvalue) {
  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }return argvalue;
}

function echo(arg)
{
	document.write(arg);
}

function keypress(e)
{
	if(!e.which){return e.keyCode}else{return e.which}
}

function submitForm(formname)
{
	try{window.external.AutoCompleteSaveForm(formname)}catch(e){};formname.submit();
}


var resize_hold_onresize;
//use fastwidth="100%" attribute when setting up div's and textareas instead of style="width:100%"
function resizeBoxes(first)
{
	var tags,tag,i,w,count=0;
	try
	{
		tags=document.getElementsByTagName('textarea');
		for(i=0;i<tags.length;i++)
		{
			tag=tags[i];
			w=a(tag,'fastwidth');
			if(w!=null)
			{
				count++;
				if(w.substr(w.length-1,1)=='%')
				{
					s(tag).width=(document.documentElement.offsetWidth*w.substring(0,w.length-1)/100-40)+'px';
				}
				else
				{
					if(first) s(tag).width=w;
				}
			}
		}

		tags=document.getElementsByTagName('div');
		for(i=0;i<tags.length;i++)
		{
			tag=tags[i];
			w=a(tag,'fastwidth');
			if(w!=null)
			{
				count++;
				if(w.substr(w.length-1,1)=='%')
				{
					s(tag).width=(document.documentElement.offsetWidth*w.substring(0,w.length-1)/100-40)+'px';
				}
				else
				{
					if(first) s(tag).width=w;
				}
			}
		}

		if(first)
		{
			resize_hold_onresize=window.onresize;
			if(count>0) window.onresize=resizeBoxes;
		}
		else
		{
			if(resize_hold_onresize) return resize_hold_onresize(); //call next handler
		}
	}
	catch(e) {}
}

//runs javascript pulled from an attribute on each form element, sort of a pseudo event
function form_run(formname,runevent)
{
	var frm=gg(formname);
	var code;
	for(var __i=0;__i<frm[0].elements.length;__i++)
	{
		var element=frm[0].elements[__i];
		code=element.getAttribute(runevent);
		if(code)
		{
			try
			{
				eval(code.replace(/this\./g,'document.'+frm[0].name+'['+__i+'].'));
			}
			catch(e) {}
		}
	}
}

function htmlencode(arg){
	var urldata=arg.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'&quot;');return urldata
}

var topPCRWindow=GetTopWindow(); //handle to the top level window, should we need it for external notifications that only watch that window (like Outlook)
function GetTopWindow()
{
	try
	{
		if(opener)
		{
			//alert('from opener');
			return opener.topPCRWindow;
		}
		else
		{
			if(parent)
			{
				if(parent.topPCRWindow==null)
				//if(parent.document.title=='PCRecruiter HTML Interface') //external title
				{
					//alert('from parent!');
					return parent.document;
				}
				else
				{
					//alert('from parent:'+parent.document.title);
					return parent.topPCRWindow;
				}
			}
			else
			{
				//alert('from ME!');
				return document;
			}
		}
	}
	catch(e) {}
	return null;
}

//notify external container by changing the top level document.title.  The external container will pick up on the onTitleChange.
//for example, this is used in Outlook so that Outlook can know when to act on web clicks, such as "send email"
function notifyExternal(packet)
{

	if(topPCRWindow)
	{
		topPCRWindow.body.setAttribute('packet',packet);
		topPCRWindow.title=topPCRWindow.title+'~';
	}
	else
	{
		document.body.setAttribute('packet',packet);
		document.title=document.title+'~';
	}
}

function pcrtarget(obj)
{
	if(isListFrame())
	{
		obj.target='detail';
	}
	else
	{
		obj.target='_blank';
	}
}

function isListFrame(){
	try
	{
		var win=parent.frames['list'].document==document;
		if(win)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	catch(e){ return false; }
}

//same as submitForm but modifies the target. use only when taregting a detail frame
function submitFormd(formname)
{
	pcrtarget(formname);
	try
	{
		window.external.AutoCompleteSaveForm(formname);
	}
	catch(e) {}
	formname.submit();
}
