/*****************************************************
 *	TO DO: USE SCRIPT FROM JONES FOR WINDOW OPENING  *
*****************************************************/

var ie = (document.all) ? true:false; // FOR INTERNET EXPLORER 5.x
var nn4 = (document.layers) ? true:false; // FOR NETSCAPE 4.x
var nn6 = (document.getElementById && !ie) ? true:false; // FOR NETSCAPE 6.x
var hideDiv = "hidden";
var showDiv = "visible";
//var bannerRef = self.parent.banner.document;
//var homeRef = self.parent.content.document;
var listingsSelected;
//var quoteHolderForm = top.frames[0].document.quoteForm;

if(nn4) 
{
	hideDiv = "hide"; 
	showDiv = "show";
}	


function hideHomeLink()
{
/*
	if(ie)bannerRef.all['homeLink'].style.visibility=hideDiv;//visible
	if(nn6) bannerRef.getElementById('homeLink').style.visibility=hideDiv;
	if(nn4) bannerRef.homeLink.visibility=hideDiv;//show
	homePageLoginCheck();
*/
}

function showHomeLink()
{
/*
	if(ie) bannerRef.all['homeLink'].style.visibility=showDiv;//visible
	if(nn6) bannerRef.getElementById('homeLink').style.visibility=showDiv;
	if(nn4) bannerRef.homeLink.visibility=showDiv;//show
*/
}

//logInForm
function homePageLoginCheck()
{
/* COMMENTED OUT UNTIL IMPLEMENTATION
	var divState = showDiv;
	if(currentAcct&&currentAcct!="notLoggedIn") divState = hideDiv;
	if(ie) homeRef.all['logInForm'].style.visibility=divState;//visible
	if(nn6) homeRef.getElementById('logInForm').style.visibility=divState;
	if(nn4) homeRef.logInForm.visibility=divState;//show

	if(divState==hideDiv)
	{

		var sPersonalContent = "<em>Welcome, Charlie!</em>";
		if(ie) 
		{
			homeRef.all['logInForm'].innerHTML = sPersonalContent;
			homeRef.all['logInForm'].style.visibility=showDiv;//visible
		}
		if(nn6) 
		{
			homeRef.getElementById('logInForm').innerHTML = sPersonalContent;
			homeRef.getElementById('logInForm').style.visibility=showDiv;
		}
		if(nn4) 
		{
			homeRef.logInForm.document.open();
			homeRef.logInForm.document.write(sPersonalContent);
			homeRef.logInForm.document.close();
			homeRef.logInForm.visibility=showDiv;//show
		}	
	}
*/	
}

function homePageLogin()
{
/* COMMENTED OUT UNTIL IMPLEMENTATION

	var acctValue;
	if(!nn4) acctValue = homeRef.forms['logIn'].elements['account'].value;
	if(nn4) acctValue = homeRef.logInForm.document.logIn.account.value;
	setAccount(acctValue);
	if(ie) homeRef.all['logInForm'].style.visibility=hideDiv;
	if(nn6) homeRef.getElementById('logInForm').style.visibility=hideDiv;
	if(nn4) homeRef.logInForm.visibility=hideDiv;

	var sPersonalContent = "<em>Welcome, Charlie!</em>";
	if(ie) 
	{
		homeRef.all['logInForm'].innerHTML = sPersonalContent;
		homeRef.all['logInForm'].style.visibility=showDiv;//visible
	}
	if(nn6) 
	{
		homeRef.getElementById('logInForm').innerHTML = sPersonalContent;
		homeRef.getElementById('logInForm').style.visibility=showDiv;
	}
	if(nn4) 
	{
		homeRef.logInForm.document.open();
		homeRef.logInForm.document.write(sPersonalContent);
		homeRef.logInForm.document.close();

		homeRef.logInForm.visibility=showDiv;//show
	}	
*/
	return void(0);
}

//account
function setAccount(account)
{
/* COMMENTED OUT UNTIL IMPLEMENTATION

	if(bannerRef.quoteHolder.account)
	{
		bannerRef.quoteHolder.account.value=account;
	}
*/
	return void(0);
}

function clearListings()
{
	document.quoteForm.elements['quoteRequests'].value = "";
//	top.frames[0].location.reload();
//	top.frames[0].document.quoteForm.submit();
//	loadRequestCount();	
}

function setListings(item)
{
//alert(item);
	if(!document.getElementById('quoteForm').elements)

	{
		return alert("Processing previous request.\nPlease try again in 30 seconds.");
	}
	
	if(document.getElementById('quoteForm').elements['quoteRequests'].value.indexOf(item)!=(-1)) return alert('Item already selected');
	if(document.getElementById('quoteForm').elements['quoteRequests'].value.length<3)
	{
		alert("When you are done selecting items,\n"+
			 	"click \"Request Info\" at the top right of the page");
	}

	document.getElementById("quoteFrame").src = "QuoteRequest.do?quoteRequests="+item;



//	QuoteRequest.do
//	quoteFrame
/*
	top.frames[0].document.quoteForm.elements['quoteRequests'].value = top.frames[0].document.quoteForm.elements['quoteRequests'].value+", "+item;
	
	top.frames[0].document.quoteForm.submit();
	loadRequestCount();	
*/	
}

function loadRequestCount()
{

	if(top.frames[0].location.href.indexOf('QuoteRequest.do')==(-1)||
		!top.frames[0].document.forms[0] || 
		!top.frames[0].document.forms[0].elements['quoteRequests'])
	{
		top.frames[0].location = 'QuoteRequest.do';
		setTimeout("loadRequestCount()",50000);
		return void(0);
	}

//	alert(top.frames[0].location.href.indexOf('QuoteRequest.do'));

	var sListingsCount = 0;
	var sListingDisplay1 = 	"<a href=\"javascript:self.parent.content.location='ConatctOum.do'\""+
							"target=\"content\"><span class=\"bannerLink\"><nobr>Request info for ";
	var sListingDisplay2 = " items</nobr></span></a>";
	var sDivContent = "";
	listingsSelected = top.frames[0].document.forms[0].elements['quoteRequests'].value;
	var stillCount = true;
	var fromCount = 0;
	var currentIndex;
	var infoWindow;
	while(stillCount)
	{
		currentIndex = listingsSelected.indexOf(",", fromCount);
		if(currentIndex >= 0)
		{
			sListingsCount++;
			fromCount = currentIndex+1;
		}
		else
		{
			stillCount = false;
			break;
		}
	}
	
	if(sListingsCount>0)
	{
		sDivContent = sListingDisplay1+sListingsCount+sListingDisplay2;
	}	

	if(ie) 
	{
		bannerRef.all['requestedItems'].innerHTML = sDivContent;
		bannerRef.all['requestedItems'].style.visibility=showDiv;//visible
	}
	if(nn6) 
	{
		bannerRef.getElementById('requestedItems').innerHTML = sDivContent;
		bannerRef.getElementById('requestedItems').style.visibility=showDiv;
	}
	if(nn4) 
	{
		bannerRef.requestedItems.document.open();
		bannerRef.requestedItems.document.write(sDivContent);
		bannerRef.requestedItems.document.close();

		bannerRef.requestedItems.visibility=showDiv;//show
	}	
	
	if (sListingsCount==1&&parent.content.location.href.indexOf('MachineryHome.do')==-1)
	{
		window.open("request_acknowlged.html",
		 "Your_Request","resizable=no,status=no,toolbar=no,menubar=no,top=100, left=100,width=320,height=280,scrollbars=no");
	}

}

function checkOut()
{
	listingsSelected = bannerRef.quoteHolder.listings.value;
	
}

function genericPop(url)
{
	var popWindow;
	popWindow = window.open(url, "Info","resizable=no,status=no,toolbar=no,menubar=no,top=100, left=100,width=350,height=350,scrollbars=no");
	popWindow.focus();
}

function photoLink(item)
{
	var slideShow;
/*
		slideShow = window.open("SlideShow.do?itemNum="+item, item,"resizable=no,status=no,toolbar=no,menubar=no,top=100, left=100,width=520,height=400,scrollbars=no");
*/
		slideShow = window.open("SlideShow.do?itemNum="+item, item,"resizable=yes,status=no,toolbar=no,menubar=no,top=20, left=20,width=700,height=520,scrollbars=yes");

		slideShow.focus();
}

function powerBold()
{
	if(ie) 
	{
		document.all['powerSearch'].style.className="powerSearchBold";
	}
	if(nn6) 
	{
		bannerRef.getElementById('powerSearch').style.className="powerSearchBold";
	}
}

function browseListings(machtype)
{
	if(self.document.equipByType)
	{
		self.document.equipByType.machtype.value = machtype;
		self.document.equipByType.submit()
	}
}

/**
* this method validates the email address
*/
function checkEmail(theForm, theElement)
{
	var theValue;
	var formatOk = true;
	var theDot;
	var theAmp;

	document.forms[theForm].elements[theElement].value = trim(document.forms[theForm].elements[theElement].value);
	theValue = document.forms[theForm].elements[theElement].value;

	if (theValue==null||theValue.length < 1)return alert('E-mail address is required');	
	
	
	// cant have these  characters - space [ ] , : ; \ ( ) "
	if (theValue.length > 0)
	{
		if (theValue.indexOf(" ") != -1)
			formatOk = false;
		if (theValue.indexOf("[") != -1)
			formatOk = false;
		if (theValue.indexOf("]") != -1)
			formatOk = false;
		if (theValue.indexOf(",") != -1)
			formatOk = false;
		if (theValue.indexOf(":") != -1)
			formatOk = false;
		if (theValue.indexOf(";") != -1)
			formatOk = false;
		if (theValue.indexOf("\\") != -1)
			formatOk = false;
		if (theValue.indexOf("\"") != -1)
			formatOk = false;
		if (theValue.indexOf("(") != -1)
			formatOk = false;
		if (theValue.indexOf(")") != -1)
			formatOk = false;
		if (theValue.indexOf("{") != -1)
			formatOk = false;
		if (theValue.indexOf("}") != -1)
			formatOk = false;
		if ((theAmp = theValue.indexOf("@")) < 1)		// must have an @ and not the first char
			formatOk = false;
		if (theValue.lastIndexOf(".") + 1 == theValue.length)	// the dot cannot be at the end
			formatOk = false;
		if (theValue.lastIndexOf("@") != theValue.indexOf("@"))	// we cannot have more that one @
			formatOk = false;
		if ((theDot = theValue.lastIndexOf(".")) < 1)		// must have at least one .
			formatOk = false;
		if (theDot < (theAmp+2)) //must be a character between the amp and the dot left of the amp
			formatOk = false;
		if (theValue.indexOf("..")!=-1) //cannot have double dots
			formatOk = false;
	}

	if (theValue.length < 3)formatOk = false;
	
	if(!formatOk)return alert('E-mail address is invalid');
	document.forms[theForm].submit();
}

/**
* This function is to trim leading spaces
*/
function lTrim(strIn) {
	var i;
	for(i=0; i<strIn.length; i++)
		if (strIn.charAt(i) != " ")
			break;
	if(i<strIn.length)
		return(strIn.substring(i, strIn.length));
	else
		return("");
}

/**
* This function is to trim trailing spaces
*/
function rTrim(strIn) {
	var i;
	if (strIn.length > 0) {
		for(i= strIn.length-1; i>=0; i--)
			if (strIn.charAt(i) != " ")
				break;
		if(i>=0)
			return(strIn.substring(0, i+1));
		else
			return("");
	}
	else {
		return("");
	}
}


/**
* This function is to trim leading and trailing spaces
*/
function trim(strIn) {
	var str="";
	str = lTrim(rTrim(strIn));
	return(str);
}

/**
* This function is to trim leading and trailing spaces from the textbox passed
*/

function trimTextBox(theForm, theTextBox)
{

	if (document.forms[theForm].elements[theTextBox].value.length > 0) {
	/* Try to trim spaces only if the text box contains something */
		var theValue = trim(document.forms[theForm].elements[theTextBox].value);

		if (NS6 && document.forms[theForm].elements[theTextBox].value.length != theValue.length) 
		 /* In Netscpae 6.0, a Text box cannot be set to its trimmed content directly.
		 * So the assignment below will set the value to "." temporarily so that it can be set to the trimmed value later */
			document.forms[theForm].elements[theTextBox].value = ".";

		document.forms[theForm].elements[theTextBox].value = theValue;
	}
}


// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. Instead,
// please just point to my URL to ensure the most up-to-date versions
// of the files. Thanks.
// ===================================================================


/* 
AnchorPosition.js
Author: Matt Kruse
Last modified: 2/18/02

DESCRIPTION: These functions find the position of an <A> tag in a document,
so other elements can be positioned relative to it.

COMPATABILITY: Works with Netscape 4.x, 6.x, IE 5.x on Windows. Some small
positioning errors - usually with Window positioning - occur on the 
Macintosh platform.

FUNCTIONS:
getAnchorPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor. Position is relative to the PAGE.

getAnchorWindowPosition(anchorname)
  Returns an Object() having .x and .y properties of the pixel coordinates
  of the upper-left corner of the anchor, relative to the WHOLE SCREEN.

NOTES:

1) For popping up separate browser windows, use getAnchorWindowPosition. 
   Otherwise, use getAnchorPosition

2) Your anchor tag MUST contain both NAME and ID attributes which are the 
   same. For example:
   <A NAME="test" ID="test"> </A>

3) There must be at least a space between <A> </A> for IE5.5 to see the 
   anchor tag correctly. Do not do <A></A> with no space.
*/ 

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (use_gebi && document.all) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=o.offsetLeft; y=o.offsetTop;
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}


// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
	var ol=el.offsetLeft;
	while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
	return ol;
	}
function AnchorPosition_getWindowOffsetLeft (el) {
	return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
	}	
function AnchorPosition_getPageOffsetTop (el) {
	var ot=el.offsetTop;
	while((el=el.offsetParent) != null) { ot += el.offsetTop; }
	return ot;
	}
function AnchorPosition_getWindowOffsetTop (el) {
	return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
	}
