// JavaScript Document

function AjaxRequest(requestType, targetUrl, parameters, resultDiv, startFunc, endFunc, errorFunc, repeatCount){
	if (!repeatCount && repeatCount != 0) repeatCount=2;
	if (startFunc) startFunc();
   $.ajax({
      type: requestType,
      url: targetUrl,
      timeout: 5000, 
      data: parameters,
      contentType: "application/x-www-form-urlencoded",
      dataType: "html",
      success: function(response) {
         $('#'+resultDiv).html(response);
         if (endFunc) endFunc();
      },
			error: function( objAJAXRequest, strError ){
//				if (strError == 'timeout'){
					if (repeatCount < 0){
						if (errorFunc) errorFunc(parameters);
					}else{
						AjaxRequest(requestType, targetUrl, parameters, resultDiv, startFunc, endFunc, errorFunc, --repeatCount);
					}
//				}else{
//					alert(strError);
//				}
			}
   });
 }


function jQueryAjaxPost(targeturl,parameters,resultdiv){
   $.ajax({
      type: "POST",
      url: targeturl,
      data: parameters,
      contentType: "application/x-www-form-urlencoded",
      dataType: "html",
      success: function(response) {
         $('#'+resultdiv).html(response);
      }
   });
}
function jQueryAjaxGet(targeturl,parameters,resultdiv){
   $.ajax({
      type: "GET",
      url: "../code/navigate.php",
      data: parameters,
      contentType: "application/x-www-form-urlencoded",
      dataType: "html",
      success: function(response) {
         $('#'+resultdiv).html(response);
      }
   });
}

function preinit()
 {
 	x=document.location.href;
  if (document.location.search.indexOf("MIT") >0 || document.location.href.indexOf("mit.de") > 0) 
       return true;
 	if (x.indexOf("/marine/") >0 || x.indexOf("/garden/") >0 || x.indexOf("/garten/") >0  || x.indexOf("/industrie/") >0  || x.indexOf("/industry/") >0) 
 	 return false;
 	return true;
}

function init() {
	if (!(self.isLoaded))
	 {
	  is = new getClient();
	  if (preinit()==false)
	   {
		for (var i=0; i<document.images.length; i++) {
   		document.images[i].oncontextmenu = function() {return false;};
		  document.images[i].galleryimg = 'no';
   	}
   }
   	self.isLoaded = true;
   }
}
var is = null;

function getClient() {
  // convert all characters to lowercase to simplify testing
  var agt=navigator.userAgent.toLowerCase()
  var apv=navigator.appVersion.toLowerCase()
  this.major = parseInt(navigator.appVersion)
  this.minor = parseFloat(navigator.appVersion)
  // browserversion
  this.opera = (agt.indexOf('opera')!=-1);
  this.ns  = ((agt.indexOf('mozilla')!=-1) && ((agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1)))
  this.ns4 = (this.ns && (this.minor >= 4.03) && (this.major < 5))
  this.ns6 = (this.ns && (this.major == 5) || this.opera)
  this.gecko = (this.ns && (this.major >= 5))
  this.ie   = (agt.indexOf("msie") != -1)
  this.ie4  = (this.ie && (this.major == 3))
  this.ie45 = (agt.indexOf('msie 4.5') != -1);
  this.ie5  = (this.ie && (this.major == 4))
  // platform
  this.mac = (apv.indexOf("macintosh")>0);
  this.win = (apv.indexOf("win")>0);	
  // compatible browsers
  this.ie4comp = ((this.ie4 && !this.mac) || this.ie45 || this.ie5)
  this.ns4comp = (this.ns4);
  this.ns6comp = (this.gecko || this.ns6);
  this.comp = (this.ie4comp || this.ns4comp || this.ns6comp);
  return (this)
}



// --------------------------------------------------------------------------------------------------
// Get Querystring Parameters

function getQueryStringParams() {
    GetParams = new Array();
    if (document.location.search) {
        QueryString = unescape(document.location.search.substring(1,document.location.search.length));
        KeyValuePairs = QueryString.split("&");
        for (i=0; i< KeyValuePairs.length; i++) {
            Param = KeyValuePairs[i].split("=");
            GetParams[Param[0]] = Param[1];
        }
    }
    return GetParams;
}


// --------------------------------------------------------------------
// Select randomly an Image out of an Array
// and change source of image tag

var currImageArrayIdx = 0;
var MainImageLoaded = false;

function showRandomImage(imageArray, imageId) {
    if (imageArray.length > 1) {
        if (document.getElementById) {
            var idx = Math.ceil(Math.random()*imageArray.length) - 1;
            while (idx == currImageArrayIdx) {
                idx = Math.ceil(Math.random()*imageArray.length) - 1;
            }
            currImageArrayIdx = idx;
        }
     	sn=imageArray[currImageArrayIdx];
    	if(sn.length>0 && sn.indexOf(".swf")<0) {
    		if(document.getElementById(imageId))
         document.getElementById(imageId).src = imageArray[idx];
      }
      else
      	{
      		 showRandomImage(imageArray, imageId);
      	}
    }
}


// --------------------------------------------------------------------
// Set Loading Status of MainImage
var random=1;
function setMainImageLoadState() {
	fname=(random==1) ? "Random":"Next";
    window.setTimeout("show"+fname+"Image(homeImages, 'MainImage')", 4500);
}

// --------------------------------------------------------------------
// Select next Image not Randomized 
function showNextImage(imageArray, imageId) {
    if (imageArray.length > 1) {
    	currImageArrayIdx =(currImageArrayIdx < (imageArray.length-1)) ? currImageArrayIdx+1:0;
    	sn=imageArray[currImageArrayIdx];
    	if(sn.length>0 && sn.indexOf(".swf")<0 ) {
        document.getElementById(imageId).src = imageArray[currImageArrayIdx];
      }
      else {
      	 showNextImage(imageArray, imageId);
      }
    }
}

var http_request = false;
function ReplaceLoadingTable(LoadingDiv,tekst) {
   if(document.getElementById(LoadingDiv)){
      document.getElementById(LoadingDiv).innerHTML = tekst;
      document.getElementById(LoadingDiv).style.display = "block";
   }
}
function makePOSTRequest(url, parameters, resultdiv) {
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
//    alert(http_request);
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
   http_request.onreadystatechange = function() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById(resultdiv).innerHTML = result;
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}


function openImageZoomWindow(imgURL,imgWidth,imgHeight){
   tmpStr = 'width='+(imgWidth + 30)+',height='+(imgHeight+50);
   tmpStr = 'toolbar=no,location=no,diretories=no,scrollbars=yes,status=no,menubar=no,resizable=yes,'+tmpStr;
   ImageZoomWindow = window.open(imgURL,'ImageZoomWindow',tmpStr);
   ImageZoomWindow.focus();
}
/*


// --------------------------------------------------------------------
// Set/Unset Colorcubes / Accessories Icons

var currentCubeContent = "&nbsp;"

function setCube(id, text) {
    if (document.getElementById) {
        if (document.getElementById("Cube"+id)) {
            currentCubeContent =  document.getElementById("Caption").innerHTML;
            document.getElementById("Caption").innerHTML = text;
        }
    }
}

function unsetCube(id, text) {
    if (document.getElementById) {
        if (document.getElementById("Cube"+id)) {
            document.getElementById("Caption").innerHTML = currentCubeContent;
        }
    }
}

function setThumb() {
    if (document.getElementById) {
        if (document.getElementById("Caption")) {
            document.getElementById("Caption").innerHTML = "";
        }
    }
}

function unsetThumb() {
    if (document.getElementById) {
        if (document.getElementById("Caption")) {
            document.getElementById("Caption").innerHTML = "";
        }
    }
}

// --------------------------------------------------------------------
// Show/Hide Tooltip

function showTooltip(evt, id) {

	var x, y;
	e = evt || window.event;
	if (!e) return null;

	if (webclient.ns4) {
		x = e.pageX;
		y = e.pageY;
	} else if (webclient.op) {
		x = e.clientX;
		y = e.clientY;
	} else if (webclient.ie) {
		x = e.x + document.body.scrollLeft;
		y = e.y + document.body.scrollTop;
	} else if (webclient.moz5) {
		x = e.pageX;
		y = e.pageY;
	}

	obj = MM_findObj(id);
	//status = obj + " at position " + x + "/" + y;
	if (obj) {
		if (webclient.ns4) {
			obj.left = x - obj.document.width - 5;
			obj.top  = y - obj.document.height - 5;
			//obj.style.top = (obj.style.top < 380) ? 380 : obj.style.top;
			obj.visibility = 'visible';
		}  else if (webclient.ie || webclient.moz5) {
			x = x - obj.offsetWidth - 5;
			y = y - obj.offsetHeight - 5;
			//y = (y < 380) ? 380 : y;
			if (webclient.ie && webclient.mac) {
				x = x + 'px';
				y = y + 'px';
			}
			//obj.style.left = x;
			obj.style.top  = y;
			obj.style.visibility = 'visible';
		} else if (webclient.op){
        	//obj.style.left = x - obj.style.pixelWidth - 5;
			obj.style.top  = y - obj.style.pixelHeight - 5;
			//obj.style.top = (obj.style.top < 380) ? 380 : obj.style.top;
			obj.style.visibility = 'visible';
		} else {
			//obj.style.left = x - 220;
			obj.style.top  = 380;
			obj.style.visibility = 'visible';
    	}
	}
}

function hideTooltip(id) {
	MM_showHideLayers(id, '', 'hide');
}


// --------------------------------------------------------------------------------------------------
// Function to open popup window for used motorcycles

function showUsedMCs(destUrl) {

	dUrl =(showUsedMCs.arguments.length >0) ?  destUrl:"/redirs/used_mcs.html";
	FE = window.open(dUrl,"usedMCs","width=665,height=580,screenX=5,screenY=5,locationbar=no,menubar=no,status=no,resizable=yes,scrollbars=yes,scrolling=auto");
}



// --------------------------------------------------------------------------------------------------
// Common function to open a popup window

var xwin=null;

function closeWindow() {

  if (xwin.closed==false)
     {
      xwin.close();
      xwin=null;
     };

}

// this function is used by imageshuffle.swf
	function openNewWindow(URLtoOpen,windowName, windowFeatures) {
		newWindow=window.open(URLtoOpen, windowName, windowFeatures);
	}
// end of imageshuffle.swf-window

function openWindow(destination, windowname, xsize, ysize, scrolling,res) {

	if (xwin != null) { closeWindow(); }
	if (res==''  || res==null) {
		res="no";
	}
	 

 	if (webclient.moz5) {
 		res="yes";
 		newx = xsize + 0;
  		newy = ysize + 0;
  	} else {
  		newx = xsize;
  		newy = ysize;
  	}

  	scrollflag = scrolling;

  	if (parseInt(navigator.appVersion) >= 4.0) {
  		POSX = (screen.availWidth-newx)/2;
    	POSY = (screen.availHeight-newy)/2;
 	} else {
  		POSX = 0;
  		POSY = 0;
  	}

	fstring ='toolbar=no,locationbar=no,directories=no,status=no,menubar=no,scrollbars=' + scrollflag + ',resizable='+res+',screenX='+POSX+',screenY='+POSY+',width='+newx + ',height='+newy;
	xwin = window.open(destination,windowname,fstring);

	if (navigator.appName != 'Netscape' && xwin != null ) {
   		xwin.focus();
   		xwin.moveTo((screen.availWidth-newx)/2,(screen.availHeight-newy)/2);
   		//xwin.resizeTo(newx,newy);
   	} else {
   		if (xwin)
   		  xwin.focus();
   	}

	if (navigator.appName == 'Netscape' && parseInt(navigator.appVersion) >= 4.0 && parseInt(navigator.appVersion) < 5.0)
   		if (xwin)
   		  xwin.resizeTo(newx,newy);
	if (navigator.appName == 'Netscape' && parseInt(navigator.appVersion) >= 5.0)
   		if (xwin)
   		  xwin.focus();
}

// --------------------------------------------------------------------------------------------------
// Function to open popup window for automatic generated pdf
function openPrintPDF(newurl)
{
  pdfwin = window.open(newurl,'pdfwin','toolbar=no,locationbar=no,directories=no,status=no,menubar=yes,scrollbars=auto,resizable=yes,width=600,height=400');
}


// --------------------------------------------------------------------------------------------------
// Function to open popup window for a enlarged view on an image

var xwin=null;

function showImage(imgName,newx,newy,ScreenX,ScreenY) {

 	picture = imgName.substring(0,imgName.length-6) + '_l.jpg'

	POSX = 400;
	POSY = 0;

	newx += 20;
	newy += 65;

	if (ScreenX == -1) {
  		if (parseInt(navigator.appVersion) >=4.0) {
    		POSX = (screen.availWidth-newx)/2;
    		POSY = (screen.availHeight-newy)/2;
    	} else {
    		POSX = 0;
    		POSY = 0;
    	}
	} else {
		if ((ScreenX != null) && (ScreenY != null)) {
			POSX = ScreenX;
			POSY = ScreenY;
		}
	}


  	if (xwin != null && navigator.appName !='Netscape') {
  		xwin.close();
  		xwin = null;
  	}

	if (xwin != null  && navigator.appName =='Netscape')
  		if (xwin.closed == false) {
      		xwin.location.href=picture;
      		xwin.resizeTo(newx,newy);
      		xwin.focus();
      		return;
     	}

	fstring ='toolbar=no,locationbar=no,directories=no,status=no,menubar=no,scrollbars=auto,resizable=yes,screenX='+POSX +',screenY='+POSY +',width='+newx + ',height='+newy;

	xwin=window.open(picture,'projector',fstring);

	if (navigator.appName !='Netscape' && xwin != null ) {
   		xwin.moveTo(POSX,POSY);
   		xwin.resizeTo(newx,newy);
	} else {
		xwin.focus();
	}

	if (navigator.appName =='Netscape')
   		xwin.resizeTo(newx,newy);
    xwin.focus();
	return;
}

//-----------------------------------------------------------
// functions for servicelinks in upper left box

function productInquiry(targetURL, model) {
    document.location.href = targetURL + "?model=" + escape(model);
}

function getTestdrive() {
    obj=MM_findObj("testdriveform");
	if (obj) {
	  obj.submit();
	 }
}

function getBrochure() {
	obj=MM_findObj("brochureform");
	if (obj) {
	  obj.submit();
	}
}

function getRequest() {
	obj=MM_findObj("contactform");
	if (obj) {
	  obj.submit();
	}
}

// ----------------------------------------------------------
// Call Search for Rent- and Testdrive-Vehicles

function searchDRVehicles(type, model_id) {
    document.RentDemoForm.model_id.value = model_id;
    document.RentDemoForm.type.value = type;
    document.RentDemoForm.submit();
}

// ----------------------------------------------------------
// Open History PopUp

function openHistory() {
    void(window.open('/specials/company/history/index.html','History','width=790,height=500,left=0,top=0'));
}

// ----------------------------------------------------------
// Open FaqMC

function FaqMC(nr) {
	 openWindow('/content/service/mc/'+nr+'.html','faqpopup',510,400,'yes');
}

// ----------------------------------------------------------
// Open FaqCAR

function FaqCAR(nr) {
	 openWindow('/content/service/car/'+nr+'.html','faqpopup',510,400,'yes');
}

// ----------------------------------------------------------
// Open Jobs PopUp

function openJobs() {
    void(window.open('/redirs/jobs.html','Jobs','width=810,height=500,left=0,top=0'));
}

// ----------------------------------------------------------
// Replace Location on Quickfinder change
var executedQuickfinder=false;
function execQuickfinder(dest) {
	executedQuickfinder=true;
    if ((dest != "") && (dest.indexOf("@@") == -1)) {
        document.location.href = dest;
    } else {
        UrlTargetPair = dest.split("@@");
        if (UrlTargetPair.length == 2) {
            if (UrlTargetPair[0] != "") {
                if (UrlTargetPair[1] == "_blank") {
                    window.open(UrlTargetPair[0]);
                } else {
                    document.location.href = UrlTargetPair[0];
                }
            }
        }
    }
}

// --------------------------------------------------------------------------------------------------
// Open Car Special "Civic 2004"




// ----------------------------------------------------------
// capture context menu and forget

 function contMen(E) { 	init();}

 document.oncontextmenu = contMen;


// --------------------------------------------------------------------------------------------------
// Open allgemeines, zentriertes Popup, z.B. KKonfigurator


function popup(URL,popupname,b,h,scrollbars) {
	var PopUpName = popupname || 'PopUp';
	var breite = b || 410;
	var hoehe = h || 450;
	var ScrollBars = scrollbars || 'auto';
	var xPos = (screen.availWidth - breite) / 2
	var yPos = (screen.availHeight - hoehe ) / 2
	window.open(URL,PopUpName,'width='+breite+',height='+hoehe+',scrollbars='+ScrollBars+',toolbar=no,menubar=no,screenX='+xPos+',screenY='+yPos+',left='+xPos+',top='+yPos);
}
*/
