$(document).ready(function() {
  	$("#country_container").click(onCountrySelectChange);
	$("#departureStationKst_container").click(onPickupStationSelectChange);
	$('#change-posl').click(function (e) {if ($('#departureStationKst :selected').val() != ""){e.preventDefault();$('#return-station').modal({overlayClose:true,onClose: returnStation.close,onShow:returnStation.onShow});}else{alert("Alış istasyonu seçilmemiş, lütfen alış istasyonunuzu seçiniz");}});
});
var returnStation = {close: function (dialog) {$.modal.close();},
	onShow: function(dialog){$('#return-station-submit').click(function (e) {$('#arrivalStationKst').val($('#arrivalStation').val());$('#arrivalStationKstText').html($('#arrivalStation :selected').text());
			$.modal.close();});
			$('#arrivalStation').selectbox();

	}
};
$(function(){
  $('.date-pick').datePickerMultiMonth({numMonths: 3});
  $('#getSelected').bind('click',function(e){alert("1: " + $('#T1').dpmmGetSelected() + ", 2: " + $('#R1').dpmmGetSelected());return false;});
  // listen for when the selects are changed and update the picker
	$('#T1').bind('change',function(){doAdjustReturnDate();});
 });

function onCountrySelectChange(){
	var selected = document.getElementById("country").value;	
		if(selected != 0){if (selected!="TR") {offer.action="http://car-rental.sixt.com/?land="+selected+"&posl=TR";offer.submit();}}
}

function onPickupStationSelectChange(){
	var selected = document.getElementById("departureStationKst").value;		
	var selectedText = document.getElementById("departureStationKst_input").value;	
	if(selected != 0){ offer.arrivalStationKst.value = selected; }
	if(selectedText != ""){ document.getElementById('arrivalStationKstText').innerHTML = selectedText; }
}
// Alis ve Dönüs Bilgilerini kontrol Et!
function doValidate(){
//Dönüs istasyonunu belirle
var pickupStation = offer.departureStationKst.value;
//dönüs istasyonu belirlenmemis ise belirle
if( offer.arrivalStationKst.value == "dochgme" ) offer.arrivalStationKst.value = offer.departureStationKst.value;
var returnStation = offer.arrivalStationKst.value;
//alis tarihini,saati,dakikasi belirle
var t1=offer.T1.value;
var pickupHour=offer.T2.value* 1;
var pickupMinute=offer.T3.value* 1;
var pickupDate=stringToDate( t1 );
pickupDate.setHours( pickupHour );
pickupDate.setMinutes( pickupMinute );
//dönüs tarihi,saati,dakikasi belirle
var r1=offer.R1.value;
var returnHour=offer.R2.value* 1;
var returnMinute=offer.R3.value* 1;
var returnDate=stringToDate( r1 );
returnDate.setHours( returnHour );
returnDate.setMinutes( returnMinute );
//alis ve dönüs tarihi bir tarih midir?
if( !isDate(r1) || !isDate(t1) ) return;
//Günün tarih ve saat bilgilerini al
var dateTime=new Date();
//verileri gönder
$.post("/doValidate.php",{ pickupStation:pickupStation,pickupDate:pickupDate,pickupHour:pickupHour,pickupMinute:pickupMinute,returnStation:returnStation,returnDate:returnDate,returnHour:returnHour,returnMinute:returnMinute,dateTime:dateTime,rand:Math.random() } ,function(data){
          if(data=='1'){offer.submit();}else{alert(data);}
 });
}
//koordinat zart zurt
if ((navigator.appName.indexOf('Microsoft')+1)) {
document.write('<style type="text/css"> .opacity1 {filter:alpha(opacity=50)} .opacity2 {filter:alpha(opacity=100)} </style>'); }
if ((navigator.appName.indexOf('Netscape')+1)) {
document.write('<style type="text/css"> .opacity1 {-moz-opacity:0.5} .opacity2 {-moz-opacity:1} </style>'); }
else {
document.write(''); }


function doAdjustReturnDate()
{
  if( ( iType == 0 || iType == "0" ) && isDate( offer.T1.value ) ) {
    Ad2=stringToDate( offer.T1.value );
    Ad0=new Date(Ad2.getTime() + 86400000);

    var M = Ad0.getDate()+"";
    if( M.length< 2 ) M = "0"+M;

    var N = (Ad0.getMonth()+1)+"";
    if( N.length< 2 ) N = "0"+N;

    offer.R1.value=M+"."+N+"."+Ad0.getFullYear();
  }

  offer.R2.value=offer.T2.value;
  offer.R3.value=offer.T3.value;
}
var iType=0; //Borrow

var dtCh= ".";
var minYear=1900;
var myWidth = 0, myHeight = 0;
var maxYear=2100;


function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag) {
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
    var daysInMonth = DaysArray(12)
    var pos1=dtStr.indexOf(dtCh)
    var pos2=dtStr.indexOf(dtCh,pos1+1)
    var strDay=dtStr.substring(0,pos1)
    var strMonth=dtStr.substring(pos1+1,pos2)
    var strYear=dtStr.substring(pos2+1)
    strYr=strYear
    if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
    if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
	if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
    }
    _month=parseInt(strMonth)
    day=parseInt(strDay)
    year=parseInt(strYr)
    if (pos1==-1 || pos2==-1){
	alert("Hatali tarih girisi yaptiniz (gg.aa.yyyy seklinde giris yapmalisiniz)")
	return false
    }
    if (strMonth.length<1 || _month<1 || _month>12){
	alert("Tarih alanina girilen ay geçerli degil");
	return false;
    }
    if (strDay.length<1 || day<1 || day>31 || (_month==2 && day>daysInFebruary(year)) || day > daysInMonth[_month]){
	alert("Tarih alanina girilen gün geçerli degil");
	return false;
    }
    if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
	alert("Tarih alanina girilen yil geçerli degil");
	return false;
    }
    if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
	alert("Hatali tarih girisi yaptiniz (gg.aa.yyyy seklinde giris yapmalisiniz)");
	return false;
    }
    return true;
}

function stringToDate(string) {
  // expects format "dd.MM.yyyy"
  var match = /^(\d\d)\.(\d\d)\.(\d{4})$/.exec(string);

  if (match) {
    return new Date(match[3],match[2]-1,match[1]);
  }
}

