// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
			&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
			&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav6up = (is_nav && (is_major >= 5));
var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

function objBrowser() {
  d=document;
  this.agt=navigator.userAgent.toLowerCase();
  this.major = parseInt(navigator.appVersion);
  this.dom=(d.getElementById)?1:0;
  this.ns=(d.layers);
  this.ns4up=(this.ns && this.major >=4);
  this.ns6=(this.dom&&navigator.appName=="Netscape");
  this.op=(window.opera? 1:0);
  this.ie=(d.all);
  this.ie4=(d.all&&!this.dom)?1:0;
  this.ie4up=(this.ie && this.major >= 4);
  this.ie5=(d.all&&this.dom);
  this.win=((this.agt.indexOf("win")!=-1) || (this.agt.indexOf("16bit")!=-1));
  this.mac=(this.agt.indexOf("mac")!=-1);
}
var oBw = new objBrowser();

function getObj(id,d) {
  var i,x;  if(!d) d=document; 
  if(!(x=d[id])&&d.all) x=d.all[id]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=getObj(id,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(id); 
  return x;
}
function getH(o) { var h=0; if (oBw.ns) { h=(o.height)? o.height:o.clip.height; return h; } h=(oBw.op)? o.style.pixelHeight:o.offsetHeight; return h; }
function setH(o,h) { if(oBw.ns) {if(o.clip) o.clip.bottom=h;}else if(oBw.op)o.style.pixelHeight=h;else o.style.height=h; }
function getW(o) { var w=0; if(oBw.ns) { w=(o.width)? o.width:o.clip.width; return w; } w=(oBw.op)? o.style.pixelWidth:o.offsetWidth; return w; }
function setW(o,w) { if(oBw.ns) {if(o.clip) o.clip.right=w;}else if(oBw.op)o.style.pixelWidth=w;else o.style.width=w; }
function getX(o) { var x=(oBw.ns)? o.left:(oBw.op)? o.style.pixelLeft:o.offsetLeft; return x;}
function setX(o,x) { (oBw.ns)? o.left=x:(oBw.op)? o.style.pixelLeft=x:o.style.left=x; }
function getPageX(o) { if(oBw.ns) { var x=(o.pageX)? o.pageX:o.x; return x; } else if (oBw.op) {  var x=0; while(eval(o)) { x+=o.stylo.pixelLeft; e=o.offsetParent; } return x; } else { var m=(oBw.mac&&oBw.ie)? document.body.leftMargin:0; var x=0; while(eval(o)) { x+=o.offsetLeft; o=o.offsetParent; } return parseInt(x)+parseInt(m) } }
function getY(o) {  var y=(oBw.ns)? o.top:(oBw.op)? o.style.pixelTop:o.offsetTop; return y;}
function setY(o,y) { (oBw.ie||oBw.dom)? o.style.top=y:(oBw.ns)? o.top=y:o.style.pixelTop=y; }
function getPageY(o) { if(oBw.ns) { var y=(o.pageY)? o.pageY:o.y; return y; } else if (oBw.op) {  var y=0; while(eval(o)) { y+=o.stylo.pixelTop; o=o.offsetParent; } return y; }  else { var m = (oBw.mac&&oBw.ie)? document.body.topMargin:0; var y=0; while(eval(o)) { y+=o.offsetTop; o=o.offsetParent; }  return parseInt(y)+parseInt(m); } }
function moveTo(o,x,y) { setX(o,x);setY(o,y); }
function setZ(o,z) { if(oBw.ns)o.zIndex=z;else o.style.zIndex=z; }
function show(o,disp) {
  (oBw.ns)? '':(!disp)? 0:o.style.display=disp;
  (oBw.ns)? o.visibility='show':o.style.visibility='visible';
}
function hide(o,disp) {
  (oBw.ns)? '':(!disp)? 0:o.style.display=disp;
  (oBw.ns)? o.visibility='hide':o.style.visibility='hidden';
}
function addEvt(o,e,f,c){ if(o.addEventListener)o.addEventListener(e,f,c);else if(o.attachEvent)o.attachEvent("on"+e,f);else eval("o.on"+e+"="+f)}

function formSubmit(FormNo) {
  if (document.forms[FormNo])
    document.forms[FormNo].submit();
}
      
function openNewWindow(strURL, strWindowName) {
  window.open(strURL, strWindowName);
}
      
function formFocus(){
  if (document.forms[0] && document.forms[0].None)
    document.forms[0].None.focus();
}
      
function openWindow(url, name, width, height, features) {
	var ref;
	var query = '';
	
	if ((width != 0) && (height != 0))
	{
		var top = 0;
		var left = 0;

		if (screen.width > width)
		  left = (screen.width - width) / 2;
		if (screen.height > height)
		  top = (screen.height - height) / 2;
		query = query + 'left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top + ',width=' + width + ',height=' + height;
	}
	if (typeof(features) != "undefined")
	  query = query + features;
	
	ref = window.open(url, name, query);
	if (navigator.appName != 'Microsoft Internet Explorer')
	  if (window.focus)
	    ref.focus();
}

function formatDateTime(strInput, blnShowAMPM, strTimeZone) {
  var strDate, objDate;
  
  objDate = new Date(strInput);
  if (!isNaN(objDate)) {
    strDate = formatDate(strInput) + ' ' + adjustTime(formatTime(strInput, blnShowAMPM, strTimeZone));
    return(strDate);
  }
  else {
    return(strInput);
  }
}

function formatDate(strInput) {
  var strDate, objDate, strTemp;
  
  objDate = new Date(strInput);
  if (!isNaN(objDate)) {
    strTemp = new String(objDate.getMonth() + 1);
    if (strTemp.length < 2)
      strTemp = '0' + strTemp;
    strDate = strTemp + '/';
    strTemp = new String(objDate.getDate());
    if (strTemp.length < 2)
      strTemp = '0' + strTemp;
    strDate += strTemp + '/';
    strDate += objDate.getFullYear();
    return(strDate);
  }
  else {
    return(strInput);
  }
}

function formatTime(strInput, blnShowAMPM, strTimeZone) {
  var strTime, objDate, strTemp, strAMPM;
  
  objDate = new Date(strInput);
  if (!isNaN(objDate)) {
    strTemp = new String(objDate.getHours());
    if (blnShowAMPM) {
      strAMPM = 'am';
      if ((strTemp * 1) > 11) {
        if ((strTemp * 1) != 12)
          strTemp = (strTemp * 1) - 12;
        strAMPM = 'pm';
      }
      if ((strTemp * 1) == 0)
        strTemp = 12;
    }
    else
      strAMPM = '';
    strTemp = new String(strTemp);
    if (strTemp.length < 2)
      strTemp = '0' + strTemp + '';
    strTime = strTemp;
    if (blnShowAMPM)
      strTime += ':';
    strTemp = new String(objDate.getMinutes());
    if (strTemp.length < 2)
      strTemp = '0' + strTemp;
    strTime += strTemp + strAMPM;
    strTemp = new String(strTimeZone);
    if (strTemp.length > 0)
      strTime += ' ' + strTemp;
    return(strTime);
  }
  else {
    return(strInput);
  }
}

function adjustTime(strInput) {
  var strTime, strTemp;
  strTemp = new String(strInput);
  if (strTemp.length < 5 && strTemp.length > 2 && isFinite(strTemp)) {
    strTime = strTemp.slice(0, strTemp.length - 2) + ':' + strTemp.slice(strTemp.length - 2, strTemp.length);
    return(strTime);
  }
  else {
    return(strInput);
  }
}

function DisplayCountDownTime(inMinutes, inSeconds) {
  var strTime;
  if (inMinutes <= 9)
    strTime = '0';
  else
    strTime = '';
  strTime += inMinutes + ':';
  if (inSeconds <= 9)
    strTime += '0' + inSeconds;
  else
    strTime += inSeconds;
  return(strTime); 
}

function BeginCountDown(inMinutes, inSeconds, strID, strEndFunction) {
  strMinutes = inMinutes;
  strSeconds = inSeconds;
  CountDown(strID, strEndFunction);
}

function CountDown(strID, strEndFunction) {
  strSeconds--;
  if (strSeconds == -1) {
    strSeconds = 59;
    strMinutes--;
  }
  if (document.all)//IE
    eval(strID).innerHTML = DisplayCountDownTime(strMinutes, strSeconds);
  else //Netscape
    document.getElementById(strID).innerHTML = DisplayCountDownTime(strMinutes, strSeconds);
  if ((strMinutes == 0) && (strSeconds == 0)) {
    eval(strEndFunction + '()');
  }
  else
    setTimeout('CountDown(\'' + strID + '\', \'' + strEndFunction + '\')', 1000);
}

function checkSelection(blnAll){
  var NumberOfBoxes, NumberOfCheckedBoxes;
  NumberOfBoxes = 0;
  NumberOfCheckedBoxes = 0;
  for (var i = 0; i < document.forms[0].elements.length; i++)
  {
    var element = document.forms[0].elements[i];
    if ((element.name != 'allbox') && (element.type == 'checkbox')) {
      if (!(element.className == 'Hidden' || element.className == 'hidden'))
        if (blnAll)
          element.checked = document.forms[0].allbox.checked;
        else {
          NumberOfBoxes++;
          if (element.checked)
            NumberOfCheckedBoxes++;
        }
      if (element.checked)
        selectRow(element);
      else
        unselectRow(element);
    }
  }
  if (!blnAll) {
    if (NumberOfCheckedBoxes == NumberOfBoxes)
      document.forms[0].allbox.checked = true;
    else
      document.forms[0].allbox.checked = false;
  }
}
      
function highlightRow(source) { 
  if (document.all) //IE 
    while (source.tagName != 'TR')
      source=source.parentElement;
  else //Netscape 
    while (source.tagName != 'TR')
      source=source.parentNode;
  if (source.className != 'TableSelectedRow')
    source.className = 'TableHighlightRow';
} 

function unhighlightRow(source) { 
  if (document.all) //IE 
    while (source.tagName != 'TR')
      source=source.parentElement;
  else //Netscape 
    while (source.tagName != 'TR')
      source=source.parentNode;
  if (source.className != 'TableSelectedRow')
    source.className='';
}

function selectRow(source) {
  if (document.all) //IE
    while (source.tagName != 'TR')
      source = source.parentElement;
  else //Netscape
    while (source.tagName != 'TR')
      source = source.parentNode;
  source.className = 'TableSelectedRow';
}

function unselectRow(source) {
  if (document.all) //IE
    while (source.tagName != 'TR')
      source = source.parentElement;
  else //Netscape
    while (source.tagName != 'TR')
      source = source.parentNode;
  source.className = '';
}
      
function disableMiles() {
	var tempDocument = disableMiles.arguments[0];
	var tempFlag = true;

	if (tempDocument.forms[0].CanadaFlag.checked || tempDocument.forms[0].CanadaFlag.value == "Yes"){
		tempFlag = false;
	}
	if (tempDocument.forms[0].ShpmntNoStops.value == 1 && tempFlag)
		{
		tempDocument.forms[0].ShipmentCarrierNotes.focus();
		}
}

function checkMiles() {
	var tempDocument = checkMiles.arguments[0];

	if (tempDocument.forms[0].ShipmentMiles.value == "")
		{
		tempDocument.forms[0].ShipmentMiles.value = 1;
		}
}

function changelayer(action, idname){
var myElement, IE, NS4
	IE=(document.all);
	NS4=(document.layers);
	if (IE){
		var myElement = document.all[idname]; //IE
	}else if(NS4){
		var myElement = document.layers[idname]; //N4
	}else{
		var myElement = document.getElementById(idname); //NS6
	}
	if(action=='visible'||action=='hidden')
		if (IE){myElement.style.visibility = action;}
		else if(NS4){myElement.visibility = action;}
		else{myElement.style.visibility = action;}
}

function clearLocations() {
	var tempDocument = clearLocations.arguments[0];
	if (clearLocations.arguments[1] == "ORIGIN") {
		tempDocument.forms[0].OriginCity.value = "";
		tempDocument.forms[0].OriginState.value = "";
		tempDocument.forms[0].OriginZip.value = "";
	}
	else if (clearLocations.arguments[1] == "DEST") {
		tempDocument.forms[0].DestCity.value = "";
		tempDocument.forms[0].DestState.value = "";
		tempDocument.forms[0].DestZip.value = "";
	}
}

function checkOfferPrice() {

	var tempDocument = checkOfferPrice.arguments[0];

	if (tempDocument.forms[0].TradeType[0].checked) 
		{
		return false;
		}
}

function calendarPopup(page) {
  var wndCalendar;
	wndCalendar = this.open(page, "wndCalendar", "status=no,toolbar=no,menubar=no,resizable=no,scrollbars=no,width=200,height=170");
	wndCalendar.focus();
}

function disableButton(strButton) {
  if (document.all) { //IE
    if (eval('document.frmDetails.' + strButton)) {
      eval('document.frmDetails.' + strButton).disabled = true;
      eval('document.frmDetails.' + strButton).className = 'ButtonDisabled';
    }
  }
  else { //Netscape
    if (document.getElementById(strButton)) {
      document.getElementById(strButton).disabled = true;
      document.getElementById(strButton).className = 'ButtonDisabled';
    }
  }
}
function enableButton(strButton) {
  if (document.all) { //IE
    if (eval('document.frmDetails.' + strButton)) {
      eval('document.frmDetails.' + strButton).disabled = false;
      eval('document.frmDetails.' + strButton).className = 'Button';
    }
  }
  else { //Netscape
    if (document.getElementById(strButton)) {
      document.getElementById(strButton).disabled = false;
      document.getElementById(strButton).className = 'Button';
    }
  }
}

function changelayer(action, idname){
  var IE, NS4;
	IE=(document.all);
	NS4=(document.layers);
	if (IE){
		if(document.all[idname]){
		var myElement = document.all[idname];
		myElement.style.visibility = action;} //IE
	}else if(NS4){
		if(document.layers[idname]){
		var myElement = document.layers[idname];
		myElement.visibility = action;}//N4
	}else{
		if(document.getElementById(idname)){
		var myElement = document.getElementById(idname); 
		myElement.style.visibility = action;}//NS6
	}
}

function ShowObject(source){
	if (document.all)
	  objElement = source;
	else
	  objElement=document.getElementById(source);
	if (objElement.className=="hidden"){
		objElement.className="NONE";//Tag does nothing but removes hidden in class
	}else{
		objElement.className="hidden";
	}
}
//If Page doesn't load successfully this will stop error page from showing a processing image
changelayer("hidden","ImageLayer")

function FocusWindow(){
	window.focus();
}

function textCounter(field, maxLen)//For Textareas
{
	if (field.value.length > maxLen) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxLen);
	}
}

function UltimateFocus(arrExemptions,strFocusOn,strFocusOnLocation, strForm){

	//arrExemptions --- Pass '' if no examptions, if there are certain fields you never want focus on then pass in an array with their names
	//strFocusOn --- If all fields have values Focus on this element
	//strFocusOnLocation --- If theres more than one do the first or last, 0 for first, 1 for last
	//strForm --- Name of Form

	for (var e = 0; e < eval('document.'+strForm+'.elements.length'); e++){
		if(eval('document.'+strForm+'.elements['+e+'].value') == ''){
			if(eval('document.'+strForm+'.elements['+e+'].type') == 'text' || eval('document.'+strForm+'.elements['+e+'].type') == 'select-one'){
				var blnFound = false;
				if(arrExemptions.length != 0){
					for(var x=0; x < arrExemptions.length; x++){
						if(arrExemptions[x].toLowerCase() == eval('document.'+strForm+'.elements['+e+'].name.toLowerCase()')){
							blnFound = true;
						}
					}
				}
				if(!blnFound){
					eval('document.'+strForm+'.elements['+e+'].focus()');
					return true;
				}
			}
		}
	}
	//If no Focus set , set focus on strFocusOn with strFocusOnLocation
	if(strFocusOnLocation == 0){
		for (var e = 0; e < eval('document.'+strForm+'.elements.length'); e++){
			if(eval('document.'+strForm+'.elements['+e+'].type') != undefined){//Some unknown items were coming up as undefined
				if(eval('document.'+strForm+'.elements['+e+'].name.toLowerCase()') == strFocusOn.toLowerCase()){
					eval('document.'+strForm+'.elements['+e+'].focus()');//Do first one
					return true;
				}
			}
		}
	}else{//strFocusOnLocation == 1
		var intValue;
		intValue = '';
		for (var e = 0; e < eval('document.'+strForm+'.elements.length'); e++){
			if(eval('document.'+strForm+'.elements['+e+'].type') != undefined){//Some unknown items were coming up as undefined
				if(eval('document.'+strForm+'.elements['+e+'].name.toLowerCase()') == strFocusOn.toLowerCase()){
					intValue = e;//loop til last one has value Do first one
				}
			}
		}
		if(intValue != ''){
			eval('document.'+strForm+'.elements['+intValue+'].focus()');
			return true;
		}
	}
}

function IgnoreCommas(field, evt){
	//alert(field + ' and ' + evt)
	var keyCode = document.layers ? evt.which : document.all ? evt.keyCode : evt.keyCode;
	if (keyCode != 188){
		return true;
	}else{
		return false;
	}
}

function SetDropDown(intRow,DropDownName,strValueToSelect,strForm)
{
	var SelectedItem;
	if(eval('document.'+strForm+'.'+DropDownName+'['+ (intRow-1) +'].length') == undefined){
		for (i = 0; i < eval('document.'+strForm+'.'+DropDownName+'.length'); ++i)//     Loop through each option in Select
			{
				//     If Option Value = Form Value Then Set the Item
				if (eval('document.'+strForm+'.'+DropDownName+'['+i+'].value') == strValueToSelect){eval('document.'+strForm+'.'+DropDownName+'.selectedIndex = '+i);}
			}
	}else{//More than one on page
		for (i = 0; i < eval('document.'+strForm+'.'+DropDownName+'['+(intRow-1)+'].length'); ++i)//     Loop through each option in Select
			{
				//     If Option Value = Form Value Then Set the Item
				if (eval('document.'+strForm+'.'+DropDownName+'['+(intRow-1)+']['+i+'].value') == strValueToSelect){eval('document.'+strForm+'.'+DropDownName+'['+(intRow-1)+'].selectedIndex = '+i);}
			}
	}
}

var IFrameObj;
function LoadFrame(strURL, strFrame, blnShow){//blnShow is for debugging only
	if (!document.createElement){return true};
	var IFrameDoc;
	var URL = strURL + buildQueryString();//if no QS needed, have empty function return ''
	if (!IFrameObj && document.createElement){
		try{
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id',strFrame);
			if(blnShow){
				tempIFrame.style.border='1px';
				tempIFrame.style.width='500px';
				tempIFrame.style.height='300px';
			}else{
				tempIFrame.style.border='0px';
				tempIFrame.style.width='0px';
				tempIFrame.style.height='0px';
			}
			IFrameObj = document.body.appendChild(tempIFrame);
			if (document.frames) {
				IFrameObj = document.frames[strFrame];
			}
		}catch(exception){
			iframeHTML='\<iframe id="'+strFrame+'" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById(strFrame);
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	if(navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument){
		setTimeout('callToServer()',10);
		return false;
	}
	if (IFrameObj.contentDocument){
		IFrameDoc = IFrameObj.contentDocument;// For NS6
	}else if (IFrameObj.contentWindow){
		IFrameDoc = IFrameObj.contentWindow.document;// For IE5.5 and IE6
	}else if (IFrameObj.document) {
		IFrameDoc = IFrameObj.document;// For IE5
	}else{
		return true;
	}
	IFrameDoc.location.replace(URL);
	return false;
}

function CreateTextBoxObject(strType, strForm, strName, strOpener){//called when we changevalues via javascript
	var oTextBox;
	//strOpener = 'opener.'  if javascript was sent from pop-up, iframe...
	oTextBox = eval(strOpener+'document.'+strForm+'.'+strName); //Create object
	if(strType == 'D'){//DATE
		DateBoxBackground(oTextBox);
	}else{//TIME
		TimeBoxBackground(oTextBox);
	}
}

function DateBoxBackground(oDateBox){ //called directly from events
	var strClass = oDateBox.className;
	if(oDateBox.value == ''){
		if(strClass != 'dateboxbg')oDateBox.className = 'dateboxbg';
	}else{
		if(strClass != 'dateboxclear')oDateBox.className = 'dateboxclear'
	}
}

function TimeBoxBackground(oTimeBox){ //called directly from events
	var strClass = oTimeBox.className;
	if(oTimeBox.value == ''){
		if(strClass != 'timeboxbg')oTimeBox.className = 'timeboxbg';
	}else{
		if(strClass != 'timeboxclear')oTimeBox.className = 'timeboxclear'
	}
}
