Array.prototype.copy = function () {
	return ((new Array()).concat(this));
};


function makeTwoChars(inp) {
        return String(inp).length < 2 ? "0" + inp : inp;
}

var initAttempts = 0;

function setDates(e) {
        // Internet Explorer will not have created the datePickers yet so we poll the datePickerController Object using a setTimeout
        // until they become available (a maximum of ten times in case something has gone horribly wrong)

        try {
                var sd = datePickerController.getDatePicker("earliestRentalStart");
                var ed = datePickerController.getDatePicker("latestRentalStart");
        } catch (err) {
                if(initAttempts++ < 10) setTimeout("setDates()", 50);
                return;
        }

        // Check the value of the input is a date of the correct format
        var dt = datePickerController.dateFormat(this.value, sd.format.charAt(0) == "m");

        // If the input's value cannot be parsed as a valid date then return
        if(dt == 0) return;

        // At this stage we have a valid YYYYMMDD date

        // Grab the value set within the endDate input and parse it using the dateFormat method
        // N.B: The second parameter to the dateFormat function, if TRUE, tells the function to favour the m-d-y date format
        var edv = datePickerController.dateFormat(document.getElementById("endDate").value, ed.format.charAt(0) == "m");

        // Set the low range of the second datePicker to be the date parsed from the first
        ed.setRangeLow( dt );
        
        // If theres a value already present within the end date input and it's smaller than the start date
        // then clear the end date value
        if(edv < dt) {
                document.getElementById("latestRentalStart").value = "";
        }
}

function removeInputEvents() {
        // Remove the onchange event handler set within the function initialiseInputs
        datePickerController.removeEvent(document.getElementById("earliestRentalStart"), "change", setDates);
}

datePickerController.addEvent(window, 'unload', removeInputEvents);

function updateSwitchModeLink(region, mode){
	priceMin = document.getElementById("priceMin").value;
	priceMax = document.getElementById("priceMax").value;
	roomSizeMin = document.getElementById("roomSizeMin").value;
	roomSizeMax = document.getElementById("roomSizeMax").value;
	earliestRentalStart = document.getElementById("earliestRentalStart").value;
	latestRentalStart = document.getElementById("latestRentalStart").value;
	link = "suche.php?region="+region+"&mode="+mode+"&priceMin="+priceMin+"&priceMax="+priceMax+"&roomSizeMin="+roomSizeMin+"&roomSizeMax="+roomSizeMax+"&earliestRentalStart="+earliestRentalStart+"&latestRentalStart="+latestRentalStart;
	if(document.getElementById("switchmode")) document.getElementById("switchmode").setAttribute("href", link);
}

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_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

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];}
}
