/* 
 * PFS Shared JavaScript Lib
 * Modified: 02-Feb-2009 [bcd]
 */

//Set up additional method on the location object
//This parses out the query string params for you.
location.getParameter = function(sParam) {
		var sKey = sParam +"=";
		var oParams = this.search.substring(1).split("&");
		for(var i = 0; i<oParams.length; i++) {
			if(oParams[i].indexOf(sKey) == 0) {
				return oParams[i].substring(sKey.length);
			}
		}
		return null;
	};

function getElement(id) {
	var itm = false;
	if(document.getElementById) {
		itm = document.getElementById(id);
	} else if(document.all) {
		itm = document.all[id];
	} else if(document.layers) {
		itm = document.layers[id];
	}
	return itm;
}

/**
 * The dollar function
 * With this you can access DOM elements like this: $('elementId').value
 *********************************/
var $ = getElement; 

/**
* Gets all elements that have the assigned class.
* The 2nd and 3rd arguments are optional.
*
* @Arg: node - The parent node to search for children in.
* @Arg: tag - only search by specified tag
*********************************************/
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	//var pattern = "/" +searchClass +"/i";
	//var re = new RegExp(pattern);
	for (i = 0, j = 0; i < elsLen; i++) {
		var oE = els[i];
		if(oE.className && oE.className.length > 0) {
			var cn = oE.className;
			if (cn == searchClass) {
				classElements[j] = els[i];
				j++;
			}
		}
	}
	return classElements;
}

/*********************************************
 * Handles and directs default events for textboxes
 *
 * Example: OnKeyDown='OnKeyDownSetEventTarget(event, targetBtnId)'
 * ..where targetBtnId is the intended default event target
 *********************************************/
function OnKeyDownSetEventTarget(evt, targetId) {
	if (targetId) {
		var elTarget = getElement(targetId);
		// Process only the enter key.
		if (evt && evt.keyCode == 13) {
			// Cancel the default submit.
			evt.returnValue = false;
			evt.cancel = true;
			// Submit the form by programmatically clicking the specified target.
			if(elTarget) {
				elTarget.click();
			}
		}
	}
}


function trim(a) {
	return a.replace(/^\s+/,'').replace(/\s+$/,'')
}

function makeArray() {
     for (i = 0; i<makeArray.arguments.length; i++)
          this[i + 1] = makeArray.arguments[i];
}

function showDate () {
    var hour, minute, day, month, yy, year;
    var months = new makeArray('January','February','March','April','May','June','July','August','September','October','November','December');

    date = new Date();
    hour = date.getHours();
    minute = date.getMinutes();
    day  = date.getDate();
    month = date.getMonth() + 1;
    yy = date.getYear();
    year = (yy < 1000) ? yy + 1900 : yy;

    var xhour = hour;
    var xminute = minute;
    var ampm = "AM";

    if (xminute < 10) { xminute = "0" + xminute };

    if (hour >= 12)  {
        ampm = "PM";
        if (hour != 12) xhour = hour - 12;
    }
		document.write(months[month] + " " + day + ", " + year);
}

function newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

var preloadFlag = false;
function preloadGNImages() {
    if (document.images) {
        _preloadImages('/images/gn-hm-newso.gif',
					   '/images/gn-hm-3plservo.gif',
					   '/images/gn-hm-techo.gif',
					   '/images/gn-hm-toolso.gif');
        preloadFlag = true;
    }
}

function preloadGNSubImages() {
    if (document.images) {
        _preloadImages('/images/gn-sub-newso.gif','/images/gn-sub-3plservo.gif','/images/gn-sub-techo.gif','/images/gn-sub-toolso.gif');
        preloadFlag = true;
    }
}

function swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function _preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function _findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() { //v3.0
  var i,j=0,x,a=swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// {{{ Dynamic Feature Links

// Set up the image files to be used.
var theImages = new Array() // do not change this
var theLinks  = new Array() // do not change this

// To add more image files, continue with the
// pattern below, adding to the array.
theImages[0] = '/images/index_securityfeature.jpg'
theImages[1] = '/images/index_genericfeature.jpg'
theImages[2] = '/images/index_mobilefeature.jpg'
theImages[3] = '/images/index_pdrivefeature.jpg'
theImages[4] = '/images/index_inv-by-email.jpg'
theImages[5] = '/images/index-feature-needle.jpg'

// array for feature links below.
theLinks[0] = '/technology/'
theLinks[1] = '/3pl/services/'
theLinks[2] = '/technology/mobile/'
theLinks[3] = '/technology/'
theLinks[4] = '/technology/'
theLinks[5] = '/3pl/services/'

// do not edit anything below this line
var whichImage = Math.round(Math.random()*(theImages.length-1));

function showImage(){
    document.write('<img src="'+theImages[whichImage]+'" usemap="#featurelink" border="0" alt=" " />');
}

function featureLink(){
    document.write('<map name="featurelink"><area shape="rect" coords="516,163,590,181" href="'+theLinks[whichImage] +'"></map>');
}
// }}}

// {{{ Window Functions
function PopWin(windowURL, windowName, windowWidth, windowHeight)
{
 return window.open(windowURL, windowName, "width="+windowWidth+",height="+windowHeight+",toolbar=1,location=0,directories=0,top=0,left=0,status=1,menuBar=0,scrollBars=1,resizable=1");
}

function PopWinCentered(windowURL, windowName){
 winW=Math.round(screen.availWidth * .85);
 winH=Math.round(screen.availHeight * .80);
 posLeft=(screen.availWidth-winW)/2;
 posRight=(screen.availHeight-winH)/2;
 return window.open(windowURL, windowName, "width=" +winW +",height=" +winH
				+",toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=1,resizable=1,left="
				+posLeft +",top=" +posRight );
}

function setNoIntroPopupCookie() {
    var nextyear = new Date ();
    nextyear.setFullYear(nextyear.getFullYear() + 1);
    document.cookie = "nointro=1; expires=" + nextyear.toGMTString();
}

function showintro() {
    var nextyear = new Date ();
    nextyear.setFullYear(nextyear.getFullYear() + 1);
    document.cookie = "nointro=0; expires=" + nextyear.toGMTString();
    PInto = PopWin('/intropopup.asp', 'PInto', 600, 500);
    PInto.focus();
}
function setFooter() {
	if (document.getElementById) {
		var windowHeight=getWindowHeight();
		if (windowHeight > 0) {
			var contentHeight=document.getElementById('PageContent').offsetHeight;
			var footerElement=document.getElementById('Footer');
			var footerHeight=footerElement.offsetHeight;
			if (windowHeight-(contentHeight+footerHeight)>=0) {
				//alert("Window: " +windowHeight +"\n" +"ContentH: " +contentHeight
							//+"\nFooterH: " +footerHeight);
				footerElement.style.position='absolute';
				footerElement.style.bottom=0;
				footerElement.style.left=0;				
				//footerElement.style.top=(windowHeight-(contentHeight+footerHeight))+'px';
			} else {
				footerElement.style.position='static';
			}
		}
	}
}

function getWindowHeight() {
	var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	}	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight=	document.documentElement.clientHeight;
		}	else {
			if (document.body&&document.body.clientHeight) {
				windowHeight=document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function autotab(original,destination){
	if (original.getAttribute && original.value.length == original.getAttribute("maxlength"))
	{ destination.focus(); }
}

/*****************************
 * Validation Functions
 *****************************/
//pass in a form object reference
function validateYear(fo,bRequired) {
	var valid = true;
	if( (fo.value != "" || bRequired) && fo.value.length < 4) {
		msg = "Please use a 4 digit year value.";
		valid = false;
	} else if(fo.value != "" &&
			(isNaN(parseInt(fo.value)) 
				|| parseInt(fo.value) < 1990 
				|| parseInt(fo.value) > 2020) ) {
		msg = "Please use a valid year value.";
		valid = false;
	} else {
		valid = true;
	}
	if(!valid) {
		alert(msg);
		fo.value = "";
		fo.focus();
	}
	return valid;
}

//pass in a form object reference
function validateMonth(fo,bRequired) {
	var valid = true;
	if( fo.value == "" && bRequired) {
		msg = "Please enter a month value.";
		valid = false;
	} else if( fo.value != "" && 
				( isNaN(parseInt(fo.value)) 
					|| parseInt(fo.value) < 1 
					|| parseInt(fo.value) > 12) ) {
		msg = "Please use a valid month value between 1 and 12.";
		valid = false;
	} 
	if(!valid) {
		alert(msg);
		fo.value = "";
		fo.focus();
	}
	return valid;
}

//pass in a form object reference
function validateDay(fo,bRequired) {
	var valid = true;
	if( fo.value == "" && bRequired) {
		msg = "Please enter a day value.";
		valid = false;
	} else if( fo.value != "" &&
			( isNaN(parseInt(fo.value)) 
				|| parseInt(fo.value) < 1 
				|| parseInt(fo.value) > 31) ) {
		msg = "Please use a valid day value between 1 and 31.";
		valid = false;
	} 
	if(!valid) {
		alert(msg);
		fo.value = "";
		fo.focus();		
	}
	return valid;
}