function updateQuickbook(start, end, persons, pets){
	
}


function setLoading(element){
	document.getElementById(element).innerHTML='<img src="http://www.residenzo.com/v_1_0/elemente/grafiken/loading.gif" style="margin-bottom: 30px;  margin-top: 30px; margin-left: 8px;"/>';
}

function sendClientData(lang){
	cdata();
	document.location.href='http://www.residenzo.com/'+lang+'/booking.htm';
}

function checkClient(lang){	
	var advance = true;
	if(document.getElementById('vorname').value==''){
		document.getElementById('vorname').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('name').value==''){
		document.getElementById('name').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('strasse').value==''){
		document.getElementById('strasse').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('hausnummer').value==''){
		document.getElementById('hausnummer').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('plz').value==''){
		document.getElementById('plz').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('ort').value==''){
		document.getElementById('ort').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('telefon').value==''){
		document.getElementById('telefon').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('cemail').value==''){
		document.getElementById('cemail').style.border='1px solid #0098FD';
		advance = false;
	}
	if(document.getElementById('land').value==''){
		document.getElementById('land').style.border='1px solid #0098FD';
		advance = false;
	}
	if(advance){
		sendClientData(lang);
	} else {		
		alert(document.getElementById('errormsg').value);
	}
}

function updatePersons(persons,oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/update_persons.php?persons='+persons,'none');
	setLoading('breq');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechner.php?oid='+oid,'breq');
		
}

function updatePersonssmall(persons,oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/update_persons.php?persons='+persons,'none');
	setLoading('breqsmall');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechnersmall.php?oid='+oid,'breqsmall');
		
}

function updatePets(pets,oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/update_pets.php?pet='+pets,'none');
	setLoading('breq');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechner.php?oid='+oid,'breq');
	
}

function updatePetssmall(pets,oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/update_pets.php?pet='+pets,'none');
	setLoading('breqsmall');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechnersmall.php?oid='+oid,'breqsmall');
	
}

function getCalendar(month, year, oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/object_calendar.php?oid='+oid+'&month='+month+'&year='+year,'calendar');
}

function updateCalendar(oid){
	//document.getElementById('none').innerHTML += '<li>Calendar update initated</li>';
  ajax('http://www.residenzo.com/v_1_0/ajax/update_calendar.php?oid='+oid, 'none');
	ajax('http://www.residenzo.com/v_1_0/ajax/object_calendar.php?oid='+oid,'calendar');
	setLoading('breq');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechner.php?oid='+oid,'breq');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreis.php?oid='+oid,'theprice');
	

	//ajax('http://www.residenzo.eu/v_1_0/ajax/update_calendar.php?oid='+oid,'none');

	// ajax('http://www.residenzo.eu/v_1_0/ajax/object_calendar.php?oid='+oid,'none');	
	//document.getElementById('none').innerHTML += '<li>'+ajax('test')+'</li>';
}

function updateCalendarsmall(oid){
	//document.getElementById('none').innerHTML += '<li>Calendar update initated</li>';
  ajax('http://www.residenzo.com/v_1_0/ajax/update_calendar.php?oid='+oid, 'none');
	ajax('http://www.residenzo.com/v_1_0/ajax/object_calendar.php?oid='+oid,'calendar');
	setLoading('breqsmall');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechnersmall.php?oid='+oid,'breqsmall');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreis.php?oid='+oid,'theprice');
	

	//ajax('http://www.residenzo.eu/v_1_0/ajax/update_calendar.php?oid='+oid,'none');

	// ajax('http://www.residenzo.eu/v_1_0/ajax/object_calendar.php?oid='+oid,'none');	
	//document.getElementById('none').innerHTML += '<li>'+ajax('test')+'</li>';
}

function cdata(){
	//alert(document.getElementById('name').value);
	

	
	ajax('http://www.residenzo.com/v_1_0/ajax/saveClientData.php?anrede='+document.getElementById('anrede').value+
										'&name='+document.getElementById('name').value+
										'&vorname='+document.getElementById('vorname').value+
										'&strasse='+document.getElementById('strasse').value+
										'&hausnummer='+document.getElementById('hausnummer').value+
										'&land='+document.getElementById('land').value+
										'&plz='+document.getElementById('plz').value+
										'&ort='+document.getElementById('ort').value+
										'&telefon='+document.getElementById('telefon').value+
										'&email='+document.getElementById('cemail').value+
										'&fax='+document.getElementById('fax').value+
										'&zahlungsart='+document.getElementById('zahlungsart').value+
										'&rid='+document.getElementById('rid').value+
										'&oid='+document.getElementById('oid').value+
										'&booklang='+document.getElementById('booklang').value,'none');
										

	//ajax('http://www.residenzo.eu/v_1_0/ajax/saveClientData.php?name='+document.getElementById("anrede").value,'none');
	//ajax('http://www.residenzo.eu/v_1_0/ajax/saveClientData.php?name='+document.getElementById('name').value,'none');
}

function getPreisrechner(){
	ajax('http://www.residenzo.com/v_1_0/ajax/booking.php','preisrechner');
	ajax('http://www.residenzo.com/v_1_0/ajax/update_arrdays.php','arrdays');
	ajax('http://www.residenzo.com/v_1_0/ajax/update_arrdayssmall.php','arrdayssmall');
}

function isNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function booking(){
	var formok=true;
	if(document.getElementById('za').value==2 || document.getElementById('za').value==4){
		// Kreditkarte / Kreditkarte & ueberwisung
		if(document.getElementById('z2').value.length!=16 || !isNumeric(document.getElementById('z2').value)){
			formok=false;
			alert('Laenge');
		} else if(document.getElementById('z3').value.length!=4 || !isNumeric(document.getElementById('z3').value)){
			formok=false;
		} else if(parseInt(document.getElementById('z3').value.substring(0,2))>12){
			formok=false;
			alert('datum');
			// maybe later: check if year is allright
		} else if(document.getElementById('z4').value.length<2){
			formok=false;
			alert('name');
		} else if(document.getElementById('z5').value.length!=3 || !isNumeric(document.getElementById('z5').value)){
			formok=false;
			alert('pruef');
		}
	} else if(document.getElementById('za').value==3){
		if(document.getElementById('z1').value.length<4){
			// length of bankname should be more than 4 chars
			formok=false;
		} else if(!isNumeric(document.getElementById('z2').value)){
			// members number should be numeric
			formok=false;
		} else if(!isNumeric(document.getElementById('z3').value) || document.getElementById('z3').value.length!=8){
			// bank number should be numeric and exactly 8 digits long
			formok=false;
		} else if(document.getElementById('z4').value.length<2){
			// owner should be at least 2 chars long
			formok=false;
		}
	}
	
	if(formok){
		
		var za = document.getElementById('za').value;
		var z1 = document.getElementById('z1').value;
		var z2 = document.getElementById('z2').value;
		var z3 = document.getElementById('z3').value;
		var z4 = document.getElementById('z4').value;
		var z5 = document.getElementById('z5').value;
		setLoading('preisrechner');
		ajax('http://www.residenzo.com/v_1_0/ajax/booking_confirmed.php?z1='+z1+
										'&z2='+z2+
										'&z3='+z3+
										'&z4='+z4+
										'&z5='+z5,'preisrechner');
		
	} else {
		alert(document.getElementById('errormsg').value);		
	}
}

/**
 * Performs the ajax query and rocesses the result depending on second prameter
 *
 * @param  {String}  The URL to request
 * @param  {String}  The target item, feasible values are
 *                   - 'none':  Just send the query and do nothing
 *                   - 'debug': Should be used for debugging, seems to be not implemented yet
 *                   - Element id: Id of an existing element to update
 *                   - Anonymous function to execute
 * @returns  {void|String}  Either the result or nothing
 */
function ajax(php,target){
  var XMLHTTP;
  if (window.XMLHttpRequest) {
     XMLHTTP = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      XMLHTTP = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (ex) {
      try{
        XMLHTTP = new ActiveXObject('Microsoft.XMLHTTP');
      } catch (ex){
      }
    }
  }
  if (XMLHTTP != null) {
		//document.getElementById('none').innerHTML += '<li>XMLHTTP succesful</li>';
    //document.getElementById("bookingcal").innerHTML = '<img src="loading.gif" alt="loading" style="margin-top: 120px; margin-left: 80px;"/>';
    XMLHTTP.open('GET', php, true);
    XMLHTTP.onreadystatechange = function(){
        if (XMLHTTP.readyState == 4) {
		      //document.getElementById('none').innerHTML += '<li>XMLHTTP status: '+XMLHTTP.status+'</li>';
          if (XMLHTTP.status == 200) {
            //alert('Funktion ausgefuehrt');
						//
                        if (target != 'none') {
                            if (typeof target == "function") {
                                target(XMLHTTP.responseText);
                            } else if (target == 'debug') {
								document.getElementById(target).innerHTML += '<li>'+XMLHTTP.responseText+'</li>';
							} else {
                                document.getElementById(target).innerHTML = XMLHTTP.responseText;
							}
						}
          }
        } else{
				  //return('AJAX failed - no correct value returned');
				}
				//document.getElementById('debug').innerHTML += '<li>XMLHTTP readyState: '+XMLHTTP.readyState+'</li>';
      }
    XMLHTTP.send(null);

		//document.getElementById('debug').innerHTML += '<li>XMLHTTP still succesful</li>';
  } else {
		if(target!=none){
			document.getElementById(target).innerHTML += '<li>AJAX failed</li>';
		}
	}
}

function updateArrival(year, month, day, duration, oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/update_arrival.php?arrday='+day+'&arrmonth='+month+'&arryear='+year+'&duration='+duration,'none');
	getCalendar(month, year, oid);
	setLoading('breq');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechner.php?oid='+oid,'breq');
		
}

function updateArrivalsmall(year, month, day, duration, oid){
	ajax('http://www.residenzo.com/v_1_0/ajax/update_arrival.php?arrday='+day+'&arrmonth='+month+'&arryear='+year+'&duration='+duration,'none');
	getCalendar(month, year, oid);
	setLoading('breqsmall');
	ajax('http://www.residenzo.com/v_1_0/ajax/getPreisrechnersmall.php?oid='+oid,'breqsmall');
		
}

function update_arrdays(month, oid){
    ajax('http://www.residenzo.com/v_1_0/ajax/update_arrdays.php?month='+month+'&oid='+oid, function(res){ 
        var el = document.getElementById('arrdays');
        el.innerHTML = '';

        var items = res.split('%%%');
        for (i=0; i<items.length;i++) {
            var props = items[i].split('$$$');
            if (props.length == 3) {
                // create an option object
                var opt = document.createElement("option");
                // assign properties
                opt.text = props[2];
                opt.value = props[0];
                if (props[1] == 'selected') {
                    opt.selected = props[1];
                }
                // add option object to select
                el.options.add(opt);
            }
        }
        
    });
}


if (!("console" in window) || !("firebug" in console)) {
(function(){
	window.console = {
		log:   function(){},
		debug: function(){},
		info:  function(){},
		warn:  function(){},
		error: function(){}
	}
})();
}

function update_arrdayssmall(month, oid){
    ajax('http://www.residenzo.com/v_1_0/ajax/update_arrdays.php?month='+month+'&oid='+oid, function(res){ 
        var el = document.getElementById('arrdays');
        el.innerHTML = '';

        var items = res.split('%%%');
        for (i=0; i<items.length;i++) {
            var props = items[i].split('$$$');
            if (props.length == 3) {
                // create an option object
                var opt = document.createElement("option");
                // assign properties
                opt.text = props[2];
                opt.value = props[0];
                if (props[1] == 'selected') {
                    opt.selected = props[1];
                }
                // add option object to select
                el.options.add(opt);
            }
        }
        
    });
}


if (!("console" in window) || !("firebug" in console)) {
(function(){
	window.console = {
		log:   function(){},
		debug: function(){},
		info:  function(){},
		warn:  function(){},
		error: function(){}
	}
})();
}



