var newWindow = true;

function openWindow(imgName, W, H, features){

	newWindow = window.open(imgName, 'name', 'width='+W+',height='+H+','+features+'')
	newWindow.focus()
}

/* ----------------------------------------------------------------------- */
/*                          Image Gallery                                  */
/* ------------------------------------------------------------------------*/

var galleryImages      = new Array();
var currentImageIndex  = 0;

function numGalleryImages() {
  return galleryImages.length;
}

function loadGalleryImage(imageURL) {
  galleryImages.push(imageURL);
}

function getArrayIndex(array, item) {
  var i;
  for (i in array) {
    if (array[i] == item) {
      return i;
    }
  }
  return null;
}

function changeImage(imageName, newImageURL) {
  var image = window.document.getElementById(imageName);
	if (image != null)
	{
		image.src         = newImageURL;
		currentImageIndex = getArrayIndex(galleryImages, newImageURL);
		return false;
	}
}

function nextImage(imageName) {
  var imageCount = numGalleryImages();
  if (imageCount > 0) {
	  if (currentImageIndex < (imageCount - 1))   {  currentImageIndex++;  }
	  else                                  {  currentImageIndex = 0;  }

	  changeImage(imageName, galleryImages[currentImageIndex]);
  }
}

function previousImage(imageName) {
  var imageCount = numGalleryImages();
  if (imageCount > 0) {
	  if (currentImageIndex == 0 )   {  currentImageIndex = (imageCount - 1); }
	  else                           {  currentImageIndex--;  }

	  changeImage(imageName, galleryImages[currentImageIndex]);
  }
}

function isNetscape()
{
	if (navigator.appName == "Netscape")
	  return true;
	else
	  return false;
}


/*------------------------------------------------------------------------------------------------*\
  Function:    textCounter
  Description: used throughout the marketplace to provide the user with a visibly decrementing 
	             text counter field when entering certain form fields.
\*------------------------------------------------------------------------------------------------*/
function textCounter(field, cntfield, maxlimit)
{
  if (field.value.length > maxlimit)
    field.value = field.value.substring(0, maxlimit);
  else
    cntfield.value = maxlimit - field.value.length;
}


<!-- Disable "Enter" key in Form script -->
function handleEnter (field, event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13)
	{
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 0) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	}
	else
		return true;
}


/*----------------------------------------------------------------------------*\
  Function:  hideShowBiddingFields
  Synopsis:  used in the eCommerce OAT page to toggle the visibility of
             bidding fields, depending on whether the selected model includes
             bidding or not. Called by the onchange handler of the neg model
			 radio field.
  Notes:     if JS is disabled, all fields are shown by default.
\* ---------------------------------------------------------------------------*/
function hideShowBiddingFields(selectedNegID)
{
  var biddingNegID              = 0;
  var biddingAndFixedPriceNegID = 1;

  if (selectedNegID == biddingNegID)
  {
    document.getElementById("biddingFields").style.display = 'block';
    //document.getElementById("biddingFields2").style.display = 'block';
	  document.getElementById("nobiddingFields").style.display = 'none';
  }

  else if (selectedNegID == biddingAndFixedPriceNegID)
  {
    document.getElementById("biddingFields").style.display = 'block';
//    document.getElementById("biddingFields2").style.display = 'none';
    document.getElementById("nobiddingFields").style.display = 'block';

	if (document.getElementById("durationField"))
      document.getElementById("durationField").style.display = 'none';
  }


  else
  {
    document.getElementById("biddingFields").style.display = 'none';
    //document.getElementById("biddingFields2").style.display = 'none';
	  document.getElementById("nobiddingFields").style.display = 'block';
  }

}


/*----------------------------------------------------------------------------*\
  Function:  initHideShowBiddingFields
  Synopsis:  helper function that's called on page load (ideally), or after
             the negotiation fields have loaded. It determines which is selected
			 and hides/shows the bidding fields appropriately.
\* ---------------------------------------------------------------------------*/
function initHideShowBiddingFields()
{
  for (var i=0; i<document.forms[0]._fnegotiationmodel.length; i++)
  {
    if (document.forms[0]._fnegotiationmodel[i].checked)
	  hideShowBiddingFields(document.forms[0]._fnegotiationmodel[i].value);
  }
}


/*----------------------------------------------------------------------------*\
  Function:  returnObjById
  Synopsis:  generic cross-browser friendly way of locating an object by ID.
\* ---------------------------------------------------------------------------*/
function returnObjById(id)
{
  if (document.getElementById)
    var el = document.getElementById(id);
  else if (document.all)
    var el = document.all[id];
  else if (document.layers)
    var el = document.layers[id];

  return el;
}



/*----------------------------------------------------------------------------*\
  Function:  submitPhoto
  Synopsis:  called by submit photos buttons on the photos page. It determines
  			 if a photo is being uploaded and if so (a) disables alll submit
  			 buttons, (b) hides the page content and displays a panel saying
  			 "Your images are being uploaded".
\* ---------------------------------------------------------------------------*/
function submitPhoto(event)
{
  var currForm = event ? event.target : this;

  // First, find out if any images are being uploaded
  var isUploadingImage = false;
  for (i=0; i<currForm.elements.length; i++)
  {
    if (currForm.elements[i].type == "file")
	{
      if (currForm.elements[i].value)
	  {
	    isUploadingImage = true;
		break;
	  }
    }
  }

  // we're uploading an image
  if (isUploadingImage)
  {
    window.scrollTo(0,0); // scroll to the top of the page

    // disable all submit buttons
    for (i=0; i<currForm.elements.length; i++)
    {
      if (currForm.elements[i].type == "submit" || currForm.elements[i].type == "image")
	    currForm.elements[i].readonly = true;
    }

    // for Safari, move the page offscreen rather than hiding it (bug #1216)
	if (navigator.userAgent.toLowerCase().indexOf("safari"))
	{
	  returnObjById("body").style.position = 'absolute';
	  returnObjById("body").style.left = '-5000px';
	  returnObjById("body").style.top = '-5000px';
	}
	else
	{
      // hide the page content and display a Loading screen
	  returnObjById("body").style.display = 'none';
	}

	// hide the page content and display a Loading screen
	returnObjById("replacement_body").style.display = 'block';
  }

  // IE workaround. We have to reset the animated gif source after submitting, since IE
  // prevents it from working
  setTimeout('returnObjById("processingimage").src = "images/processing.gif"', 200);

  // submit the form
  this.submit();
}


function toggleVisibility(id)
{
  state = document.getElementById(id).style.display;

  if (state == 'block') { state = 'none'; }
  else                  { state = 'block'; }

  if (document.getElementById)  { document.getElementById(id).style.display = state; }  // Modern Browsers
  else {
  	if (document.layers)        { document.id.display = state; }                        // Netscape 4
  	else                        { document.all.id.style.display = state; }              // IE 4
  }
}

function MM_preloadImages() { //v3.0
  var currentDocument = document;
  if(currentDocument.images)
  {
    if(!currentDocument.MM_p) {
      currentDocument.MM_p = new Array();
    }

    var i,j = currentDocument.MM_p.length, a = MM_preloadImages.arguments;
    for(i = 0; i < a.length; i++)
    {
      if (a[i].indexOf("#") != 0)
      {
        currentDocument.MM_p[j] = new Image;
        currentDocument.MM_p[j++].src = a[i];
      }
    }
  }
}

function MM_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 MM_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 = MM_findObj(n, d.layers[i].document);
      }

      if (!x && d.getElementById)
      {
        x = d.getElementById(n);
        return x;
      }
    }
  }
}

function MM_swapImage() { //v3.0
  var i,j = 0, x, a = MM_swapImage.arguments;
  document.MM_sr = new Array;
  for (i = 0; i < (a.length - 2); i += 3)
  {
    if ((x = MM_findObj(a[i])) != null)
    {
      document.MM_sr[j++] = x;
      if(!x.oSrc)
      {
        x.oSrc = x.src;
        x.src = a[i + 2];
      }
    }
  }
}

// ----------------------------

var browser=navigator.appName;

// open popup according to browser
function popup()
{
  // find which browser it is
  if (browser.indexOf("Netscape") >= 0)
  {
    var version=navigator.appVersion;
    if (version.indexOf("3.") >= 0 || version.indexOf("4.") >= 0)
      showPopup();
  }
  else if (browser == null)
    showPopup();
  else if (browser.indexOf("Microsoft") >= 0)
  {
    var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    result = re.exec(navigator.userAgent);
    if (result != null)
	{
      if (parseFloat(result[1]) < 5)
        showPopup();
	}
  }
}

function showPopup()
{
  window.open('detect.html','BrowserDetection','scrollbars=yes,resizable=yes,width=400,height=400,screenX=100,screenY=250,top=100,left=250');
}

function openWindow(imgName, W, H, features)
{
  newWindow=window.open(imgName,'name','width=' + W + ',height=' + H + ',' + features + '');
  newWindow.focus();
}



/*----------------------------------------------------------------------------*\
  Function:  build_headline
  Synopsis:  used in the automotive package in the OAT. Builds the headline 
	           based on the Make, Model and Year
\* ---------------------------------------------------------------------------*/
function build_headline(f)
{
  var make  = f._fMake.value.trim();
  var model = f._fModel.value.trim();
  var year  = f._fYear.value.trim();
	
	f._fname.value = year + " " + make + " " + model;
}

String.prototype.trim = function()
{
  return(this.replace(/^\s+/,"").replace(/\s+$/,""));
}



/*----------------------------------------------------------------------------*\
  Function:  check_url
  Synopsis:  used to pop open a window containing a URL entered by a user
\* ---------------------------------------------------------------------------*/
function check_url(url, return_id)
{
  // first check the user HAS entered a URL 
  var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/

  if (!regexp.test(url))
  {
    alert("Please enter a valid URL.");
	return;
  } 

  // now pop it open
  window.open("check_url.jsp?url=" + url + "&return_id=" + return_id, "test_url_page", "height=400,width=700,menu=no,resizable=yes");

}


















