/**
This is the ERIC 1.5 JavaScript File
it contains all JavaScript functions being called from the Search
@version 0.1
@date 06.20.2006
*/
var currentOverId = "";
var mouseIsOver = false;
var currentTimeout = null;
var thumbUrl = "";
// default value for bmw
var overDivTop = 376;

var indOverDiv = true;

function showOverDiv(text)
{
  if(mouseIsOver) {
    return overlib(text, WIDTH, 720, HAUTO, FIXX, 86, FIXY, overDivTop, FGCOLOR, 'CEDEFF', BGCOLOR, '0099FF', BORDER, '0');
  }else{
    return null;
  }
}

/**
 * #################################################
 * AJAX FUNCTIONS FOR EVALUATION HERE
 * #################################################
 *
 */
function createRequestObject() {
    var ro;
    if(window.ActiveXObject){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var req = createRequestObject();

function showThumbPreview(carID, tUrl) {
    // add timeshift 1 second here
    thumbUrl = tUrl;
    mouseIsOver = true;
    currentOverId = carID;
    currentTimeout = window.setTimeout("doPreview()", 1000);
}
function mouseOutThumb()
{
  mouseIsOver = false;
  window.clearTimeout(currentTimeout);
  if(req.readyState == 4)
  {
    nd();
  }
}
function doPreview()
{
    if(mouseIsOver)
    {
      url = thumbUrl + "?thumbId=" + currentOverId;
      req.open('post', url);
      req.onreadystatechange = processPreview;
      req.send(null);
    }

}


function processPreview()
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			// process
          if(indOverDiv)
          {
			// individual function can be defined on page and var indOverDiv = true
            return showOverDiv(req.responseText);
	 	  }
          else
          {
            return overlib(req.responseText, WIDTH, 720, HAUTO, FIXX, 86, FIXY, 376, FGCOLOR, 'CEDEFF', BGCOLOR, '0099FF', BORDER, '0');
          }


		}
		else
		{
			// Error
		}

	}
}
/**
 * Calls a url wehn selecting a table row
 */
function callURL(url, requestParamName, requestParamValue)
{
	var completeURL = url + "?" + requestParamName + "=" + requestParamValue;
	window.location.href = completeURL ;
}


/**
 * submits the form if a search Element has changed it's value
 * is being called through an onChange / onClick Handler
*/
function itemChanged(item)
{
    showLayer("transSearchBlocker");
    hideAllSelectBoxes();
	item.form.submit();
}
function hideAllSelectBoxes()
{
    var numForms = document.forms.length;
    for(i  = 0;i < numForms; i++)
    {
      var numEl = document.forms[i].elements.length;
      for(z = 0;z < numEl;z++)
      {
        if(document.forms[i].elements[z].type == "select-one" || document.forms[i].elements[z].type == "select-multiple")
        {
          document.forms[i].elements[z].style.visibility = 'hidden';
        }
      }

    }
}
function showAllSelectBoxes()
{
    var numForms = document.forms.length;
    for(i  = 0;i < numForms; i++)
    {
      var numEl = document.forms[i].elements.length;
      for(z = 0;z < numEl;z++)
      {
        if(document.forms[i].elements[z].type == "select-one" || document.forms[i].elements[z].type == "select-multiple")
        {
          document.forms[i].elements[z].style.visibility = 'visible';
        }
      }

    }
}

/**
 * Chekcbox was chekced - empty function call
 */
function checked(item)
{
	// do nothing so far
}

/**
 * highlights the background color of a row
 * and changes the cursor type
 *
 */
function highlightRow(id, color)
{
	var currentRow = document.getElementById('result_row' + id);
	currentRow.style.cursor='pointer';
	currentRow.style.backgroundColor= color ;
}
/**
 * resets the background color of a row to its original style
 *
 */
function resetRowColor(id, color)
{
	var currentRow = document.getElementById('result_row' + id);
	currentRow.style.backgroundColor= color;
}
/** displays a thumbnail of the car option in the Options / Configuration tab when the mouse moves over a checkbox */
function switchCarFeatureImage(imageURI, defaultURI) {
		var image = document.getElementById("carFeatureImage");
		// From HTML tag :
		// <img src="picture1.gif" onerror="this.onerror=null;this.src='missing.gif';">
		var img = new Image();
		if(defaultURI == null)
		{
			defaultURI = imageURI;
		}

		image.onerror = function (evt) {
  			this.src = defaultURI;
  		}
		image.src = imageURI;
	}

  /**
   * functions for search Tab PRice
  */
function disablePriceFields(enable)
        {
          var dddownpayment = document.getElementById("downpayment");
          var ddmonthlyrate = document.getElementById("monthlyrate");
          var ddduration = document.getElementById("duration");
          var ddmaxprice = document.getElementById("ddmaxprice");

          dddownpayment.disabled = !enable;
          ddmonthlyrate.disabled = !enable;
          ddduration.disabled = !enable;
          ddmaxprice.disabled = enable;

        }
function enablePriceSearch()
        {
          var radiomaxprice = document.getElementById("maxprice");
          var radiofinance = document.getElementById("calculation");
          radiofinance.checked = false;
          radiomaxprice.checked = true;
          disablePriceFields(false)
        }
function enableFinanceSearch()
        {
          var radiomaxprice = document.getElementById("maxprice");
          var radiofinance = document.getElementById("calculation");
          radiofinance.checked = true;
          radiomaxprice.checked = false;
          disablePriceFields(true)
        }

/**
 * Diashow from ERIC 1.0
 *
*/
var currentImageNumber = 0;

function Diashow(name, imageUris, width, height, border) {

  this.name = name;
  this.imageUris = imageUris; // Array
  this.width = width;
  this.height = height;
  this.border = border;
  this.timer;


  this.initialize = function() {
    if(this.imageUris.length < 1)
      alert('Please specify image URIs for diashow.');
  }
  this.initialize();

  this.showImage = function() {
    var image = document.getElementById(this.name);
    currentImagePath = imageUris[currentImageNumber];
    image.src = currentImagePath;
    if(currentImageNumber == imageUris.length - 1)
    {
      currentImageNumber  = 0
    }
    else
    {
      currentImageNumber ++;
    }
  }

  this.start = function(interval) {


    if(!window.diashows) {
      window.diashows = new Array();
    }
    window.diashows.push(this);
    var functionCall = "window.diashows[" + (window.diashows.length - 1) + "].showImage()";
    if( window.setInterval )
      this.timer = window.setInterval(functionCall, interval);
  }

  this.addAttribute = function(name, value) {
    if(value != UNDEF)
      return name + ":" + value + ";";

    return "";
  }

  this.stop = function() {
    if(this.timer)
      window.clearInterval(this.timer);
  }
}

function showLayer(layerId)
{
      obj = document.getElementById(layerId);
      v= 'show';
      if (obj.style)
      {
        obj=obj.style;
        v = 'visible';
      }
      obj.visibility= v;
}

function hideLayer(layerId)
{
      obj = document.getElementById(layerId);
      v= 'hide';
      if (obj.style)
      {
        obj=obj.style;
        v = 'hidden';
      }
      obj.visibility= v;
}
function toggleLayer(layerId)
{
			obj = document.getElementById(layerId);
      v= 'show';
      if (obj.style)
      {
        obj=obj.style;
        v = 'visible';
      }
      if(obj.visibility == v)
      {
      	hideLayer(layerId);
      }
      else
      {
      	showLayer(layerId)
      }
}


function showPopup(uri, name, width, height) {

  var _name = "EricPopup";
  var _config = "toolbar=no, scrollbars=yes,resizable=yes, status=no";
  var _top = 0;
  var _left = 0;

  if (name && name.length > 0) {
    _name = name;
  }

  if (width > 0 && height > 0) {
  	if(width <= window.screen.width && height <= window.screen.height)
  	{
    	_top  = Math.max((window.screen.height - height) / 2, 0);
    	_left = Math.max((window.screen.width - width) / 2, 0);
    }
    else
    {
    	width = window.screen.width - 20;
    	height = window.screen.height - 60;
    }
    _config += ",top="+_top+",left="+_left+",width="+width+",height="+height+"";
  }

  var popup = window.open(uri, _name, _config);
  popup.focus();
}


function showDealerContactPopup(url)
{
  var left;
  var top;
  var w = 740;
  var h = 590;
  if (window.screen.width < 1024) {
    left = 0;
    top  = 0;
  } else {
    top  = (window.screen.height - h) / 2;
    left = (window.screen.width - w) / 2;
  }

  detailimg = window.open(url,"HugeImageView", "toolbar=no,scrollbars=no,resizable=no,top="+top+",left="+left+",width="+w+",height="+h+"");
}

function showImagePopup(url, w, h)
{
  var left;
  var top;

  if (window.screen.width < 1024) {
    left = 0;
    top  = 0;
  } else {
    top  = (window.screen.height - h) / 2;
    left = (window.screen.width - w) / 2;
  }

 detailimg = window.open(url,"HugeImageView", "toolbar=no,scrollbars=no,resizable=no,top="+top+",left="+left+",width="+w+",height="+h+"");

}
function showImagePopupReturn(url, w, h)
{
  var left;
  var top;

  if (window.screen.width < 1024) {
    left = 0;
    top  = 0;
  } else {
    top  = (window.screen.height - h) / 2;
    left = (window.screen.width - w) / 2;
  }

 detailimg = window.open(url,"HugeImageView", "toolbar=no,scrollbars=no,resizable=no,top="+top+",left="+left+",width="+w+",height="+h+"");
 return detailimg;
}

function showPrintPopupReturn(url)
{
 detailimg = window.open(url,"HugeImageView", "toolbar=no,scrollbars=no,resizable=yes,top=0,left=0,height=600,width=980");
 return detailimg;
}

function showPrintPopup(url)
{
  showPrintPopupReturn(url);
}

function showImagePopupBmw(url)
{
  var w = 700;
  var h = 600;
  showImagePopup(url, w, h);

}
function showImagePopupMini(url)
{
  var w = 600;
  var h = 705;
  showImagePopup(url, w, h);

}

function switchLargeImg(imgSrc, isCosy)
{
  var largeImg = document.getElementById("vehicleLargeImg");
  largeImg.src = imgSrc;
  var div = document.getElementById("cosyImgTextDIV");

  if(isCosy == 'true')
  {
    div.style.display = "inline";
  }
  else
  {
    div.style.display = "none";
  }

}
function openPdf(url)
{
  pdfwin = window.open(url, "pdf");
}

function openLegalDisclaimerWindow(disclaimerURL) {

	//A very simple test to check if the resource key url contains an url that at the very least contains 'http'.

	if( disclaimerURL .indexOf("http") != -1) {
		window.open(disclaimerURL,"","scrollbars=auto,resizable=yes,menubar=no,toolbar=no, status=yes, width=730, height=610, top=25, left=100");
	}
}

function checkAreaLength(field, maxlength)
{
  completeText = field.value;
  ist = completeText.length;

  rest = maxlength - ist;
  if (rest < 0)
  {
    field.value = completeText.substr(0,maxlength);
  }
}
