// This file is intended to hold javascript that might be common to any
// page in the site. It is included by the head.jspi file (which is 
// included by every page), so it is already available to the whole site.


//----------------------------------------------------------------------------
// The menus we are using require an onLoad() handler. Since a page can
// only have one onLoad handler, the menu library was modified to ensure that
// this function is called after the menus are initialized. Any page that
// requires initialization (to set the focus for example), should redefine this
// function and put the initialization code in it. This version of the function
// is just a placeholder to ensure that every page has this function. This way
// no JavaScript errors are generated when it is called on a page that does not
// require initialization. This version of this function should remain empty;
// declaring another version in a page will cause this version to be "deleted".
//-----------------------------------------------------------------------------
function VB_InitializePage()
{
}


//-----------------------------------------------------------------------------
// This function is used to prevent users from submitting the same form twice.
// The intention is that it will be called just before any form on the page is
// submitted. If it returns true, the page can submit the form. If a form has
// already been submitted, then an alert window will be displayed. The alert
// window is only displayed once; if the user continues to click after they see
// the alert, those clicks will be ignored.
//-----------------------------------------------------------------------------
g_clicks=0;
function oneClick()
{
   g_clicks++;

   if (g_clicks > 1)
   {
      if (g_clicks == 2)
         alert('Please wait. Your request has been sent to the server for processing');

      return false;
   }

   return true;
}

//------------------------------------------------------------------------------------
// This function parses any passed integer range and returns array of size 2 with min 
// and max value in it.
// For example : If rValue = 2-5 Then rangesArray[0] = 2 and rangesArray[1] = 5
//		 If rValue = >100 Then rangesArray[0] = 101 and rangesArray[1] = ""
//		 If rValue = <20 Then rangesArray[0] = "" and rangesArray[1] = 19
//		 If rValue = 50+ Then rangesArray[0] = 50 and rangesArray[1] = ""
//
//------------------------------------------------------------------------------------

function getRange(rValue){
            rangesArray = rValue.split("-");
            if(rangesArray.length == 1 && rangesArray[0].indexOf("+") >= 0){
                tmpAr = rValue.split("+");
                rangesArray = new Array(2);
                rangesArray[0] = tmpAr[0];
                rangesArray[1] = "";
            }
            else if(rangesArray.length == 1 && rValue.indexOf("<") >= 0){
                ind = rValue.indexOf("<");
                len = rValue.length - (ind+1);
                tmpStr = rValue.substr(ind+1,len);
                rangesArray = new Array(2);
                rangesArray[0] = "";
                rangesArray[1] = eval(eval(tmpStr)-1);;
            }
            else if(rangesArray.length == 1 && rValue.indexOf(">") >= 0){
                ind = rValue.indexOf(">");
                len = rValue.length - (ind+1);
                tmpStr = rValue.substr(ind+1,len);
                rangesArray = new Array(2);
                rangesArray[0] = eval(eval(tmpStr)+1);
                rangesArray[1] = "";
            }
            else if(rangesArray.length == 1 && isNaN(parseInt(rangesArray[0]))){
                rangesArray = new Array(2);
                rangesArray[0] = "";
                rangesArray[1] = "";
            }
            return rangesArray;
  }
  
//this opens the public VB Demo in a new window  
function openDemo(){
	window.open('/demo/VBPublicDemo.html', 'KnightBondPoint', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=800,height=600,screenX=80,screenY=100,left=80,top=100');
}

//this opens the VB Site tutorial in a new window
function openTutorial(){
	window.open('/demo/VBSiteDemo.html', 'KnightBondPoint', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,width=800,height=600,screenX=80,screenY=100,left=80,top=100');
}

//Functions for the menus
var NN4 = ((parseInt(navigator.appVersion)>=4 && parseInt(navigator.appVersion)<5)&&(navigator.appName.indexOf("Netscape")!=-1))? 1:0;
var NN6 = ((parseInt(navigator.appVersion)>=5)&&(navigator.appName.indexOf("Netscape")!=-1))? 1:0;
var IE4 = ((parseInt(navigator.appVersion)>=4)&&(navigator.appName.indexOf("Microsoft")!=-1))? 1:0;

function loadMenu()
{
var box; 
var selbox;
var slBut;
var selBg;
var subBg;
for(i=0; i<=3; i++) 
{
	box = "VBTab" +i;
	selbox = "ONSelTab" +i;
	slBut = "ONBut" +i;
	selBg = "ONSelBG" +i;
	subBg = "ONSubBG" +i;
	
if(NN6) 
{
	document.getElementById(selBg).style.backgroundColor=BG_Outline;
	document.getElementById(subBg).style.backgroundColor=BG_Color;
} 
else if(IE4) 
{
	document.all[selBg].style.backgroundColor=BG_Outline;
	document.all[subBg].style.backgroundColor=BG_Color;
}
else if(NN4) 
{
	document.layers[selbox].document.layers[selBg].bgColor=BG_Outline;
	document.layers[selbox].document.layers[subBg].bgColor=BG_Color;
}
 
}

//This code pre-selects the tab for the current 'category' (Trade, tools, etc)
if(firstLoad)
{	
	if(cat == "Trade")
	{
		showONTab(0);
		firstLoad = false;
	}
	else if(cat == "Offerings") 
	{
		showONTab(1);
		firstLoad = false;
	}
	else if(cat == "Tools")
	{
		showONTab(2);
		firstLoad = false;
	}
	else if(cat == "BidWanted")
	{
		showONTab(3);
		firstLoad = false;
	}
	else
	{
		showONTab(0);
		firstLoad = false;
	}
}
} 

function showTab(id,count){
if(IE4)
{
if(count == 1)
	var vis = 'visible';
else if(count == 0)
	var vis = 'hidden';
		
document.all[id].style.visibility=vis;
}
else if(NN6)
{
if(count == 1)
	var vis = 'visible';
else if(count == 0)
	var vis = 'hidden';
	
document.getElementById(id).style.visibility=vis;
}
else if(NN4)
{
if(count == 1)
	var vis = 'show';
else if(count == 0)
	var vis = 'hide';
	
document.layers[id].visibility=vis;
}

}

function showONTab(index) 
{
index=parseInt(index);
showTab("ONTab" + index, 1);
showTab("ONSelTab" + index, 1);
var lastTab=parseInt(index) - 1;
for(i=lastTab; i>=0; i--) 
{
	showTab("ONTab" + i, 0);
	showTab("ONSelTab" + i, 0);
}
var nextTab=parseInt(index) + 1;
for(i=nextTab; i<=3; i++) 
{
	showTab("ONTab" + i, 0);
	showTab("ONSelTab" + i, 0);
}

}

function hilightButton(butName, on)
{
var bgClr = '#FFFFFF';
var textClr = '#000066';
if(on)
{
	bgClr = '#000066';
	textClr = '#FFFFFF';
}
if(NN6) 
{
	document.getElementById(butName).style.backgroundColor=bgClr;
	document.getElementById(butName).style.color=textClr;
} 
else if(IE4) 
{
	document.all[butName].style.backgroundColor=bgClr;
	document.all[butName].style.color=textClr;
}
//will not function correctly in NN4, so nothing happens

}

function Trim(strInput) {
    /* Check to see if the length of the input string is zero. */
    if(strInput.length == 0) {
        /* If the length is zero, return a zero length sting. */
        return "";
    }
    else {
        /* If the length is greater than zero, find out what the
           last character in the string is */
        strTemp = strInput.substring(strInput.length - 1)
    }

    /* If the last character is a space, trim it from the string */
    while (strTemp == " ") {
        /* Set the input string equal to the input sting, minus the last charater */
        strInput = strInput.substring(0, strInput.length - 1)

        /* Check to see if the string has a zero length again. */
        if (strInput.length == 0) {
            /* If the length is zero, return a zero length sting. */
            strTemp = "";
        }
        else {
            /* If the length is greater than zero, find out what the
               last character in the string is */
            strTemp = strInput.substring(strInput.length - 1)
        }
    }
	
    /* Do the same thing, but for the beginning of the string */
    if (strInput.length == 0) {
        strTemp = "";
    }
    else {
        strTemp = strInput.substring(0, 1)
    }
		
    while (strTemp == " ") {
        strInput = strInput.substring(1)
			
        if (strInput.length == 0) {
            strTemp = "";
        }
        else {
            strTemp = strInput.substring(0, 1)
        }
    }
    return strInput;
}

function isDate(day,month,year) {
    var today = new Date();
    if (!year) {
    	year = today.getFullYear();
    } else {
    	year = "" + year;
    	len = year.length;
    	curYear = today.getFullYear();
    	curYear = "" + curYear;
    	if (len > 0 && len < 4) {
            year = curYear.substring(0, 4 - len) + year;
        }
    }
    month = ((!month) ? today.getMonth():month-1);
    if (!day) day = 1;
    var test = new Date(year,month,day);
    if ( (year == test.getFullYear()) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}

function trim(value)
{
   while(''+value.charAt(0)==' ')
      value=value.substring(1,value.length);
      
   while(''+value.charAt(value.length-1)==' ')
      value=value.substring(0,value.length-1);
      
   return value;
}

function validEmailInfo(toAddr, allowEmpty)
{
   if ((toAddr == null) || (toAddr == '') || (trim(toAddr) == '')) {
      return allowEmpty;
   }

   var toAddrArray = toAddr.split(",");
   for (i = 0 ; i < toAddrArray.length; i++) {
      if (!(isValidEmail(toAddrArray[i]))) {
         return false;
      }
   }
   
   return true;
}

function isValidEmail(emailAddr) {
   var atIndex = emailAddr.indexOf('@', 0);
   var dotIndex = -1;
   if ((atIndex == 0) || (atIndex == -1) || (emailAddr.indexOf('@', atIndex + 1) > -1)) {
      return false;
   }
   dotIndex = emailAddr.indexOf('.', atIndex);
   if ((dotIndex == -1) || (dotIndex + 1 == emailAddr.length)) {
      return false;
   }
   return true;
}

function confirmGen(q, btn1, btn2)
{
   height = '150px';
   width = '200px';
   if (q.length > 30) {
      width = '300px';
   } else if (q.length > 100) {
      width = '400px';
      height = '300px';
   }
   return showModalDialog("/include/yesNoConfirm.html", q + "||" + btn1 + "||" + btn2, "help:no; resizeable:yes; status:no; dialogWidth=" + width + ";dialogHeight=" + height);
}

function confirmGenOrderEntry(q, btn1, btn2, btn3)
{
   height = '110px';
   width = '400px';
   return showModalDialog("/include/yesNoConfirmOrderEntry.html", q + "||" + btn1 + "||" + btn2 + " ||" + btn3, "help:no; resizeable:yes; status:no; dialogWidth=" + width + ";dialogHeight=" + height);
}

function confirmYesNo(q)
{
   return confirmGen(q, "Yes", "No");
}
function confirmYesNoOrderEntry(q)
{
   return confirmGenOrderEntry(q, "Yes", "No", "Match Best Price");
}