﻿var Reservations = Class.create(true);

Reservations.Form = {
    
    validate: function() {
        var _title = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _firstname = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _lastname = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _addressLine1 = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _addressLine2 = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _town = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _postcode = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _country = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _otherCountry = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _telephoneDaytime = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _telephoneEvening = new API.Web.UI.Client.Service.Forms.validationValue();
        var _email = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _arrival = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _departure = new API.Web.UI.Client.Service.Forms.validationValue();        
        var _noOfadults = new API.Web.UI.Client.Service.Forms.validationValue();
        var _adultsList = new API.Web.UI.Client.Service.Forms.validationValue();
        var _noOfchildren = new API.Web.UI.Client.Service.Forms.validationValue();
        var _childrenList = new API.Web.UI.Client.Service.Forms.validationValue();        
        var _comments = new API.Web.UI.Client.Service.Forms.validationValue();
        var _activityList = new API.Web.UI.Client.Service.Forms.validationValue();
        var _roomList = new API.Web.UI.Client.Service.Forms.validationValue();
        
	    var failed = function() {alert('Reservations.Form.validate:Something went wrong');}
	    
	    var succeeded = function(result) {
		    if(result.length > 0){
		        var errorlist ='';
			    for (var index = 0; index < result.length; ++index) 
			    {   
				    var item = result[index];		
				    $('#'+item.property).parent().addClass('error');
				    errorlist = errorlist + '<ul><li>'+item.error+'</li></ul>';
			    }
			    $('#error-message').show(); 
		    }
		    else
		    {
		        Reservations.Form.send();
			}
	    }
	    
	    $('#title').parent().removeClass('error');
	    $('#first-name').parent().removeClass('error');
	    $('#last-name').parent().removeClass('error');
	    $('#address-line-1').parent().removeClass('error');
	    $('#address-line-2').parent().removeClass('error');
	    $('#town').parent().removeClass('error');
	    $('#postcode').parent().removeClass('error');
	    $('#country').parent().removeClass('error');
	    $('#other-country').parent().removeClass('error');
        $('#telephone-daytime').parent().removeClass('error');
        $('#telephone-evening').parent().removeClass('error');
        $('#email-address').parent().removeClass('error');
        $('#no-of-adults').parent().removeClass('error');
        $('#no-of-children').parent().removeClass('error');
        $('#arrival').parent().removeClass('error');
        $('#departure').parent().removeClass('error');
    
        _title.property = 'title';
	    _title.value = $('#title').val();
	    
	    _firstname.property = 'first-name';
	    _firstname.value = $('#first-name').val();
	    
	    _lastname.property = 'last-name';
	    _lastname.value = $('#last-name').val();	    
	    
	    _addressLine1.property = 'address-line-1';
	    _addressLine1.value = $('#address-line-1').val();

	    _addressLine2.property = 'address-line-2';
	    _addressLine2.value = $('#address-line-2').val();
	    
	    _town.property = 'town';
	    _town.value = $('#town').val();
	    
	    _postcode.property = 'postcode';
	    _postcode.value = $('#postcode').val();
	    
	    _country.property = 'country';
	    _country.value = $('#country').val();
	    
	    _otherCountry.property = 'other-country';
	    _otherCountry.value = $('#other-country').val();
	    
	    _telephoneDaytime.property = 'telephone-daytime';
	    _telephoneDaytime.value = $('#telephone-daytime').val();
	    
	    _telephoneEvening.property = 'telephone-evening';
	    _telephoneEvening.value = $('#telephone-evening').val();
	    
	    _email.property = 'email-address';
	    _email.value = $('#email-address').val();
	    
	    _arrival.property = 'arrival';
	    _arrival.value = $('#arrival').val();
	    	    
	    _departure.property = 'departure';
	    _departure.value = $('#departure').val();
	    	    	    	    
	    _noOfadults.property = 'no-of-adults';
	    _noOfadults.value = $('#no-of-adults').val();
	    	    	    	    
	    _noOfchildren.property = 'no-of-children';
	    _noOfchildren.value = $('#no-of-children').val();
    
	    _comments.property = 'comments-requirements';
	    _comments.value = $('#comments-requirements').val();
	    
	    _activityList.property = 'activity-list';
	    _activityList.value = Reservations.Form.getActivityList();
	    
	    _roomList.property = 'room-list';
	    _roomList.value = Reservations.Form.getRoomList();
	    
	    _adultsList.property = 'adults-list';
	    _adultsList.value = Reservations.Form.getAdultNames();
	    
	    _childrenList.property = 'children-list';
	    _childrenList.value = Reservations.Form.getChildrenNames();
	     
	    	    
	    var _formArray = [_title,_firstname,_lastname,_addressLine1,_addressLine2,_town,_postcode,_country,_otherCountry,_telephoneDaytime,_telephoneEvening,_email,_arrival,_departure,_noOfadults,_adultsList,_noOfchildren,_childrenList,_comments,_activityList,_roomList];
    	
	    API.Web.UI.Client.Service.Forms.ReservationForm.validateForm(_formArray,succeeded,failed)
    	
	    return false;
    
    },
    send: function() {
        var _title = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _firstname = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _lastname = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _addressLine1 = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _addressLine2 = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _town = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _postcode = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _country = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _otherCountry = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _telephoneDaytime = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _telephoneEvening = new API.Web.UI.Client.Service.Forms.validationValue();
        var _email = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _arrival = new API.Web.UI.Client.Service.Forms.validationValue();
	    var _departure = new API.Web.UI.Client.Service.Forms.validationValue();        
        var _noOfadults = new API.Web.UI.Client.Service.Forms.validationValue();
        var _adultsList = new API.Web.UI.Client.Service.Forms.validationValue();
        var _noOfchildren = new API.Web.UI.Client.Service.Forms.validationValue();
        var _childrenList = new API.Web.UI.Client.Service.Forms.validationValue();        
        var _comments = new API.Web.UI.Client.Service.Forms.validationValue();
        var _activityList = new API.Web.UI.Client.Service.Forms.validationValue();
        var _roomList = new API.Web.UI.Client.Service.Forms.validationValue();
        var _pageUri = new API.Web.UI.Client.Service.Forms.validationValue();
        
	    var failed = function() {alert('Reservations.Form.send:Something went wrong');}
	    
	    var succeeded = function(result) {
	        $('#error-message').hide(); 
            $('#success-message').show(); 
	    }
	    
	    $('#title').parent().removeClass('error');
	    $('#first-name').parent().removeClass('error');
	    $('#last-name').parent().removeClass('error');
	    $('#address-line-1').parent().removeClass('error');
	    $('#address-line-2').parent().removeClass('error');
	    $('#town').parent().removeClass('error');
	    $('#postcode').parent().removeClass('error');
	    $('#country').parent().removeClass('error');
	    $('#other-country').parent().removeClass('error');
        $('#telephone-daytime').parent().removeClass('error');
        $('#telephone-evening').parent().removeClass('error');
        $('#email-address').parent().removeClass('error');
        $('#no-of-adults').parent().removeClass('error');
        $('#no-of-children').parent().removeClass('error');
        $('#arrival').parent().removeClass('error');
        $('#departure').parent().removeClass('error');
        
	    _title.property = 'title';
	    _title.value = $('#title').val();
	    
	    _firstname.property = 'first-name';
	    _firstname.value = $('#first-name').val();
	    
	    _lastname.property = 'last-name';
	    _lastname.value = $('#last-name').val();	    
	    
	    _addressLine1.property = 'address-line-1';
	    _addressLine1.value = $('#address-line-1').val();

	    _addressLine2.property = 'address-line-2';
	    _addressLine2.value = $('#address-line-2').val();
	    
	    _town.property = 'town';
	    _town.value = $('#town').value;
	   
	    _postcode.property = 'postcode';
	    _postcode.value = $('#postcode').val();
	    
	    _country.property = 'country';
	    _country.value = $('#country').val();
	    
	    _otherCountry.property = 'other-country';
	    _otherCountry.value = $('#other-country').val();
	    
	    _telephoneDaytime.property = 'telephone-daytime';
	    _telephoneDaytime.value = $('#telephone-daytime').val();
	    
	    _telephoneEvening.property = 'telephone-evening';
	    _telephoneEvening.value = $('#telephone-evening').val();
	    
	    _email.property = 'email-address';
	    _email.value = $('#email-address').val();
	    
	    _arrival.property = 'arrival';
	    _arrival.value = $('#arrival').val();
	    	    
	    _departure.property = 'departure';
	    _departure.value = $('#departure').val();
	    	    	    	    
	    _noOfadults.property = 'no-of-adults';
	    _noOfadults.value = $('#no-of-adults').val();
	    	    	    	    
	    _noOfchildren.property = 'no-of-children';
	    _noOfchildren.value = $('#no-of-children').val();
    
	    _comments.property = 'comments-requirements';
	    _comments.value = $('#comments-requirements').val();
	    
	    _activityList.property = 'activity-list';
	    _activityList.value = Reservations.Form.getActivityList();
	    
	    _roomList.property = 'room-list';
	    _roomList.value = Reservations.Form.getRoomList();
	    
	    _adultsList.property = 'adults-list';
	    _adultsList.value = Reservations.Form.getAdultNames();
	    
	    _childrenList.property = 'children-list';
	    _childrenList.value = Reservations.Form.getChildrenNames();
	    
	    _pageUri.property = 'pageuri';
	    _pageUri.value = document.location.href;
	    	    
	    var _formArray = [_title,_firstname,_lastname,_addressLine1,_addressLine2,_town,_postcode,_country,_otherCountry,_telephoneDaytime,_telephoneEvening,_email,_arrival,_departure,_noOfadults,_adultsList,_noOfchildren,_childrenList,_comments,_activityList,_roomList,_pageUri];
    	 	    	
	    API.Web.UI.Client.Service.Forms.ReservationForm.sendForm(_formArray,succeeded,failed)
    },
    
    getActivityList : function() {
        var list = '';        
        $('input.activity:checked')
            .each( function() {
                list += this.value +'\n';
            });
        return list;
    },
    
    getRoomList : function() {
        var list = '';        
        $('input.roomtype:checked')
            .each( function(index) {
                list += this.value +'\n';  
            });
        return list;
    },
    
    updateNoOfAdults : function() {
        var count = $('#no-of-adults').val();
        $('#adults-list p')
            .each( function(index) {
                if (index < count) {
                    this.style.display='';
                } else {
                    this.style.display='none';
                }
            });
    },
    
    getAdultNames : function() {
        var names = '';
        $('#adults-list p:visible')
            .each( function(index) {
                names += $('#adult-'+(index+1)).val() +'\n';       
            });
        return names;
    },
    
    updateNoOfChildren : function() {
        var count = $('#no-of-children').val();
        $('#children-list p')
            .each( function(index) {
                if (index < count) {
                    this.style.display='';
                } else {
                    this.style.display='none';
                }
            });
    },
    
    getChildrenNames : function() {
        var names = '';
        $('#children-list p:visible')
            .each( function(index) {
                names += $('#child-'+(index+1)).val() +'\n';     
            });
        return names;
    },
    
    reset: function() {
    
        $('#error-message').hide(); 
        $('#success-message').hide(); 
            
        $('#title').parent().removeClass('error');
	    $('#first-name').parent().removeClass('error');
	    $('#last-name').parent().removeClass('error');
	    $('#address-line-1').parent().removeClass('error');
	    $('#address-line-2').parent().removeClass('error');
	    $('#town').parent().removeClass('error');
	    $('#postcode').parent().removeClass('error');
	    $('#country').parent().removeClass('error');
	    $('#other-country').parent().removeClass('error');
        $('#telephone-daytime').parent().removeClass('error');
        $('#telephone-evening').parent().removeClass('error');
        $('#email-address').parent().removeClass('error');
        $('#no-of-adults').parent().removeClass('error');
        $('#no-of-children').parent().removeClass('error');
        $('#arrival').parent().removeClass('error');
        $('#departure').parent().removeClass('error');
        
        $('#title').val('');
        $('#first-name').val('');
	    $('#last-name').val('');
	    $('#address-line-1').val('');
	    $('#address-line-2').val('');
	    $('#town').val('');
	    $('#postcode').val('');
	    $('#country').val('');
	    $('#other-country').val('');
        $('#telephone-daytime').val('');
        $('#telephone-evening').val('');
        $('#year-of-birth').val('');
        $('#email-address').val('');
        $('#no-of-adults').val('');
        $('#no-of-children').val('');
        $('#arrival').val('');
        $('#departure').val('');
        $('#comments-requirements').val('');
        
        $('#adults-list p').each( function(index) {
            $('#adult-'+(index+1)).val('');
            $(this).hide();
        });
        
        $('#children-list p').each( function(index) {
            $('#child-'+(index+1)).val('');
            $(this).hide();
        });
        
        $('input.roomtype').each( function(index) {
            this.checked = false;       
        });
        
        $('input.activity').each( function(index) {
             this.checked = false;       
        });
    }
};

