function create_http() {
try
  {
        // Firefox, Opera 8.0+, Safari
         http=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    http=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      http=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  return http;
}

function AvailabilityRequest(packageid, departuredate, departurepoint, unittype, boardtype, duration, volwassenen, route, prijs, teller, priceper) {
	document.location='#prijsberekening';
        http = create_http();
        var url = "/ajax/availabilityrequest.php";

	var params = 'PackageID='+packageid+'&DepartureDate='+departuredate+'&DeparturePoint='+departurepoint+'&UnitType='+unittype+'&BoardType='+boardtype+'&Duration='+duration+'&numVolwassenen='+volwassenen+'&RouteID='+route+'&Price='+prijs+'&teller='+teller+'&PricePer='+priceper;

        http.open("POST", url, true);

	document.getElementById('prijscheck').innerHTML = '<FONT COLOR="#838383"><center>'+taalarray["CHECKINGTRIP"]+'<br><br><img src="/afbeeldingen/laden.gif"></center></FONT>';

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4) {
			var rt = http.responseText;
                        document.getElementById('prijscheck').innerHTML = rt;
                      	if(document.getElementById('errorcode')) {
                                var errorcode = document.getElementById('errorcode').innerHTML;
                                var errormsg = document.getElementById('errormsg').innerHTML;
                                var action = document.getElementById('action').innerHTML;
				if(errorcode == '1015') {
					update_zoek_alias(packageid);
					OnRequest(packageid,departuredate,departurepoint,unittype,boardtype,duration,route,prijs,readCookie('volw'),readCookie('kind'),readCookie('babie'));
					return;
//					document.getElementById(teller).innerHTML = '<deprijs;
				} else {
					update_zoek_alias(packageid);
					document.getElementById(teller).innerHTML = '<del>'+prijs+'</del>';
				}
				document.getElementById('prijscheck').innerHTML = errormsg;	
				return;
                              	//alert(errorcode+'\n'+errormsg+'\n'+action);
				//eval(action);
                        }
                }
        }
        http.send(params);	
}

function AvailabilityRequest2() {
        document.location='#prijsberekening';
	var packageid = document.forms['availabilityrequest'].PackageID.value;
        var departuredate = document.forms['availabilityrequest'].DepartureDate.value;
        var departurepoint = document.forms['availabilityrequest'].DeparturePoint.value;
        var boardtype = document.forms['availabilityrequest'].BoardType.value;
        var duration = document.forms['availabilityrequest'].Duration.value;
        var accommodationid = document.forms['availabilityrequest'].AccommodationID.value;
        if(document.forms['availabilityrequest'].AccommodationType) {
                accommodationtype = document.forms['availabilityrequest'].AccommodationType.value;
        }
        var unittype =  document.forms['availabilityrequest'].UnitType.value;
        var acco_arrivaldate = document.forms['availabilityrequest'].Acco_ArrivalDate.value;
        var acco_departuredate = document.forms['availabilityrequest'].Acco_DepartureDate.value;
        var messagesequence = document.forms['availabilityrequest'].MessageSequence.value;
        var receiversessionid = document.forms['availabilityrequest'].ReceiverSessionID.value;
        var vervoer = document.forms['availabilityrequest'].vervoer.value;
        var price = document.forms['availabilityrequest'].Price.value;
	var accoinfo = document.forms['availabilityrequest'].Accoinfo.value;
        var numVolwassenen = document.forms['availabilityrequest'].numVolwassenen.value;
	var teller = document.forms['availabilityrequest'].teller.value;
	var priceper = document.forms['availabilityrequest'].PricePer.value;
        var volw = readCookie('volw');
        var kind = readCookie('kind');
        var babie = readCookie('babie');
        var to_transport = '';
        var from_transport = '';
        var routeid = '';
        if(document.forms['availabilityrequest'].RouteID.value) {
                routeid = routeid = document.forms['availabilityrequest'].RouteID.value;
        }
        autohuur = '';
        reisgezelschap = parseInt(volw) + parseInt(kind) + parseInt(babie);
        if(babie>volw) {
                alert(taalarray["BABYERROR"]);
                return;
        }

        if(reisgezelschap>8) {
                alert(taalarray["CHECKGROUP"]);
                return;
        }

        units = new Array();
        min = 0;
        max = 0;
        aantal_kamers = 0;
        error = 0;
        msg1 = '';
        msg2 = '';
        msg3 = '';
        msg4 = '';
        msg5 = '';
        msg6 = '';
        msg7 = '';
        msg8 = '';
        from_index = 0;
        to_index = 0;
        to_carrier = '';
        from_carrier = '';
        up_unit = 0;

        for(i=0; i<document.forms['availabilityrequest'].elements.length; i++){
                //Tui heeft geen opties, alleen een hidden element
                if(document.forms['availabilityrequest'].elements[i].name.match("autohuur") && document.forms['availabilityrequest'].elements[i].type=="hidden") {
                        autohuur = document.forms['availabilityrequest'].elements[i].value;
                }
                if(document.forms['availabilityrequest'].elements[i].name.match("autohuur") && document.forms['availabilityrequest'].elements[i].checked) {
                        autohuur = document.forms['availabilityrequest'].elements[i].value;
                }
                if(document.forms['availabilityrequest'].elements[i].name.match("to_transport") && document.forms['availabilityrequest'].elements[i].checked) {
                        info = document.forms['availabilityrequest'].elements[i].value.split("|");
                        stoelen_heen = info[11];
                        to_transport = document.forms['availabilityrequest'].elements[i].value;
                        var temp = to_transport.split("|");
                        to_carrier = temp[5];
                        to_index = document.forms['availabilityrequest'].elements[i].id;
                }
                if(document.forms['availabilityrequest'].elements[i].name.match("from_transport") && document.forms['availabilityrequest'].elements[i].checked) {
                        info = document.forms['availabilityrequest'].elements[i].value.split("|");
                        stoelen_terug = info[11];
                        from_transport = document.forms['availabilityrequest'].elements[i].value;
                        var temp = from_transport.split("|");
                        from_carrier = temp[5];
                        from_index = document.forms['availabilityrequest'].elements[i].id;
                }
                if(document.forms['availabilityrequest'].elements[i].name.match("unittype")) {
                        string = document.forms['availabilityrequest'].elements[i].name+"|"+document.forms['availabilityrequest'].elements[i].selectedIndex;
                        parts = string.split("|");
                        if(trim(parts[1]).match(trim(unittype)) && parts[5] == 1) {
                                up_unit = 1;
                        }
                        aantal_kamers = aantal_kamers + parseInt(document.forms['availabilityrequest'].elements[i].selectedIndex);
                        min = min + parts[3]*document.forms['availabilityrequest'].elements[i].selectedIndex;
                        max = max + parts[4]*document.forms['availabilityrequest'].elements[i].selectedIndex;
                        //alert(string);
                        units.push(string);
                }
        }
        if(error == 1) {
                msg = msg1 + msg2 + msg3 + msg4 + msg5 + msg6 + msg7 + msg8;
	                alert(msg);
        	        return;
        }

        if(min > reisgezelschap) {
                alert(taalarray["TOOMUCHROOMS"]);
                return;
        }
        if(max < reisgezelschap) {
                if(babie<aantal_kamers) {
                        aantal_kamers = babie;
                }
                if(max < reisgezelschap-aantal_kamers ) {
        	                alert(taalarray["NOTENOUGHROOMS"]);
	                        return;
                }
        }
        pax = parseFloat(volw)+parseFloat(kind);
        if(to_transport != '' && stoelen_heen<pax) {
                alert('Voor de gekozen heenreis zijn nog maar '+stoelen_heen+' plaatsen vrij.\nKies een andere heenreis.');
                return;
        }
        if(from_transport != '' && stoelen_terug<pax) {
                alert('Voor de gekozen terugreis zijn nog maar '+stoelen_terug+' plaatsen vrij.\nKies een andere terugreis.');
                return;
        }

        //if(to_carrier != from_carrier && !to_transport.match("|bu|")) {
        //        alert('De luchtvaartmaatschappij op de heenreis dient hetzelfde te zijn als op de terugreis.');
        //        return;
        //}

//Check if we should update the price when there is a difference
        update = '';

        if(numVolwassenen == volw) {
                update = update+'1';
        } else {
                update = update+'0';
        }
        update = update+to_index+from_index+up_unit;
        http = create_http();
        var url = "/ajax/availabilityrequest2.php";

        var params = 'PackageID='+packageid+'&DepartureDate='+departuredate+'&DeparturePoint='+departurepoint+'&units='+units.join(";")+'&BoardType='+boardtype+'&Duration='+duration+'&AccommodationID='+accommodationid+'&Acco_ArrivalDate='+acco_arrivaldate+'&Acco_DepartureDate='+acco_departuredate+'&MessageSequence='+messagesequence+'&ReceiverSessionID='+receiversessionid+'&to_transport='+to_transport+'&from_transport='+from_transport+'&Price='+price+'&autohuur='+autohuur+'&RouteID='+routeid+'&update='+update+'&UnitType='+unittype+'&Accoinfo='+accoinfo+'&PricePer='+priceper+'&AccommodationType='+accommodationtype;
        params += '&vervoer='+vervoer;

        http.open("POST", url, true);

	document.getElementById('prijscheck').innerHTML = '<center><FONT COLOR="#838383">'+taalarray["CHECKINGTRIP"]+'<br><br><img src="/afbeeldingen/laden.gif"></center></FONT>';

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.onreadystatechange = function() {//Call a function when the state changes.
                if(http.readyState == 4) {
                        var rt = http.responseText;
                        document.getElementById('prijscheck').innerHTML = rt;
                        if(document.getElementById('errorcode')) {
                                //alert(document.getElementById('errorcode').innerHTML);
                                var errorcode = document.getElementById('errorcode').innerHTML;
                                var errormsg = document.getElementById('errormsg').innerHTML;
                                if(document.getElementById('action')) {
                                        var action = document.getElementById('action').innerHTML;
                                }
 ///                               alert(errorcode+'\n'+errormsg+'\n'+action);
                                if(errorcode == 9999) { //Prijs is veranderd, errormsg de nieuwe prijs
                                        var tmp = document.getElementById(teller).innerHTML;
                                        var value = /(<a.*\">)/i.exec(tmp);
                                        //alert(value[1]);
                                        var link = value[1]+errormsg+"</a>";
                                        document.getElementById(teller).innerHTML="<b>"+link+"</b>";
                                        update_zoek_alias(packageid);
                                        return;
                                } else if(errorcode = 1015){
					OnRequest(packageid,departuredate,departurepoint,unittype,boardtype,duration,routeid,price,readCookie('volw'),readCookie('kind'),readCookie('babie'));
				}
                        }
                }
        }
        http.send(params);
}

function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}

function getRetourDate(departuredate, duration) {
                var parts = departuredate.split("-");
                var humDate = new Date( Date.UTC(parts[0],
                                (parts[1]-1),
                                 parts[2], 0, 0, 0));
                var timestamp = (humDate.getTime()/1000.0);
                var timestamp2 = (timestamp + (3600 * 24 * duration));
                var theDate = new Date(timestamp2 * 1000);
                var month = theDate.getMonth()+1;
                if(month<10) {
                        month = "0"+month;
                }
                retourdate = theDate.getFullYear()+"-"+month+"-"+theDate.getDate();
                return retourdate;
}

function OnRequest(packageid, departuredate, departurepoint, unitid, boardtype, duration, routeid, price, volw, kids, babies, pricetype) {
	document.location='#prijsberekening';
	price = parseInt(price);
	if(pricetype == 'W') {
		price = parseInt(price / (parseInt(volw) + parseInt(kids) + parseInt(babies)));
	}
	var i = 0; var volwtekst = ""; var kidstekst = ""; var babiestekst = ""; var totaaltekst = ""; totaalprijs = 0;
	for (i=0;i<volw;i++) { var volwtekst = volwtekst+'<td align="left">'+taalarray["ADULT"]+' '+(i+1)+'</td><td align="right">&euro;'+price+'.00</td></tr>'; totaalprijs = (totaalprijs+price); }
        for (i=0;i<kids;i++) { var kidstekst = kidstekst+'<td align="left">'+taalarray["CHILD"]+' '+(i+1)+'</td><td align="right">&euro;'+price+'.00</td></tr>'; totaalprijs = (totaalprijs+price); }
        for (i=0;i<babies;i++) { var babiestekst = babiestekst+'<td align="left">'+taalarray["BABY"]+' '+(i+1)+'</td><td align="right">&euro;'+price+'.00</td></tr>'; totaalprijs = (totaalprijs+price); }
	var totaaltekst = '<tr><td align="left">'+taalarray["RESERVATIONFEE"]+'</td><td align="right">&euro; 25.00</td></tr>'; totaalprijs = (totaalprijs+25);
	var totaaltekst = totaaltekst+'<tr><td colspan=2><hr noshade style="color:#FFBA49;"></td></tr><tr style="color:#006795"><td align="left"><b>'+taalarray["TOTALPRICE"]+'</b></td><td align="right"><b>&euro;'+totaalprijs+'.00</b></td></tr>';
	var url = "/OnRequest?Stap=1&"+'PackageID='+packageid+'&DepartureDate='+departuredate+'&DeparturePoint='+departurepoint+'&UnitID='+unitid+'&BoardType='+boardtype+'&Duration='+duration+'&RouteID='+routeid+'&Price='+price;
        document.getElementById('prijscheck').innerHTML = "<div style='text-align:justify;'>"+taalarray["ONREQUESTMESSAGE"]+"</div><br><br><table width='100%' cellpadding=0 cellspacing=0 style='color:#505050'>"+volwtekst+kidstekst+babiestekst+totaaltekst+"</table><br><div align=right><a href='"+url+"'><img src='/afbeeldingen/"+taalarray["REQUESTIMAGE"]+"' border='0'></a></div>";
}
