var http_request 		 = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
        //http_request.overrideMimeType('text/xml');
    }
} else if (window.ActiveXObject) { // IE
    try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
    }
}

if (!http_request) {
    alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
}

function dummy() {
	alert('nop');
}
function wo(dateiname, winname, top, left, width, height, scrolling) {
   w = window.open(dateiname,winname,'top='+top+',left='+left+',alwaysRaised=no,location=no,toolbar=no,directories=no,status=no,menubar=no,scrollbars='+scrolling+',resizable=yes,width='+width+',height='+height);
   w.focus();
}
function swapImage(imageid,imagepath) {
	document.getElementById(imageid).src = imagepath;
}
function loadImage(imgsrc) {
		var img;
		img = new Image();
		// load
		// img.onload=function(){};
		img.src = imgsrc;
}
function openImgGallery(gurl,gid) {	
	wo(''+gurl,''+gid,10,10,900,600,'yes');
}
function msg_produktConfigRequest() {		
    if (http_request.readyState == 4) {
        if (http_request.status == 200 || http_request.status == 301) {
        		responseText = http_request.responseText.replace(/\s/g,"");
						if(responseText != "") {
							updateSumme(responseText);
            	return true;
            }
        } else {
            alert('Error [' + http_request.status + '].');
        }
    }
   return false;
}
function pcr(id,opt) {
  try {
  	http_request.open('GET', "/gebrauchtmaschinen/configure.php5?ie=" + new Date() + "&id="+id+"&opt="+opt, true);
  	http_request.onreadystatechange = msg_produktConfigRequest;    	
  	http_request.send(null);
  } catch(e) {alert(e);top.location.href=location.href;}	
}
function updateSumme(p) {
	var summeDiv = document.getElementById('summeDiv');	
	summeDiv.firstChild.data = p;
}
function urlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir
    // %          note: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}
function do_displayForm(formId,setdspl) {
	var myform = null;
	if(document.all) {
		myform = document.all(formId);		
	} else {
		myform = document.getElementById(formId);
	}		
	if(myform) {		
		if(setdspl != null) {
			myform.style.display = (setdspl == true) ? "block" : "";		
		} else {
			if(myform.style.display == "") {
				myform.style.display = "block";
			}
			else {
				myform.style.display = "";								
			}
		}
	}
	return;
}

function do_hideForms(formId) {
	if(formId != 'offer')
		do_displayForm('offer',false);
	if(formId != 'rental')
		do_displayForm('rental',false);
	if(formId != 'hirepurchase')
		do_displayForm('hirepurchase',false);
}

function btn_offerDisplay() {
	do_hideForms('offer');
	return do_displayForm('offer',null);
}
function btn_rentalDisplay() {
	do_hideForms('rental');
	return do_displayForm('rental',null);
}
function btn_hirepurchaseDisplay() {	
	do_hideForms('hirepurchase-myform');	//
	return do_displayForm('hirepurchase-myform',null);
}
function btn_offerSubmit() {
	foffer = document.getElementById('offer');
}
function check_formFields1() {
	var pname  = document.getElementById('param_name').value;
	var pemail = document.getElementById('param_email').value;
	var pprice = document.getElementById('param_offer_price').value;
	//
	if(pname.length == 0) {
		alert('Please fill in field \'Name\'!');
		return false;
	}
	if(pemail.length == 0) {
		alert('Please fill in field \'Email\'!');
		return false;
	}
	if(pprice.length == 0) {
		alert('Please fill in field \'Price\'!');
		return false;
	}
	//
	return true;	
}
function check_formFields2() {
	var pname  = document.getElementById('param_name2').value;
	var pemail = document.getElementById('param_email2').value;	
	//
	if(pname.length == 0) {
		alert('Please fill in field \'Name\'!');
		return false;
	}
	if(pemail.length == 0) {
		alert('Please fill in field \'Email\'!');
		return false;
	}
	//
	return true;	
}
function check_formArray(arElements) {
	for(idx=0; idx < arElements.length; idx++) {
		el = arElements[idx];
		if(el.value && el.value.length > 0)
			continue;
		else {
			alert('Please fill in the required fields!')
			return false;
		}
	}
	return true;
}
function submit_hirepurchaseForm() {
	document.getElementById('post_code').value=Date();
	document.getElementById('form_hirepurchase').action="/kontakt/sendmail.php5?phire=1";
	document.getElementById('form_hirepurchase').submit();
}
function check_offerForm() {
	if(check_formFields1() != true)
		return false;
	//				
	document.getElementById('post_code').value=Date();
	document.getElementById('form_offer').action="/kontakt/sendmail.php5";
	document.getElementById('form_offer').submit();
}
function check_rentalofferForm() {
	if(check_formFields2() != true)
		return false;
	//				
	document.getElementById('post_code').value=Date();
	document.getElementById('form_rental').action="/kontakt/sendmail.php5?prental=1";
	document.getElementById('form_rental').submit();		
}
function check_hirepurchaseDisplay() {
	if(check_formFields2() != true)
		return false;
	//				
	document.getElementById('post_code').value=Date();
	document.getElementById('form_rental').action="/kontakt/sendmail.php5?prental=1";
	document.getElementById('form_rental').submit();		
}
var geocoder;
var map;
function mapsinit(canvas) {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(49.70737, 8.68286);
  var myOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  }
  map = new google.maps.Map(canvas, myOptions);
}
function codeAddress(address) {
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);        
      var marker = new google.maps.Marker({
          map: map, 
          position: results[0].geometry.location
      });
    } else {
      alert("GEO: " + status);
    }
  });
}
/* 
RankingsPush by Michael King 
http://www.seomoz.org/blog/gettings-rankings-into-ga-using-custom-variables
*/
function rankingsPush()
{
  var url = String(document.referrer);

    // confirm they came from G
    if (url.indexOf ("google") !=-1)
    {    
        var urlVars = {};
        var parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value)
        {
            urlVars[key] = value;
        });
        // Push to GA Custom Variables
        _gaq.push(['_setCustomVar', '1', 'Rankings', urlVars["cd"], 1]);
        //
    }
}


