var strError = '';
var date_comparison = 0;

function validate_flight_form(departure_location, going_to_location, departure_date, returning_date, language){
    var error_value = true;
    date_comparison = 0;
    if(!validate_form(returning_date, "date", language)) error_value = false;
    if(!validate_form(departure_date, "date", language)) error_value = false;
    if(!validate_form(going_to_location, "location", language)) error_value = false;
    if(!validate_form(departure_location, "location", language)) error_value = false;
    if(!compare_date(departure_date, returning_date, language, "flight")) error_value = false;
    return error_value;
}

function validate_car_form(pickup_location, dropoff_location, pickup_date, dropoff_date, language){
    var error_value = true;
    date_comparison = 0;
    if(!validate_form(dropoff_date, "date", language)) error_value = false;
    if(!validate_form(pickup_date, "date", language)) error_value = false;
    if(!validate_form(dropoff_location, "location", language)) error_value = false;
    if(!validate_form(pickup_location, "location", language)) error_value = false;
    if(!compare_date(pickup_date, dropoff_date, language, "car")) error_value = false;
    return error_value;
}

function validate_hotel_form(destination_location, checkin_date, checkout_date, language){
    var error_value = true;
    date_comparison = 0;
    if(!validate_form(checkout_date, "date", language)) error_value = false;
    if(!validate_form(checkin_date, "date", language)) error_value = false;
    if(!validate_form(destination_location, "location", language)) error_value = false;
    if(!compare_date(checkin_date, checkout_date, language, "hotel")) error_value = false;
    return error_value;
}

function validate_form(id, command, language){
    var validate = false;
    switch(command){
        case "location":
            {
                validate = validate_location(id, language);
                break;
            }
        case "date":
            {
                validate=validate_date(id, language);
                break;
            }
    }//switch
    error_message = id + "_error_message";
    error_border = id + "_error_border";
    if (!validate){
        document.getElementById(error_border).className = 'fieldWithErrors';
        document.getElementById(error_message).firstChild.nodeValue = strError;
        //firefox hack
        tmpId=id;
        window.setTimeout('focusBackDate()', 1);
        return false;
    }
    else{
        if (!(document.getElementById(error_border).className == "")) {
            document.getElementById(error_message).firstChild.nodeValue = "";
        }
        document.getElementById(error_border).className = "";
        remove_focus(id);
        return true;
    }
}

function focusBackDate(){
    set_focus(tmpId);
    document.getElementById(tmpId).value = document.getElementById(tmpId).value;
}

function compare_date(id1, id2, language, item){
    if (date_comparison == 2) {
        var objValue1=document.getElementById(id1);
        var objValue2=document.getElementById(id2);
        var objLanguage=document.getElementById(language);
        var split_date = /[\/]/;
        var splitted1 = objValue1.value.split(split_date);
        var splitted2 = objValue2.value.split(split_date);
        var userDay1 = parseInt(splitted1[0], 10);
        var userMonth1 = parseInt(splitted1[1], 10)-1;
        var userYear1 = parseInt(splitted1[2], 10);
        var userDay2 = parseInt(splitted2[0], 10);
        var userMonth2 = parseInt(splitted2[1], 10)-1;
        var userYear2 = parseInt(splitted2[2], 10);
        var date1 = new Date(userYear1, userMonth1, userDay1);
        var date2 = new Date(userYear2, userMonth2, userDay2);
    
        error_message = id2 + "_error_message";
        error_border = id2 + "_error_border";
        if(date1.getTime() > date2.getTime()){
            if(objLanguage.value == "de") {
              if(item == "flight") {
                strError="Das Rückflugdatum sollte nicht vor dem Abflugdatum sein. Bitte noch einmal versuchen."
              } else if(item == "car") {
                strError="Das Rückgabedatum sollte nicht vor dem Abholdatum liegen. Bitte noch einmal versuchen."
              } else {
                strError="Das Abreisedatum sollte nicht vor dem Anreisedatum liegen. Bitte noch einmal versuchen."
              }
            } 
            if(objLanguage.value == "en") {
              if(item == "flight") {
                strError="The returning date shouldn't be before the departure date. Please try again.";
              } else if(item == "car") {
                strError="The droppoff date shouldn't be before the pickup date. Please try again."
              } else {
                strError="The checkout date shouldn't be before the checkin date. Please try again."
              }
            }
            document.getElementById(error_border).className = 'fieldWithErrors';
            document.getElementById(error_message).firstChild.nodeValue = strError;
            tmpId=id2;
            window.setTimeout('focusBackDate()', 1);
            return false;
        }
        else{
            if (!(document.getElementById(error_border).className == "")) {
                document.getElementById(error_message).firstChild.nodeValue = "";
            }
            document.getElementById(error_border).className = "";
            remove_focus(id1);
            return true;
        }
    }
    return true;
}

function validate_location(id, language){
    var value = document.getElementById(id).value;
    var objLanguage=document.getElementById(language);
    if (value == '') {
        if(objLanguage.value == "de") {
            strError="Die Suche nach einem nicht exisiterendem Ort ist schwer. Bitte noch einmal versuchen.";
        } 
        if(objLanguage.value == "en") {
            strError = "Looking for a non existing location is difficult. Please try again.";
        }
        return false;
    }//if 
    else return true;
}

function validate_date(id, language){
    var objValue=document.getElementById(id);
    var objLanguage=document.getElementById(language);
    var daysMonth;
    var thisDate = new Date();
    var regexp_date =/^\d{1,2}[\/]\d{1,2}[\/]\d\d\d\d$/;
    var split_date = /[\/]/;
    var splitted = objValue.value.split(split_date);
    var userDay = parseInt(splitted[0], 10);
    var userMonth = parseInt(splitted[1], 10);
    var userYear = parseInt(splitted[2], 10);
    var user_Date = new Date(userYear, (userMonth-1), userDay);
    if(id == "flight_request_returning_date"){
        if(objValue.value == "No return flight" || objValue.value =="" || objValue.value == "Kein Rückflug") return true;
    }
  
    if(objValue.value.match(regexp_date) == null) {
        if(objLanguage.value == "de") {
            strError="Das Datumsformat ist nicht korrekt. Bitte nutzen Sie tt/mm/jjjj.";
        } 
        if(objLanguage.value == "en") {
            strError="The format of the date is not correct. Please use the following format dd/mm/yyyy.";        
        }
        return false; 
    }//if 
    else  {
        // set days of month and consider leap year
        if(userMonth==4 || userMonth==6 || userMonth==9 || userMonth==11){
            daysMonth=30;
        }
        else if(userMonth==1 || userMonth==3 || userMonth==5 || userMonth==7 || userMonth==8 || userMonth==10 || userMonth==12 ){
            daysMonth=31;
        }
        else if(userMonth==2 && userYear%4==0 && userYear%100!=0 || userYear%400==0){
            daysMonth=29;
        }
        else if(userMonth==2 && userYear%4!=0 || userYear%100==0 && userYear%400!=0){
            daysMonth=28;
        }
        if(objLanguage.value == "de") {
            strError="Das ausgewählte Datum liegt in der Vergangenheit. Bitte noch einmal versuchen."
        } 
        if(objLanguage.value == "en") {
            strError="The chosen date has already passed. Please try again.";         
        }

        if(userDay >= 1 && userDay <= daysMonth && userMonth >= 1 && userMonth <= 12){
            if(thisDate.getTime() > user_Date.getTime()) return false;
        }
        else{
            alert(userDay); alert(userMonth);
            strError="The date is invalid.";
            return false; 
        }         
        date_comparison += 1;
        return true; 
    }
}
